mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: convert synchronous file operations to asynchronous for improved performance (#5059)
* refactor: convert synchronous file operations to asynchronous for improved performance * fix: update copy_icon_file to use asynchronous directory creation * refactor: remove unnecessary variable assignments in shortcut management functions
This commit is contained in:
@@ -4,7 +4,7 @@ use anyhow::{Result, anyhow};
|
||||
use log::info;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::{fs, os::windows::process::CommandExt, path::Path, path::PathBuf};
|
||||
use std::{os::windows::process::CommandExt, path::Path, path::PathBuf};
|
||||
|
||||
/// Windows 下的开机启动文件夹路径
|
||||
#[cfg(target_os = "windows")]
|
||||
@@ -36,20 +36,24 @@ pub fn get_exe_path() -> Result<PathBuf> {
|
||||
|
||||
/// 创建快捷方式
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn create_shortcut() -> Result<()> {
|
||||
pub async fn create_shortcut() -> Result<()> {
|
||||
use crate::utils::dirs::PathBufExec;
|
||||
|
||||
let exe_path = get_exe_path()?;
|
||||
let startup_dir = get_startup_dir()?;
|
||||
let old_shortcut_path = startup_dir.join("Clash-Verge.lnk");
|
||||
let new_shortcut_path = startup_dir.join("Clash Verge.lnk");
|
||||
|
||||
// 移除旧的快捷方式
|
||||
if old_shortcut_path.exists() {
|
||||
if let Err(e) = fs::remove_file(&old_shortcut_path) {
|
||||
info!(target: "app", "移除旧快捷方式失败: {e}");
|
||||
} else {
|
||||
info!(target: "app", "成功移除旧快捷方式");
|
||||
}
|
||||
}
|
||||
let _ = old_shortcut_path
|
||||
.remove_if_exists()
|
||||
.await
|
||||
.inspect(|_| {
|
||||
info!(target: "app", "成功移除旧启动快捷方式");
|
||||
})
|
||||
.inspect_err(|err| {
|
||||
log::error!(target: "app", "移除旧启动快捷方式失败: {err}");
|
||||
});
|
||||
|
||||
// 如果新快捷方式已存在,直接返回成功
|
||||
if new_shortcut_path.exists() {
|
||||
@@ -85,30 +89,36 @@ pub fn create_shortcut() -> Result<()> {
|
||||
|
||||
/// 删除快捷方式
|
||||
#[cfg(target_os = "windows")]
|
||||
pub fn remove_shortcut() -> Result<()> {
|
||||
pub async fn remove_shortcut() -> Result<()> {
|
||||
use crate::utils::dirs::PathBufExec;
|
||||
|
||||
let startup_dir = get_startup_dir()?;
|
||||
let old_shortcut_path = startup_dir.join("Clash-Verge.lnk");
|
||||
let new_shortcut_path = startup_dir.join("Clash Verge.lnk");
|
||||
|
||||
let mut removed_any = false;
|
||||
|
||||
// 删除旧的快捷方式
|
||||
if old_shortcut_path.exists() {
|
||||
fs::remove_file(&old_shortcut_path).map_err(|e| anyhow!("删除旧快捷方式失败: {}", e))?;
|
||||
info!(target: "app", "成功删除旧启动快捷方式");
|
||||
removed_any = true;
|
||||
}
|
||||
let _ = old_shortcut_path
|
||||
.remove_if_exists()
|
||||
.await
|
||||
.inspect(|_| {
|
||||
info!(target: "app", "成功删除旧启动快捷方式");
|
||||
removed_any = true;
|
||||
})
|
||||
.inspect_err(|err| {
|
||||
log::error!(target: "app", "删除旧启动快捷方式失败: {err}");
|
||||
});
|
||||
|
||||
// 删除新的快捷方式
|
||||
if new_shortcut_path.exists() {
|
||||
fs::remove_file(&new_shortcut_path).map_err(|e| anyhow!("删除快捷方式失败: {}", e))?;
|
||||
info!(target: "app", "成功删除启动快捷方式");
|
||||
removed_any = true;
|
||||
}
|
||||
|
||||
if !removed_any {
|
||||
info!(target: "app", "启动快捷方式不存在,无需删除");
|
||||
}
|
||||
let _ = new_shortcut_path
|
||||
.remove_if_exists()
|
||||
.await
|
||||
.inspect(|_| {
|
||||
info!(target: "app", "成功删除启动快捷方式");
|
||||
removed_any = true;
|
||||
})
|
||||
.inspect_err(|err| {
|
||||
log::error!(target: "app", "删除启动快捷方式失败: {err}");
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -121,19 +131,3 @@ pub fn is_shortcut_enabled() -> Result<bool> {
|
||||
|
||||
Ok(new_shortcut_path.exists())
|
||||
}
|
||||
|
||||
// 非 Windows 平台使用的空方法
|
||||
// #[cfg(not(target_os = "windows"))]
|
||||
// pub fn create_shortcut() -> Result<()> {
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// #[cfg(not(target_os = "windows"))]
|
||||
// pub fn remove_shortcut() -> Result<()> {
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// #[cfg(not(target_os = "windows"))]
|
||||
// pub fn is_shortcut_enabled() -> Result<bool> {
|
||||
// Ok(false)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user