feat: 重写为 Tauri + React + TypeScript (v4.0)
完全移除旧 C+IUP 代码,改用 Tauri 2.x + React 19 + TypeScript + Rust 技术栈重写。 功能与 v3.1 完全等价: - React 前端:Tailwind CSS 4、Zustand 状态管理、i18next 国际化 - Rust 后端:winreg 注册表读写、Win32 API FFI 调用 - 核心逻辑:StringList、UndoRedoManager、PathManager、Import/Export - 深色模式、中英文切换、键盘快捷键、合并预览 - 66 个 Vitest 单元测试 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@@ -0,0 +1,4 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
/gen/schemas
|
||||
@@ -0,0 +1,29 @@
|
||||
[package]
|
||||
name = "patheditor"
|
||||
version = "4.0.0"
|
||||
description = "Windows PATH Environment Variable Editor"
|
||||
authors = ["刘航宇"]
|
||||
license = "MIT"
|
||||
repository = "https://github.com/LHY0125/PathEditor"
|
||||
edition = "2021"
|
||||
rust-version = "1.77.2"
|
||||
|
||||
[lib]
|
||||
name = "app_lib"
|
||||
crate-type = ["staticlib", "rlib"]
|
||||
|
||||
[build-dependencies]
|
||||
tauri-build = { version = "2.6.2", features = [] }
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
log = "0.4"
|
||||
tauri = { version = "2.11.2", features = [] }
|
||||
tauri-plugin-log = "2"
|
||||
tauri-plugin-dialog = "2"
|
||||
|
||||
# Windows API
|
||||
winreg = "0.52"
|
||||
dirs = "5"
|
||||
chrono = "0.4"
|
||||
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
tauri_build::build()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"$schema": "../gen/schemas/desktop-schema.json",
|
||||
"identifier": "default",
|
||||
"description": "enables the default permissions",
|
||||
"windows": [
|
||||
"main"
|
||||
],
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"dialog:default"
|
||||
]
|
||||
}
|
||||
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 9.0 KiB |
|
After Width: | Height: | Size: 12 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 5.9 KiB |
|
After Width: | Height: | Size: 7.4 KiB |
|
After Width: | Height: | Size: 3.9 KiB |
|
After Width: | Height: | Size: 37 KiB |
|
After Width: | Height: | Size: 49 KiB |
@@ -0,0 +1,58 @@
|
||||
use chrono::Local;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// 获取 APPDATA 路径下的备份目录
|
||||
#[tauri::command]
|
||||
pub fn get_appdata_dir() -> String {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("C:\\"))
|
||||
.join("PathEditor")
|
||||
.join("backups")
|
||||
.to_string_lossy()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// 备份当前注册表中的系统 PATH 和用户 PATH
|
||||
/// 返回备份文件的路径
|
||||
#[tauri::command]
|
||||
pub fn backup_registry(custom_dir: Option<String>, sys_paths: Vec<String>, user_paths: Vec<String>) -> Result<String, String> {
|
||||
// 确定备份目录
|
||||
let backup_dir = match custom_dir {
|
||||
Some(ref dir) if !dir.is_empty() => PathBuf::from(dir),
|
||||
_ => {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("C:\\"))
|
||||
.join("PathEditor")
|
||||
.join("backups")
|
||||
}
|
||||
};
|
||||
|
||||
// 创建目录
|
||||
fs::create_dir_all(&backup_dir)
|
||||
.map_err(|e| format!("无法创建备份目录: {}", e))?;
|
||||
|
||||
// 生成带时间戳的文件名
|
||||
let timestamp = Local::now().format("%Y%m%d_%H%M%S");
|
||||
let filename = format!("path_backup_{}.txt", timestamp);
|
||||
let filepath = backup_dir.join(&filename);
|
||||
|
||||
// 写入备份内容
|
||||
let mut content = String::new();
|
||||
content.push_str(&format!("PathEditor Backup - {}\n", Local::now().format("%Y-%m-%d %H:%M:%S")));
|
||||
content.push_str("\n[System PATH]\n");
|
||||
for path in &sys_paths {
|
||||
content.push_str(&format!("{}\n", path));
|
||||
}
|
||||
content.push_str("\n[User PATH]\n");
|
||||
for path in &user_paths {
|
||||
content.push_str(&format!("{}\n", path));
|
||||
}
|
||||
|
||||
fs::write(&filepath, &content)
|
||||
.map_err(|e| format!("无法写入备份文件: {}", e))?;
|
||||
|
||||
let result = filepath.to_string_lossy().to_string();
|
||||
log::info!("备份已保存到: {}", result);
|
||||
Ok(result)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod registry;
|
||||
pub mod system;
|
||||
pub mod backup;
|
||||
@@ -0,0 +1,83 @@
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
const SYS_REG_PATH: &str = "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
|
||||
const USER_REG_PATH: &str = "Environment";
|
||||
const PATH_VALUE: &str = "Path";
|
||||
|
||||
/// 从注册表加载系统 PATH
|
||||
#[tauri::command]
|
||||
pub fn load_system_paths() -> Result<Vec<String>, String> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
let env_key = hklm
|
||||
.open_subkey_with_flags(SYS_REG_PATH, KEY_READ)
|
||||
.map_err(|e| format!("无法打开系统注册表项: {}", e))?;
|
||||
|
||||
let value: String = env_key
|
||||
.get_value(PATH_VALUE)
|
||||
.map_err(|e| format!("无法读取系统 PATH: {}", e))?;
|
||||
|
||||
Ok(split_path(&value))
|
||||
}
|
||||
|
||||
/// 从注册表加载用户 PATH
|
||||
#[tauri::command]
|
||||
pub fn load_user_paths() -> Result<Vec<String>, String> {
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let env_key = hkcu
|
||||
.open_subkey_with_flags(USER_REG_PATH, KEY_READ)
|
||||
.map_err(|e| format!("无法打开用户注册表项: {}", e))?;
|
||||
|
||||
let value: String = env_key
|
||||
.get_value(PATH_VALUE)
|
||||
.map_err(|e| format!("无法读取用户 PATH: {}", e))?;
|
||||
|
||||
Ok(split_path(&value))
|
||||
}
|
||||
|
||||
/// 保存系统 PATH 到注册表
|
||||
#[tauri::command]
|
||||
pub fn save_system_paths(paths: Vec<String>) -> Result<(), String> {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
let env_key = hklm
|
||||
.open_subkey_with_flags(SYS_REG_PATH, KEY_WRITE)
|
||||
.map_err(|e| format!("无法写入系统注册表(需要管理员权限): {}", e))?;
|
||||
|
||||
let value = join_path(&paths);
|
||||
env_key
|
||||
.set_value(PATH_VALUE, &value)
|
||||
.map_err(|e| format!("无法写入系统 PATH: {}", e))?;
|
||||
|
||||
log::info!("已保存系统 PATH,{} 个条目", paths.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 保存用户 PATH 到注册表
|
||||
#[tauri::command]
|
||||
pub fn save_user_paths(paths: Vec<String>) -> Result<(), String> {
|
||||
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
|
||||
let env_key = hkcu
|
||||
.open_subkey_with_flags(USER_REG_PATH, KEY_WRITE)
|
||||
.map_err(|e| format!("无法写入用户注册表: {}", e))?;
|
||||
|
||||
let value = join_path(&paths);
|
||||
env_key
|
||||
.set_value(PATH_VALUE, &value)
|
||||
.map_err(|e| format!("无法写入用户 PATH: {}", e))?;
|
||||
|
||||
log::info!("已保存用户 PATH,{} 个条目", paths.len());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 用分号分割 PATH 字符串
|
||||
fn split_path(raw: &str) -> Vec<String> {
|
||||
raw.split(';')
|
||||
.map(|s| s.trim().to_string())
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// 用分号连接路径列表
|
||||
fn join_path(paths: &[String]) -> String {
|
||||
paths.join(";")
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
use winreg::enums::*;
|
||||
use winreg::RegKey;
|
||||
|
||||
/// 检测当前进程是否有管理员权限(尝试写入系统注册表键)
|
||||
#[tauri::command]
|
||||
pub fn check_admin() -> bool {
|
||||
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
|
||||
hklm.open_subkey_with_flags(
|
||||
"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
|
||||
KEY_WRITE,
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
/// 验证路径是否存在于文件系统中(且是目录)
|
||||
/// 包含 % 的路径(环境变量路径)无法验证,返回 true
|
||||
#[tauri::command]
|
||||
pub fn validate_path(path: &str) -> bool {
|
||||
if path.contains('%') {
|
||||
return true;
|
||||
}
|
||||
std::fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false)
|
||||
}
|
||||
|
||||
/// 展开路径中的环境变量(如 %JAVA_HOME%\bin → C:\Program Files\Java\jdk-17\bin)
|
||||
#[tauri::command]
|
||||
pub fn expand_env_vars(path: &str) -> String {
|
||||
if !path.contains('%') {
|
||||
return path.to_string();
|
||||
}
|
||||
|
||||
// 转为 UTF-16 宽字符串
|
||||
let wide_path: Vec<u16> = path
|
||||
.encode_utf16()
|
||||
.chain(std::iter::once(0))
|
||||
.collect();
|
||||
|
||||
// 先查询需要的缓冲区大小 (lpDst=NULL)
|
||||
let required = unsafe {
|
||||
ExpandEnvironmentStringsW(wide_path.as_ptr(), std::ptr::null_mut(), 0)
|
||||
};
|
||||
|
||||
if required == 0 {
|
||||
return path.to_string();
|
||||
}
|
||||
|
||||
// 实际展开
|
||||
let mut buffer: Vec<u16> = vec![0; required as usize];
|
||||
let result = unsafe {
|
||||
ExpandEnvironmentStringsW(wide_path.as_ptr(), buffer.as_mut_ptr(), required)
|
||||
};
|
||||
|
||||
if result == 0 {
|
||||
return path.to_string();
|
||||
}
|
||||
|
||||
// 转回 UTF-8 (去掉结尾 null)
|
||||
let len = buffer.iter().position(|&c| c == 0).unwrap_or(buffer.len());
|
||||
String::from_utf16_lossy(&buffer[..len])
|
||||
}
|
||||
|
||||
/// 广播环境变量更改通知(WM_SETTINGCHANGE)
|
||||
#[tauri::command]
|
||||
pub fn broadcast_env_change() {
|
||||
const HWND_BROADCAST: isize = 0xFFFF;
|
||||
const WM_SETTINGCHANGE: u32 = 0x001A;
|
||||
const SMTO_ABORTIFHUNG: u32 = 0x0002;
|
||||
|
||||
let env_str: Vec<u16> = "Environment\0".encode_utf16().collect();
|
||||
|
||||
let result = unsafe {
|
||||
SendMessageTimeoutW(
|
||||
HWND_BROADCAST,
|
||||
WM_SETTINGCHANGE,
|
||||
0,
|
||||
env_str.as_ptr() as isize,
|
||||
SMTO_ABORTIFHUNG,
|
||||
5000,
|
||||
std::ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
|
||||
if result == 0 {
|
||||
log::warn!("广播 WM_SETTINGCHANGE 失败");
|
||||
} else {
|
||||
log::info!("已广播环境变量更改通知");
|
||||
}
|
||||
}
|
||||
|
||||
// ── 外部 FFI 声明 ──
|
||||
|
||||
extern "system" {
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-expandenvironmentstringsw
|
||||
fn ExpandEnvironmentStringsW(
|
||||
lpSrc: *const u16,
|
||||
lpDst: *mut u16,
|
||||
nSize: u32,
|
||||
) -> u32;
|
||||
|
||||
/// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagetimeoutw
|
||||
fn SendMessageTimeoutW(
|
||||
hWnd: isize,
|
||||
Msg: u32,
|
||||
wParam: usize,
|
||||
lParam: isize,
|
||||
fuFlags: u32,
|
||||
uTimeout: u32,
|
||||
lpdwResult: *mut usize,
|
||||
) -> isize;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
use serde::Serialize;
|
||||
|
||||
/// 传给前端的统一错误类型
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct AppError {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AppError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for AppError {
|
||||
fn from(s: &str) -> Self {
|
||||
AppError {
|
||||
message: s.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for AppError {
|
||||
fn from(s: String) -> Self {
|
||||
AppError { message: s }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for AppError {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
AppError {
|
||||
message: format!("IO 错误: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
mod commands;
|
||||
mod error;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
tauri_plugin_log::Builder::default()
|
||||
.level(log::LevelFilter::Info)
|
||||
.build(),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::registry::load_system_paths,
|
||||
commands::registry::load_user_paths,
|
||||
commands::registry::save_system_paths,
|
||||
commands::registry::save_user_paths,
|
||||
commands::system::check_admin,
|
||||
commands::system::validate_path,
|
||||
commands::system::expand_env_vars,
|
||||
commands::system::broadcast_env_change,
|
||||
commands::backup::backup_registry,
|
||||
commands::backup::get_appdata_dir,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
app_lib::run();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
|
||||
"productName": "PathEditor",
|
||||
"version": "4.0.0",
|
||||
"identifier": "com.liuhangyu.patheditor",
|
||||
"build": {
|
||||
"frontendDist": "../dist",
|
||||
"devUrl": "http://localhost:5173",
|
||||
"beforeDevCommand": "npm run dev",
|
||||
"beforeBuildCommand": "npm run build"
|
||||
},
|
||||
"app": {
|
||||
"windows": [
|
||||
{
|
||||
"title": "PathEditor v4.0",
|
||||
"width": 900,
|
||||
"height": 700,
|
||||
"minWidth": 800,
|
||||
"minHeight": 600,
|
||||
"resizable": true,
|
||||
"fullscreen": false
|
||||
}
|
||||
],
|
||||
"security": {
|
||||
"csp": null
|
||||
}
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"targets": "nsis",
|
||||
"icon": [
|
||||
"icons/32x32.png",
|
||||
"icons/128x128.png",
|
||||
"icons/128x128@2x.png",
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
]
|
||||
}
|
||||
}
|
||||