diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index d276333..73c5d0a 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -10,7 +10,7 @@ use crate::{ use anyhow::Result; use tauri::{ menu::CheckMenuItem, - tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, + tray::{MouseButton, MouseButtonState, TrayIconEvent}, }; use tauri::{ menu::{MenuEvent, MenuItem, PredefinedMenuItem, Submenu}, @@ -20,47 +20,46 @@ use tauri::{AppHandle, Manager}; pub struct Tray {} impl Tray { - pub fn create_systray(app_handle: &AppHandle) -> Result<()> { - let _ = TrayIconBuilder::with_id("main") - .on_tray_icon_event(|tray, event| { - let tray_event = { Config::verge().latest().tray_event.clone() }; - let tray_event: String = tray_event.unwrap_or("main_window".into()); + pub fn update_systray(app_handle: &AppHandle) -> Result<()> { + let tray = app_handle.tray_by_id("main").unwrap(); - #[cfg(target_os = "macos")] - if let TrayIconEvent::Click { - button: MouseButton::Right, - button_state: MouseButtonState::Down, - .. - } = event - { - let app = tray.app_handle(); - match tray_event.as_str() { - "system_proxy" => feat::toggle_system_proxy(), - "tun_mode" => feat::toggle_tun_mode(), - "main_window" => resolve::create_window(app), - _ => {} - } - } + tray.on_tray_icon_event(|tray, event| { + let tray_event = { Config::verge().latest().tray_event.clone() }; + let tray_event: String = tray_event.unwrap_or("main_window".into()); - #[cfg(not(target_os = "macos"))] - if let TrayIconEvent::Click { - button: MouseButton::Left, - button_state: MouseButtonState::Down, - .. - } = event - { - let app = tray.app_handle(); - match tray_event.as_str() { - "system_proxy" => feat::toggle_system_proxy(), - "tun_mode" => feat::toggle_tun_mode(), - "main_window" => resolve::create_window(app), - _ => {} - } + #[cfg(target_os = "macos")] + if let TrayIconEvent::Click { + button: MouseButton::Right, + button_state: MouseButtonState::Down, + .. + } = event + { + let app = tray.app_handle(); + match tray_event.as_str() { + "system_proxy" => feat::toggle_system_proxy(), + "tun_mode" => feat::toggle_tun_mode(), + "main_window" => resolve::create_window(app), + _ => {} } - }) - .on_menu_event(on_menu_event) - .menu_on_left_click(false) - .build(app_handle); + } + + #[cfg(not(target_os = "macos"))] + if let TrayIconEvent::Click { + button: MouseButton::Left, + button_state: MouseButtonState::Down, + .. + } = event + { + let app = tray.app_handle(); + match tray_event.as_str() { + "system_proxy" => feat::toggle_system_proxy(), + "tun_mode" => feat::toggle_tun_mode(), + "main_window" => resolve::create_window(app), + _ => {} + } + } + }); + tray.on_menu_event(on_menu_event); Ok(()) } diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 80c271a..2f06aeb 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -85,7 +85,7 @@ pub async fn resolve_setup(app: &mut App) { server::embed_server(app.app_handle()); log::trace!("init system tray"); - log_err!(tray::Tray::create_systray(&app.app_handle())); + log_err!(tray::Tray::update_systray(&app.app_handle())); let silent_start = { Config::verge().data().enable_silent_start }; if !silent_start.unwrap_or(false) {