[gnome-builder] minimap: support GdkEventScroll to scroll source view



commit f8a70ea75d19ef94a4df4aa70ac2a03d8efc72af
Author: Christian Hergert <christian hergert me>
Date:   Sat Apr 25 01:45:17 2015 -0700

    minimap: support GdkEventScroll to scroll source view
    
    This will translate the scroll event into a ::move-viewport on the
    primary GtkSourceView widget.
    
    It would be nice if we could deal with smooth scrolling here, but the
    X11 gdk backend seems to be doing that pretty well with my touchpad.
    
    We'll see how big of an issue it is based on user feedback.

 libide/ide-source-map.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide-source-map.c b/libide/ide-source-map.c
index ccde3da..fe7fe0e 100644
--- a/libide/ide-source-map.c
+++ b/libide/ide-source-map.c
@@ -645,6 +645,54 @@ ide_source_map_size_allocate (GtkWidget     *widget,
   update_scrubber_height (self);
 }
 
+static gboolean
+ide_source_map_do_scroll_event (IdeSourceMap   *self,
+                                GdkEventScroll *event,
+                                GtkWidget      *widget)
+{
+  g_assert (IDE_IS_SOURCE_MAP (self));
+  g_assert (event != NULL);
+  g_assert (GTK_IS_WIDGET (widget));
+
+#define SCROLL_ACCELERATION 4
+
+  /*
+   * TODO: This doesn't propagate kinetic scrolling or anything.
+   *       We should probably make something that does that.
+   */
+  if (self->view != NULL)
+    {
+      gdouble x;
+      gdouble y;
+      gint count = 0;
+
+      if (event->direction == GDK_SCROLL_UP)
+        {
+          count = -SCROLL_ACCELERATION;
+        }
+      else if (event->direction == GDK_SCROLL_DOWN)
+        {
+          count = SCROLL_ACCELERATION;
+        }
+      else
+        {
+          gdk_event_get_scroll_deltas ((GdkEvent *)event, &x, &y);
+
+          if (y > 0)
+            count = SCROLL_ACCELERATION;
+          else if (y < 0)
+            count = -SCROLL_ACCELERATION;
+        }
+
+      if (count != 0)
+        g_signal_emit_by_name (self->view, "move-viewport", GTK_SCROLL_STEPS, count);
+    }
+
+#undef SCROLL_ACCELERATION
+
+  return GDK_EVENT_PROPAGATE;
+}
+
 static void
 ide_source_map_finalize (GObject *object)
 {
@@ -760,6 +808,12 @@ ide_source_map_init (IdeSourceMap *self)
                            G_CALLBACK (ide_source_map__child_view_button_press_event),
                            self,
                            G_CONNECT_SWAPPED);
+  gtk_widget_add_events (GTK_WIDGET (self->child_view), GDK_SCROLL_MASK);
+  g_signal_connect_object (self->child_view,
+                           "scroll-event",
+                           G_CALLBACK (ide_source_map_do_scroll_event),
+                           self,
+                           G_CONNECT_SWAPPED);
   g_signal_connect_object (self->child_view,
                            "state-flags-changed",
                            G_CALLBACK (ide_source_map__child_view_state_flags_changed),
@@ -809,6 +863,12 @@ ide_source_map_init (IdeSourceMap *self)
                            G_CALLBACK (ide_source_map__overlay_box_button_press_event),
                            self,
                            G_CONNECT_SWAPPED);
+  gtk_widget_add_events (GTK_WIDGET (self->overlay_box), GDK_SCROLL_MASK);
+  g_signal_connect_object (self->overlay_box,
+                           "scroll-event",
+                           G_CALLBACK (ide_source_map_do_scroll_event),
+                           self,
+                           G_CONNECT_SWAPPED);
   g_signal_connect_object (self->overlay_box,
                            "button-release-event",
                            G_CALLBACK (ide_source_map__overlay_box_button_release_event),


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