diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index 26a832d4..35a05624 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -24,16 +24,6 @@ impl Hotkey { *self.app_handle.lock() = Some(app_handle.clone()); let verge = Config::verge(); - #[cfg(target_os = "macos")] - { - log_err!(self.register("CMD+Q", "quit")); - } - - #[cfg(not(target_os = "macos"))] - { - log_err!(self.register("Control+Q", "quit")); - } - if let Some(hotkeys) = verge.latest().hotkeys.as_ref() { for hotkey in hotkeys.iter() { let mut iter = hotkey.split(','); @@ -57,7 +47,7 @@ impl Hotkey { Ok(()) } - fn register(&self, hotkey: &str, func: &str) -> Result<()> { + pub fn register(&self, hotkey: &str, func: &str) -> Result<()> { let app_handle = self.app_handle.lock(); if app_handle.is_none() { bail!("failed to get the hotkey manager"); @@ -97,7 +87,7 @@ impl Hotkey { Ok(()) } - fn unregister(&self, hotkey: &str) -> Result<()> { + pub fn unregister(&self, hotkey: &str) -> Result<()> { let app_handle = self.app_handle.lock(); if app_handle.is_none() { bail!("failed to get the hotkey manager"); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 46411770..8ea864b5 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -5,6 +5,7 @@ mod enhance; mod feat; mod utils; +use crate::core::hotkey; use crate::utils::{resolve, resolve::resolve_scheme, server}; #[cfg(target_os = "macos")] use tauri::Listener; @@ -149,6 +150,28 @@ pub fn run() { tauri::WindowEvent::Moved(_) | tauri::WindowEvent::Resized(_) => { let _ = resolve::save_window_size_position(app_handle, false); } + tauri::WindowEvent::Focused(true) => { + #[cfg(target_os = "macos")] + { + log_err!(hotkey::Hotkey::global().register("CMD+Q", "quit")); + } + + #[cfg(not(target_os = "macos"))] + { + log_err!(hotkey::Hotkey::global().register()("Control+Q", "quit")); + }; + } + tauri::WindowEvent::Focused(false) => { + #[cfg(target_os = "macos")] + { + log_err!(hotkey::Hotkey::global().unregister("CMD+Q")); + } + + #[cfg(not(target_os = "macos"))] + { + log_err!(hotkey::Hotkey::global().unregister()("Control+Q")); + }; + } _ => {} } }