mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
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
This commit is contained in:
@@ -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<u8>) {
|
||||
let verge = Config::verge().await.latest_arc();
|
||||
async fn get_common_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||
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<u8>) {
|
||||
let verge = Config::verge().await.latest_arc();
|
||||
async fn get_sysproxy_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||
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<u8>) {
|
||||
let verge = Config::verge().await.latest_arc();
|
||||
async fn get_tun_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user