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 a59eed3 commit 299333a
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/move_module.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use eframe::egui;
use native_dialog::FileDialog;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use std::thread; // 添加路径选择器依赖

pub struct MoveModule {
pub show_window: bool,
pub folder_name: String,
pub folder_name: String, // 完整路径
pub selected_path: Option<PathBuf>,
pub progress: f32, // 复制进度
pub status_message: Option<String>, // 操作状态
Expand Down Expand Up @@ -40,8 +41,14 @@ impl MoveModule {
ui.label(path.display().to_string());
}
if ui.button("选择目标路径").clicked() {
// 模拟路径选择逻辑
self.selected_path = Some(PathBuf::from("C:/YourSelectedPath"));
// 弹出系统文件选择器
if let Ok(Some(path)) = FileDialog::new().show_open_single_dir() {
self.selected_path = Some(path);
println!(
"选定目标路径: {}",
self.selected_path.as_ref().unwrap().display()
);
}
}
});

Expand Down Expand Up @@ -71,9 +78,10 @@ impl MoveModule {

fn start_move_folder(&mut self, target_path: PathBuf) {
let source_path = PathBuf::from(&self.folder_name);

if !source_path.exists() {
self.status_message = Some(format!("源文件夹 {} 不存在", self.folder_name));
println!("源文件夹 {} 不存在", self.folder_name);
self.status_message = Some(format!("源文件夹不存在: {}", source_path.display()));
println!("源文件夹不存在: {}", source_path.display());
return;
}

Expand All @@ -86,6 +94,12 @@ impl MoveModule {

// 启动后台线程执行移动逻辑
thread::spawn(move || {
println!(
"开始复制: 从 {} 到 {}",
source_path.display(),
target_path.display()
);

if let Err(err) = fs::create_dir_all(&target_path) {
let _ = tx.send(Err(format!("无法创建目标目录: {}", err)));
return;
Expand Down Expand Up @@ -172,6 +186,12 @@ fn copy_dir_with_progress(
let src_path = entry.path();
let dest_path = target.join(entry.file_name());

println!(
"复制文件: 从 {} 到 {}",
src_path.display(),
dest_path.display()
);

if file_type.is_dir() {
fs::create_dir_all(&dest_path).map_err(|err| format!("无法创建目录: {}", err))?;
copy_dir_with_progress(&src_path, &dest_path, tx)?;
Expand Down

0 comments on commit 299333a

Please sign in to comment.