From d8d75b4afa399767525f24fc896594b18ee87224 Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Sun, 26 Nov 2023 18:35:21 +0800 Subject: [PATCH 1/2] feat: Config Tray Click Event --- src-tauri/src/config/verge.rs | 4 ++++ src-tauri/src/core/tray.rs | 17 ++++++++++++----- src/components/setting/setting-verge.tsx | 21 +++++++++++++++++++-- src/locales/en.json | 2 ++ src/locales/ru.json | 2 ++ src/locales/zh.json | 2 ++ src/services/types.d.ts | 1 + 7 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 961280e..f0c1969 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -23,6 +23,9 @@ pub struct IVerge { /// maybe be able to set the alpha pub theme_blur: Option, + /// tray click event + pub tray_event: Option, + /// enable traffic graph default is true pub traffic_graph: Option, @@ -166,6 +169,7 @@ impl IVerge { patch!(language); patch!(theme_mode); patch!(theme_blur); + patch!(tray_event); patch!(traffic_graph); patch!(enable_memory_usage); diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index 01a5045..da0d890 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -145,9 +145,20 @@ impl Tray { Ok(()) } - + pub fn on_left_click(app_handle: &AppHandle) { + let tray_event = { Config::verge().latest().tray_event.clone() }; + let tray_event = tray_event.unwrap_or("main_window".into()); + println!("{tray_event}"); + match tray_event.as_str() { + "system_proxy" => feat::toggle_system_proxy(), + "tun_mode" => feat::toggle_tun_mode(), + _ => resolve::create_window(app_handle), + } + } pub fn on_system_tray_event(app_handle: &AppHandle, event: SystemTrayEvent) { match event { + #[cfg(not(target_os = "linux"))] + SystemTrayEvent::LeftClick { .. } => Tray::on_left_click(app_handle), SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { mode @ ("rule_mode" | "global_mode" | "direct_mode" | "script_mode") => { let mode = &mode[0..mode.len() - 5]; @@ -177,10 +188,6 @@ impl Tray { } _ => {} }, - #[cfg(target_os = "windows")] - SystemTrayEvent::LeftClick { .. } => { - resolve::create_window(app_handle); - } _ => {} } } diff --git a/src/components/setting/setting-verge.tsx b/src/components/setting/setting-verge.tsx index d54094d..b0507f3 100644 --- a/src/components/setting/setting-verge.tsx +++ b/src/components/setting/setting-verge.tsx @@ -29,8 +29,7 @@ const SettingVerge = ({ onError }: Props) => { const { t } = useTranslation(); const { verge, patchVerge, mutateVerge } = useVerge(); - const { theme_mode, language } = verge ?? {}; - + const { theme_mode, language, tray_event } = verge ?? {}; const configRef = useRef(null); const hotkeyRef = useRef(null); const miscRef = useRef(null); @@ -91,6 +90,24 @@ const SettingVerge = ({ onError }: Props) => { + {OS !== "linux" && ( + + e.target.value} + onChange={(e) => onChangeData({ tray_event: e })} + onGuard={(e) => patchVerge({ tray_event: e })} + > + + + + )} + Date: Sun, 26 Nov 2023 18:49:48 +0800 Subject: [PATCH 2/2] chore: Remove Debug Info --- src-tauri/src/core/tray.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index da0d890..3921f51 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -145,16 +145,17 @@ impl Tray { Ok(()) } + pub fn on_left_click(app_handle: &AppHandle) { let tray_event = { Config::verge().latest().tray_event.clone() }; let tray_event = tray_event.unwrap_or("main_window".into()); - println!("{tray_event}"); match tray_event.as_str() { "system_proxy" => feat::toggle_system_proxy(), "tun_mode" => feat::toggle_tun_mode(), _ => resolve::create_window(app_handle), } } + pub fn on_system_tray_event(app_handle: &AppHandle, event: SystemTrayEvent) { match event { #[cfg(not(target_os = "linux"))]