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: <https://gitlab.gnome.org/GNOME/loupe/-/merge_requests/258>
This commit is contained in:
Sophie Herold 2023-07-15 20:21:00 +02:00
parent 78b16518e5
commit cf8fa499d9
2 changed files with 12 additions and 20 deletions

View file

@ -78,6 +78,8 @@ mod imp {
#[template_child] #[template_child]
pub sliding_view: TemplateChild<LpSlidingView>, pub sliding_view: TemplateChild<LpSlidingView>,
pub drag_source: gtk::DragSource,
pub(super) model: RefCell<LpFileModel>, pub(super) model: RefCell<LpFileModel>,
pub(super) preserve_content: Cell<bool>, pub(super) preserve_content: Cell<bool>,
@ -164,10 +166,9 @@ mod imp {
self.current_image_signals.set(signal_group).unwrap(); self.current_image_signals.set(signal_group).unwrap();
let source = gtk::DragSource::new(); self.drag_source.set_exclusive(true);
source.set_exclusive(true);
source.connect_prepare( self.drag_source.connect_prepare(
glib::clone!(@weak obj => @default-return None, move |gesture, _, _| { glib::clone!(@weak obj => @default-return None, move |gesture, _, _| {
let is_scrollable = obj let is_scrollable = obj
.current_page() .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()) { if let Some(paintable) = obj.current_image().and_then(|p| p.thumbnail()) {
// -6 for cursor width, +16 for margin in .drag-icon // -6 for cursor width, +16 for margin in .drag-icon
source.set_icon(Some(&paintable), paintable.intrinsic_width() / 2 - 6 + 16, -12); 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()) .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 /// Returns `true` if there is an image before the current one
pub fn is_previous_available(&self) -> bool { pub fn is_previous_available(&self) -> bool {
if let Some(file) = self.current_file() { if let Some(file) = self.current_file() {

View file

@ -377,23 +377,10 @@ mod imp {
self.drop_target.set_types(&[gdk::FileList::static_type()]); self.drop_target.set_types(&[gdk::FileList::static_type()]);
//only accept drops from out side the app // Only accept drops from external sources or different windows
//https://gitlab.gnome.org/GNOME/Incubator/loupe/-/merge_requests/244#note_1788260
self.drop_target.connect_accept( self.drop_target.connect_accept(
clone!(@weak obj => @default-return false, move |_drop_target, drop| { clone!(@weak obj => @default-return false, move |_drop_target, drop| {
//drag is between Loupe windows drop.drag().is_none() || drop.drag() != obj.image_view().drag_source().drag()
if let Some(drop) = drop.drag() {
let file = drop
.content()
.value(gdk::FileList::static_type())
.ok()
.and_then(|value| value.get::<gdk::FileList>().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(&current_image);
}
//drag from another window, should always be accepted
true
}), }),
); );