Skip to content

Commit

Permalink
Add confirmation prompt to file/folder trash action (flxzt#1237)
Browse files Browse the repository at this point in the history
* feat: add deletion prompt

* refactor: use new clone syntax

* fix: dialogue stopped working after cancelation

* fix: satisfy clippy

* chore: append AUTHORS file
  • Loading branch information
RayJW authored Sep 29, 2024
1 parent bbf5417 commit 7a5338a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Seio Inoue
Moritz Mechelk
PhilProg
Silvan Schmidt
Doublonmousse
Doublonmousse
RayJW
11 changes: 11 additions & 0 deletions crates/rnote-ui/data/ui/dialogs/dialogs.ui
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,15 @@ Changes which are not saved will be permanently lost.</property>
<object class="RnIconPicker" id="edit_selected_workspace_icon_picker"></object>
</child>
</object>

<object class="AdwAlertDialog" id="dialog_trash_file">
<property name="heading" translatable="yes">Trash File</property>
<property name="body" translatable="yes">Are you sure you want to move this file to the trash?</property>
<property name="default-response">cancel</property>
<property name="close-response">cancel</property>
<responses>
<response id="cancel" translatable="yes">Cancel</response>
<response id="trash" appearance="destructive" translatable="yes">Trash</response>
</responses>
</object>
</interface>
44 changes: 44 additions & 0 deletions crates/rnote-ui/src/dialogs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,50 @@ pub(crate) async fn dialog_edit_selected_workspace(appwindow: &RnAppWindow) {
dialog.present(appwindow.root().as_ref());
}

pub(crate) async fn dialog_trash_file(appwindow: &RnAppWindow, current_file: &gio::File) {
let builder = Builder::from_resource(
(String::from(config::APP_IDPATH) + "ui/dialogs/dialogs.ui").as_str(),
);
let dialog: adw::AlertDialog = builder.object("dialog_trash_file").unwrap();

match dialog.choose_future(appwindow).await.as_str() {
"trash" => {
glib::spawn_future_local(clone!(
#[weak]
appwindow,
#[strong]
current_file,
async move {
current_file.trash_async(
glib::source::Priority::DEFAULT,
None::<&gio::Cancellable>,
clone!(
#[weak]
appwindow,
#[strong]
current_file,
move |res| {
if let Err(e) = res {
appwindow
.overlays()
.dispatch_toast_error(&gettext("Trashing file failed"));
error!(
"Trash filerow file `{current_file:?}` failed , Err: {e:?}"
);
return;
}
}
),
);
}
));
}
_ => {
// Cancel
}
}
}

const WORKSPACELISTENTRY_ICONS_LIST: &[&str] = &[
"workspacelistentryicon-bandaid-symbolic",
"workspacelistentryicon-bank-symbolic",
Expand Down
37 changes: 12 additions & 25 deletions crates/rnote-ui/src/workspacebrowser/filerow/actions/trash.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Imports
use crate::workspacebrowser::RnFileRow;
use crate::RnAppWindow;
use gettextrs::gettext;
use gtk4::{gio, glib, glib::clone, prelude::FileExt};
use tracing::debug;
use crate::{dialogs, workspacebrowser::RnFileRow};
use gtk4::{gio, glib, glib::clone};

/// Create a new `trash` action.
pub(crate) fn trash(filerow: &RnFileRow, appwindow: &RnAppWindow) -> gio::SimpleAction {
Expand All @@ -13,30 +11,19 @@ pub(crate) fn trash(filerow: &RnFileRow, appwindow: &RnAppWindow) -> gio::Simple
filerow,
#[weak]
appwindow,
move |_action_trash_file, _| {
move |_, _| {
let Some(current_file) = filerow.current_file() else {
return;
};
current_file.trash_async(
glib::source::Priority::DEFAULT,
None::<&gio::Cancellable>,
clone!(
#[weak]
filerow,
#[strong]
current_file,
move |res| {
if let Err(e) = res {
appwindow
.overlays()
.dispatch_toast_error(&gettext("Trashing file failed"));
debug!("Trash filerow file `{current_file:?}` failed , Err: {e:?}");
return;
}
filerow.set_current_file(None);
}
),
);
glib::spawn_future_local(clone!(
#[weak]
appwindow,
#[strong]
current_file,
async move {
dialogs::dialog_trash_file(&appwindow, &current_file).await;
}
));
}
));
action
Expand Down

0 comments on commit 7a5338a

Please sign in to comment.