154 lines
4.1 KiB
Rust
Raw Normal View History

2022-09-14 01:19:02 +08:00
use crate::{data::*, feat, log_if_err};
use anyhow::{bail, Result};
use std::collections::HashMap;
use tauri::{AppHandle, GlobalShortcutManager};
2022-09-14 01:19:02 +08:00
pub struct Hotkey {
2022-09-18 15:50:03 +08:00
current: Vec<String>, // 保存当前的热键设置
manager: Option<AppHandle>,
2022-09-14 01:19:02 +08:00
}
impl Hotkey {
pub fn new() -> Hotkey {
Hotkey {
2022-09-18 15:50:03 +08:00
current: Vec::new(),
manager: None,
2022-09-14 01:19:02 +08:00
}
}
pub fn init(&mut self, app_handle: AppHandle) -> Result<()> {
self.manager = Some(app_handle);
2022-09-14 01:19:02 +08:00
let data = Data::global();
let verge = data.verge.lock();
if let Some(hotkeys) = verge.hotkeys.as_ref() {
for hotkey in hotkeys.iter() {
let mut iter = hotkey.split(',');
let func = iter.next();
let key = iter.next();
if func.is_some() && key.is_some() {
log_if_err!(self.register(key.unwrap(), func.unwrap()));
2022-09-14 01:19:02 +08:00
} else {
log::error!(target: "app", "invalid hotkey \"{}\":\"{}\"", key.unwrap_or("None"), func.unwrap_or("None"));
2022-09-14 01:19:02 +08:00
}
}
2022-09-18 15:50:03 +08:00
self.current = hotkeys.clone();
2022-09-14 01:19:02 +08:00
}
Ok(())
}
fn get_manager(&self) -> Result<impl GlobalShortcutManager> {
if self.manager.is_none() {
bail!("failed to get hotkey manager");
}
Ok(self.manager.as_ref().unwrap().global_shortcut_manager())
}
fn register(&mut self, hotkey: &str, func: &str) -> Result<()> {
let mut manager = self.get_manager()?;
2022-09-14 01:19:02 +08:00
if manager.is_registered(hotkey)? {
manager.unregister(hotkey)?;
2022-09-14 01:19:02 +08:00
}
let f = match func.trim() {
"clash_mode_rule" => || feat::change_clash_mode("rule"),
"clash_mode_global" => || feat::change_clash_mode("global"),
"clash_mode_direct" => || feat::change_clash_mode("direct"),
"clash_mode_script" => || feat::change_clash_mode("script"),
2022-09-14 01:19:02 +08:00
"toggle_system_proxy" => || feat::toggle_system_proxy(),
"enable_system_proxy" => || feat::enable_system_proxy(),
"disable_system_proxy" => || feat::disable_system_proxy(),
"toggle_tun_mode" => || feat::toggle_tun_mode(),
"enable_tun_mode" => || feat::enable_tun_mode(),
"disable_tun_mode" => || feat::disable_tun_mode(),
_ => bail!("invalid function \"{func}\""),
};
manager.register(hotkey, f)?;
log::info!(target: "app", "register hotkey {hotkey} {func}");
2022-09-14 01:19:02 +08:00
Ok(())
}
fn unregister(&mut self, hotkey: &str) -> Result<()> {
self.get_manager()?.unregister(&hotkey)?;
log::info!(target: "app", "unregister hotkey {hotkey}");
2022-09-14 01:19:02 +08:00
Ok(())
}
pub fn update(&mut self, new_hotkeys: Vec<String>) -> Result<()> {
2022-09-18 15:50:03 +08:00
let current = self.current.to_owned();
let old_map = Self::get_map_from_vec(&current);
2022-09-14 01:19:02 +08:00
let new_map = Self::get_map_from_vec(&new_hotkeys);
let (del, add) = Self::get_diff(old_map, new_map);
del.iter().for_each(|key| {
let _ = self.unregister(key);
});
add.iter().for_each(|(key, func)| {
log_if_err!(self.register(key, func));
});
2022-09-14 01:19:02 +08:00
2022-09-18 15:50:03 +08:00
self.current = new_hotkeys;
2022-09-14 01:19:02 +08:00
Ok(())
}
fn get_map_from_vec<'a>(hotkeys: &'a Vec<String>) -> HashMap<&'a str, &'a str> {
let mut map = HashMap::new();
hotkeys.iter().for_each(|hotkey| {
let mut iter = hotkey.split(',');
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
}
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)>) {
let mut del_list = vec![];
let mut add_list = vec![];
2022-09-14 01:19:02 +08:00
old_map.iter().for_each(|(&key, func)| {
match new_map.get(key) {
2022-09-14 01:19:02 +08:00
Some(new_func) => {
if new_func != func {
del_list.push(key);
add_list.push((key, *new_func));
2022-09-14 01:19:02 +08:00
}
}
None => del_list.push(key),
};
});
2022-09-14 01:19:02 +08:00
new_map.iter().for_each(|(&key, &func)| {
2022-09-14 01:19:02 +08:00
if old_map.get(key).is_none() {
add_list.push((key, func));
2022-09-14 01:19:02 +08:00
}
});
(del_list, add_list)
2022-09-14 01:19:02 +08:00
}
}
impl Drop for Hotkey {
fn drop(&mut self) {
if let Ok(mut manager) = self.get_manager() {
let _ = manager.unregister_all();
}
2022-09-14 01:19:02 +08:00
}
}