[nautilus/wip/antoniof/switch-to-gtk4: 18/40] files-view: Use event controller for zooming




commit 9c4720d5d223a1f3ccec07525e9899b3e605ce85
Author: António Fernandes <antoniof gnome org>
Date:   Mon Dec 20 16:53:36 2021 +0000

    files-view: Use event controller for zooming
    
    Note that GtkEventControllerScroll::scroll abstracts both smooth and
    discrete scroll events into a discrete `gdouble dy` parameter. This
    makes for simpler code here.

 src/nautilus-files-view.c | 70 ++++++++++-------------------------------------
 1 file changed, 15 insertions(+), 55 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index c32af3dd5..0838ec1d3 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -9122,42 +9122,26 @@ nautilus_files_view_set_property (GObject      *object,
 
 /* handle Ctrl+Scroll, which will cause a zoom-in/out */
 static gboolean
-on_event (GtkWidget *widget,
-          GdkEvent  *event,
-          gpointer   user_data)
+on_scroll (GtkEventControllerScroll *scroll,
+           gdouble                   dx,
+           gdouble                   dy,
+           gpointer                  user_data)
 {
     NautilusFilesView *directory_view;
-    static gdouble total_delta_y = 0;
     GdkModifierType state;
-    GdkScrollDirection direction;
-    gdouble delta_x, delta_y;
 
-    directory_view = NAUTILUS_FILES_VIEW (widget);
+    directory_view = NAUTILUS_FILES_VIEW (user_data);
 
-    if (gdk_event_get_event_type (event) != GDK_SCROLL)
+    state = gtk_event_controller_get_current_event_state (GTK_EVENT_CONTROLLER (scroll));
+    if (state & GDK_CONTROL_MASK)
     {
-        return GDK_EVENT_PROPAGATE;
-    }
-
-    if (!gdk_event_get_state (event, &state))
-    {
-        return GDK_EVENT_PROPAGATE;
-    }
-
-    if (!(state & GDK_CONTROL_MASK))
-    {
-        return GDK_EVENT_PROPAGATE;
-    }
-
-    if (gdk_event_get_scroll_direction (event, &direction))
-    {
-        if (direction == GDK_SCROLL_UP)
+        if (dy <= -1)
         {
             /* Zoom In */
             nautilus_files_view_bump_zoom_level (directory_view, 1);
             return GDK_EVENT_STOP;
         }
-        else if (direction == GDK_SCROLL_DOWN)
+        else if (dy >= 1)
         {
             /* Zoom Out */
             nautilus_files_view_bump_zoom_level (directory_view, -1);
@@ -9165,32 +9149,6 @@ on_event (GtkWidget *widget,
         }
     }
 
-    if (gdk_event_get_scroll_deltas (event, &delta_x, &delta_y))
-    {
-        /* try to emulate a normal scrolling event by summing deltas */
-        total_delta_y += delta_y;
-
-        if (total_delta_y >= 1)
-        {
-            total_delta_y = 0;
-            /* emulate scroll down */
-            nautilus_files_view_bump_zoom_level (directory_view, -1);
-            return GDK_EVENT_STOP;
-        }
-        else if (total_delta_y <= -1)
-        {
-            total_delta_y = 0;
-            /* emulate scroll up */
-            nautilus_files_view_bump_zoom_level (directory_view, 1);
-            return GDK_EVENT_STOP;
-        }
-        else
-        {
-            /* eat event */
-            return GDK_EVENT_STOP;
-        }
-    }
-
     return GDK_EVENT_PROPAGATE;
 }
 
@@ -9512,6 +9470,7 @@ nautilus_files_view_init (NautilusFilesView *view)
 #endif
     NautilusDirectory *scripts_directory;
     NautilusDirectory *templates_directory;
+    GtkEventController *controller;
     gchar *templates_uri;
 #if 0 && NAUTILUS_CLIPBOARD_NEEDS_GTK4_REIMPLEMENTATION
     GtkClipboard *clipboard;
@@ -9612,10 +9571,11 @@ nautilus_files_view_init (NautilusFilesView *view)
                                     GTK_POLICY_AUTOMATIC);
     gtk_widget_show (priv->scrolled_window);
 
-    g_signal_connect_swapped (priv->scrolled_window,
-                              "event",
-                              G_CALLBACK (on_event),
-                              view);
+    controller = gtk_event_controller_scroll_new (GTK_EVENT_CONTROLLER_SCROLL_VERTICAL |
+                                                  GTK_EVENT_CONTROLLER_SCROLL_DISCRETE);
+    gtk_widget_add_controller (priv->scrolled_window, controller);
+    gtk_event_controller_set_propagation_phase (controller, GTK_PHASE_CAPTURE);
+    g_signal_connect (controller, "scroll", G_CALLBACK (on_scroll), view);
 
     gtk_overlay_set_child (GTK_OVERLAY (priv->overlay), priv->scrolled_window);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]