diff --git a/src-tauri/src/cmd/app.rs b/src-tauri/src/cmd/app.rs index f9e447b2..eefd4c3e 100644 --- a/src-tauri/src/cmd/app.rs +++ b/src-tauri/src/cmd/app.rs @@ -1,9 +1,5 @@ -use crate::{ - utils::dirs, - feat, - wrap_err, -}; use super::CmdResult; +use crate::{feat, utils::dirs, wrap_err}; use tauri::Manager; /// 打开应用程序所在目录 @@ -93,26 +89,47 @@ pub async fn download_icon_cache(url: String, name: String) -> CmdResult Ok(icon_path.to_string_lossy().to_string()) } +#[derive(Debug, serde::Serialize, serde::Deserialize)] +pub struct IconInfo { + name: String, + previous_t: String, + current_t: String, +} + /// 复制图标文件 #[tauri::command] -pub fn copy_icon_file(path: String, name: String) -> CmdResult { - let file_path = std::path::Path::new(&path); +pub fn copy_icon_file(path: String, icon_info: IconInfo) -> CmdResult { + use std::fs; + use std::path::Path; + + let file_path = Path::new(&path); + let icon_dir = wrap_err!(dirs::app_home_dir())?.join("icons"); if !icon_dir.exists() { - let _ = std::fs::create_dir_all(&icon_dir); + let _ = fs::create_dir_all(&icon_dir); } let ext = match file_path.extension() { Some(e) => e.to_string_lossy().to_string(), None => "ico".to_string(), }; - let png_dest_path = icon_dir.join(format!("{name}.png")); - let ico_dest_path = icon_dir.join(format!("{name}.ico")); - let dest_path = icon_dir.join(format!("{name}.{ext}")); + let dest_path = icon_dir.join(format!( + "{0}-{1}.{ext}", + icon_info.name, icon_info.current_t + )); if file_path.exists() { - std::fs::remove_file(png_dest_path).unwrap_or_default(); - std::fs::remove_file(ico_dest_path).unwrap_or_default(); - match std::fs::copy(file_path, &dest_path) { + if icon_info.previous_t.trim() != "" { + fs::remove_file( + icon_dir.join(format!("{0}-{1}.png", icon_info.name, icon_info.previous_t)), + ) + .unwrap_or_default(); + fs::remove_file( + icon_dir.join(format!("{0}-{1}.ico", icon_info.name, icon_info.previous_t)), + ) + .unwrap_or_default(); + } + + match fs::copy(file_path, &dest_path) { Ok(_) => Ok(dest_path.to_string_lossy().to_string()), Err(err) => Err(err.to_string()), } diff --git a/src/components/setting/mods/layout-viewer.tsx b/src/components/setting/mods/layout-viewer.tsx index 21f79f5e..e76706ee 100644 --- a/src/components/setting/mods/layout-viewer.tsx +++ b/src/components/setting/mods/layout-viewer.tsx @@ -22,6 +22,18 @@ import getSystem from "@/utils/get-system"; const OS = getSystem(); +const getIcons = async (icon_dir: string, name: string) => { + const updateTime = localStorage.getItem(`icon_${name}_update_time`) || ""; + + const icon_png = await join(icon_dir, `${name}-${updateTime}.png`); + const icon_ico = await join(icon_dir, `${name}-${updateTime}.ico`); + + return { + icon_png, + icon_ico, + }; +}; + export const LayoutViewer = forwardRef((props, ref) => { const { t } = useTranslation(); const { verge, patchVerge, mutateVerge } = useVerge(); @@ -37,13 +49,20 @@ export const LayoutViewer = forwardRef((props, ref) => { async function initIconPath() { const appDir = await getAppDir(); + const icon_dir = await join(appDir, "icons"); - const common_icon_png = await join(icon_dir, "common.png"); - const common_icon_ico = await join(icon_dir, "common.ico"); - const sysproxy_icon_png = await join(icon_dir, "sysproxy.png"); - const sysproxy_icon_ico = await join(icon_dir, "sysproxy.ico"); - const tun_icon_png = await join(icon_dir, "tun.png"); - const tun_icon_ico = await join(icon_dir, "tun.ico"); + + const { icon_png: common_icon_png, icon_ico: common_icon_ico } = + await getIcons(icon_dir, "common"); + + const { icon_png: sysproxy_icon_png, icon_ico: sysproxy_icon_ico } = + await getIcons(icon_dir, "sysproxy"); + + const { icon_png: tun_icon_png, icon_ico: tun_icon_ico } = await getIcons( + icon_dir, + "tun", + ); + if (await exists(common_icon_ico)) { setCommonIcon(common_icon_ico); } else { @@ -212,11 +231,13 @@ export const LayoutViewer = forwardRef((props, ref) => { }, ], }); + if (selected) { await copyIconFile(`${selected}`, "common"); await initIconPath(); onChangeData({ common_tray_icon: true }); patchVerge({ common_tray_icon: true }); + console.log(); } } }} diff --git a/src/services/cmds.ts b/src/services/cmds.ts index 3104183f..9338406d 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -199,7 +199,19 @@ export async function copyIconFile( path: string, name: "common" | "sysproxy" | "tun", ) { - return invoke("copy_icon_file", { path, name }); + const key = `icon_${name}_update_time`; + const previousTime = localStorage.getItem(key) || ""; + + const currentTime = String(Date.now()); + localStorage.setItem(key, currentTime); + + const iconInfo = { + name, + previous_t: previousTime, + current_t: currentTime, + }; + + return invoke("copy_icon_file", { path, iconInfo }); } export async function downloadIconCache(url: string, name: string) {