From 83d338eaa426d6e6e15a1ab895ec7b52188fe4b6 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Wed, 19 Mar 2025 18:41:26 +0800 Subject: [PATCH] fix: hotkeys on windows crash --- src-tauri/src/core/hotkey.rs | 151 ++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 64 deletions(-) diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index 3200335b..bef15409 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -7,7 +7,7 @@ use tauri::{async_runtime, Manager}; use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState}; pub struct Hotkey { - current: Arc>>, + current: Arc>>, // 保存当前的热键设置 } impl Hotkey { @@ -21,25 +21,25 @@ impl Hotkey { pub fn init(&self) -> Result<()> { let verge = Config::verge(); - let verge_config = verge.latest(); - let enable_global_hotkey = verge_config.enable_global_hotkey.unwrap_or(true); + let enable_global_hotkey = verge.latest().enable_global_hotkey.unwrap_or(true); + println!( + "Initializing hotkeys, global hotkey enabled: {}", + enable_global_hotkey + ); log::info!(target: "app", "Initializing hotkeys, global hotkey enabled: {}", enable_global_hotkey); - // If global hotkey is disabled, skip registration + // 如果全局热键被禁用,则不注册热键 if !enable_global_hotkey { + println!("Global hotkey is disabled, skipping registration"); log::info!(target: "app", "Global hotkey is disabled, skipping registration"); return Ok(()); } - if let Some(hotkeys) = verge_config.hotkeys.as_ref() { + if let Some(hotkeys) = verge.latest().hotkeys.as_ref() { + println!("Found {} hotkeys to register", hotkeys.len()); log::info!(target: "app", "Found {} hotkeys to register", hotkeys.len()); - // Pre-allocate the vector for current hotkeys - let mut current = self.current.lock(); - current.clear(); - current.reserve(hotkeys.len()); - for hotkey in hotkeys.iter() { let mut iter = hotkey.split(','); let func = iter.next(); @@ -47,24 +47,27 @@ impl Hotkey { match (key, func) { (Some(key), Some(func)) => { + println!("Registering hotkey: {} -> {}", key, func); log::info!(target: "app", "Registering hotkey: {} -> {}", key, func); if let Err(e) = self.register(key, func) { + println!("Failed to register hotkey {} -> {}: {:?}", key, func, e); log::error!(target: "app", "Failed to register hotkey {} -> {}: {:?}", key, func, e); } else { + println!("Successfully registered hotkey {} -> {}", key, func); log::info!(target: "app", "Successfully registered hotkey {} -> {}", key, func); } } _ => { let key = key.unwrap_or("None"); let func = func.unwrap_or("None"); + println!("Invalid hotkey configuration: `{key}`:`{func}`"); log::error!(target: "app", "Invalid hotkey configuration: `{key}`:`{func}`"); } } } - - // Use extend instead of clone_from to avoid reallocating - current.extend(hotkeys.iter().cloned()); + self.current.lock().clone_from(hotkeys); } else { + println!("No hotkeys configured"); log::info!(target: "app", "No hotkeys configured"); } @@ -72,40 +75,54 @@ impl Hotkey { } pub fn reset(&self) -> Result<()> { - if let Some(app_handle) = handle::Handle::global().app_handle() { - app_handle.global_shortcut().unregister_all()?; - } + let app_handle = handle::Handle::global().app_handle().unwrap(); + let manager = app_handle.global_shortcut(); + manager.unregister_all()?; Ok(()) } pub fn register(&self, hotkey: &str, func: &str) -> Result<()> { - let app_handle = match handle::Handle::global().app_handle() { - Some(handle) => handle, - None => bail!("Failed to get app handle"), - }; + let app_handle = handle::Handle::global().app_handle().unwrap(); let manager = app_handle.global_shortcut(); + println!( + "Attempting to register hotkey: {} for function: {}", + hotkey, func + ); log::info!(target: "app", "Attempting to register hotkey: {} for function: {}", hotkey, func); if manager.is_registered(hotkey) { + println!( + "Hotkey {} was already registered, unregistering first", + hotkey + ); log::info!(target: "app", "Hotkey {} was already registered, unregistering first", hotkey); manager.unregister(hotkey)?; } let f = match func.trim() { "open_or_close_dashboard" => { + println!("Registering open_or_close_dashboard function"); log::info!(target: "app", "Registering open_or_close_dashboard function"); || { + println!("=== Hotkey Dashboard Window Operation Start ==="); log::info!(target: "app", "=== Hotkey Dashboard Window Operation Start ==="); + // 使用 spawn_blocking 来确保在正确的线程上执行 async_runtime::spawn_blocking(|| { + println!("Toggle dashboard window visibility"); log::info!(target: "app", "Toggle dashboard window visibility"); + // 检查窗口是否存在 if let Some(window) = handle::Handle::global().get_window() { + // 如果窗口可见,则隐藏它 if window.is_visible().unwrap_or(false) { + println!("Window is visible, hiding it"); log::info!(target: "app", "Window is visible, hiding it"); let _ = window.hide(); } else { + // 如果窗口不可见,则显示它 + println!("Window is hidden, showing it"); log::info!(target: "app", "Window is hidden, showing it"); if window.is_minimized().unwrap_or(false) { let _ = window.unminimize(); @@ -114,11 +131,14 @@ impl Hotkey { let _ = window.set_focus(); } } else { + // 如果窗口不存在,创建一个新窗口 + println!("Window does not exist, creating a new one"); log::info!(target: "app", "Window does not exist, creating a new one"); resolve::create_window(); } }); + println!("=== Hotkey Dashboard Window Operation End ==="); log::info!(target: "app", "=== Hotkey Dashboard Window Operation End ==="); } } @@ -130,6 +150,7 @@ impl Hotkey { "quit" => || feat::quit(Some(0)), _ => { + println!("Invalid function: {}", func); log::error!(target: "app", "Invalid function: {}", func); bail!("invalid function \"{func}\""); } @@ -139,27 +160,34 @@ impl Hotkey { let _ = manager.on_shortcut(hotkey, move |app_handle, hotkey, event| { if event.state == ShortcutState::Pressed { + println!("Hotkey pressed: {:?}", hotkey); log::info!(target: "app", "Hotkey pressed: {:?}", hotkey); if hotkey.key == Code::KeyQ && is_quit { if let Some(window) = app_handle.get_webview_window("main") { if window.is_focused().unwrap_or(false) { + println!("Executing quit function"); log::info!(target: "app", "Executing quit function"); f(); } } } else { + // 直接执行函数,不做任何状态检查 + println!("Executing function directly"); log::info!(target: "app", "Executing function directly"); - // Cache config values to avoid multiple lookups - let verge = Config::verge(); - let verge_config = verge.latest(); - let is_lite_mode = verge_config.enable_lite_mode.unwrap_or(false); - let is_enable_global_hotkey = verge_config.enable_global_hotkey.unwrap_or(true); + // 获取轻量模式状态和全局热键状态 + let is_lite_mode = Config::verge().latest().enable_lite_mode.unwrap_or(false); + let is_enable_global_hotkey = Config::verge() + .latest() + .enable_global_hotkey + .unwrap_or(true); + // 在轻量模式下或配置了全局热键时,始终执行热键功能 if is_lite_mode || is_enable_global_hotkey { f(); } else if let Some(window) = app_handle.get_webview_window("main") { + // 非轻量模式且未启用全局热键时,只在窗口可见且有焦点的情况下响应热键 let is_visible = window.is_visible().unwrap_or(false); let is_focused = window.is_focused().unwrap_or(false); @@ -171,54 +199,52 @@ impl Hotkey { } }); + println!("Successfully registered hotkey {} for {}", hotkey, func); log::info!(target: "app", "Successfully registered hotkey {} for {}", hotkey, func); Ok(()) } pub fn unregister(&self, hotkey: &str) -> Result<()> { - if let Some(app_handle) = handle::Handle::global().app_handle() { - app_handle.global_shortcut().unregister(hotkey)?; - log::debug!(target: "app", "unregister hotkey {hotkey}"); - } + let app_handle = handle::Handle::global().app_handle().unwrap(); + let manager = app_handle.global_shortcut(); + manager.unregister(hotkey)?; + log::debug!(target: "app", "unregister hotkey {hotkey}"); Ok(()) } pub fn update(&self, new_hotkeys: Vec) -> Result<()> { - // Create maps outside of lock to minimize lock duration - let current = self.current.lock().clone(); + let mut current = self.current.lock(); let old_map = Self::get_map_from_vec(¤t); let new_map = Self::get_map_from_vec(&new_hotkeys); let (del, add) = Self::get_diff(old_map, new_map); - // Unregister and register outside the lock - for key in del { + del.iter().for_each(|key| { let _ = self.unregister(key); - } + }); - for (key, func) in add { + add.iter().for_each(|(key, func)| { log_err!(self.register(key, func)); - } + }); - // Update current hotkeys with minimal lock duration - let mut current = self.current.lock(); *current = new_hotkeys; - Ok(()) } fn get_map_from_vec(hotkeys: &[String]) -> HashMap<&str, &str> { - // Pre-allocate HashMap to avoid resizing - let mut map = HashMap::with_capacity(hotkeys.len()); + let mut map = HashMap::new(); - for hotkey in hotkeys { + hotkeys.iter().for_each(|hotkey| { let mut iter = hotkey.split(','); - if let (Some(func), Some(key)) = (iter.next(), iter.next()) { - let func = func.trim(); - let key = key.trim(); + let func = iter.next(); + let key = iter.next(); + + if func.is_some() && key.is_some() { + let func = func.unwrap().trim(); + let key = key.unwrap().trim(); map.insert(key, func); } - } + }); map } @@ -226,28 +252,26 @@ impl Hotkey { old_map: HashMap<&'a str, &'a str>, new_map: HashMap<&'a str, &'a str>, ) -> (Vec<&'a str>, Vec<(&'a str, &'a str)>) { - // Pre-allocate vectors with appropriate capacity - let mut del_list = Vec::with_capacity(old_map.len()); - let mut add_list = Vec::with_capacity(new_map.len()); + let mut del_list = vec![]; + let mut add_list = vec![]; - // Find keys to delete or update - for (&key, &func) in old_map.iter() { + old_map.iter().for_each(|(&key, func)| { match new_map.get(key) { - Some(&new_func) if new_func != func => { - del_list.push(key); - add_list.push((key, new_func)); + Some(new_func) => { + if new_func != func { + del_list.push(key); + add_list.push((key, *new_func)); + } } None => del_list.push(key), - _ => {} // Key exists with same function, no change needed - } - } + }; + }); - // Find new keys to add - for (&key, &func) in new_map.iter() { + new_map.iter().for_each(|(&key, &func)| { if !old_map.contains_key(key) { add_list.push((key, func)); } - } + }); (del_list, add_list) } @@ -255,10 +279,9 @@ impl Hotkey { impl Drop for Hotkey { fn drop(&mut self) { - if let Some(app_handle) = handle::Handle::global().app_handle() { - if let Err(e) = app_handle.global_shortcut().unregister_all() { - log::error!(target:"app", "Error unregistering all hotkeys: {:?}", e); - } + let app_handle = handle::Handle::global().app_handle().unwrap(); + if let Err(e) = app_handle.global_shortcut().unregister_all() { + log::error!(target:"app", "Error unregistering all hotkeys: {:?}", e); } } }