From 5cf3e1a81753fd0b62bc0d09e6da89cc283e284d Mon Sep 17 00:00:00 2001 From: Tunglies Date: Wed, 11 Jun 2025 00:19:06 +0800 Subject: [PATCH] refactor: streamline macOS-specific code by consolidating conditional imports and logic in sysopt, tray, lightweight, and mihomo modules --- src-tauri/src/core/sysopt.rs | 10 +-- src-tauri/src/core/tray/mod.rs | 110 +++++++++++++++++----------- src-tauri/src/lib.rs | 1 + src-tauri/src/module/lightweight.rs | 5 +- src-tauri/src/module/mihomo.rs | 5 +- 5 files changed, 81 insertions(+), 50 deletions(-) diff --git a/src-tauri/src/core/sysopt.rs b/src-tauri/src/core/sysopt.rs index 004108b5..8caa3684 100644 --- a/src-tauri/src/core/sysopt.rs +++ b/src-tauri/src/core/sysopt.rs @@ -235,13 +235,11 @@ impl Sysopt { } else { return Ok(()); } + } else if let Err(e) = startup_shortcut::remove_shortcut() { + log::error!(target: "app", "删除启动快捷方式失败: {}", e); + self.try_original_autostart_method(is_enable); } else { - if let Err(e) = startup_shortcut::remove_shortcut() { - log::error!(target: "app", "删除启动快捷方式失败: {}", e); - self.try_original_autostart_method(is_enable); - } else { - return Ok(()); - } + return Ok(()); } } diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 120cfc63..ab58c13e 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -288,6 +288,7 @@ impl Tray { } /// 更新托盘图标 + #[cfg(target_os = "macos")] pub fn update_icon(&self, rate: Option) -> Result<()> { let app_handle = match handle::Handle::global().app_handle() { Some(handle) => handle, @@ -315,58 +316,83 @@ impl Tray { (false, true) => TrayState::get_tun_tray_icon(), (false, false) => TrayState::get_common_tray_icon(), }; - #[cfg(target_os = "macos")] - { - let enable_tray_speed = verge.enable_tray_speed.unwrap_or(false); - let enable_tray_icon = verge.enable_tray_icon.unwrap_or(true); - let colorful = verge.tray_icon.clone().unwrap_or("monochrome".to_string()); - let is_colorful = colorful == "colorful"; - if !enable_tray_speed { - let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?)); - let _ = tray.set_icon_as_template(!is_colorful); - return Ok(()); - } + let enable_tray_speed = verge.enable_tray_speed.unwrap_or(false); + let enable_tray_icon = verge.enable_tray_icon.unwrap_or(true); + let colorful = verge.tray_icon.clone().unwrap_or("monochrome".to_string()); + let is_colorful = colorful == "colorful"; - let rate = if let Some(rate) = rate { - Some(rate) - } else { - let guard = self.speed_rate.lock(); - if let Some(guard) = guard.as_ref() { - if let Some(rate) = guard.get_curent_rate() { - Some(rate) - } else { - Some(Rate::default()) - } + if !enable_tray_speed { + let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?)); + let _ = tray.set_icon_as_template(!is_colorful); + return Ok(()); + } + + let rate = if let Some(rate) = rate { + Some(rate) + } else { + let guard = self.speed_rate.lock(); + if let Some(guard) = guard.as_ref() { + if let Some(rate) = guard.get_curent_rate() { + Some(rate) } else { Some(Rate::default()) } + } else { + Some(Rate::default()) + } + }; + + let mut rate_guard = self.rate_cache.lock(); + if *rate_guard != rate { + *rate_guard = rate; + + let bytes = if enable_tray_icon { + Some(icon_bytes) + } else { + None }; - let mut rate_guard = self.rate_cache.lock(); - if *rate_guard != rate { - *rate_guard = rate; - - let bytes = if enable_tray_icon { - Some(icon_bytes) - } else { - None - }; - - let rate = rate_guard.as_ref(); - if let Ok(rate_bytes) = SpeedRate::add_speed_text(is_custom_icon, bytes, rate) { - let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&rate_bytes)?)); - let _ = tray.set_icon_as_template(!is_custom_icon && !is_colorful); - } + let rate = rate_guard.as_ref(); + if let Ok(rate_bytes) = SpeedRate::add_speed_text(is_custom_icon, bytes, rate) { + let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&rate_bytes)?)); + let _ = tray.set_icon_as_template(!is_custom_icon && !is_colorful); } - Ok(()) } + Ok(()) + } - #[cfg(not(target_os = "macos"))] - { - let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?)); - Ok(()) - } + #[cfg(not(target_os = "macos"))] + pub fn update_icon(&self, _rate: Option) -> Result<()> { + let app_handle = match handle::Handle::global().app_handle() { + Some(handle) => handle, + None => { + log::warn!(target: "app", "更新托盘图标失败: app_handle不存在"); + return Ok(()); + } + }; + + let tray = match app_handle.tray_by_id("main") { + Some(tray) => tray, + None => { + log::warn!(target: "app", "更新托盘图标失败: 托盘不存在"); + return Ok(()); + } + }; + + let verge = Config::verge().latest().clone(); + 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(), + (true, false) => TrayState::get_sysproxy_tray_icon(), + (false, true) => TrayState::get_tun_tray_icon(), + (false, false) => TrayState::get_common_tray_icon(), + }; + + let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?)); + Ok(()) } /// 更新托盘提示 diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4c83036e..35f68cac 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -16,6 +16,7 @@ use config::Config; use std::sync::{Mutex, Once}; use tauri::AppHandle; use tauri::Manager; +#[cfg(target_os = "macos")] use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_deep_link::DeepLinkExt; use tokio::time::{timeout, Duration}; diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index d8033f17..11f8c64a 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -3,9 +3,11 @@ use crate::{ core::{handle, timer::Timer}, log_err, logging, logging_error, utils::logging::Type, - AppHandleManager, }; +#[cfg(target_os = "macos")] +use crate::AppHandleManager; + use anyhow::{Context, Result}; use delay_timer::prelude::TaskBuilder; use once_cell::sync::OnceCell; @@ -112,6 +114,7 @@ pub fn exit_lightweight_mode() { exit_lock.0 = false; } +#[cfg(target_os = "macos")] pub fn add_light_weight_timer() { logging_error!(Type::Lightweight, setup_light_weight_timer()); } diff --git a/src-tauri/src/module/mihomo.rs b/src-tauri/src/module/mihomo.rs index 79e53104..0fc40d31 100644 --- a/src-tauri/src/module/mihomo.rs +++ b/src-tauri/src/module/mihomo.rs @@ -3,7 +3,9 @@ use mihomo_api; use once_cell::sync::Lazy; use parking_lot::{Mutex, RwLock}; use std::time::{Duration, Instant}; -use tauri::http::{HeaderMap, HeaderValue}; +use tauri::http::HeaderMap; +#[cfg(target_os = "macos")] +use tauri::http::HeaderValue; // 缓存的最大有效期(5秒) const CACHE_TTL: Duration = Duration::from_secs(5); @@ -105,6 +107,7 @@ impl MihomoManager { } // 提供默认值的版本,避免在connection_info为None时panic + #[cfg(target_os = "macos")] fn get_clash_client_info_or_default() -> (String, HeaderMap) { Self::get_clash_client_info().unwrap_or_else(|| { let mut headers = HeaderMap::new();