diff --git a/src-tauri/src/commands/failover.rs b/src-tauri/src/commands/failover.rs index d0aad3900..e4e48ddc1 100644 --- a/src-tauri/src/commands/failover.rs +++ b/src-tauri/src/commands/failover.rs @@ -162,7 +162,7 @@ pub async fn set_auto_failover_enabled( // 刷新托盘菜单,确保状态同步 if let Ok(new_menu) = crate::tray::create_tray_menu(&app, &state) { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(crate::tray::TRAY_ID) { let _ = tray.set_menu(Some(new_menu)); } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index dbfb63eae..a1764143a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -168,7 +168,7 @@ async fn update_tray_menu( ) -> Result { match tray::create_tray_menu(&app, state.inner()) { Ok(new_menu) => { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(tray::TRAY_ID) { tray.set_menu(Some(new_menu)) .map_err(|e| format!("更新托盘菜单失败: {e}"))?; return Ok(true); @@ -728,7 +728,7 @@ pub fn run() { let menu = tray::create_tray_menu(app.handle(), &app_state)?; // 构建托盘 - let mut tray_builder = TrayIconBuilder::with_id("main") + let mut tray_builder = TrayIconBuilder::with_id(tray::TRAY_ID) .on_tray_icon_event(|_tray, event| match event { // 左键点击已通过 show_menu_on_left_click(true) 打开菜单,这里不再额外处理 TrayIconEvent::Click { .. } => {} diff --git a/src-tauri/src/proxy/failover_switch.rs b/src-tauri/src/proxy/failover_switch.rs index 1aa1d1b0e..135115ea3 100644 --- a/src-tauri/src/proxy/failover_switch.rs +++ b/src-tauri/src/proxy/failover_switch.rs @@ -111,7 +111,7 @@ impl FailoverSwitchManager { } if let Ok(new_menu) = crate::tray::create_tray_menu(app, app_state.inner()) { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(crate::tray::TRAY_ID) { if let Err(e) = tray.set_menu(Some(new_menu)) { log::error!("[Failover] 更新托盘菜单失败: {e}"); } diff --git a/src-tauri/src/tray.rs b/src-tauri/src/tray.rs index 4bd63f1f6..a1d852bc9 100644 --- a/src-tauri/src/tray.rs +++ b/src-tauri/src/tray.rs @@ -58,6 +58,7 @@ pub struct TrayAppSection { /// Auto 菜单项后缀 pub const AUTO_SUFFIX: &str = "auto"; +pub const TRAY_ID: &str = "cc-switch"; pub const TRAY_SECTIONS: [TrayAppSection; 3] = [ TrayAppSection { @@ -207,7 +208,7 @@ fn handle_auto_click(app: &tauri::AppHandle, app_type: &AppType) -> Result<(), A // 4) 更新托盘菜单 if let Ok(new_menu) = create_tray_menu(app, app_state.inner()) { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(TRAY_ID) { let _ = tray.set_menu(Some(new_menu)); } } @@ -255,7 +256,7 @@ fn handle_provider_click( // 更新托盘菜单 if let Ok(new_menu) = create_tray_menu(app, app_state.inner()) { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(TRAY_ID) { let _ = tray.set_menu(Some(new_menu)); } } @@ -402,7 +403,7 @@ pub fn refresh_tray_menu(app: &tauri::AppHandle) { if let Some(state) = app.try_state::() { if let Ok(new_menu) = create_tray_menu(app, state.inner()) { - if let Some(tray) = app.tray_by_id("main") { + if let Some(tray) = app.tray_by_id(TRAY_ID) { if let Err(e) = tray.set_menu(Some(new_menu)) { log::error!("刷新托盘菜单失败: {e}"); } @@ -411,6 +412,17 @@ pub fn refresh_tray_menu(app: &tauri::AppHandle) { } } +#[cfg(test)] +mod tests { + use super::TRAY_ID; + + #[test] + fn tray_id_is_unique_to_app() { + assert_eq!(TRAY_ID, "cc-switch"); + assert_ne!(TRAY_ID, "main"); + } +} + #[cfg(target_os = "macos")] pub fn apply_tray_policy(app: &tauri::AppHandle, dock_visible: bool) { use tauri::ActivationPolicy;