Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(macos): Expose tao event Event::ExitRequested #11568

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/macos-dock-exit-requested-event.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion crates/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -3708,6 +3711,25 @@ fn handle_event_loop<T: UserEvent>(
} => 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];
}
}
_ => (),
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub enum RunEvent<T: UserEvent> {
},
/// A custom event defined by the user.
UserEvent(T),
/// Emitted when the NSApplicationDelegate's applicationShouldTerminate gets called
#[cfg(target_os = "macos")]
DockExitRequested {
tx: Sender<ExitRequestedEventAction>,
},
}

/// Action to take when the event loop is about to exit
Expand Down
10 changes: 10 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventLoopMessage> for RunEvent {
Expand Down Expand Up @@ -2236,6 +2242,10 @@ fn on_event_loop_event<R: Runtime>(
} => RunEvent::Reopen {
has_visible_windows,
},
#[cfg(target_os = "macos")]
RuntimeRunEvent::DockExitRequested { tx } => RunEvent::DockExitRequested {
api: ExitRequestApi(tx)
},
_ => unimplemented!(),
};

Expand Down