gtkeventcontrollerscroll: Map surface scroll unit in discrete steps
authorpanoplie <59100-panoplie@users.noreply.gitlab.gnome.org>
Thu, 24 Feb 2022 22:36:58 +0000 (23:36 +0100)
committerpanoplie <59100-panoplie@users.noreply.gitlab.gnome.org>
Fri, 18 Mar 2022 23:41:26 +0000 (00:41 +0100)
gtk/gtkeventcontrollerscroll.c

index e1422e1598eb42fb5f1cbcf2db783ce06a08db04..500993bb8ffa176f7fb2f7a41d930d2ec67dbe83 100644 (file)
@@ -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