Skip to content

Commit

Permalink
eframe: rename quit/exit to "close"
Browse files Browse the repository at this point in the history
Since #1919 we can continue
the application after closing the native window. It therefore makes
more sense to call `frame.close()` to close the native window,
instead of `frame.quit()`.
  • Loading branch information
emilk committed Aug 20, 2022
1 parent 2453756 commit b99c332
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions crates/eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
* Fixed bug where the result returned from `App::on_exit_event` would sometimes be ignored ([#1696](https://github.com/emilk/egui/pull/1696)).
* Added `NativeOptions::follow_system_theme` and `NativeOptions::default_theme` ([#1726](https://github.com/emilk/egui/pull/1726)).
* Selectively expose parts of the API based on target arch (`wasm32` or not) ([#1867](https://github.com/emilk/egui/pull/1867)).
* `Frame::quit` has been renamed to `Frame::close` and `App::on_exit_event` is now `App::on_close_event`.

#### Desktop/Native:
* Fixed clipboard on Wayland ([#1613](https://github.com/emilk/egui/pull/1613)).
Expand Down
33 changes: 24 additions & 9 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,19 @@ pub trait App {
/// where `APPNAME` is what is given to `eframe::run_native`.
fn save(&mut self, _storage: &mut dyn Storage) {}

/// Called before an exit that can be aborted.
/// By returning `false` the exit will be aborted. To continue the exit return `true`.
/// 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.
fn on_exit_event(&mut self) -> bool {
#[cfg(not(target_arch = "wasm32"))]
#[doc(alias = "exit")]
#[doc(alias = "quit")]
fn on_close_event(&mut self) -> bool {
true
}

Expand Down Expand Up @@ -571,11 +575,23 @@ impl Frame {
self.wgpu_render_state.as_ref()
}

/// Signal the app to stop/exit/quit the app (only works for native apps, not web apps).
/// The framework will not quit immediately, but at the end of the this frame.
/// Tell `eframe` to close the desktop window.
///
/// The window will not close immediately, but at the end of the this frame.
///
/// Calling this will likely result in the app quitting, unless
/// you have more code after the call to [`eframe::run_native`].
#[cfg(not(target_arch = "wasm32"))]
#[doc(alias = "exit")]
#[doc(alias = "quit")]
pub fn close(&mut self) {
self.output.close = true;
}

#[cfg(not(target_arch = "wasm32"))]
#[deprecated("Renamed `close`")]
pub fn quit(&mut self) {
self.output.quit = true;
self.close();
}

/// Set the desired inner size of the window (in egui points).
Expand Down Expand Up @@ -797,10 +813,9 @@ pub(crate) mod backend {
#[derive(Clone, Debug, Default)]
#[must_use]
pub struct AppOutput {
/// Set to `true` to stop the app.
/// This does nothing for web apps.
/// Set to `true` to close the native window (which often quits the app).
#[cfg(not(target_arch = "wasm32"))]
pub quit: bool,
pub close: bool,

/// Set to some size to resize the outer window (e.g. glium window) to this size.
#[cfg(not(target_arch = "wasm32"))]
Expand Down
22 changes: 11 additions & 11 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn handle_app_output(
app_output: epi::backend::AppOutput,
) {
let epi::backend::AppOutput {
quit: _,
close: _,
window_size,
window_title,
decorated,
Expand Down Expand Up @@ -183,8 +183,8 @@ pub struct EpiIntegration {
pub egui_ctx: egui::Context,
pending_full_output: egui::FullOutput,
egui_winit: egui_winit::State,
/// When set, it is time to quit
quit: bool,
/// When set, it is time to close the native window.
close: bool,
can_drag_window: bool,
}

Expand Down Expand Up @@ -228,7 +228,7 @@ impl EpiIntegration {
egui_ctx,
egui_winit,
pending_full_output: Default::default(),
quit: false,
close: false,
can_drag_window: false,
}
}
Expand All @@ -243,17 +243,17 @@ impl EpiIntegration {
self.egui_ctx.clear_animations();
}

/// If `true`, it is time to shut down.
pub fn should_quit(&self) -> bool {
self.quit
/// If `true`, it is time to close the native window.
pub fn should_close(&self) -> bool {
self.close
}

pub fn on_event(&mut self, app: &mut dyn epi::App, event: &winit::event::WindowEvent<'_>) {
use winit::event::{ElementState, MouseButton, WindowEvent};

match event {
WindowEvent::CloseRequested => self.quit = app.on_exit_event(),
WindowEvent::Destroyed => self.quit = true,
WindowEvent::CloseRequested => self.close = app.on_close_event(),
WindowEvent::Destroyed => self.close = true,
WindowEvent::MouseInput {
button: MouseButton::Left,
state: ElementState::Pressed,
Expand Down Expand Up @@ -285,8 +285,8 @@ impl EpiIntegration {
let mut app_output = self.frame.take_app_output();
app_output.drag_window &= self.can_drag_window; // Necessary on Windows; see https://github.com/emilk/egui/pull/1108
self.can_drag_window = false;
if app_output.quit {
self.quit = app.on_exit_event();
if app_output.close {
self.close = app.on_close_event();
}
handle_app_output(window, self.egui_ctx.pixels_per_point(), app_output);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ mod glow_integration {
gl_window.swap_buffers().unwrap();
}

let control_flow = if integration.should_quit() {
let control_flow = if integration.should_close() {
EventResult::Exit
} else if repaint_after.is_zero() {
EventResult::RepaintAsap
Expand Down Expand Up @@ -426,7 +426,7 @@ mod glow_integration {
self.gl_window.resize(**new_inner_size);
}
winit::event::WindowEvent::CloseRequested
if self.integration.should_quit() =>
if self.integration.should_close() =>
{
return EventResult::Exit
}
Expand All @@ -435,7 +435,7 @@ mod glow_integration {

self.integration.on_event(self.app.as_mut(), &event);

if self.integration.should_quit() {
if self.integration.should_close() {
EventResult::Exit
} else {
// TODO(emilk): ask egui if the event warrants a repaint
Expand Down Expand Up @@ -624,7 +624,7 @@ mod wgpu_integration {

integration.post_rendering(app.as_mut(), window);

let control_flow = if integration.should_quit() {
let control_flow = if integration.should_close() {
EventResult::Exit
} else if repaint_after.is_zero() {
EventResult::RepaintAsap
Expand Down Expand Up @@ -690,15 +690,15 @@ mod wgpu_integration {
.on_window_resized(new_inner_size.width, new_inner_size.height);
}
winit::event::WindowEvent::CloseRequested
if self.integration.should_quit() =>
if self.integration.should_close() =>
{
return EventResult::Exit
}
_ => {}
};

self.integration.on_event(self.app.as_mut(), &event);
if self.integration.should_quit() {
if self.integration.should_close() {
EventResult::Exit
} else {
// TODO(emilk): ask egui if the event warrants a repaint
Expand Down
2 changes: 1 addition & 1 deletion examples/confirm_exit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct MyApp {
}

impl eframe::App for MyApp {
fn on_exit_event(&mut self) -> bool {
fn on_close_event(&mut self) -> bool {
self.is_exiting = true;
self.can_exit
}
Expand Down

0 comments on commit b99c332

Please sign in to comment.