Files
clash-proxy/src-tauri/src/core/hotkey.rs

289 lines
12 KiB
Rust
Raw Normal View History

2025-03-23 14:27:51 +08:00
use crate::{config::Config, core::handle, feat, log_err, utils::resolve};
2022-09-14 01:19:02 +08:00
use anyhow::{bail, Result};
2022-11-14 01:26:33 +08:00
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc};
use tauri::{async_runtime, Manager};
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState};
2022-09-14 01:19:02 +08:00
pub struct Hotkey {
2025-03-19 18:41:26 +08:00
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
2022-09-14 01:19:02 +08:00
}
impl Hotkey {
2022-11-14 01:26:33 +08:00
pub fn global() -> &'static Hotkey {
static HOTKEY: OnceCell<Hotkey> = OnceCell::new();
HOTKEY.get_or_init(|| Hotkey {
current: Arc::new(Mutex::new(Vec::new())),
})
2022-09-14 01:19:02 +08:00
}
2022-11-12 11:37:23 +08:00
pub fn init(&self) -> Result<()> {
2022-11-16 01:26:41 +08:00
let verge = Config::verge();
2025-03-19 18:41:26 +08:00
let enable_global_hotkey = verge.latest().enable_global_hotkey.unwrap_or(true);
2025-03-19 18:41:26 +08:00
println!(
"Initializing hotkeys, global hotkey enabled: {}",
enable_global_hotkey
);
log::info!(target: "app", "Initializing hotkeys, global hotkey enabled: {}", enable_global_hotkey);
2025-03-19 18:41:26 +08:00
// 如果全局热键被禁用,则不注册热键
if !enable_global_hotkey {
2025-03-19 18:41:26 +08:00
println!("Global hotkey is disabled, skipping registration");
log::info!(target: "app", "Global hotkey is disabled, skipping registration");
return Ok(());
}
2022-11-12 11:37:23 +08:00
2025-03-19 18:41:26 +08:00
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());
2022-11-12 11:37:23 +08:00
for hotkey in hotkeys.iter() {
let mut iter = hotkey.split(',');
let func = iter.next();
let key = iter.next();
match (key, func) {
(Some(key), Some(func)) => {
2025-03-19 18:41:26 +08:00
println!("Registering hotkey: {} -> {}", key, func);
log::info!(target: "app", "Registering hotkey: {} -> {}", key, func);
if let Err(e) = self.register(key, func) {
2025-03-19 18:41:26 +08:00
println!("Failed to register hotkey {} -> {}: {:?}", key, func, e);
log::error!(target: "app", "Failed to register hotkey {} -> {}: {:?}", key, func, e);
} else {
2025-03-19 18:41:26 +08:00
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");
2025-03-19 18:41:26 +08:00
println!("Invalid hotkey configuration: `{key}`:`{func}`");
log::error!(target: "app", "Invalid hotkey configuration: `{key}`:`{func}`");
}
2022-11-12 11:37:23 +08:00
}
}
2025-03-19 18:41:26 +08:00
self.current.lock().clone_from(hotkeys);
} else {
2025-03-19 18:41:26 +08:00
println!("No hotkeys configured");
log::info!(target: "app", "No hotkeys configured");
2022-09-14 01:19:02 +08:00
}
2022-11-12 11:37:23 +08:00
Ok(())
2022-09-14 01:19:02 +08:00
}
pub fn reset(&self) -> Result<()> {
2025-03-19 18:41:26 +08:00
let app_handle = handle::Handle::global().app_handle().unwrap();
let manager = app_handle.global_shortcut();
manager.unregister_all()?;
Ok(())
}
2024-09-18 15:16:43 +08:00
pub fn register(&self, hotkey: &str, func: &str) -> Result<()> {
2025-03-19 18:41:26 +08:00
let app_handle = handle::Handle::global().app_handle().unwrap();
let manager = app_handle.global_shortcut();
2022-11-12 11:37:23 +08:00
2025-03-19 18:41:26 +08:00
println!(
"Attempting to register hotkey: {} for function: {}",
hotkey, func
);
log::info!(target: "app", "Attempting to register hotkey: {} for function: {}", hotkey, func);
2024-09-02 19:33:17 +08:00
if manager.is_registered(hotkey) {
2025-03-19 18:41:26 +08:00
println!(
"Hotkey {} was already registered, unregistering first",
hotkey
);
log::info!(target: "app", "Hotkey {} was already registered, unregistering first", hotkey);
2022-11-12 11:37:23 +08:00
manager.unregister(hotkey)?;
}
let f = match func.trim() {
"open_or_close_dashboard" => {
2025-03-19 18:41:26 +08:00
println!("Registering open_or_close_dashboard function");
log::info!(target: "app", "Registering open_or_close_dashboard function");
|| {
2025-03-19 18:41:26 +08:00
println!("=== Hotkey Dashboard Window Operation Start ===");
log::info!(target: "app", "=== Hotkey Dashboard Window Operation Start ===");
2025-03-19 18:41:26 +08:00
// 使用 spawn_blocking 来确保在正确的线程上执行
async_runtime::spawn_blocking(|| {
2025-03-19 18:41:26 +08:00
println!("Toggle dashboard window visibility");
log::info!(target: "app", "Toggle dashboard window visibility");
2025-03-19 18:41:26 +08:00
// 检查窗口是否存在
if let Some(window) = handle::Handle::global().get_window() {
2025-03-19 18:41:26 +08:00
// 如果窗口可见,则隐藏它
if window.is_visible().unwrap_or(false) {
2025-03-19 18:41:26 +08:00
println!("Window is visible, hiding it");
log::info!(target: "app", "Window is visible, hiding it");
let _ = window.hide();
} else {
2025-03-19 18:41:26 +08:00
// 如果窗口不可见,则显示它
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();
}
let _ = window.show();
let _ = window.set_focus();
}
} else {
2025-03-19 18:41:26 +08:00
// 如果窗口不存在,创建一个新窗口
println!("Window does not exist, creating a new one");
log::info!(target: "app", "Window does not exist, creating a new one");
resolve::create_window();
}
});
2025-03-19 18:41:26 +08:00
println!("=== Hotkey Dashboard Window Operation End ===");
log::info!(target: "app", "=== Hotkey Dashboard Window Operation End ===");
}
}
2022-11-14 01:26:33 +08:00
"clash_mode_rule" => || feat::change_clash_mode("rule".into()),
"clash_mode_global" => || feat::change_clash_mode("global".into()),
"clash_mode_direct" => || feat::change_clash_mode("direct".into()),
"toggle_system_proxy" => || feat::toggle_system_proxy(),
"toggle_tun_mode" => || feat::toggle_tun_mode(None),
"entry_lightweight_mode" => || feat::lightweight_mode(),
2024-09-25 11:47:01 +08:00
"quit" => || feat::quit(Some(0)),
#[cfg(target_os = "macos")]
"hide" => || feat::hide(),
2024-09-25 11:47:01 +08:00
_ => {
2025-03-19 18:41:26 +08:00
println!("Invalid function: {}", func);
log::error!(target: "app", "Invalid function: {}", func);
bail!("invalid function \"{func}\"");
}
2022-11-12 11:37:23 +08:00
};
let is_quit = func.trim() == "quit";
let _ = manager.on_shortcut(hotkey, move |app_handle, hotkey, event| {
if event.state == ShortcutState::Pressed {
2025-03-19 18:41:26 +08:00
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) {
2025-03-19 18:41:26 +08:00
println!("Executing quit function");
log::info!(target: "app", "Executing quit function");
f();
}
}
} else {
2025-03-19 18:41:26 +08:00
// 直接执行函数,不做任何状态检查
println!("Executing function directly");
log::info!(target: "app", "Executing function directly");
// 获取全局热键状态
2025-03-19 18:41:26 +08:00
let is_enable_global_hotkey = Config::verge()
.latest()
.enable_global_hotkey
.unwrap_or(true);
if is_enable_global_hotkey {
f();
} else if let Some(window) = app_handle.get_webview_window("main") {
2025-03-19 18:41:26 +08:00
// 非轻量模式且未启用全局热键时,只在窗口可见且有焦点的情况下响应热键
let is_visible = window.is_visible().unwrap_or(false);
let is_focused = window.is_focused().unwrap_or(false);
if is_focused && is_visible {
f();
}
}
}
}
});
2025-03-19 18:41:26 +08:00
println!("Successfully registered hotkey {} for {}", hotkey, func);
log::info!(target: "app", "Successfully registered hotkey {} for {}", hotkey, func);
2022-11-12 11:37:23 +08:00
Ok(())
}
2022-09-14 01:19:02 +08:00
2024-09-18 15:16:43 +08:00
pub fn unregister(&self, hotkey: &str) -> Result<()> {
2025-03-19 18:41:26 +08:00
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}");
2022-11-12 11:37:23 +08:00
Ok(())
}
2022-11-14 01:26:33 +08:00
pub fn update(&self, new_hotkeys: Vec<String>) -> Result<()> {
2025-03-19 18:41:26 +08:00
let mut current = self.current.lock();
2022-11-12 11:37:23 +08:00
let old_map = Self::get_map_from_vec(&current);
let new_map = Self::get_map_from_vec(&new_hotkeys);
let (del, add) = Self::get_diff(old_map, new_map);
2025-03-19 18:41:26 +08:00
del.iter().for_each(|key| {
2022-11-12 11:37:23 +08:00
let _ = self.unregister(key);
2025-03-19 18:41:26 +08:00
});
2022-09-14 01:19:02 +08:00
2025-03-19 18:41:26 +08:00
add.iter().for_each(|(key, func)| {
2022-11-14 01:26:33 +08:00
log_err!(self.register(key, func));
2025-03-19 18:41:26 +08:00
});
2022-11-12 11:37:23 +08:00
2022-11-14 01:26:33 +08:00
*current = new_hotkeys;
2022-11-12 11:37:23 +08:00
Ok(())
2022-09-14 01:19:02 +08:00
}
2024-09-24 20:09:30 +08:00
fn get_map_from_vec(hotkeys: &[String]) -> HashMap<&str, &str> {
2025-03-19 18:41:26 +08:00
let mut map = HashMap::new();
2022-11-12 11:37:23 +08:00
2025-03-19 18:41:26 +08:00
hotkeys.iter().for_each(|hotkey| {
2022-11-12 11:37:23 +08:00
let mut iter = hotkey.split(',');
2025-03-19 18:41:26 +08:00
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();
2022-11-12 11:37:23 +08:00
map.insert(key, func);
}
2025-03-19 18:41:26 +08:00
});
2022-11-12 11:37:23 +08:00
map
}
fn get_diff<'a>(
old_map: HashMap<&'a str, &'a str>,
new_map: HashMap<&'a str, &'a str>,
) -> (Vec<&'a str>, Vec<(&'a str, &'a str)>) {
2025-03-19 18:41:26 +08:00
let mut del_list = vec![];
let mut add_list = vec![];
2022-11-12 11:37:23 +08:00
2025-03-19 18:41:26 +08:00
old_map.iter().for_each(|(&key, func)| {
2022-11-12 11:37:23 +08:00
match new_map.get(key) {
2025-03-19 18:41:26 +08:00
Some(new_func) => {
if new_func != func {
del_list.push(key);
add_list.push((key, *new_func));
}
2022-11-12 11:37:23 +08:00
}
None => del_list.push(key),
2025-03-19 18:41:26 +08:00
};
});
2022-11-12 11:37:23 +08:00
2025-03-19 18:41:26 +08:00
new_map.iter().for_each(|(&key, &func)| {
2024-06-12 10:00:22 +08:00
if !old_map.contains_key(key) {
2022-11-12 11:37:23 +08:00
add_list.push((key, func));
}
2025-03-19 18:41:26 +08:00
});
2022-11-12 11:37:23 +08:00
(del_list, add_list)
}
2022-09-14 01:19:02 +08:00
}
impl Drop for Hotkey {
2022-11-12 11:37:23 +08:00
fn drop(&mut self) {
2025-03-19 18:41:26 +08:00
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);
2022-11-12 11:37:23 +08:00
}
}
2022-09-14 01:19:02 +08:00
}