hotkey.rs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. pub struct Hotkey {
  8. current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
  9. app_handle: Arc<Mutex<Option<AppHandle>>>,
  10. }
  11. impl Hotkey {
  12. pub fn global() -> &'static Hotkey {
  13. static HOTKEY: OnceCell<Hotkey> = OnceCell::new();
  14. HOTKEY.get_or_init(|| Hotkey {
  15. current: Arc::new(Mutex::new(Vec::new())),
  16. app_handle: Arc::new(Mutex::new(None)),
  17. })
  18. }
  19. pub fn init(&self, app_handle: AppHandle) -> Result<()> {
  20. *self.app_handle.lock() = Some(app_handle);
  21. let verge = Config::verge();
  22. if let Some(hotkeys) = verge.latest().hotkeys.as_ref() {
  23. for hotkey in hotkeys.iter() {
  24. let mut iter = hotkey.split(',');
  25. let func = iter.next();
  26. let key = iter.next();
  27. match (key, func) {
  28. (Some(key), Some(func)) => {
  29. log_err!(self.register(key, func));
  30. }
  31. _ => {
  32. let key = key.unwrap_or("None");
  33. let func = func.unwrap_or("None");
  34. log::error!(target: "app", "invalid hotkey `{key}`:`{func}`");
  35. }
  36. }
  37. }
  38. *self.current.lock() = hotkeys.clone();
  39. }
  40. Ok(())
  41. }
  42. fn get_manager(&self) -> Result<impl GlobalShortcutManager> {
  43. let app_handle = self.app_handle.lock();
  44. if app_handle.is_none() {
  45. bail!("failed to get the hotkey manager");
  46. }
  47. Ok(app_handle.as_ref().unwrap().global_shortcut_manager())
  48. }
  49. fn register(&self, hotkey: &str, func: &str) -> Result<()> {
  50. let mut manager = self.get_manager()?;
  51. if manager.is_registered(hotkey)? {
  52. manager.unregister(hotkey)?;
  53. }
  54. let f = match func.trim() {
  55. "open_dashboard" => feat::open_dashboard,
  56. "clash_mode_rule" => || feat::change_clash_mode("rule".into()),
  57. "clash_mode_global" => || feat::change_clash_mode("global".into()),
  58. "clash_mode_direct" => || feat::change_clash_mode("direct".into()),
  59. "clash_mode_script" => || feat::change_clash_mode("script".into()),
  60. "toggle_system_proxy" => feat::toggle_system_proxy,
  61. "enable_system_proxy" => feat::enable_system_proxy,
  62. "disable_system_proxy" => feat::disable_system_proxy,
  63. "toggle_tun_mode" => feat::toggle_tun_mode,
  64. "enable_tun_mode" => feat::enable_tun_mode,
  65. "disable_tun_mode" => feat::disable_tun_mode,
  66. _ => bail!("invalid function \"{func}\""),
  67. };
  68. manager.register(hotkey, f)?;
  69. log::info!(target: "app", "register hotkey {hotkey} {func}");
  70. Ok(())
  71. }
  72. fn unregister(&self, hotkey: &str) -> Result<()> {
  73. self.get_manager()?.unregister(hotkey)?;
  74. log::info!(target: "app", "unregister hotkey {hotkey}");
  75. Ok(())
  76. }
  77. pub fn update(&self, new_hotkeys: Vec<String>) -> Result<()> {
  78. let mut current = self.current.lock();
  79. let old_map = Self::get_map_from_vec(&current);
  80. let new_map = Self::get_map_from_vec(&new_hotkeys);
  81. let (del, add) = Self::get_diff(old_map, new_map);
  82. del.iter().for_each(|key| {
  83. let _ = self.unregister(key);
  84. });
  85. add.iter().for_each(|(key, func)| {
  86. log_err!(self.register(key, func));
  87. });
  88. *current = new_hotkeys;
  89. Ok(())
  90. }
  91. fn get_map_from_vec(hotkeys: &Vec<String>) -> HashMap<&str, &str> {
  92. let mut map = HashMap::new();
  93. hotkeys.iter().for_each(|hotkey| {
  94. let mut iter = hotkey.split(',');
  95. let func = iter.next();
  96. let key = iter.next();
  97. if func.is_some() && key.is_some() {
  98. let func = func.unwrap().trim();
  99. let key = key.unwrap().trim();
  100. map.insert(key, func);
  101. }
  102. });
  103. map
  104. }
  105. fn get_diff<'a>(
  106. old_map: HashMap<&'a str, &'a str>,
  107. new_map: HashMap<&'a str, &'a str>,
  108. ) -> (Vec<&'a str>, Vec<(&'a str, &'a str)>) {
  109. let mut del_list = vec![];
  110. let mut add_list = vec![];
  111. old_map.iter().for_each(|(&key, func)| {
  112. match new_map.get(key) {
  113. Some(new_func) => {
  114. if new_func != func {
  115. del_list.push(key);
  116. add_list.push((key, *new_func));
  117. }
  118. }
  119. None => del_list.push(key),
  120. };
  121. });
  122. new_map.iter().for_each(|(&key, &func)| {
  123. if old_map.get(key).is_none() {
  124. add_list.push((key, func));
  125. }
  126. });
  127. (del_list, add_list)
  128. }
  129. }
  130. impl Drop for Hotkey {
  131. fn drop(&mut self) {
  132. if let Ok(mut manager) = self.get_manager() {
  133. let _ = manager.unregister_all();
  134. }
  135. }
  136. }