From f1d08860878db01302c2418dfd46c413a595b71d Mon Sep 17 00:00:00 2001 From: panoplie <59100-panoplie@users.noreply.gitlab.gnome.org> Date: Thu, 24 Feb 2022 23:36:58 +0100 Subject: [PATCH] gtkeventcontrollerscroll: Map surface scroll unit in discrete steps --- gtk/gtkeventcontrollerscroll.c | 40 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/gtk/gtkeventcontrollerscroll.c b/gtk/gtkeventcontrollerscroll.c index e1422e1598..500993bb8f 100644 --- a/gtk/gtkeventcontrollerscroll.c +++ b/gtk/gtkeventcontrollerscroll.c @@ -67,6 +67,7 @@ #define SCROLL_CAPTURE_THRESHOLD_MS 150 #define HOLD_TIMEOUT_MS 50 +#define SURFACE_UNIT_DISCRETE_MAPPING 10 typedef struct { @@ -352,6 +353,7 @@ gtk_event_controller_scroll_handle_event (GtkEventController *controller, double dx = 0, dy = 0; gboolean handled = GDK_EVENT_PROPAGATE; GdkEventType event_type; + GdkScrollUnit scroll_unit; event_type = gdk_event_get_event_type (event); @@ -367,7 +369,7 @@ gtk_event_controller_scroll_handle_event (GtkEventController *controller, g_clear_handle_id (&scroll->hold_timeout_id, g_source_remove); - scroll->cur_unit = gdk_scroll_event_get_unit (event); + scroll_unit = gdk_scroll_event_get_unit (event); /* FIXME: Handle device changes */ direction = gdk_scroll_event_get_direction (event); @@ -389,18 +391,31 @@ gtk_event_controller_scroll_handle_event (GtkEventController *controller, scroll->cur_dy += dy; dx = dy = 0; - if (ABS (scroll->cur_dx) >= 1) + if (scroll_unit == GDK_SCROLL_UNIT_SURFACE) { - steps = trunc (scroll->cur_dx); - scroll->cur_dx -= steps; - dx = steps; - } + dx = (int) scroll->cur_dx / SURFACE_UNIT_DISCRETE_MAPPING; + scroll->cur_dx -= dx * SURFACE_UNIT_DISCRETE_MAPPING; + + dy = (int) scroll->cur_dy / SURFACE_UNIT_DISCRETE_MAPPING; + scroll->cur_dy -= dy * SURFACE_UNIT_DISCRETE_MAPPING; - if (ABS (scroll->cur_dy) >= 1) + scroll_unit = GDK_SCROLL_UNIT_WHEEL; + } + else { - steps = trunc (scroll->cur_dy); - scroll->cur_dy -= steps; - dy = steps; + if (ABS (scroll->cur_dx) >= 1) + { + steps = trunc (scroll->cur_dx); + scroll->cur_dx -= steps; + dx = steps; + } + + if (ABS (scroll->cur_dy) >= 1) + { + steps = trunc (scroll->cur_dy); + scroll->cur_dy -= steps; + dy = steps; + } } } } @@ -432,6 +447,8 @@ gtk_event_controller_scroll_handle_event (GtkEventController *controller, dx = 0; } + scroll->cur_unit = scroll_unit; + if (dx != 0 || dy != 0) g_signal_emit (controller, signals[SCROLL], 0, dx, dy, &handled); else if (direction == GDK_SCROLL_SMOOTH && @@ -629,6 +646,9 @@ gtk_event_controller_scroll_get_flags (GtkEventControllerScroll *scroll) * Gets the scroll unit of the last * [signal@Gtk.EventControllerScroll::scroll] signal received. * + * Always returns %GDK_SCROLL_UNIT_WHEEL if the + * %GTK_EVENT_CONTROLLER_SCROLL_DISCRETE flag is set. + * * Returns: the scroll unit. * * Since: 4.8 -- 2.30.2