window: Better solution for swipe sending motion

Swipe gestures send motion events without changed cursor position.
Catch that in a better way to avoid showing overlay controls on swipe.
This commit is contained in:
Sophie Herold 2023-05-01 18:06:38 +02:00
parent 24bbd7eae2
commit dbb65fa2cb
2 changed files with 22 additions and 18 deletions

View file

@ -88,7 +88,7 @@
<property name="show-end-title-buttons" bind-source="LpWindow" bind-property="fullscreened" bind-flags="sync-create|invert-boolean"/>
<property name="show-start-title-buttons" bind-source="LpWindow" bind-property="fullscreened" bind-flags="sync-create|invert-boolean"/>"
<style>
<class name="fullscreen-osd" />
<class name="fullscreen-osd"/>
</style>
<child type="start">
<object class="GtkButton">
@ -118,7 +118,7 @@
<property name="tooltip-text" translatable="yes">Rotate Left</property>
<property name="action-name">win.rotate_ccw</property>
<style>
<class name="flat" />
<class name="flat"/>
</style>
</object>
</child>
@ -128,7 +128,7 @@
<property name="tooltip-text" translatable="yes">Rotate Right</property>
<property name="action-name">win.rotate_cw</property>
<style>
<class name="flat" />
<class name="flat"/>
</style>
</object>
</child>
@ -143,7 +143,7 @@
</object>
</child>
<child>
<object class="GtkEventControllerMotion" id="headerbar_events" />
<object class="GtkEventControllerMotion" id="headerbar_events"/>
</child>
</object>
</child>
@ -208,12 +208,6 @@
</property>
</object>
</property>
<child>
<object class="GtkEventControllerMotion" id="motion_controller">
<signal name="motion" handler="on_motion_cb" swapped="yes"/>
<signal name="enter" handler="on_motion_cb" swapped="yes"/>
<signal name="leave" handler="on_motion_cb" swapped="yes"/>
</object>
</child>
</template>
</interface>

View file

@ -31,7 +31,7 @@ use gtk::CompositeTemplate;
use once_cell::sync::OnceCell;
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::path::{Path, PathBuf};
use crate::config;
@ -92,8 +92,8 @@ mod imp {
#[template_child]
pub(super) drop_target: TemplateChild<gtk::DropTarget>,
/// Motion controller for complete window
#[template_child]
pub(super) motion_controller: TemplateChild<gtk::EventControllerMotion>,
pub(super) motion_controller: gtk::EventControllerMotion,
pub(super) pointer_position: Cell<(f64, f64)>,
pub(super) show_controls_animation: OnceCell<adw::TimedAnimation>,
pub(super) hide_controls_animation: OnceCell<adw::TimedAnimation>,
@ -261,6 +261,12 @@ mod imp {
.connect_pressed(glib::clone!(@weak obj => move |_, _, _, _| obj.on_click()));
obj.add_controller(gesture_click);
self.motion_controller
.connect_motion(glib::clone!(@weak obj => move |_, x, y| obj.on_motion((x,y))));
self.motion_controller
.connect_enter(glib::clone!(@weak obj => move |_, x, y| obj.on_motion((x,y))));
obj.add_controller(self.motion_controller.clone());
let current_image_signals = self.image_view.current_image_signals();
// clone! is a macro from glib-rs that allows
// you to easily handle references in callbacks
@ -846,12 +852,16 @@ impl LpWindow {
}
}
#[template_callback]
fn on_motion_cb(&self) {
let controller = &self.imp().motion_controller;
if controller.current_event_time() == 0 {
fn on_motion(&self, pointer_position: (f64, f64)) {
let imp = self.imp();
// Check if position really changed since swipe gesture sends change event with same position.
// Also, don't connect this to "leave" because swipe also sends fake "leave" events when
// the widget under the cursor changes.
if imp.pointer_position.get() == pointer_position {
return;
}
imp.pointer_position.set(pointer_position);
self.show_controls();