From cf8fa499d977780fb05c23e3004d7cbb01141fd5 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Sat, 15 Jul 2023 20:21:00 +0200 Subject: [PATCH] window: New way to detect own window in d+d This is a (hopefully) cleaner way to detect if we are dragging out of the own window to not show drag indicator in that case. Part-of: --- src/widgets/image_view.rs | 15 ++++++++++----- src/window.rs | 17 ++--------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index c96b87b..9557a37 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -78,6 +78,8 @@ mod imp { #[template_child] pub sliding_view: TemplateChild, + pub drag_source: gtk::DragSource, + pub(super) model: RefCell, pub(super) preserve_content: Cell, @@ -164,10 +166,9 @@ mod imp { self.current_image_signals.set(signal_group).unwrap(); - let source = gtk::DragSource::new(); - source.set_exclusive(true); + self.drag_source.set_exclusive(true); - source.connect_prepare( + self.drag_source.connect_prepare( glib::clone!(@weak obj => @default-return None, move |gesture, _, _| { let is_scrollable = obj .current_page() @@ -184,7 +185,7 @@ mod imp { }), ); - source.connect_drag_begin(glib::clone!(@weak obj => move |source, _| { + self.drag_source.connect_drag_begin(glib::clone!(@weak obj => move |source, _| { if let Some(paintable) = obj.current_image().and_then(|p| p.thumbnail()) { // -6 for cursor width, +16 for margin in .drag-icon source.set_icon(Some(&paintable), paintable.intrinsic_width() / 2 - 6 + 16, -12); @@ -198,7 +199,7 @@ mod imp { }; })); - obj.add_controller(source); + obj.add_controller(self.drag_source.clone()); } } @@ -545,6 +546,10 @@ impl LpImageView { .and_then(|x| x.image().file()) } + pub fn drag_source(&self) -> gtk::DragSource { + self.imp().drag_source.clone() + } + /// Returns `true` if there is an image before the current one pub fn is_previous_available(&self) -> bool { if let Some(file) = self.current_file() { diff --git a/src/window.rs b/src/window.rs index 828a704..3444220 100644 --- a/src/window.rs +++ b/src/window.rs @@ -377,23 +377,10 @@ mod imp { self.drop_target.set_types(&[gdk::FileList::static_type()]); - //only accept drops from out side the app - //https://gitlab.gnome.org/GNOME/Incubator/loupe/-/merge_requests/244#note_1788260 + // Only accept drops from external sources or different windows self.drop_target.connect_accept( clone!(@weak obj => @default-return false, move |_drop_target, drop| { - //drag is between Loupe windows - if let Some(drop) = drop.drag() { - let file = drop - .content() - .value(gdk::FileList::static_type()) - .ok() - .and_then(|value| value.get::().ok()) - .and_then(|files| files.files().first().and_then(|file| file.path())); - let current_image = obj.image_view().current_file().and_then(|file| file.path()); - return file.ne(¤t_image); - } - //drag from another window, should always be accepted - true + drop.drag().is_none() || drop.drag() != obj.image_view().drag_source().drag() }), );