From 33f575ed4d10b8b6161477eca4c0ef21775e1856 Mon Sep 17 00:00:00 2001 From: Tunglies Date: Mon, 10 Nov 2025 19:06:37 +0800 Subject: [PATCH] refactor: update icon handling in tray methods to accept IVerge reference and improve logging #5379 (#5387) * refactor: update icon handling in tray methods to accept IVerge reference and improve logging #5379 * refactor: simplify icon retrieval logic in Tray implementation --- src-tauri/src/core/tray/mod.rs | 52 +++++++++++-------- src-tauri/src/feat/clash.rs | 2 +- src-tauri/src/feat/config.rs | 7 ++- src-tauri/src/utils/dirs.rs | 15 ++++-- src/components/setting/mods/layout-viewer.tsx | 1 - 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 574e9294f..5595d15a2 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -4,7 +4,7 @@ use tauri_plugin_mihomo::models::Proxies; use tokio::fs; #[cfg(target_os = "macos")] pub mod speed_rate; -use crate::config::PrfSelected; +use crate::config::{IVerge, PrfSelected}; use crate::core::service; use crate::module::lightweight; use crate::process::AsyncHandler; @@ -83,8 +83,7 @@ pub struct Tray { } impl TrayState { - pub async fn get_common_tray_icon() -> (bool, Vec) { - let verge = Config::verge().await.latest_arc(); + async fn get_common_tray_icon(verge: &IVerge) -> (bool, Vec) { let is_common_tray_icon = verge.common_tray_icon.unwrap_or(false); if is_common_tray_icon && let Ok(Some(common_icon_path)) = find_target_icons("common") @@ -120,8 +119,7 @@ impl TrayState { } } - pub async fn get_sysproxy_tray_icon() -> (bool, Vec) { - let verge = Config::verge().await.latest_arc(); + async fn get_sysproxy_tray_icon(verge: &IVerge) -> (bool, Vec) { let is_sysproxy_tray_icon = verge.sysproxy_tray_icon.unwrap_or(false); if is_sysproxy_tray_icon && let Ok(Some(sysproxy_icon_path)) = find_target_icons("sysproxy") @@ -157,8 +155,7 @@ impl TrayState { } } - pub async fn get_tun_tray_icon() -> (bool, Vec) { - let verge = Config::verge().await.latest_arc(); + async fn get_tun_tray_icon(verge: &IVerge) -> (bool, Vec) { let is_tun_tray_icon = verge.tun_tray_icon.unwrap_or(false); if is_tun_tray_icon && let Ok(Some(tun_icon_path)) = find_target_icons("tun") @@ -351,7 +348,7 @@ impl Tray { /// 更新托盘图标 #[cfg(target_os = "macos")] - pub async fn update_icon(&self) -> Result<()> { + pub async fn update_icon(&self, verge: Option<&IVerge>) -> Result<()> { if handle::Handle::global().is_exiting() { logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新"); return Ok(()); @@ -371,15 +368,19 @@ impl Tray { } }; - let verge = Config::verge().await.latest_arc(); + let verge = match verge { + Some(v) => v, + None => &Config::verge().await.data_arc(), + }; + let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false); let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false); let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) { - (true, true) => TrayState::get_tun_tray_icon().await, - (true, false) => TrayState::get_sysproxy_tray_icon().await, - (false, true) => TrayState::get_tun_tray_icon().await, - (false, false) => TrayState::get_common_tray_icon().await, + (true, true) => TrayState::get_tun_tray_icon(verge).await, + (true, false) => TrayState::get_sysproxy_tray_icon(verge).await, + (false, true) => TrayState::get_tun_tray_icon(verge).await, + (false, false) => TrayState::get_common_tray_icon(verge).await, }; let colorful = verge @@ -394,7 +395,7 @@ impl Tray { } #[cfg(not(target_os = "macos"))] - pub async fn update_icon(&self) -> Result<()> { + pub async fn update_icon(&self, verge: Option<&IVerge>) -> Result<()> { if handle::Handle::global().is_exiting() { logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新"); return Ok(()); @@ -414,15 +415,19 @@ impl Tray { } }; - let verge = Config::verge().await.latest_arc(); + let verge = match verge { + Some(v) => v, + None => &Config::verge().await.data_arc(), + }; + let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false); let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false); let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) { - (true, true) => TrayState::get_tun_tray_icon().await, - (true, false) => TrayState::get_sysproxy_tray_icon().await, - (false, true) => TrayState::get_tun_tray_icon().await, - (false, false) => TrayState::get_common_tray_icon().await, + (true, true) => TrayState::get_tun_tray_icon(verge).await, + (true, false) => TrayState::get_sysproxy_tray_icon(verge).await, + (false, true) => TrayState::get_tun_tray_icon(verge).await, + (false, false) => TrayState::get_common_tray_icon(verge).await, }; let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?)); @@ -506,12 +511,12 @@ impl Tray { return Ok(()); } self.update_menu().await?; - self.update_icon().await?; + self.update_icon(None).await?; self.update_tooltip().await?; Ok(()) } - pub async fn create_tray_from_handle(&self, app_handle: &AppHandle) -> Result<()> { + async fn create_tray_from_handle(&self, app_handle: &AppHandle) -> Result<()> { if handle::Handle::global().is_exiting() { logging!(debug, Type::Tray, "应用正在退出,跳过托盘创建"); return Ok(()); @@ -519,8 +524,10 @@ impl Tray { logging!(info, Type::Tray, "正在从AppHandle创建系统托盘"); + let verge = Config::verge().await.data_arc(); + // 获取图标 - let icon_bytes = TrayState::get_common_tray_icon().await.1; + let icon_bytes = TrayState::get_common_tray_icon(&verge).await.1; let icon = tauri::image::Image::from_bytes(&icon_bytes)?; #[cfg(target_os = "linux")] @@ -530,6 +537,7 @@ impl Tray { #[cfg(any(target_os = "macos", target_os = "windows"))] let show_menu_on_left_click = { + // TODO 优化这里 复用 verge let tray_event = { Config::verge().await.latest_arc().tray_event.clone() }; let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into()); tray_event.as_str() == "tray_menu" diff --git a/src-tauri/src/feat/clash.rs b/src-tauri/src/feat/clash.rs index 5fe26b2b3..ce178c15e 100644 --- a/src-tauri/src/feat/clash.rs +++ b/src-tauri/src/feat/clash.rs @@ -82,7 +82,7 @@ pub async fn change_clash_mode(mode: String) { if clash_data.save_config().await.is_ok() { handle::Handle::refresh_clash(); logging_error!(Type::Tray, tray::Tray::global().update_menu().await); - logging_error!(Type::Tray, tray::Tray::global().update_icon().await); + logging_error!(Type::Tray, tray::Tray::global().update_icon(None).await); } let is_auto_close_connection = Config::verge() diff --git a/src-tauri/src/feat/config.rs b/src-tauri/src/feat/config.rs index e7aec2a1f..33c11ea07 100644 --- a/src-tauri/src/feat/config.rs +++ b/src-tauri/src/feat/config.rs @@ -22,7 +22,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> { } else { if patch.get("mode").is_some() { logging_error!(Type::Tray, tray::Tray::global().update_menu().await); - logging_error!(Type::Tray, tray::Tray::global().update_icon().await); + logging_error!(Type::Tray, tray::Tray::global().update_icon(None).await); } Config::runtime() .await @@ -180,6 +180,7 @@ fn determine_update_flags(patch: &IVerge) -> i32 { update_flags } +#[allow(clippy::cognitive_complexity)] async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<()> { // Process updates based on flags if (update_flags & (UpdateFlags::RestartCore as i32)) != 0 { @@ -211,7 +212,9 @@ async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<( tray::Tray::global().update_menu().await?; } if (update_flags & (UpdateFlags::SystrayIcon as i32)) != 0 { - tray::Tray::global().update_icon().await?; + tray::Tray::global() + .update_icon(Some(&Config::verge().await.latest_arc())) + .await?; } if (update_flags & (UpdateFlags::SystrayTooltip as i32)) != 0 { tray::Tray::global().update_tooltip().await?; diff --git a/src-tauri/src/utils/dirs.rs b/src-tauri/src/utils/dirs.rs index 6f678696e..f00e33f33 100644 --- a/src-tauri/src/utils/dirs.rs +++ b/src-tauri/src/utils/dirs.rs @@ -106,10 +106,17 @@ pub fn find_target_icons(target: &str) -> Result> { let icon_path = fs::read_dir(&icons_dir)? .filter_map(|entry| entry.ok().map(|e| e.path())) .find(|path| { - path.file_prefix().is_some_and(|prefix| prefix == target) - && path - .extension() - .is_some_and(|ext| ext == "ico" || ext == "png") + let prefix_matches = path + .file_prefix() + .and_then(|p| p.to_str()) + .is_some_and(|prefix| prefix.starts_with(target)); + let ext_matches = path + .extension() + .and_then(|e| e.to_str()) + .is_some_and(|ext| { + ext.eq_ignore_ascii_case("ico") || ext.eq_ignore_ascii_case("png") + }); + prefix_matches && ext_matches }); icon_path diff --git a/src/components/setting/mods/layout-viewer.tsx b/src/components/setting/mods/layout-viewer.tsx index 6f1482f4d..3f367a3fa 100644 --- a/src/components/setting/mods/layout-viewer.tsx +++ b/src/components/setting/mods/layout-viewer.tsx @@ -425,7 +425,6 @@ export const LayoutViewer = forwardRef((_, ref) => { await initIconPath(); onChangeData({ common_tray_icon: true }); patchVerge({ common_tray_icon: true }); - console.log(); } } }}