diff --git a/src-tauri/src/config/config.rs b/src-tauri/src/config/config.rs index c523bbda..4cdc5ac7 100644 --- a/src-tauri/src/config/config.rs +++ b/src-tauri/src/config/config.rs @@ -65,7 +65,7 @@ impl Config { d.enable_tun_mode = Some(false); }); verge.apply(); - let _ = tray::Tray::global().update_tray_display().await; + let _ = tray::Tray::global().update_menu().await; // 分离数据获取和异步调用避免Send问题 let verge_data = Config::verge().await.latest_arc(); diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index 40a01b3a..427015d8 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -532,7 +532,7 @@ impl ServiceManager { return Err(anyhow::anyhow!("服务不可用: {}", reason)); } } - let _ = tray::Tray::global().update_tray_display().await; + let _ = tray::Tray::global().update_menu().await; Ok(()) } } diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index e88f863d..a3eb9791 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -161,9 +161,7 @@ impl Timer { .set_frequency_count_down_by_seconds(3, 3) .spawn_async_routine(|| async move { logging!(debug, Type::Timer, "Updating tray menu"); - crate::core::tray::Tray::global() - .update_tray_display() - .await + crate::core::tray::Tray::global().update_menu().await }) .context("failed to create update tray menu timer task")?; delay_timer diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index e8797ece..01818eb9 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -1,5 +1,4 @@ use once_cell::sync::OnceCell; -use tauri::Emitter; use tauri::tray::TrayIconBuilder; use tauri_plugin_mihomo::models::Proxies; use tokio::fs; @@ -431,24 +430,6 @@ impl Tray { Ok(()) } - /// 更新托盘显示状态的函数 - pub async fn update_tray_display(&self) -> Result<()> { - if handle::Handle::global().is_exiting() { - logging!(debug, Type::Tray, "应用正在退出,跳过托盘显示状态更新"); - return Ok(()); - } - - let app_handle = handle::Handle::app_handle(); - let _tray = app_handle - .tray_by_id("main") - .ok_or_else(|| anyhow::anyhow!("Failed to get main tray"))?; - - // 更新菜单 - self.update_menu().await?; - - Ok(()) - } - /// 更新托盘提示 pub async fn update_tooltip(&self) -> Result<()> { if handle::Handle::global().is_exiting() { @@ -523,9 +504,7 @@ impl Tray { logging!(debug, Type::Tray, "应用正在退出,跳过托盘局部更新"); return Ok(()); } - // self.update_menu().await?; - // 更新轻量模式显示状态 - self.update_tray_display().await?; + self.update_menu().await?; self.update_icon().await?; self.update_tooltip().await?; Ok(()) @@ -620,22 +599,6 @@ impl Tray { tray.on_menu_event(on_menu_event); Ok(()) } - - // 托盘统一的状态更新函数 - pub async fn update_all_states(&self) -> Result<()> { - if handle::Handle::global().is_exiting() { - logging!(debug, Type::Tray, "应用正在退出,跳过托盘状态更新"); - return Ok(()); - } - - // 确保所有状态更新完成 - self.update_tray_display().await?; - // self.update_menu().await?; - self.update_icon().await?; - self.update_tooltip().await?; - - Ok(()) - } } fn create_hotkeys(hotkeys: &Option>) -> HashMap { @@ -1253,66 +1216,20 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { } id if id.starts_with("proxy_") => { // proxy_{group_name}_{proxy_name} - let parts: Vec<&str> = id.splitn(3, '_').collect(); - - if parts.len() == 3 && parts[0] == "proxy" { - let group_name = parts[1]; - let proxy_name = parts[2]; - - match handle::Handle::mihomo() - .await - .select_node_for_group(group_name, proxy_name) - .await - { - Ok(_) => { - logging!( - info, - Type::Tray, - "切换代理成功: {} -> {}", - group_name, - proxy_name - ); - let _ = handle::Handle::app_handle() - .emit("verge://refresh-proxy-config", ()); - } - Err(e) => { - logging!( - error, - Type::Tray, - "切换代理失败: {} -> {}, 错误: {:?}", - group_name, - proxy_name, - e - ); - - // Fallback to IPC update - if (handle::Handle::mihomo() - .await - .select_node_for_group(group_name, proxy_name) - .await) - .is_ok() - { - logging!( - info, - Type::Tray, - "代理切换回退成功: {} -> {}", - group_name, - proxy_name - ); - - let app_handle = handle::Handle::app_handle(); - let _ = app_handle.emit("verge://force-refresh-proxies", ()); - } - } - } - } + let rest = match id.strip_prefix("proxy_") { + Some(r) => r, + None => return, + }; + let (group_name, proxy_name) = match rest.split_once('_') { + Some((g, p)) => (g, p), + None => return, + }; + feat::switch_proxy_node(group_name, proxy_name).await; } _ => {} } - // Ensure tray state update is awaited and properly handled - if let Err(e) = Tray::global().update_all_states().await { - logging!(warn, Type::Tray, "Failed to update tray state: {e}"); - } + // We dont expected to refresh tray state here + // as the inner handle function (SHOULD) already takes care of it }); } diff --git a/src-tauri/src/feat/profile.rs b/src-tauri/src/feat/profile.rs index 6bf267e5..b43c8950 100644 --- a/src-tauri/src/feat/profile.rs +++ b/src-tauri/src/feat/profile.rs @@ -7,6 +7,7 @@ use crate::{ }; use anyhow::{Result, bail}; use smartstring::alias::String; +use tauri::Emitter; /// Toggle proxy profile pub async fn toggle_proxy_profile(profile_index: String) { @@ -23,6 +24,66 @@ pub async fn toggle_proxy_profile(profile_index: String) { } } +pub async fn switch_proxy_node(group_name: &str, proxy_name: &str) { + match handle::Handle::mihomo() + .await + .select_node_for_group(group_name, proxy_name) + .await + { + Ok(_) => { + logging!( + info, + Type::Tray, + "切换代理成功: {} -> {}", + group_name, + proxy_name + ); + let _ = handle::Handle::app_handle().emit("verge://refresh-proxy-config", ()); + let _ = tray::Tray::global().update_menu().await; + return; + } + Err(err) => { + logging!( + error, + Type::Tray, + "切换代理失败: {} -> {}, 错误: {:?}", + group_name, + proxy_name, + err + ); + } + } + + match handle::Handle::mihomo() + .await + .select_node_for_group(group_name, proxy_name) + .await + { + Ok(_) => { + logging!( + info, + Type::Tray, + "代理切换回退成功: {} -> {}", + group_name, + proxy_name + ); + let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ()); + let _ = tray::Tray::global().update_menu().await; + } + Err(err) => { + logging!( + error, + Type::Tray, + "代理切换最终失败: {} -> {}, 错误: {:?}", + group_name, + proxy_name, + err + ); + let _ = handle::Handle::app_handle().emit("verge://force-refresh-proxies", ()); + } + } +} + async fn should_update_profile( uid: &String, ignore_auto_update: bool, diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index 802b83da..86017df7 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -79,7 +79,7 @@ pub fn is_in_lightweight_mode() -> bool { } async fn refresh_lightweight_tray_state() { - if let Err(err) = Tray::global().update_tray_display().await { + if let Err(err) = Tray::global().update_menu().await { logging!(warn, Type::Lightweight, "更新托盘轻量模式状态失败: {err}"); } }