From 8fa7fb3b1f0fb3a49237b0e8f416f37323ce797d Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Sun, 18 Sep 2022 15:50:03 +0800
Subject: [PATCH] feat: refresh ui when hotkey clicked

---
 src-tauri/src/core/hotkey.rs | 20 ++++++++------------
 src-tauri/src/core/mod.rs    |  6 ++++++
 src-tauri/src/feat.rs        | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs
index d0239b8..815a2e4 100644
--- a/src-tauri/src/core/hotkey.rs
+++ b/src-tauri/src/core/hotkey.rs
@@ -4,12 +4,14 @@ use std::collections::HashMap;
 use tauri_hotkey::{parse_hotkey, HotkeyManager};
 
 pub struct Hotkey {
+  current: Vec<String>, // 保存当前的热键设置
   manager: HotkeyManager,
 }
 
 impl Hotkey {
   pub fn new() -> Hotkey {
     Hotkey {
+      current: Vec::new(),
       manager: HotkeyManager::new(),
     }
   }
@@ -30,6 +32,7 @@ impl Hotkey {
           log::error!(target: "app", "invalid hotkey \"{}\":\"{}\"", func.unwrap_or("None"), key.unwrap_or("None"));
         }
       }
+      self.current = hotkeys.clone();
     }
 
     Ok(())
@@ -58,23 +61,20 @@ impl Hotkey {
     };
 
     self.manager.register(hotkey, f)?;
+    log::info!(target: "app", "register hotkey {func} {key}");
     Ok(())
   }
 
   fn unregister(&mut self, key: &str) -> Result<()> {
     let hotkey = parse_hotkey(key.trim())?;
     self.manager.unregister(&hotkey)?;
+    log::info!(target: "app", "unregister hotkey {key}");
     Ok(())
   }
 
   pub fn update(&mut self, new_hotkeys: Vec<String>) -> Result<()> {
-    let data = Data::global();
-    let mut verge = data.verge.lock();
-
-    let default = Vec::new();
-    let old_hotkeys = verge.hotkeys.as_ref().unwrap_or(&default);
-
-    let old_map = Self::get_map_from_vec(old_hotkeys);
+    let current = self.current.to_owned();
+    let old_map = Self::get_map_from_vec(&current);
     let new_map = Self::get_map_from_vec(&new_hotkeys);
 
     for diff in Self::get_diff(old_map, new_map).iter() {
@@ -92,11 +92,7 @@ impl Hotkey {
       }
     }
 
-    verge.patch_config(Verge {
-      hotkeys: Some(new_hotkeys),
-      ..Verge::default()
-    })?;
-
+    self.current = new_hotkeys;
     Ok(())
   }
 
diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs
index e5c162f..3d122b3 100644
--- a/src-tauri/src/core/mod.rs
+++ b/src-tauri/src/core/mod.rs
@@ -202,6 +202,11 @@ impl Core {
       handle.update_systray()?;
     }
 
+    if patch.hotkeys.is_some() {
+      let mut hotkey = self.hotkey.lock();
+      hotkey.update(patch.hotkeys.unwrap())?;
+    }
+
     Ok(())
   }
 
@@ -225,6 +230,7 @@ impl Core {
 
     // update tray
     let handle = self.handle.lock();
+    handle.refresh_clash();
     handle.update_systray_clash()?;
 
     Ok(())
diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs
index 4b9bc4c..47dabbf 100644
--- a/src-tauri/src/feat.rs
+++ b/src-tauri/src/feat.rs
@@ -21,6 +21,9 @@ pub fn toggle_system_proxy() {
     enable_system_proxy: Some(enable),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
 
 // 打开系统代理
@@ -30,6 +33,9 @@ pub fn enable_system_proxy() {
     enable_system_proxy: Some(true),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
 
 // 关闭系统代理
@@ -39,6 +45,9 @@ pub fn disable_system_proxy() {
     enable_system_proxy: Some(false),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
 
 // 切换tun模式
@@ -54,6 +63,9 @@ pub fn toggle_tun_mode() {
     enable_tun_mode: Some(enable),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
 
 // 打开tun模式
@@ -63,6 +75,9 @@ pub fn enable_tun_mode() {
     enable_tun_mode: Some(true),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
 
 // 关闭tun模式
@@ -72,4 +87,7 @@ pub fn disable_tun_mode() {
     enable_tun_mode: Some(false),
     ..Verge::default()
   }));
+
+  let handle = core.handle.lock();
+  let _ = handle.refresh_verge();
 }
-- 
GitLab