From dffd663d7a8c0fa2553ede9c9e86a12c6ba6adbb Mon Sep 17 00:00:00 2001 From: huzibaca Date: Fri, 20 Sep 2024 16:26:23 +0800 Subject: [PATCH] Fix: Custom tray icon not working 1. Contains potential deadlock --- src-tauri/capabilities/migrated.json | 5 +++++ src-tauri/src/core/handle.rs | 4 ++-- src-tauri/src/feat.rs | 5 +---- src-tauri/src/utils/dirs.rs | 3 +-- src/components/setting/mods/layout-viewer.tsx | 14 +++++++------- src/components/setting/setting-verge.tsx | 6 +++--- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src-tauri/capabilities/migrated.json b/src-tauri/capabilities/migrated.json index b548532a..7cce56b5 100644 --- a/src-tauri/capabilities/migrated.json +++ b/src-tauri/capabilities/migrated.json @@ -11,6 +11,11 @@ "identifier": "fs:scope", "allow": ["$APPDATA/**", "$RESOURCE/../**", "**"] }, + "fs:allow-write-file", + { + "identifier": "fs:scope", + "allow": ["$APPDATA/**", "$RESOURCE/../**", "**"] + }, "core:window:allow-create", "core:window:allow-center", "core:window:allow-request-user-attention", diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index 08a1d564..ad1323c8 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -57,7 +57,7 @@ impl Handle { } pub fn update_systray() -> Result<()> { - let app_handle = Self::global().app_handle.lock(); + let app_handle = Self::global().app_handle.lock().clone(); if app_handle.is_none() { bail!("update_systray unhandled error"); } @@ -67,7 +67,7 @@ impl Handle { /// update the system tray state pub fn update_systray_part() -> Result<()> { - let app_handle = Self::global().app_handle.lock(); + let app_handle = Self::global().app_handle.lock().clone(); if app_handle.is_none() { bail!("update_systray unhandled error"); } diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index 94e90a92..8bd36d84 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -244,13 +244,10 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { || common_tray_icon.is_some() || sysproxy_tray_icon.is_some() || tun_tray_icon.is_some() + || tray_icon.is_some() { handle::Handle::update_systray_part()?; } - #[cfg(target_os = "macos")] - if tray_icon.is_some() { - handle::Handle::update_systray_part()?; - } >::Ok(()) }; diff --git a/src-tauri/src/utils/dirs.rs b/src-tauri/src/utils/dirs.rs index 96bedd64..ffc77622 100644 --- a/src-tauri/src/utils/dirs.rs +++ b/src-tauri/src/utils/dirs.rs @@ -44,7 +44,6 @@ pub fn app_home_dir() -> Result { .ok_or(anyhow::anyhow!("failed to get the portable app dir"))?; return Ok(PathBuf::from(app_dir).join(".config").join(APP_ID)); } - let handle = handle::Handle::global(); let app_handle = handle.app_handle.lock().clone(); @@ -65,7 +64,7 @@ pub fn app_home_dir() -> Result { /// get the resources dir pub fn app_resources_dir() -> Result { let handle = handle::Handle::global(); - let app_handle = handle.app_handle.lock(); + let app_handle = handle.app_handle.lock().clone(); if let Some(app_handle) = app_handle.as_ref() { match app_handle.path().resource_dir() { Ok(dir) => { diff --git a/src/components/setting/mods/layout-viewer.tsx b/src/components/setting/mods/layout-viewer.tsx index 4253545d..be0d70cd 100644 --- a/src/components/setting/mods/layout-viewer.tsx +++ b/src/components/setting/mods/layout-viewer.tsx @@ -196,8 +196,8 @@ export const LayoutViewer = forwardRef((props, ref) => { }, ], }); - if (selected?.path.length) { - await copyIconFile(`${selected.path}`, "common"); + if (selected) { + await copyIconFile(`${selected}`, "common"); await initIconPath(); onChangeData({ common_tray_icon: true }); patchVerge({ common_tray_icon: true }); @@ -242,8 +242,8 @@ export const LayoutViewer = forwardRef((props, ref) => { }, ], }); - if (selected?.path.length) { - await copyIconFile(`${selected.path}`, "sysproxy"); + if (selected) { + await copyIconFile(`${selected}`, "sysproxy"); await initIconPath(); onChangeData({ sysproxy_tray_icon: true }); patchVerge({ sysproxy_tray_icon: true }); @@ -281,13 +281,13 @@ export const LayoutViewer = forwardRef((props, ref) => { multiple: false, filters: [ { - name: "Tray Icon Image", + name: "Tun Icon Image", extensions: ["png", "ico"], }, ], }); - if (selected?.path.length) { - await copyIconFile(`${selected.path}`, "tun"); + if (selected) { + await copyIconFile(`${selected}`, "tun"); await initIconPath(); onChangeData({ tun_tray_icon: true }); patchVerge({ tun_tray_icon: true }); diff --git a/src/components/setting/setting-verge.tsx b/src/components/setting/setting-verge.tsx index 63e94d4d..1a4dd8d3 100644 --- a/src/components/setting/setting-verge.tsx +++ b/src/components/setting/setting-verge.tsx @@ -194,9 +194,9 @@ const SettingVerge = ({ onError }: Props) => { }, ], }); - if (selected?.path.length) { - onChangeData({ startup_script: `${selected.path}` }); - patchVerge({ startup_script: `${selected.path}` }); + if (selected) { + onChangeData({ startup_script: `${selected}` }); + patchVerge({ startup_script: `${selected}` }); } }} >