hotkey.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. use crate::{config::Config, feat, log_err};
  2. use anyhow::{bail, Result};
  3. use once_cell::sync::OnceCell;
  4. use parking_lot::Mutex;
  5. use std::{collections::HashMap, sync::Arc};
  6. use tauri::{AppHandle, GlobalShortcutManager};
  7. use wry::application::accelerator::Accelerator;
  8. pub struct Hotkey {
  9. current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
  10. app_handle: Arc<Mutex<Option<AppHandle>>>,
  11. }
  12. impl Hotkey {
  13. pub fn global() -> &'static Hotkey {
  14. static HOTKEY: OnceCell<Hotkey> = OnceCell::new();
  15. HOTKEY.get_or_init(|| Hotkey {
  16. current: Arc::new(Mutex::new(Vec::new())),
  17. app_handle: Arc::new(Mutex::new(None)),
  18. })
  19. }
  20. pub fn init(&self, app_handle: AppHandle) -> Result<()> {
  21. *self.app_handle.lock() = Some(app_handle);
  22. let verge = Config::verge();
  23. if let Some(hotkeys) = verge.latest().hotkeys.as_ref() {
  24. for hotkey in hotkeys.iter() {
  25. let mut iter = hotkey.split(',');
  26. let func = iter.next();
  27. let key = iter.next();
  28. match (key, func) {
  29. (Some(key), Some(func)) => {
  30. log_err!(Self::check_key(key).and_then(|_| self.register(key, func)));
  31. }
  32. _ => {
  33. let key = key.unwrap_or("None");
  34. let func = func.unwrap_or("None");
  35. log::error!(target: "app", "invalid hotkey `{key}`:`{func}`");
  36. }
  37. }
  38. }
  39. *self.current.lock() = hotkeys.clone();
  40. }
  41. Ok(())
  42. }
  43. /// 检查一个键是否合法
  44. fn check_key(hotkey: &str) -> Result<()> {
  45. // fix #287
  46. // tauri的这几个方法全部有Result expect,会panic,先检测一遍避免挂了
  47. if hotkey.parse::<Accelerator>().is_err() {
  48. bail!("invalid hotkey `{hotkey}`");
  49. }
  50. Ok(())
  51. }
  52. fn get_manager(&self) -> Result<impl GlobalShortcutManager> {
  53. let app_handle = self.app_handle.lock();
  54. if app_handle.is_none() {
  55. bail!("failed to get the hotkey manager");
  56. }
  57. Ok(app_handle.as_ref().unwrap().global_shortcut_manager())
  58. }
  59. fn register(&self, hotkey: &str, func: &str) -> Result<()> {
  60. let mut manager = self.get_manager()?;
  61. if manager.is_registered(hotkey)? {
  62. manager.unregister(hotkey)?;
  63. }
  64. let f = match func.trim() {
  65. "open_dashboard" => || feat::open_dashboard(),
  66. "clash_mode_rule" => || feat::change_clash_mode("rule".into()),
  67. "clash_mode_global" => || feat::change_clash_mode("global".into()),
  68. "clash_mode_direct" => || feat::change_clash_mode("direct".into()),
  69. "clash_mode_script" => || feat::change_clash_mode("script".into()),
  70. "toggle_system_proxy" => || feat::toggle_system_proxy(),
  71. "enable_system_proxy" => || feat::enable_system_proxy(),
  72. "disable_system_proxy" => || feat::disable_system_proxy(),
  73. "toggle_tun_mode" => || feat::toggle_tun_mode(),
  74. "enable_tun_mode" => || feat::enable_tun_mode(),
  75. "disable_tun_mode" => || feat::disable_tun_mode(),
  76. _ => bail!("invalid function \"{func}\""),
  77. };
  78. manager.register(hotkey, f)?;
  79. log::info!(target: "app", "register hotkey {hotkey} {func}");
  80. Ok(())
  81. }
  82. fn unregister(&self, hotkey: &str) -> Result<()> {
  83. self.get_manager()?.unregister(&hotkey)?;
  84. log::info!(target: "app", "unregister hotkey {hotkey}");
  85. Ok(())
  86. }
  87. pub fn update(&self, new_hotkeys: Vec<String>) -> Result<()> {
  88. let mut current = self.current.lock();
  89. let old_map = Self::get_map_from_vec(&current);
  90. let new_map = Self::get_map_from_vec(&new_hotkeys);
  91. let (del, add) = Self::get_diff(old_map, new_map);
  92. // 先检查一遍所有新的热键是不是可以用的
  93. for (hotkey, _) in add.iter() {
  94. Self::check_key(hotkey)?;
  95. }
  96. del.iter().for_each(|key| {
  97. let _ = self.unregister(key);
  98. });
  99. add.iter().for_each(|(key, func)| {
  100. log_err!(self.register(key, func));
  101. });
  102. *current = new_hotkeys;
  103. Ok(())
  104. }
  105. fn get_map_from_vec<'a>(hotkeys: &'a Vec<String>) -> HashMap<&'a str, &'a str> {
  106. let mut map = HashMap::new();
  107. hotkeys.iter().for_each(|hotkey| {
  108. let mut iter = hotkey.split(',');
  109. let func = iter.next();
  110. let key = iter.next();
  111. if func.is_some() && key.is_some() {
  112. let func = func.unwrap().trim();
  113. let key = key.unwrap().trim();
  114. map.insert(key, func);
  115. }
  116. });
  117. map
  118. }
  119. fn get_diff<'a>(
  120. old_map: HashMap<&'a str, &'a str>,
  121. new_map: HashMap<&'a str, &'a str>,
  122. ) -> (Vec<&'a str>, Vec<(&'a str, &'a str)>) {
  123. let mut del_list = vec![];
  124. let mut add_list = vec![];
  125. old_map.iter().for_each(|(&key, func)| {
  126. match new_map.get(key) {
  127. Some(new_func) => {
  128. if new_func != func {
  129. del_list.push(key);
  130. add_list.push((key, *new_func));
  131. }
  132. }
  133. None => del_list.push(key),
  134. };
  135. });
  136. new_map.iter().for_each(|(&key, &func)| {
  137. if old_map.get(key).is_none() {
  138. add_list.push((key, func));
  139. }
  140. });
  141. (del_list, add_list)
  142. }
  143. }
  144. impl Drop for Hotkey {
  145. fn drop(&mut self) {
  146. if let Ok(mut manager) = self.get_manager() {
  147. let _ = manager.unregister_all();
  148. }
  149. }
  150. }