This commit is contained in:
0XE 2025-03-04 20:46:17 +08:00 committed by GitHub
parent 1ee8786ab7
commit 7ea7ca1415
3 changed files with 71 additions and 21 deletions

View File

@ -1,9 +1,5 @@
use crate::{
utils::dirs,
feat,
wrap_err,
};
use super::CmdResult; use super::CmdResult;
use crate::{feat, utils::dirs, wrap_err};
use tauri::Manager; use tauri::Manager;
/// 打开应用程序所在目录 /// 打开应用程序所在目录
@ -93,26 +89,47 @@ pub async fn download_icon_cache(url: String, name: String) -> CmdResult<String>
Ok(icon_path.to_string_lossy().to_string()) 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] #[tauri::command]
pub fn copy_icon_file(path: String, name: String) -> CmdResult<String> { pub fn copy_icon_file(path: String, icon_info: IconInfo) -> CmdResult<String> {
let file_path = std::path::Path::new(&path); use std::fs;
use std::path::Path;
let file_path = Path::new(&path);
let icon_dir = wrap_err!(dirs::app_home_dir())?.join("icons"); let icon_dir = wrap_err!(dirs::app_home_dir())?.join("icons");
if !icon_dir.exists() { 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() { let ext = match file_path.extension() {
Some(e) => e.to_string_lossy().to_string(), Some(e) => e.to_string_lossy().to_string(),
None => "ico".to_string(), None => "ico".to_string(),
}; };
let png_dest_path = icon_dir.join(format!("{name}.png")); let dest_path = icon_dir.join(format!(
let ico_dest_path = icon_dir.join(format!("{name}.ico")); "{0}-{1}.{ext}",
let dest_path = icon_dir.join(format!("{name}.{ext}")); icon_info.name, icon_info.current_t
));
if file_path.exists() { if file_path.exists() {
std::fs::remove_file(png_dest_path).unwrap_or_default(); if icon_info.previous_t.trim() != "" {
std::fs::remove_file(ico_dest_path).unwrap_or_default(); fs::remove_file(
match std::fs::copy(file_path, &dest_path) { 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()), Ok(_) => Ok(dest_path.to_string_lossy().to_string()),
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string()),
} }

View File

@ -22,6 +22,18 @@ import getSystem from "@/utils/get-system";
const OS = getSystem(); 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<DialogRef>((props, ref) => { export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { verge, patchVerge, mutateVerge } = useVerge(); const { verge, patchVerge, mutateVerge } = useVerge();
@ -37,13 +49,20 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
async function initIconPath() { async function initIconPath() {
const appDir = await getAppDir(); const appDir = await getAppDir();
const icon_dir = await join(appDir, "icons"); 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 { icon_png: common_icon_png, icon_ico: common_icon_ico } =
const sysproxy_icon_png = await join(icon_dir, "sysproxy.png"); await getIcons(icon_dir, "common");
const sysproxy_icon_ico = await join(icon_dir, "sysproxy.ico");
const tun_icon_png = await join(icon_dir, "tun.png"); const { icon_png: sysproxy_icon_png, icon_ico: sysproxy_icon_ico } =
const tun_icon_ico = await join(icon_dir, "tun.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)) { if (await exists(common_icon_ico)) {
setCommonIcon(common_icon_ico); setCommonIcon(common_icon_ico);
} else { } else {
@ -212,11 +231,13 @@ export const LayoutViewer = forwardRef<DialogRef>((props, ref) => {
}, },
], ],
}); });
if (selected) { if (selected) {
await copyIconFile(`${selected}`, "common"); await copyIconFile(`${selected}`, "common");
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();
} }
} }
}} }}

View File

@ -199,7 +199,19 @@ export async function copyIconFile(
path: string, path: string,
name: "common" | "sysproxy" | "tun", name: "common" | "sysproxy" | "tun",
) { ) {
return invoke<void>("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<void>("copy_icon_file", { path, iconInfo });
} }
export async function downloadIconCache(url: string, name: string) { export async function downloadIconCache(url: string, name: string) {