Skip to content

Commit

Permalink
fix: #753 macos support quit fullscreen by close button
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonnow committed Aug 5, 2024
1 parent 4458a7b commit a25e53f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
26 changes: 22 additions & 4 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
35 changes: 33 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
#![cfg(target_os = "macos")]
#[macro_use]
extern crate objc;

mod app;
mod util;
Expand Down Expand Up @@ -67,8 +70,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"))]
Expand Down

0 comments on commit a25e53f

Please sign in to comment.