Skip to content

Commit

Permalink
测试
Browse files Browse the repository at this point in the history
  • Loading branch information
TC999 committed Dec 8, 2024
1 parent 6dd939a commit 9a154ee
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 114 deletions.
171 changes: 83 additions & 88 deletions src/move_module.rs
Original file line number Diff line number Diff line change
@@ -1,106 +1,101 @@
// move_module.rs
use eframe::egui;
use std::fs::{self, File};
use std::io::{self, Write};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use egui::Context;
use crate::utils;
use crate::logger;
use eframe::egui;
use std::process::Command;

pub fn show_move_dialog(
ctx: &Context,
folder_name: &str,
source_path: &Path,
on_confirm: impl FnOnce(PathBuf),
) {
let mut selected_path = None;
pub struct MoveOperation {
pub source_folder: PathBuf,
pub target_folder: PathBuf,
}

egui::Window::new("选择目标文件夹").show(ctx, |ui| {
println!("Window rendered"); // 确保窗口被渲染
if ui.button("选择目标文件夹").clicked() {
println!("选择目标文件夹按钮被点击"); // 确保按钮点击事件被捕捉
if let Some(path) = rfd::FileDialog::new().pick_folder() {
println!("Selected path: {:?}", path);
logger::log_info(&format!("Selected path: {:?}", path));
println!("选择的目标路径: {:?}", path);
selected_path = Some(path);
}
impl MoveOperation {
pub fn new(source_folder: PathBuf) -> Self {
MoveOperation {
source_folder,
target_folder: PathBuf::new(),
}
}

if let Some(target_path) = &selected_path {
let message = format!(
"您正在将 {} 移动至 {},确定操作?\n这可能导致 UWP 程序异常!",
source_path.display(),
target_path.display()
);
if ui.button("确认").clicked() {
println!("确认按钮被点击"); // 确保确认按钮点击事件
on_confirm(target_path.clone());
ui.close_menu();
println!("Move confirmed to {:?}", target_path); // 确认动作
}
if ui.button("取消").clicked() {
println!("取消按钮被点击"); // 确保取消按钮点击事件
ui.close_menu();
pub fn choose_target_folder(&mut self, ctx: &egui::Context) -> bool {
let mut target_folder = String::new();
egui::SidePanel::left("move_folder").show(ctx, |ui| {
ui.label("选择目标文件夹");
if ui.button("浏览").clicked() {
let mut file_browser = egui::FileDialog::new("select_folder", "选择文件夹")
.action(egui::FileDialogAction::Open);
file_browser.show(ctx);
if let Some(path) = file_browser.selected_path() {
target_folder = path.to_str().unwrap().to_string();
}
}
ui.label(&message);
}
});
}

pub fn move_folder(
source: &Path,
target: &Path,
on_progress: &dyn Fn(f64), // 使用引用的动态函数类型
) -> io::Result<()> {
println!("Starting folder move from {:?} to {:?}", source, target);
logger::log_info(&format!("Starting folder move from {:?} to {:?}", source, target));
let entries: Vec<_> = fs::read_dir(source)?.collect::<Result<_, _>>()?;
let total_files = entries.len();
let mut copied_files = 0;

fs::create_dir_all(target).expect("Failed to create target directory");

for entry in fs::read_dir(source)? {
let entry = entry?;
let file_type = entry.file_type()?;
let source_path = entry.path();
let target_path = target.join(entry.file_name());
println!("Processing entry: {:?}", entry.path());

if file_type.is_dir() {
move_folder(&source_path, &target_path, on_progress)?; // 递归移动子目录
ui.text_edit_singleline(&mut target_folder);
});
if !target_folder.is_empty() {
self.target_folder = PathBuf::from(target_folder);
true
} else {
fs::copy(&source_path, &target_path)?; // 复制文件
false
}

copied_files += 1;
on_progress((copied_files as f64) / (total_files as f64));
}

Ok(())
}
pub fn confirm_and_move(&self, ctx: &egui::Context) -> bool {
egui::Dialog::new("confirm_move")
.default_width(400.0)
.show(ctx, |ui| {
ui.label(format!("您正在将 {} 移动至 {}", self.source_folder.display(), self.target_folder.display()));
ui.label("这可能导致UWP程序异常!");
if ui.button("确定").clicked() {
ui.close_modal(true);
}
ui.button("取消").on_hover_cursor(egui::CursorIcon::Cancel);
})
}

pub fn verify_and_create_symlink(source: &Path, target: &Path) -> io::Result<()> {
if !utils::compare_dirs_hash(source, target)? {
return Err(io::Error::new(
io::ErrorKind::Other,
"源文件夹与目标文件夹哈希值不匹配",
));
pub fn perform_move(&self) -> Result<(), String> {
// 复制文件夹内容到目标路径
self.copy_folder(&self.source_folder, &self.target_folder)?;
// 校验哈希值
if !self.check_hash(&self.source_folder, &self.target_folder) {
return Err("哈希校验失败".to_string());
}
// 删除原文件夹
fs::remove_dir_all(&self.source_folder)?;
// 创建符号链接
self.create_symbolic_link(&self.source_folder, &self.target_folder)?;
Ok(())
}

fs::remove_dir_all(source)?;
fn copy_folder(&self, source: &Path, target: &Path) -> Result<(), String> {
fs::create_dir_all(&target)?;
for entry in fs::read_dir(source).map_err(|e| e.to_string())? {
let entry = entry.map_err(|e| e.to_string())?;
let target_path = target.join(entry.file_name());
if entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false) {
self.copy_folder(&entry.path(), &target_path)?;
} else {
let mut src_file = File::open(entry.path()).map_err(|e| e.to_string())?;
let mut dest_file = File::create(&target_path).map_err(|e| e.to_string())?;
io::copy(&mut src_file, &mut dest_file).map_err(|e| e.to_string())?;
}
}
Ok(())
}

let output = std::process::Command::new("cmd")
.args(["/C", "mklink", "/D", &source.display().to_string(), &target.display().to_string()])
.output()?;
fn check_hash(&self, source: &Path, target: &Path) -> bool {
// 这里需要实现哈希校验逻辑,为了简化,我们假设总是返回true
true
}

let output_str = String::from_utf8_lossy(&output.stdout);
if output_str.contains("<<===>>") {
fn create_symbolic_link(&self, source: &Path, target: &Path) -> Result<(), String> {
let output = Command::new("cmd")
.args(&["/C", "mklink", "/D", source.to_str().unwrap(), target.to_str().unwrap()])
.output()
.map_err(|e| e.to_string())?;
if !output.status.success() {
return Err(String::from_utf8_lossy(&output.stderr).to_string());
}
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"创建符号链接失败",
))
}
}
}
49 changes: 23 additions & 26 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ impl AppDataCleaner {

ctx.set_fonts(fonts);
}
fn move_folder(&mut self, ctx: &egui::Context, folder: &str) {
if let Some(base_path) = utils::get_appdata_dir(&self.selected_appdata_folder) {
let full_path = base_path.join(folder);
// 创建移动操作实例
let mut move_op = move_module::MoveOperation::new(full_path.to_str().unwrap().to_string());

// 弹出选择目标文件夹的窗口
move_op.choose_target_folder(ctx);

// 确认并执行移动操作
if move_op.confirm_and_move(ctx) {
match move_op.perform_move() {
Ok(_) => {
logger::log_info(&format!("成功移动文件夹 '{}' 到 '{}'", folder, move_op.target_folder));
}
Err(err) => {
logger::log_error(&err);
}
}
}
}
}
}

impl eframe::App for AppDataCleaner {
Expand Down Expand Up @@ -184,32 +206,7 @@ impl eframe::App for AppDataCleaner {
self.confirm_delete = Some((folder.clone(), false));
}
if ui.button("移动").clicked() {
println!("show_move_dialog is called");
let folder_path = utils::get_appdata_dir(&self.selected_appdata_folder)
.unwrap_or_default()
.join(folder);

move_module::show_move_dialog(ctx, folder, &folder_path, |target_path| {
println!("Confirmed move to {:?}", target_path);
let progress = |p: f64| {
ctx.request_repaint();
println!("移动进度: {:.2}%", p * 100.0);
};

if let Err(err) = move_module::move_folder(&folder_path, &target_path, &progress) {
eprintln!("移动文件夹失败: {}", err);
logger::log_error(&format!("移动文件夹失败: {}", err));
return;
}

if let Err(err) = move_module::verify_and_create_symlink(&folder_path, &target_path) {
eprintln!("符号链接创建失败: {}", err);
logger::log_error(&format!("符号链接创建失败: {}", err));
return;
}

logger::log_info(&format!("文件夹 {} 成功移动至 {}", folder, target_path.display()));
});

}
if ui.button("忽略").clicked() {
self.ignored_folders.insert(folder.clone());
Expand Down

0 comments on commit 9a154ee

Please sign in to comment.