mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 17:15:38 +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;
|
use tokio::fs;
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub mod speed_rate;
|
pub mod speed_rate;
|
||||||
use crate::config::PrfSelected;
|
use crate::config::{IVerge, PrfSelected};
|
||||||
use crate::core::service;
|
use crate::core::service;
|
||||||
use crate::module::lightweight;
|
use crate::module::lightweight;
|
||||||
use crate::process::AsyncHandler;
|
use crate::process::AsyncHandler;
|
||||||
@@ -83,8 +83,7 @@ pub struct Tray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TrayState {
|
impl TrayState {
|
||||||
pub async fn get_common_tray_icon() -> (bool, Vec<u8>) {
|
async fn get_common_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||||
let verge = Config::verge().await.latest_arc();
|
|
||||||
let is_common_tray_icon = verge.common_tray_icon.unwrap_or(false);
|
let is_common_tray_icon = verge.common_tray_icon.unwrap_or(false);
|
||||||
if is_common_tray_icon
|
if is_common_tray_icon
|
||||||
&& let Ok(Some(common_icon_path)) = find_target_icons("common")
|
&& 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>) {
|
async fn get_sysproxy_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||||
let verge = Config::verge().await.latest_arc();
|
|
||||||
let is_sysproxy_tray_icon = verge.sysproxy_tray_icon.unwrap_or(false);
|
let is_sysproxy_tray_icon = verge.sysproxy_tray_icon.unwrap_or(false);
|
||||||
if is_sysproxy_tray_icon
|
if is_sysproxy_tray_icon
|
||||||
&& let Ok(Some(sysproxy_icon_path)) = find_target_icons("sysproxy")
|
&& 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>) {
|
async fn get_tun_tray_icon(verge: &IVerge) -> (bool, Vec<u8>) {
|
||||||
let verge = Config::verge().await.latest_arc();
|
|
||||||
let is_tun_tray_icon = verge.tun_tray_icon.unwrap_or(false);
|
let is_tun_tray_icon = verge.tun_tray_icon.unwrap_or(false);
|
||||||
if is_tun_tray_icon
|
if is_tun_tray_icon
|
||||||
&& let Ok(Some(tun_icon_path)) = find_target_icons("tun")
|
&& let Ok(Some(tun_icon_path)) = find_target_icons("tun")
|
||||||
@@ -351,7 +348,7 @@ impl Tray {
|
|||||||
|
|
||||||
/// 更新托盘图标
|
/// 更新托盘图标
|
||||||
#[cfg(target_os = "macos")]
|
#[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() {
|
if handle::Handle::global().is_exiting() {
|
||||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
||||||
return Ok(());
|
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 system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
||||||
let tun_mode = verge.enable_tun_mode.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) {
|
let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) {
|
||||||
(true, true) => TrayState::get_tun_tray_icon().await,
|
(true, true) => TrayState::get_tun_tray_icon(verge).await,
|
||||||
(true, false) => TrayState::get_sysproxy_tray_icon().await,
|
(true, false) => TrayState::get_sysproxy_tray_icon(verge).await,
|
||||||
(false, true) => TrayState::get_tun_tray_icon().await,
|
(false, true) => TrayState::get_tun_tray_icon(verge).await,
|
||||||
(false, false) => TrayState::get_common_tray_icon().await,
|
(false, false) => TrayState::get_common_tray_icon(verge).await,
|
||||||
};
|
};
|
||||||
|
|
||||||
let colorful = verge
|
let colorful = verge
|
||||||
@@ -394,7 +395,7 @@ impl Tray {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
#[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() {
|
if handle::Handle::global().is_exiting() {
|
||||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
logging!(debug, Type::Tray, "应用正在退出,跳过托盘图标更新");
|
||||||
return Ok(());
|
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 system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
|
||||||
let tun_mode = verge.enable_tun_mode.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) {
|
let (_is_custom_icon, icon_bytes) = match (*system_mode, *tun_mode) {
|
||||||
(true, true) => TrayState::get_tun_tray_icon().await,
|
(true, true) => TrayState::get_tun_tray_icon(verge).await,
|
||||||
(true, false) => TrayState::get_sysproxy_tray_icon().await,
|
(true, false) => TrayState::get_sysproxy_tray_icon(verge).await,
|
||||||
(false, true) => TrayState::get_tun_tray_icon().await,
|
(false, true) => TrayState::get_tun_tray_icon(verge).await,
|
||||||
(false, false) => TrayState::get_common_tray_icon().await,
|
(false, false) => TrayState::get_common_tray_icon(verge).await,
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?));
|
let _ = tray.set_icon(Some(tauri::image::Image::from_bytes(&icon_bytes)?));
|
||||||
@@ -506,12 +511,12 @@ impl Tray {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
self.update_menu().await?;
|
self.update_menu().await?;
|
||||||
self.update_icon().await?;
|
self.update_icon(None).await?;
|
||||||
self.update_tooltip().await?;
|
self.update_tooltip().await?;
|
||||||
Ok(())
|
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() {
|
if handle::Handle::global().is_exiting() {
|
||||||
logging!(debug, Type::Tray, "应用正在退出,跳过托盘创建");
|
logging!(debug, Type::Tray, "应用正在退出,跳过托盘创建");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -519,8 +524,10 @@ impl Tray {
|
|||||||
|
|
||||||
logging!(info, Type::Tray, "正在从AppHandle创建系统托盘");
|
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)?;
|
let icon = tauri::image::Image::from_bytes(&icon_bytes)?;
|
||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
@@ -530,6 +537,7 @@ impl Tray {
|
|||||||
|
|
||||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||||
let show_menu_on_left_click = {
|
let show_menu_on_left_click = {
|
||||||
|
// TODO 优化这里 复用 verge
|
||||||
let tray_event = { Config::verge().await.latest_arc().tray_event.clone() };
|
let tray_event = { Config::verge().await.latest_arc().tray_event.clone() };
|
||||||
let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into());
|
let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into());
|
||||||
tray_event.as_str() == "tray_menu"
|
tray_event.as_str() == "tray_menu"
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ pub async fn change_clash_mode(mode: String) {
|
|||||||
if clash_data.save_config().await.is_ok() {
|
if clash_data.save_config().await.is_ok() {
|
||||||
handle::Handle::refresh_clash();
|
handle::Handle::refresh_clash();
|
||||||
logging_error!(Type::Tray, tray::Tray::global().update_menu().await);
|
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()
|
let is_auto_close_connection = Config::verge()
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
|
|||||||
} else {
|
} else {
|
||||||
if patch.get("mode").is_some() {
|
if patch.get("mode").is_some() {
|
||||||
logging_error!(Type::Tray, tray::Tray::global().update_menu().await);
|
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()
|
Config::runtime()
|
||||||
.await
|
.await
|
||||||
@@ -180,6 +180,7 @@ fn determine_update_flags(patch: &IVerge) -> i32 {
|
|||||||
update_flags
|
update_flags
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::cognitive_complexity)]
|
||||||
async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<()> {
|
async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<()> {
|
||||||
// Process updates based on flags
|
// Process updates based on flags
|
||||||
if (update_flags & (UpdateFlags::RestartCore as i32)) != 0 {
|
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?;
|
tray::Tray::global().update_menu().await?;
|
||||||
}
|
}
|
||||||
if (update_flags & (UpdateFlags::SystrayIcon as i32)) != 0 {
|
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 {
|
if (update_flags & (UpdateFlags::SystrayTooltip as i32)) != 0 {
|
||||||
tray::Tray::global().update_tooltip().await?;
|
tray::Tray::global().update_tooltip().await?;
|
||||||
|
|||||||
@@ -106,10 +106,17 @@ pub fn find_target_icons(target: &str) -> Result<Option<String>> {
|
|||||||
let icon_path = fs::read_dir(&icons_dir)?
|
let icon_path = fs::read_dir(&icons_dir)?
|
||||||
.filter_map(|entry| entry.ok().map(|e| e.path()))
|
.filter_map(|entry| entry.ok().map(|e| e.path()))
|
||||||
.find(|path| {
|
.find(|path| {
|
||||||
path.file_prefix().is_some_and(|prefix| prefix == target)
|
let prefix_matches = path
|
||||||
&& path
|
.file_prefix()
|
||||||
.extension()
|
.and_then(|p| p.to_str())
|
||||||
.is_some_and(|ext| ext == "ico" || ext == "png")
|
.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
|
icon_path
|
||||||
|
|||||||
@@ -425,7 +425,6 @@ export const LayoutViewer = forwardRef<DialogRef>((_, ref) => {
|
|||||||
await initIconPath();
|
await initIconPath();
|
||||||
onChangeData({ common_tray_icon: true });
|
onChangeData({ common_tray_icon: true });
|
||||||
patchVerge({ common_tray_icon: true });
|
patchVerge({ common_tray_icon: true });
|
||||||
console.log();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|||||||
Reference in New Issue
Block a user