From 0e440372392dc5cba11ffdba0b4e1a0ae0165121 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Sun, 28 Jan 2024 21:05:55 +0100 Subject: [PATCH] ImageView: Reliably show image restored from trash Closes #316 --- src/widgets/image_view.rs | 14 ++++++++++++++ src/window.rs | 17 ++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index b08f921..e654c0e 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -81,6 +81,10 @@ mod imp { pub drag_source: gtk::DragSource, + /// File where trash operation has been undone + #[property(get, set, nullable)] + trash_restore: RefCell>, + pub(super) model: RefCell, pub(super) preserve_content: Cell, @@ -522,6 +526,16 @@ impl LpImageView { /// Handle files are added or removed from directory fn model_content_changed_cb(&self) { + // Animate to image that was restored from trash + if let Some(trash_restore) = self.trash_restore() { + if self.model().contains(&trash_restore) { + self.set_trash_restore(gio::File::NONE); + self.update_sliding_view(&trash_restore); + self.scroll_sliding_view(&trash_restore); + return; + } + } + let Some(current_file) = self.current_file() else { return; }; diff --git a/src/window.rs b/src/window.rs index ff6aef1..4fbb6c8 100644 --- a/src/window.rs +++ b/src/window.rs @@ -667,16 +667,15 @@ impl LpWindow { toast.connect_button_clicked(glib::clone!(@weak self as win => move |_| { let path = path.clone(); glib::spawn_future_local(async move { + win.image_view() + .set_trash_restore(Some(gio::File::for_path(&path))); let result = crate::util::untrash(&path).await; - match result { - Ok(()) => win.image_view().set_images_from_files(Vec::from([gio::File::for_path(&path)])), - Err(err) => { - log::error!("Failed to untrash {path:?}: {err}"); - win.show_toast( - gettext("Failed to restore image from trash"), - adw::ToastPriority::High, - ); - } + if let Err(err) = result { + log::error!("Failed to untrash {path:?}: {err}"); + win.show_toast( + gettext("Failed to restore image from trash"), + adw::ToastPriority::High, + ); } }); }));