Support permanently deleting files and directories using Shift+Del

Add a confirmation dialog to limit risks of data lost.
This commit is contained in:
Tim Dengel 2024-08-12 05:23:04 +02:00 committed by Gwen Lg
parent 8ced8b0551
commit 1a66d7b184
4 changed files with 97 additions and 1 deletions

View file

@ -33,7 +33,7 @@ use cosmic::{
widget::{
self,
dnd_destination::DragId,
horizontal_space,
horizontal_space, icon,
menu::{action::MenuAction, key_bind::KeyBind},
segmented_button::{self, Entity},
vertical_space,
@ -138,6 +138,7 @@ pub enum Action {
OpenTerminal,
OpenWith,
Paste,
PermanentlyDelete,
Preview,
Rename,
RestoreFromTrash,
@ -205,6 +206,7 @@ impl Action {
Action::OpenTerminal => Message::OpenTerminal(entity_opt),
Action::OpenWith => Message::OpenWithDialog(entity_opt),
Action::Paste => Message::Paste(entity_opt),
Action::PermanentlyDelete => Message::PermanentlyDelete(entity_opt),
Action::Preview => Message::Preview(entity_opt),
Action::Rename => Message::Rename(entity_opt),
Action::RestoreFromTrash => Message::RestoreFromTrash(entity_opt),
@ -346,6 +348,7 @@ pub enum Message {
PendingError(u64, OperationError),
PendingPause(u64, bool),
PendingPauseAll(bool),
PermanentlyDelete(Option<Entity>),
Preview(Option<Entity>),
RescanTrash,
Rename(Option<Entity>),
@ -481,6 +484,9 @@ pub enum DialogPage {
selected: usize,
store_opt: Option<MimeApp>,
},
PermanentlyDelete {
paths: Vec<PathBuf>,
},
RenameItem {
from: PathBuf,
parent: PathBuf,
@ -2461,6 +2467,9 @@ impl Application for App {
}
}
}
DialogPage::PermanentlyDelete { paths } => {
return self.operation(Operation::PermanentlyDelete { paths });
}
DialogPage::RenameItem {
from, parent, name, ..
} => {
@ -3137,6 +3146,13 @@ impl Application for App {
}
}
}
Message::PermanentlyDelete(entity_opt) => {
let paths = self.selected_paths(entity_opt);
if !paths.is_empty() {
self.dialog_pages
.push_back(DialogPage::PermanentlyDelete { paths });
}
}
Message::Preview(entity_opt) => {
match self.mode {
Mode::App => {
@ -4655,6 +4671,35 @@ impl Application for App {
dialog
}
DialogPage::PermanentlyDelete { paths } => {
let target = if paths.len() == 1 {
format!(
"« {} »",
paths[0]
.file_name()
.map(std::ffi::OsStr::to_string_lossy)
.unwrap_or_else(|| paths[0].to_string_lossy())
)
} else {
fl!("selected-items", items = paths.len())
};
widget::dialog()
.title(fl!("permanently-delete-question"))
.icon(icon::from_name("dialog-warning").size(32))
.primary_action(
widget::button::destructive(fl!("delete"))
.on_press(Message::DialogComplete),
)
.secondary_action(
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
)
.control(widget::text(fl!(
"permanently-delete-warning",
nb_items = paths.len(),
target = target
)))
}
DialogPage::RenameItem {
from,
parent,