feat: Support Custom Tray Icon

This commit is contained in:
MystiPanda
2024-02-24 00:52:21 +08:00
parent 447f7530af
commit 6f546a424e
10 changed files with 201 additions and 10 deletions

View File

@@ -36,6 +36,13 @@ pub struct IVerge {
/// show memory info (only for Clash Meta)
pub enable_memory_usage: Option<bool>,
/// common tray icon
pub common_tray_icon: Option<String>,
pub sysproxy_tray_icon: Option<String>,
pub tun_tray_icon: Option<String>,
/// clash tun mode
pub enable_tun_mode: Option<bool>,
@@ -204,6 +211,9 @@ impl IVerge {
patch!(startup_script);
patch!(traffic_graph);
patch!(enable_memory_usage);
patch!(common_tray_icon);
patch!(sysproxy_tray_icon);
patch!(tun_tray_icon);
patch!(enable_tun_mode);
patch!(enable_service_mode);

View File

@@ -129,26 +129,47 @@ impl Tray {
let verge = verge.latest();
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
let common_tray_icon = verge.common_tray_icon.clone().unwrap_or("".to_string());
let sysproxy_tray_icon = verge.sysproxy_tray_icon.clone().unwrap_or("".to_string());
let tun_tray_icon = verge.tun_tray_icon.clone().unwrap_or("".to_string());
let mut indication_icon = if *system_proxy {
#[cfg(not(target_os = "macos"))]
let icon = include_bytes!("../../icons/tray-icon-sys.png").to_vec();
let mut icon = include_bytes!("../../icons/tray-icon-sys.png").to_vec();
#[cfg(target_os = "macos")]
let icon = include_bytes!("../../icons/mac-tray-icon-sys.png").to_vec();
let mut icon = include_bytes!("../../icons/mac-tray-icon-sys.png").to_vec();
if !sysproxy_tray_icon.is_empty() {
let path = std::path::Path::new(&sysproxy_tray_icon);
if path.exists() {
icon = std::fs::read(path).unwrap();
}
}
icon
} else {
#[cfg(not(target_os = "macos"))]
let icon = include_bytes!("../../icons/tray-icon.png").to_vec();
let mut icon = include_bytes!("../../icons/tray-icon.png").to_vec();
#[cfg(target_os = "macos")]
let icon = include_bytes!("../../icons/mac-tray-icon.png").to_vec();
let mut icon = include_bytes!("../../icons/mac-tray-icon.png").to_vec();
if !common_tray_icon.is_empty() {
let path = std::path::Path::new(&common_tray_icon);
if path.exists() {
icon = std::fs::read(path).unwrap();
}
}
icon
};
if *tun_mode {
#[cfg(not(target_os = "macos"))]
let icon = include_bytes!("../../icons/tray-icon-tun.png").to_vec();
let mut icon = include_bytes!("../../icons/tray-icon-tun.png").to_vec();
#[cfg(target_os = "macos")]
let icon = include_bytes!("../../icons/mac-tray-icon-tun.png").to_vec();
let mut icon = include_bytes!("../../icons/mac-tray-icon-tun.png").to_vec();
if !tun_tray_icon.is_empty() {
let path = std::path::Path::new(&tun_tray_icon);
if path.exists() {
icon = std::fs::read(path).unwrap();
}
}
indication_icon = icon
}

View File

@@ -230,6 +230,9 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
let proxy_bypass = patch.system_proxy_bypass;
let language = patch.language;
let port = patch.verge_mixed_port;
let common_tray_icon = patch.common_tray_icon;
let sysproxy_tray_icon = patch.sysproxy_tray_icon;
let tun_tray_icon = patch.tun_tray_icon;
match {
#[cfg(target_os = "windows")]
@@ -269,7 +272,12 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
if language.is_some() {
handle::Handle::update_systray()?;
} else if system_proxy.or(tun_mode).is_some() {
} else if system_proxy.is_some()
|| tun_mode.is_some()
|| common_tray_icon.is_some()
|| sysproxy_tray_icon.is_some()
|| tun_tray_icon.is_some()
{
handle::Handle::update_systray_part()?;
}