diff --git a/examples/calculator/main.rs b/examples/calculator/main.rs index 4ef4d23..ca3f0c7 100644 --- a/examples/calculator/main.rs +++ b/examples/calculator/main.rs @@ -49,6 +49,9 @@ impl AppDelegate for CalculatorApp { self.window.set_content_view(&self.content); self.window.show(); } + fn should_terminate_after_last_window_closed(&self) -> bool { + true + } } impl Dispatcher for CalculatorApp { diff --git a/src/macos/app/delegate.rs b/src/macos/app/delegate.rs index 01b66b8..f969f48 100644 --- a/src/macos/app/delegate.rs +++ b/src/macos/app/delegate.rs @@ -226,6 +226,14 @@ extern fn should_open_untitled_file(this: &Object, _: Sel, _: id } } +/// Fired when the application receives an `applicationShouldTerminateAfterLastWindowClosed:` message. +extern fn should_terminate_after_last_window_closed(this: &Object, _: Sel, _: id) -> BOOL { + match app::(this).should_terminate_after_last_window_closed() { + true => YES, + false => NO + } +} + /// Fired when the application receives an `applicationOpenUntitledFile:` message. extern fn open_untitled_file(this: &Object, _: Sel, _: id) -> BOOL { match app::(this).open_untitled_file() { @@ -308,6 +316,7 @@ pub(crate) fn register_app_delegate_class() -> *co // Terminating Applications decl.add_method(sel!(applicationShouldTerminate:), should_terminate:: as extern fn(&Object, _, _) -> NSUInteger); decl.add_method(sel!(applicationWillTerminate:), will_terminate:: as extern fn(&Object, _, _)); + decl.add_method(sel!(applicationShouldTerminateAfterLastWindowClosed:), should_terminate_after_last_window_closed:: as extern fn(&Object, _, _) -> BOOL); // Hiding Applications decl.add_method(sel!(applicationWillHide:), will_hide:: as extern fn(&Object, _, _)); diff --git a/src/macos/app/traits.rs b/src/macos/app/traits.rs index d21134a..1735aa6 100644 --- a/src/macos/app/traits.rs +++ b/src/macos/app/traits.rs @@ -98,6 +98,10 @@ pub trait AppDelegate { /// back. fn should_terminate(&self) -> TerminateResponse { TerminateResponse::Now } + /// Called after closing the last open window. Return `true` here if you want + /// the application to terminate. + fn should_terminate_after_last_window_closed(&self) -> bool { false } + /// Sent by the application to the delegate prior to default behavior to reopen AppleEvents. /// /// `has_visible_windows` indicates whether the Application object found any visible windows in your application.