From 84a5cf6b896c1af759dcdbf028351c283fd18312 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Thu, 20 Mar 2025 13:01:58 +0800 Subject: [PATCH] feat(hotkey): macos support CMD+W to close window as default --- src-tauri/src/core/hotkey.rs | 4 +++- src-tauri/src/feat/window.rs | 12 ++++++++++++ src-tauri/src/lib.rs | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index ddf84108..f2e48f5f 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -148,6 +148,8 @@ impl Hotkey { "toggle_system_proxy" => || feat::toggle_system_proxy(), "toggle_tun_mode" => || feat::toggle_tun_mode(None), "quit" => || feat::quit(Some(0)), + #[cfg(target_os = "macos")] + "hide" => || feat::hide(), _ => { println!("Invalid function: {}", func); @@ -181,7 +183,7 @@ impl Hotkey { .latest() .enable_global_hotkey .unwrap_or(true); - + if is_enable_global_hotkey { f(); } else if let Some(window) = app_handle.get_webview_window("main") { diff --git a/src-tauri/src/feat/window.rs b/src-tauri/src/feat/window.rs index 253c1f71..3c235e14 100644 --- a/src-tauri/src/feat/window.rs +++ b/src-tauri/src/feat/window.rs @@ -1,3 +1,5 @@ +#[cfg(target_os = "macos")] +use crate::AppHandleManager; use crate::{ config::Config, core::{handle, sysopt, CoreManager}, @@ -137,3 +139,13 @@ pub fn quit(code: Option) { app_handle.exit(code.unwrap_or(0)); }); } + +#[cfg(target_os = "macos")] +pub fn hide() { + if let Some(window) = handle::Handle::global().get_window() { + if window.is_visible().unwrap_or(false) { + AppHandleManager::global().set_activation_policy_accessory(); + let _ = window.hide(); + } + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 628089b2..3f9beb79 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -290,6 +290,7 @@ pub fn run() { #[cfg(target_os = "macos")] { log_err!(hotkey::Hotkey::global().register("CMD+Q", "quit")); + log_err!(hotkey::Hotkey::global().register("CMD+W", "hide")); } #[cfg(not(target_os = "macos"))] @@ -310,6 +311,7 @@ pub fn run() { #[cfg(target_os = "macos")] { log_err!(hotkey::Hotkey::global().unregister("CMD+Q")); + log_err!(hotkey::Hotkey::global().unregister("CMD+W")); } #[cfg(not(target_os = "macos"))] { @@ -329,6 +331,7 @@ pub fn run() { #[cfg(target_os = "macos")] { log_err!(hotkey::Hotkey::global().unregister("CMD+Q")); + log_err!(hotkey::Hotkey::global().unregister("CMD+W")); } #[cfg(not(target_os = "macos"))]