From c0b7c666b3ca6e2ae5bbb3e4eb6279e1af22e1fc Mon Sep 17 00:00:00 2001 From: luoffei Date: Sat, 2 Nov 2024 16:24:05 +0800 Subject: [PATCH 1/2] feat(macos): Expose tao event `Event::ExitRequested` --- crates/tauri-runtime-wry/Cargo.toml | 2 +- crates/tauri-runtime-wry/src/lib.rs | 22 ++++++++++++++++++++++ crates/tauri-runtime/src/lib.rs | 5 +++++ crates/tauri/src/app.rs | 10 ++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/tauri-runtime-wry/Cargo.toml b/crates/tauri-runtime-wry/Cargo.toml index dd7ab2009b39..77a89055613b 100644 --- a/crates/tauri-runtime-wry/Cargo.toml +++ b/crates/tauri-runtime-wry/Cargo.toml @@ -23,7 +23,7 @@ wry = { version = "0.46.1", default-features = false, features = [ "os-webview", "linux-body", ] } -tao = { version = "0.30.2", default-features = false, features = ["rwh_06"] } +tao = { version = "0.30.2", default-features = false, features = ["rwh_06"], path = "../../../tao" } tauri-runtime = { version = "2.1.0", path = "../tauri-runtime" } tauri-utils = { version = "2.0.2", path = "../tauri-utils" } raw-window-handle = "0.6" diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 4dd17d494910..b7381ac7a26b 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -30,6 +30,9 @@ use tauri_runtime::{ UserEvent, WebviewDispatch, WebviewEventId, WindowDispatch, WindowEventId, }; +#[cfg(target_os = "macos")] +#[macro_use] +extern crate objc2; #[cfg(any(target_os = "macos", target_os = "ios"))] use objc2::rc::Retained; #[cfg(target_os = "macos")] @@ -3708,6 +3711,25 @@ fn handle_event_loop( } => callback(RunEvent::Reopen { has_visible_windows, }), + #[cfg(target_os = "macos")] + Event::ExitRequested => { + let (tx, rx) = channel(); + callback(RunEvent::DockExitRequested {tx}); + let recv = rx.try_recv(); + let should_prevent = matches!(recv, Ok(ExitRequestedEventAction::Prevent)); + + use objc2::ffi::{NO, YES}; + use objc2_app_kit::NSApplication; + use objc2_foundation::MainThreadMarker; + + unsafe { + let mtm = MainThreadMarker::new_unchecked(); + let app = NSApplication::sharedApplication(mtm); + + let action = if should_prevent { NO } else { YES }; + let () = msg_send![app.as_ref(), replyToApplicationShouldTerminate: action]; + } + } _ => (), } } diff --git a/crates/tauri-runtime/src/lib.rs b/crates/tauri-runtime/src/lib.rs index b1e2c7184f61..0382231e2049 100644 --- a/crates/tauri-runtime/src/lib.rs +++ b/crates/tauri-runtime/src/lib.rs @@ -249,6 +249,11 @@ pub enum RunEvent { }, /// A custom event defined by the user. UserEvent(T), + /// Emitted when the NSApplicationDelegate's applicationShouldTerminate gets called + #[cfg(target_os = "macos")] + DockExitRequested { + tx: Sender, + }, } /// Action to take when the event loop is about to exit diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index b513ed7f8964..da468e1067f7 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -246,6 +246,12 @@ pub enum RunEvent { /// Indicates whether the NSApplication object found any visible windows in your application. has_visible_windows: bool, }, + /// Emitted when the NSApplicationDelegate's applicationShouldTerminate gets called + #[cfg(target_os = "macos")] + DockExitRequested { + /// Event API + api: ExitRequestApi, + } } impl From for RunEvent { @@ -2236,6 +2242,10 @@ fn on_event_loop_event( } => RunEvent::Reopen { has_visible_windows, }, + #[cfg(target_os = "macos")] + RuntimeRunEvent::DockExitRequested { tx } => RunEvent::DockExitRequested { + api: ExitRequestApi(tx) + }, _ => unimplemented!(), }; From 25037ab405a9d841fccd5fe04361a5fc8c446a14 Mon Sep 17 00:00:00 2001 From: luoffei Date: Sat, 2 Nov 2024 16:39:19 +0800 Subject: [PATCH 2/2] add change doc --- .changes/macos-dock-exit-requested-event.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/macos-dock-exit-requested-event.md diff --git a/.changes/macos-dock-exit-requested-event.md b/.changes/macos-dock-exit-requested-event.md new file mode 100644 index 000000000000..05d80f42d2cc --- /dev/null +++ b/.changes/macos-dock-exit-requested-event.md @@ -0,0 +1,7 @@ +--- +'tauri': 'minor:feat' +'tauri-runtime': 'minor:feat' +'tauri-runtime-wry': 'minor:feat' +--- + +Added `RunEvent::DockExitRequested` handle for dock icon exit on macOS. \ No newline at end of file