From 4b38de5a9a63b189b8df9dd3c23097a2121f613b Mon Sep 17 00:00:00 2001 From: jeasonnow Date: Mon, 5 Aug 2024 12:25:52 +0800 Subject: [PATCH] fix: #753 macos support quit fullscreen by close button --- src-tauri/Cargo.lock | 26 ++++++++++++++++++++++---- src-tauri/Cargo.toml | 4 ++++ src-tauri/src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 9bf5d30bba..3370e94d73 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -67,6 +67,8 @@ name = "app" version = "0.1.0" dependencies = [ "cargo-bloat", + "cocoa 0.25.0", + "objc", "serde", "serde_json", "tauri", @@ -573,6 +575,22 @@ dependencies = [ "objc", ] +[[package]] +name = "cocoa" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c" +dependencies = [ + "bitflags 1.3.2", + "block", + "cocoa-foundation", + "core-foundation", + "core-graphics 0.23.2", + "foreign-types 0.5.0", + "libc", + "objc", +] + [[package]] name = "cocoa-foundation" version = "0.1.2" @@ -3705,7 +3723,7 @@ dependencies = [ "bitflags 1.3.2", "cairo-rs", "cc", - "cocoa", + "cocoa 0.24.1", "core-foundation", "core-graphics 0.22.3", "crossbeam-channel", @@ -3781,7 +3799,7 @@ checksum = "7a4fab012dcf1e72762561cef2c08a6538aec2ca44d282158128117f79eb9d39" dependencies = [ "anyhow", "bytes", - "cocoa", + "cocoa 0.24.1", "dirs-next", "dunce", "embed_plist", @@ -3944,7 +3962,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a777eaa0d88ae47d8081cdc667eba8941b3a18af0f0ccb22640d8f0c3431d6d1" dependencies = [ "arboard", - "cocoa", + "cocoa 0.24.1", "gtk", "percent-encoding", "rand 0.8.5", @@ -5267,7 +5285,7 @@ checksum = "a04e72739ee84a218e3dbf8625888eadc874285637003ed21ab96a1bbbb538ec" dependencies = [ "base64 0.13.1", "block", - "cocoa", + "cocoa 0.24.1", "core-graphics 0.22.3", "crossbeam-channel", "dunce", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a2ae9148f2..150fe814da 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -21,6 +21,10 @@ tauri = { version = "1.6.3", features = ["api-all", "system-tray"] } tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } tauri-plugin-oauth = { git = "https://github.com/FabianLars/tauri-plugin-oauth", branch = "main" } +[target.'cfg(target_os = "macos")'.dependencies] +cocoa = "0.25.0" +objc = "0.2.7" + [dev-dependencies] cargo-bloat = "0.11.1" diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 0570dab38f..a29046b7ad 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,6 +3,10 @@ windows_subsystem = "windows" )] +#![cfg(target_os = "macos")] +#[macro_use] +extern crate objc; + mod app; mod util; @@ -67,8 +71,36 @@ pub fn run_app() { if let tauri::WindowEvent::CloseRequested { api, .. } = event.event() { #[cfg(target_os = "macos")] { - event.window().minimize().unwrap(); - event.window().hide().unwrap(); + use cocoa::base::id; + use cocoa::appkit::NSWindow; + use cocoa::delegate; + use objc::runtime::{Object, Sel}; + + let ns_window = event.window().ns_window().unwrap() as id; + + extern fn on_exit_fullscreen(this: &Object, _cmd: Sel, _notification: id) { + unsafe { + let window: id = *this.get_ivar("window"); + window.miniaturize_(window); + } + } + + unsafe { + ns_window.setDelegate_(delegate!("PakeWindowDelegate", { + window: id = ns_window, + (windowDidExitFullScreen:) => on_exit_fullscreen as extern fn(&Object, Sel, id) + })); + } + + if event.window().is_fullscreen().unwrap() { + let ns_window = event.window().ns_window().unwrap() as id; + unsafe { + ns_window.toggleFullScreen_(ns_window); + } + } else { + event.window().minimize().unwrap(); + event.window().hide().unwrap(); + } } #[cfg(not(target_os = "macos"))]