diff options
author | 2021-03-31 10:20:22 +0200 | |
---|---|---|
committer | 2021-03-31 10:20:22 +0200 | |
commit | b9ec44446ed4d99b9b17aceafdcb353dd1595877 (patch) | |
tree | 86b3e4d9a7257a6d5b0d82988111f2a3a5ca7117 /winit/src | |
parent | bbb4e4678f14b4b187f9537a32063440e727e919 (diff) | |
parent | 8f952452ce3d61203856bcebae7016372556be31 (diff) | |
download | iced-b9ec44446ed4d99b9b17aceafdcb353dd1595877.tar.gz iced-b9ec44446ed4d99b9b17aceafdcb353dd1595877.tar.bz2 iced-b9ec44446ed4d99b9b17aceafdcb353dd1595877.zip |
Merge pull request #804 from hecrj/feature/graceful-exit
Graceful exiting for `Application`
Diffstat (limited to 'winit/src')
-rw-r--r-- | winit/src/application.rs | 23 | ||||
-rw-r--r-- | winit/src/conversion.rs | 3 | ||||
-rw-r--r-- | winit/src/settings.rs | 4 |
3 files changed, 27 insertions, 3 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs index ef6c8463..106d5218 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -91,6 +91,13 @@ pub trait Application: Program<Clipboard = Clipboard> { fn scale_factor(&self) -> f64 { 1.0 } + + /// Returns whether the [`Application`] should be terminated. + /// + /// By default, it returns `false`. + fn should_exit(&self) -> bool { + false + } } /// Runs an [`Application`] with an executor, compositor, and the provided @@ -149,10 +156,11 @@ where application, compositor, renderer, - window, runtime, debug, receiver, + window, + settings.exit_on_close_request, )); let mut context = task::Context::from_waker(task::noop_waker_ref()); @@ -196,10 +204,11 @@ async fn run_instance<A, E, C>( mut application: A, mut compositor: C, mut renderer: A::Renderer, - window: winit::window::Window, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, mut debug: Debug, mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>, + window: winit::window::Window, + exit_on_close_request: bool, ) where A: Application + 'static, E: Executor + 'static, @@ -279,6 +288,8 @@ async fn run_instance<A, E, C>( // Update window state.synchronize(&application, &window); + let should_exit = application.should_exit(); + user_interface = ManuallyDrop::new(build_user_interface( &mut application, cache, @@ -286,6 +297,10 @@ async fn run_instance<A, E, C>( state.logical_size(), &mut debug, )); + + if should_exit { + break; + } } debug.draw_started(); @@ -358,7 +373,9 @@ async fn run_instance<A, E, C>( event: window_event, .. } => { - if requests_exit(&window_event, state.modifiers()) { + if requests_exit(&window_event, state.modifiers()) + && exit_on_close_request + { break; } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 0e04b35d..0fa27413 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -33,6 +33,9 @@ pub fn window_event( height: logical_size.height, })) } + WindowEvent::CloseRequested => { + Some(Event::Window(window::Event::CloseRequested)) + } WindowEvent::CursorMoved { position, .. } => { let position = position.to_logical::<f64>(scale_factor); diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 2e8715cd..9ce5cfc5 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -23,6 +23,10 @@ pub struct Settings<Flags> { /// /// [`Application`]: crate::Application pub flags: Flags, + + /// Whether the [`Application`] should exit when the user requests the + /// window to close (e.g. the user presses the close button). + pub exit_on_close_request: bool, } /// The window settings of an application. |