Skip to content

Commit

Permalink
feat: support monochrome tray icon
Browse files Browse the repository at this point in the history
  • Loading branch information
MystiPanda committed May 15, 2024
1 parent 72edd2e commit f24e4bb
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 14 deletions.
Binary file removed src-tauri/icons/mac-tray-icon.png
Binary file not shown.
Binary file added src-tauri/icons/tray-icon-mono.ico
Binary file not shown.
Binary file added src-tauri/icons/tray-icon-sys-mono.ico
Binary file not shown.
Binary file added src-tauri/icons/tray-icon-tun-mono.ico
Binary file not shown.
8 changes: 8 additions & 0 deletions src-tauri/src/config/verge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct IVerge {
/// common tray icon
pub common_tray_icon: Option<bool>,

/// tray icon
pub tray_icon: Option<String>,

/// menu icon
pub menu_icon: Option<String>,

Expand Down Expand Up @@ -198,6 +201,10 @@ impl IVerge {
traffic_graph: Some(true),
enable_memory_usage: Some(true),
enable_group_icon: Some(true),
#[cfg(target_os = "macos")]
tray_icon: Some("monochrome".into()),
#[cfg(not(target_os = "macos"))]
tray_icon: Some("colorful".into()),
menu_icon: Some("monochrome".into()),
common_tray_icon: Some(false),
sysproxy_tray_icon: Some(false),
Expand Down Expand Up @@ -255,6 +262,7 @@ impl IVerge {
patch!(traffic_graph);
patch!(enable_memory_usage);
patch!(enable_group_icon);
patch!(tray_icon);
patch!(menu_icon);
patch!(common_tray_icon);
patch!(sysproxy_tray_icon);
Expand Down
53 changes: 40 additions & 13 deletions src-tauri/src/core/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,34 @@ 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);
#[cfg(target_os = "macos")]
let tray_icon = verge.tray_icon.clone().unwrap_or("monochrome".to_string());
#[cfg(not(target_os = "macos"))]
let tray_icon = verge.tray_icon.clone().unwrap_or("colorful".to_string());
let common_tray_icon = verge.common_tray_icon.as_ref().unwrap_or(&false);
let sysproxy_tray_icon = verge.sysproxy_tray_icon.as_ref().unwrap_or(&false);
let tun_tray_icon = verge.tun_tray_icon.as_ref().unwrap_or(&false);

#[cfg(target_os = "macos")]
match tray_icon.as_str() {
"monochrome" => {
let _ = tray.set_icon_as_template(true);
}
"colorful" => {
#[cfg(target_os = "macos")]
let _ = tray.set_icon_as_template(false);
}
_ => {}
}
let mut indication_icon = if *system_proxy {
#[cfg(target_os = "macos")]
let _ = tray.set_icon_as_template(false);
let mut icon = include_bytes!("../../icons/tray-icon-sys.ico").to_vec();
let mut icon = match tray_icon.as_str() {
"monochrome" => include_bytes!("../../icons/tray-icon-sys-mono.ico").to_vec(),
"colorful" => include_bytes!("../../icons/tray-icon-sys.ico").to_vec(),
#[cfg(target_os = "macos")]
_ => include_bytes!("../../icons/tray-icon-sys-mono.ico").to_vec(),
#[cfg(not(target_os = "macos"))]
_ => include_bytes!("../../icons/tray-icon-sys.ico").to_vec(),
};

if *sysproxy_tray_icon {
let icon_dir_path = dirs::app_home_dir()?.join("icons");
let png_path = icon_dir_path.join("sysproxy.png");
Expand All @@ -192,12 +212,14 @@ impl Tray {
}
icon
} else {
#[cfg(target_os = "macos")]
let _ = tray.set_icon_as_template(true);
#[cfg(target_os = "macos")]
let mut icon = include_bytes!("../../icons/mac-tray-icon.png").to_vec();
#[cfg(not(target_os = "macos"))]
let mut icon = include_bytes!("../../icons/tray-icon.ico").to_vec();
let mut icon = match tray_icon.as_str() {
"monochrome" => include_bytes!("../../icons/tray-icon-mono.ico").to_vec(),
"colorful" => include_bytes!("../../icons/tray-icon.ico").to_vec(),
#[cfg(target_os = "macos")]
_ => include_bytes!("../../icons/tray-icon-mono.ico").to_vec(),
#[cfg(not(target_os = "macos"))]
_ => include_bytes!("../../icons/tray-icon.ico").to_vec(),
};
if *common_tray_icon {
let icon_dir_path = dirs::app_home_dir()?.join("icons");
let png_path = icon_dir_path.join("common.png");
Expand All @@ -212,9 +234,14 @@ impl Tray {
};

if *tun_mode {
#[cfg(target_os = "macos")]
let _ = tray.set_icon_as_template(false);
let mut icon = include_bytes!("../../icons/tray-icon-tun.ico").to_vec();
let mut icon = match tray_icon.as_str() {
"monochrome" => include_bytes!("../../icons/tray-icon-tun-mono.ico").to_vec(),
"colorful" => include_bytes!("../../icons/tray-icon-tun.ico").to_vec(),
#[cfg(target_os = "macos")]
_ => include_bytes!("../../icons/tray-icon-tun-mono.ico").to_vec(),
#[cfg(not(target_os = "macos"))]
_ => include_bytes!("../../icons/tray-icon-tun.ico").to_vec(),
};
if *tun_tray_icon {
let icon_dir_path = dirs::app_home_dir()?.join("icons");
let png_path = icon_dir_path.join("tun.png");
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/feat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ 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 tray_icon = patch.tray_icon;
let common_tray_icon = patch.common_tray_icon;
let sysproxy_tray_icon = patch.sysproxy_tray_icon;
let tun_tray_icon = patch.tun_tray_icon;
Expand Down Expand Up @@ -234,6 +235,7 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
handle::Handle::update_systray()?;
} else if system_proxy.is_some()
|| tun_mode.is_some()
|| tray_icon.is_some()
|| common_tray_icon.is_some()
|| sysproxy_tray_icon.is_some()
|| tun_tray_icon.is_some()
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.macos.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "../node_modules/@tauri-apps/cli/schema.json",
"tauri": {
"systemTray": {
"iconPath": "icons/mac-tray-icon.png",
"iconPath": "icons/tray-icon-mono.ico",
"iconAsTemplate": true
},
"bundle": {
Expand Down
20 changes: 20 additions & 0 deletions src/components/setting/mods/layout-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { convertFileSrc } from "@tauri-apps/api/tauri";
import { copyIconFile, getAppDir } from "@/services/cmds";
import { join } from "@tauri-apps/api/path";
import { exists } from "@tauri-apps/api/fs";
import getSystem from "@/utils/get-system";

export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -129,6 +130,25 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
</GuardState>
</SettingItem>

<SettingItem label={t("Tray Icon")}>
<GuardState
value={
verge?.tray_icon ?? getSystem() === "macos"
? "monochrome"
: "colorful"
}
onCatch={onError}
onFormat={(e: any) => e.target.value}
onChange={(e) => onChangeData({ tray_icon: e })}
onGuard={(e) => patchVerge({ tray_icon: e })}
>
<Select size="small" sx={{ width: 140, "> div": { py: "7.5px" } }}>
<MenuItem value="monochrome">{t("Monochrome")}</MenuItem>
<MenuItem value="colorful">{t("Colorful")}</MenuItem>
</Select>
</GuardState>
</SettingItem>

<SettingItem label={t("Common Tray Icon")}>
<GuardState
value={verge?.common_tray_icon}
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"Traffic Graph": "Traffic Graph",
"Memory Usage": "Memory Usage",
"Proxy Group Icon": "Proxy Group Icon",
"Tray Icon": "Tray Icon",
"Menu Icon": "Menu Icon",
"Monochrome": "Monochrome",
"Colorful": "Colorful",
Expand Down
1 change: 1 addition & 0 deletions src/locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"Traffic Graph": "График трафика",
"Memory Usage": "Использование памяти",
"Proxy Group Icon": "Иконка Группы прокси",
"Tray Icon": "Иконка лотка",
"Menu Icon": "Иконка меню",
"Monochrome": "Монохромный",
"Colorful": "Полноцветный",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"Traffic Graph": "流量图显",
"Memory Usage": "内存使用",
"Proxy Group Icon": "代理组图标",
"Tray Icon": "托盘图标",
"Menu Icon": "菜单图标",
"Monochrome": "单色图标",
"Colorful": "彩色图标",
Expand Down
1 change: 1 addition & 0 deletions src/services/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ interface IVergeConfig {
enable_memory_usage?: boolean;
enable_group_icon?: boolean;
menu_icon?: "monochrome" | "colorful" | "disable";
tray_icon?: "monochrome" | "colorful";
common_tray_icon?: boolean;
sysproxy_tray_icon?: boolean;
tun_tray_icon?: boolean;
Expand Down

0 comments on commit f24e4bb

Please sign in to comment.