diff --git a/src/app.rs b/src/app.rs index 34bb78a..98c5865 100644 --- a/src/app.rs +++ b/src/app.rs @@ -654,17 +654,11 @@ impl Application for App { match dialog_page { DialogPage::NewItem { parent, name, dir } => { let path = parent.join(name); - match if dir { - fs::create_dir(&path) + self.operation(if dir { + Operation::NewFolder { path } } else { - fs::File::create(&path).map(|_| ()) - } { - Ok(()) => {} - Err(err) => { - //TODO: dialog - log::warn!("failed to create {:?}: {}", path, err); - } - } + Operation::NewFile { path } + }); } } } @@ -762,6 +756,7 @@ impl Application for App { } Message::PendingError(id, err) => { if let Some((op, _)) = self.pending_operations.remove(&id) { + //TODO: dialog? self.failed_operations.insert(id, (op, err)); } } diff --git a/src/operation.rs b/src/operation.rs index caee0f6..d049ee0 100644 --- a/src/operation.rs +++ b/src/operation.rs @@ -1,5 +1,5 @@ use cosmic::iced::futures::{channel::mpsc, SinkExt}; -use std::path::PathBuf; +use std::{fs, path::PathBuf}; use crate::app::Message; @@ -10,13 +10,29 @@ fn err_str(err: T) -> String { #[derive(Clone, Debug, Eq, Hash, PartialEq)] pub enum Operation { /// Copy items - Copy { paths: Vec, to: PathBuf }, + Copy { + paths: Vec, + to: PathBuf, + }, /// Move items to the trash - Delete { paths: Vec }, + Delete { + paths: Vec, + }, /// Move items - Move { paths: Vec, to: PathBuf }, + Move { + paths: Vec, + to: PathBuf, + }, + NewFile { + path: PathBuf, + }, + NewFolder { + path: PathBuf, + }, /// Restore a path from the trash - Restore { paths: Vec }, + Restore { + paths: Vec, + }, } impl Operation { @@ -44,6 +60,20 @@ impl Operation { .await; } } + Self::NewFolder { path } => { + tokio::task::spawn_blocking(|| fs::create_dir(path)) + .await + .map_err(err_str)? + .map_err(err_str)?; + let _ = msg_tx.send(Message::PendingProgress(id, 100.0)).await; + } + Self::NewFile { path } => { + tokio::task::spawn_blocking(|| fs::File::create(path)) + .await + .map_err(err_str)? + .map_err(err_str)?; + let _ = msg_tx.send(Message::PendingProgress(id, 100.0)).await; + } Self::Restore { paths } => { let total = paths.len(); let mut count = 0;