1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

Fix up some examples (#3614)

This commit is contained in:
Emil Ernerfeldt
2023-11-23 12:58:44 +01:00
committed by GitHub
parent a2b15b23ad
commit 24913ceeba
17 changed files with 123 additions and 82 deletions

View File

@@ -8,6 +8,7 @@ Changes since the last release can be found by running the `scripts/generate_cha
## Unreleased
* `NativeOptions::fullsize_content` has been replaced with four settings: `ViewportBuilder::with_fullsize_content_view`, `with_title_shown`, `with_titlebar_shown`, `with_titlebar_buttons_shown`
* `App::on_close_event` has been replaced with `ctx.input(|i| i.viewport().close_requested())` and `ctx.send_viewport_cmd(ViewportCommand::CancelClose)`.
## 0.23.0 - 2023-09-27
* Update MSRV to Rust 1.70.0 [#3310](https://github.com/emilk/egui/pull/3310)

View File

@@ -149,25 +149,10 @@ pub trait App {
/// On native the path is picked using [`crate::storage_dir`].
fn save(&mut self, _storage: &mut dyn Storage) {}
/// Called when the user attempts to close the desktop window and/or quit the application.
///
/// By returning `false` the closing will be aborted. To continue the closing return `true`.
///
/// A scenario where this method will be run is after pressing the close button on a native
/// window, which allows you to ask the user whether they want to do something before exiting.
/// See the example at <https://github.com/emilk/egui/blob/master/examples/confirm_exit/> for practical usage.
///
/// It will _not_ be called on the web or when the window is forcefully closed.
#[cfg(not(target_arch = "wasm32"))]
#[doc(alias = "exit")]
#[doc(alias = "quit")]
fn on_close_event(&mut self) -> bool {
true
}
/// Called once on shutdown, after [`Self::save`].
///
/// If you need to abort an exit use [`Self::on_close_event`].
/// If you need to abort an exit check `ctx.input(|i| i.viewport().close_requested())`
/// and respond with [`egui::ViewportCommand::CancelClose`].
///
/// To get a [`glow`] context you need to compile with the `glow` feature flag,
/// and run eframe with the glow backend.

View File

@@ -229,22 +229,14 @@ impl EpiIntegration {
pub fn on_window_event(
&mut self,
app: &mut dyn epi::App,
event: &winit::event::WindowEvent<'_>,
egui_winit: &mut egui_winit::State,
viewport_id: ViewportId,
) -> EventResponse {
crate::profile_function!(egui_winit::short_window_event_description(event));
use winit::event::{ElementState, MouseButton, WindowEvent};
match event {
WindowEvent::CloseRequested => {
if viewport_id == ViewportId::ROOT {
self.close = app.on_close_event();
log::debug!("App::on_close_event returned {}", self.close);
}
}
WindowEvent::Destroyed => {
log::debug!("Received WindowEvent::Destroyed");
self.close = true;
@@ -281,23 +273,32 @@ impl EpiIntegration {
) -> egui::FullOutput {
raw_input.time = Some(self.beginning.elapsed().as_secs_f64());
let close_requested = raw_input.viewport().close_requested();
let full_output = self.egui_ctx.run(raw_input, |egui_ctx| {
if let Some(viewport_ui_cb) = viewport_ui_cb {
// Child viewport
crate::profile_scope!("viewport_callback");
viewport_ui_cb(egui_ctx);
} else {
// Root viewport
if egui_ctx.input(|i| i.viewport().close_requested()) {
self.close = app.on_close_event();
log::debug!("App::on_close_event returned {}", self.close);
}
crate::profile_scope!("App::update");
app.update(egui_ctx, &mut self.frame);
}
});
let is_root_viewport = viewport_ui_cb.is_none();
if is_root_viewport && close_requested {
let canceled = full_output.viewport_output[&ViewportId::ROOT]
.commands
.contains(&egui::ViewportCommand::CancelClose);
if canceled {
log::debug!("Closing of root viewport canceled with ViewportCommand::CancelClose");
} else {
log::debug!("Closing root viewport (ViewportCommand::CancelClose was not sent)");
self.close = true;
}
}
self.pending_full_output.append(full_output);
std::mem::take(&mut self.pending_full_output)
}

View File

@@ -568,6 +568,7 @@ impl GlowWinitRunning {
} = &mut *glutin;
let viewport = viewports.get_mut(&viewport_id).unwrap();
viewport.info.events.clear(); // they should have been processed
let window = viewport.window.as_ref().unwrap();
let gl_surface = viewport.gl_surface.as_ref().unwrap();
let egui_winit = viewport.egui_winit.as_mut().unwrap();
@@ -748,12 +749,9 @@ impl GlowWinitRunning {
};
if let Some(viewport_id) = viewport_id {
if let Some(viewport) = glutin.viewports.get_mut(&viewport_id) {
event_response = self.integration.on_window_event(
self.app.as_mut(),
event,
viewport.egui_winit.as_mut().unwrap(),
viewport.ids.this,
);
event_response = self
.integration
.on_window_event(event, viewport.egui_winit.as_mut().unwrap());
}
}
@@ -1340,6 +1338,7 @@ fn render_immediate_viewport(
let Some(viewport) = viewports.get_mut(&ids.this) else {
return;
};
viewport.info.events.clear(); // they should have been processed
let Some(winit_state) = &mut viewport.egui_winit else {
return;

View File

@@ -565,6 +565,8 @@ impl WgpuWinitRunning {
return EventResult::Wait;
};
viewport.info.events.clear(); // they should have been processed
let Viewport {
window: Some(window),
egui_winit: Some(egui_winit),
@@ -657,8 +659,8 @@ impl WgpuWinitRunning {
let Self {
integration,
app,
shared,
..
} = self;
let mut shared = shared.borrow_mut();
@@ -742,9 +744,10 @@ impl WgpuWinitRunning {
let event_response = viewport_id
.and_then(|viewport_id| {
shared.viewports.get_mut(&viewport_id).and_then(|viewport| {
viewport.egui_winit.as_mut().map(|egui_winit| {
integration.on_window_event(app.as_mut(), event, egui_winit, viewport_id)
})
viewport
.egui_winit
.as_mut()
.map(|egui_winit| integration.on_window_event(event, egui_winit))
})
})
.unwrap_or_default();
@@ -923,6 +926,7 @@ fn render_immediate_viewport(
let Some(viewport) = viewports.get_mut(&ids.this) else {
return;
};
viewport.info.events.clear(); // they should have been processed
let Some(winit_state) = &mut viewport.egui_winit else {
return;
};

View File

@@ -1089,6 +1089,9 @@ fn process_viewport_command(
ViewportCommand::Close => {
info.events.push(egui::ViewportEvent::Close);
}
ViewportCommand::CancelClose => {
// Need to be handled elsewhere
}
ViewportCommand::StartDrag => {
// If `is_viewport_focused` is not checked on x11 the input will be permanently taken until the app is killed!

View File

@@ -1392,11 +1392,16 @@ impl Context {
/// Sets zoom factor of the UI.
/// Will become active at the start of the next frame.
///
/// Note that calling this will not update [`Self::zoom_factor`] until the end of the frame.
///
/// This is used to calculate the `pixels_per_point`
/// for the UI as `pixels_per_point = zoom_fator * native_pixels_per_point`.
///
/// The default is 1.0.
/// Make larger to make everything larger.
///
/// It is better to call this than modifying
/// [`Options::zoom_factor`].
#[inline(always)]
pub fn set_zoom_factor(&self, zoom_factor: f32) {
self.write(|ctx| {

View File

@@ -156,7 +156,12 @@ impl RawInput {
pub enum ViewportEvent {
/// The user clicked the close-button on the window, or similar.
///
/// It is up to the user to react to this by _not_ showing the viewport in the next frame in the parent viewport.
/// If this is the root viewport, the application will exit
/// after this frame unless you send a
/// [`crate::ViewportCommand::CancelClose`] command.
///
/// If this is not the root viewport,
/// it is up to the user to hide this viewport the next frame.
///
/// This even will wake up both the child and parent viewport.
Close,
@@ -216,6 +221,14 @@ pub struct ViewportInfo {
}
impl ViewportInfo {
/// This viewport has been told to close.
///
/// If this is the root viewport, the application will exit
/// after this frame unless you send a
/// [`crate::ViewportCommand::CancelClose`] command.
///
/// If this is not the root viewport,
/// it is up to the user to hide this viewport the next frame.
pub fn close_requested(&self) -> bool {
self.events
.iter()

View File

@@ -178,6 +178,9 @@ pub struct Options {
///
/// The default is 1.0.
/// Make larger to make everything larger.
///
/// Please call [`crate::Context::set_zoom_factor`]
/// instead of modifying this directly!
pub zoom_factor: f32,
/// If `true`, egui will change the scale of the ui ([`crate::Context::zoom_factor`]) when the user

View File

@@ -831,6 +831,9 @@ pub enum ViewportCommand {
/// For other viewports, the [`crate::ViewportInfo::close_requested`] flag will be set.
Close,
/// Calcel the closing that was signaled by [`crate::ViewportInfo::close_requested`].
CancelClose,
/// Set the window title.
Title(String),