mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-30 10:35:54 +08:00
b159407773
Rust: - save_paths 添加 Windows PATH 32767 字符上限检查防静默截断 - backup_registry 回退路径统一为 backup_base_dir() 三级链 Store: - 新增 isSaving 并发守卫防止重复保存 - 保存失败详情通过 Promise.allSettled.reason 展示 - isModified 改为与上次保存快照比较(undo/redo 后准确反映状态) - 批删除合并为单条撤销记录(N 次删除 → 1 次 Ctrl+Z 恢复) - 保存失败/备份失败使用 i18n 键(消除硬编码中文) UI: - 拖拽添加改用 webkitGetAsEntry().isDirectory 校验文件夹 - Ctrl+F 快捷键聚焦搜索框 - handleClean 使用 is_valid_path_format(替代不完整的 inline 函数) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
126 lines
3.4 KiB
Rust
126 lines
3.4 KiB
Rust
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";
|
||
|
||
fn load_paths(root: winreg::HKEY, sub_path: &str, label: &str) -> Result<Vec<String>, String> {
|
||
let key = RegKey::predef(root);
|
||
let env_key = key
|
||
.open_subkey_with_flags(sub_path, KEY_READ)
|
||
.map_err(|e| format!("无法打开{}注册表项: {}", label, e))?;
|
||
|
||
let value: String = env_key
|
||
.get_value(PATH_VALUE)
|
||
.map_err(|e| format!("无法读取{} PATH: {}", label, e))?;
|
||
|
||
Ok(split_path(&value))
|
||
}
|
||
|
||
fn save_paths(root: winreg::HKEY, sub_path: &str, label: &str, paths: &[String]) -> Result<(), String> {
|
||
let value = join_path(paths);
|
||
|
||
// Windows 注册表 REG_EXPAND_SZ 上限 32767 字符
|
||
const MAX_PATH_LEN: usize = 32767;
|
||
if value.len() > MAX_PATH_LEN {
|
||
return Err(format!(
|
||
"{} PATH 总长度 {} 超出 Windows 限制 {} 字符,请移除部分路径后再保存",
|
||
label, value.len(), MAX_PATH_LEN
|
||
));
|
||
}
|
||
|
||
let key = RegKey::predef(root);
|
||
let env_key = key
|
||
.open_subkey_with_flags(sub_path, KEY_WRITE)
|
||
.map_err(|e| format!("无法写入{}注册表(需要管理员权限): {}", label, e))?;
|
||
|
||
env_key
|
||
.set_value(PATH_VALUE, &value)
|
||
.map_err(|e| format!("无法写入{} PATH: {}", label, e))?;
|
||
|
||
log::info!("已保存{} PATH,{} 个条目", label, paths.len());
|
||
Ok(())
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub fn load_system_paths() -> Result<Vec<String>, String> {
|
||
load_paths(HKEY_LOCAL_MACHINE, SYS_REG_PATH, "系统")
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub fn load_user_paths() -> Result<Vec<String>, String> {
|
||
load_paths(HKEY_CURRENT_USER, USER_REG_PATH, "用户")
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub fn save_system_paths(paths: Vec<String>) -> Result<(), String> {
|
||
save_paths(HKEY_LOCAL_MACHINE, SYS_REG_PATH, "系统", &paths)
|
||
}
|
||
|
||
#[tauri::command]
|
||
pub fn save_user_paths(paths: Vec<String>) -> Result<(), String> {
|
||
save_paths(HKEY_CURRENT_USER, USER_REG_PATH, "用户", &paths)
|
||
}
|
||
|
||
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
|
||
.iter()
|
||
.map(|p| p.trim())
|
||
.filter(|p| !p.is_empty())
|
||
.collect::<Vec<_>>()
|
||
.join(";")
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn split_empty() {
|
||
assert_eq!(split_path(""), Vec::<String>::new());
|
||
}
|
||
|
||
#[test]
|
||
fn split_single() {
|
||
assert_eq!(split_path("C:\\Windows"), vec!["C:\\Windows"]);
|
||
}
|
||
|
||
#[test]
|
||
fn split_multiple() {
|
||
assert_eq!(
|
||
split_path("C:\\Windows;D:\\Projects"),
|
||
vec!["C:\\Windows", "D:\\Projects"]
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn split_trims_and_filters_empty() {
|
||
assert_eq!(
|
||
split_path(" C:\\ ; ; D:\\ "),
|
||
vec!["C:\\", "D:\\"]
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn join_and_split_roundtrip() {
|
||
let paths = vec!["C:\\Windows".to_string(), "D:\\Projects".to_string()];
|
||
let joined = join_path(&paths);
|
||
let split = split_path(&joined);
|
||
assert_eq!(split, paths);
|
||
}
|
||
|
||
#[test]
|
||
fn join_trims_entries() {
|
||
let paths = vec![" C:\\Windows ".to_string(), " D:\\ ".to_string()];
|
||
assert_eq!(join_path(&paths), "C:\\Windows;D:\\");
|
||
}
|
||
}
|