[libhandy/swipe-tracker-public: 4/16] swipe-tracker: Register event capture handler implicitly



commit 714cec456b9ee2808e7ff8fd46a9bd5336e3bdf8
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Jun 14 22:10:41 2020 +0500

    swipe-tracker: Register event capture handler implicitly
    
    The way we register the event handler is a hack, and the only reason to do
    it is because GTK3 doesn't offer other ways to do it unlike GTK4. It's not
    something we'd like to ever expose in a public API, so let's encapsulate it
    better.
    
    Signed-off-by: Alexander Mikhaylenko <alexm gnome org>

 debian/libhandy-1-0.symbols     |  1 -
 src/hdy-carousel.c              | 29 +++-------------------
 src/hdy-carousel.ui             |  1 +
 src/hdy-deck.c                  | 17 -------------
 src/hdy-leaflet.c               | 17 -------------
 src/hdy-stackable-box-private.h |  2 --
 src/hdy-stackable-box.c         |  7 ------
 src/hdy-swipe-tracker-private.h |  3 ---
 src/hdy-swipe-tracker.c         | 55 ++++++++++++++++++++---------------------
 9 files changed, 32 insertions(+), 100 deletions(-)
---
diff --git a/debian/libhandy-1-0.symbols b/debian/libhandy-1-0.symbols
index 08965f0a..f41f7989 100644
--- a/debian/libhandy-1-0.symbols
+++ b/debian/libhandy-1-0.symbols
@@ -271,7 +271,6 @@ libhandy-1.so.0 libhandy-1-0 #MINVER#
  hdy_squeezer_transition_type_get_type@LIBHANDY_1_0 0.0.10
  hdy_stackable_box_add@LIBHANDY_1_0 0.80.0
  hdy_stackable_box_begin_swipe@LIBHANDY_1_0 0.80.0
- hdy_stackable_box_captured_event@LIBHANDY_1_0 0.80.0
  hdy_stackable_box_direction_changed@LIBHANDY_1_0 0.80.0
  hdy_stackable_box_draw@LIBHANDY_1_0 0.80.0
  hdy_stackable_box_end_swipe@LIBHANDY_1_0 0.80.0
diff --git a/src/hdy-carousel.c b/src/hdy-carousel.c
index 759e1827..4a72e50f 100644
--- a/src/hdy-carousel.c
+++ b/src/hdy-carousel.c
@@ -622,8 +622,8 @@ scroll_timeout_cb (HdyCarousel *self)
 }
 
 static gboolean
-handle_discrete_scroll_event (HdyCarousel *self,
-                              GdkEvent    *event)
+scroll_event_cb (HdyCarousel *self,
+                 GdkEvent    *event)
 {
   GdkDevice *source_device;
   GdkInputSource input_source;
@@ -712,16 +712,6 @@ handle_discrete_scroll_event (HdyCarousel *self,
   return GDK_EVENT_STOP;
 }
 
-static gboolean
-captured_event_cb (HdyCarousel *self,
-                   GdkEvent    *event)
-{
-  if (hdy_swipe_tracker_captured_event (self->tracker, event))
-    return GDK_EVENT_STOP;
-
-  return handle_discrete_scroll_event (self, event);
-}
-
 static void
 hdy_carousel_destroy (GtkWidget *widget)
 {
@@ -798,11 +788,7 @@ hdy_carousel_dispose (GObject *object)
 {
   HdyCarousel *self = (HdyCarousel *)object;
 
-  if (self->tracker) {
-    g_clear_object (&self->tracker);
-
-    g_object_set_data (object, "captured-event-handler", NULL);
-  }
+  g_clear_object (&self->tracker);
 
   if (self->scroll_timeout_id != 0) {
     g_source_remove (self->scroll_timeout_id);
@@ -1151,6 +1137,7 @@ hdy_carousel_class_init (HdyCarouselClass *klass)
   gtk_widget_class_bind_template_child (widget_class, HdyCarousel, empty_box);
   gtk_widget_class_bind_template_child (widget_class, HdyCarousel, scrolling_box);
   gtk_widget_class_bind_template_child (widget_class, HdyCarousel, indicators);
+  gtk_widget_class_bind_template_callback (widget_class, scroll_event_cb);
   gtk_widget_class_bind_template_callback (widget_class, draw_indicators_cb);
   gtk_widget_class_bind_template_callback (widget_class, notify_n_pages_cb);
   gtk_widget_class_bind_template_callback (widget_class, notify_position_cb);
@@ -1175,14 +1162,6 @@ hdy_carousel_init (HdyCarousel *self)
   self->tracker = hdy_swipe_tracker_new (HDY_SWIPEABLE (self));
   hdy_swipe_tracker_set_allow_mouse_drag (self->tracker, TRUE);
   self->can_scroll = TRUE;
-
-  /*
-   * HACK: GTK3 has no other way to get events on capture phase.
-   * This is a reimplementation of _gtk_widget_set_captured_event_handler(),
-   * which is private. In GTK4 it can be replaced with GtkEventControllerLegacy
-   * with capture propagation phase
-   */
-  g_object_set_data (G_OBJECT (self), "captured-event-handler", captured_event_cb);
 }
 
 /**
diff --git a/src/hdy-carousel.ui b/src/hdy-carousel.ui
index d17f5d55..e0b177a4 100644
--- a/src/hdy-carousel.ui
+++ b/src/hdy-carousel.ui
@@ -4,6 +4,7 @@
   <template class="HdyCarousel" parent="GtkEventBox">
     <property name="visible">True</property>
     <property name="orientation">horizontal</property>
+    <signal name="scroll-event" handler="scroll_event_cb"/>
     <child>
       <object class="GtkBox" id="box">
         <property name="visible">True</property>
diff --git a/src/hdy-deck.c b/src/hdy-deck.c
index 8fcdd48f..024bb226 100644
--- a/src/hdy-deck.c
+++ b/src/hdy-deck.c
@@ -705,8 +705,6 @@ hdy_deck_finalize (GObject *object)
 
   g_clear_object (&priv->box);
 
-  g_object_set_data (object, "captured-event-handler", NULL);
-
   G_OBJECT_CLASS (hdy_deck_parent_class)->finalize (object);
 }
 
@@ -828,13 +826,6 @@ hdy_deck_get_cancel_progress (HdySwipeable *swipeable)
   return hdy_stackable_box_get_cancel_progress (HDY_GET_HELPER (swipeable));
 }
 
-static gboolean
-captured_event_cb (HdyDeck  *self,
-                   GdkEvent *event)
-{
-  return hdy_stackable_box_captured_event (HDY_GET_HELPER (self), event);
-}
-
 static void
 hdy_deck_class_init (HdyDeckClass *klass)
 {
@@ -1080,14 +1071,6 @@ hdy_deck_init (HdyDeck *self)
   g_signal_connect_object (priv->box, "notify::can-swipe-back", G_CALLBACK (notify_can_swipe_back_cb), self, 
G_CONNECT_SWAPPED);
   g_signal_connect_object (priv->box, "notify::can-swipe-forward", G_CALLBACK (notify_can_swipe_forward_cb), 
self, G_CONNECT_SWAPPED);
   g_signal_connect_object (priv->box, "notify::orientation", G_CALLBACK (notify_orientation_cb), self, 
G_CONNECT_SWAPPED);
-
-  /*
-   * HACK: GTK3 has no other way to get events on capture phase.
-   * This is a reimplementation of _gtk_widget_set_captured_event_handler(),
-   * which is private. In GTK4 it can be replaced with GtkEventControllerLegacy
-   * with capture propagation phase
-   */
-  g_object_set_data (G_OBJECT (self), "captured-event-handler", captured_event_cb);
 }
 
 static void
diff --git a/src/hdy-leaflet.c b/src/hdy-leaflet.c
index 0addd255..8d658f90 100644
--- a/src/hdy-leaflet.c
+++ b/src/hdy-leaflet.c
@@ -769,8 +769,6 @@ hdy_leaflet_finalize (GObject *object)
 
   g_clear_object (&priv->box);
 
-  g_object_set_data (object, "captured-event-handler", NULL);
-
   G_OBJECT_CLASS (hdy_leaflet_parent_class)->finalize (object);
 }
 
@@ -901,13 +899,6 @@ hdy_leaflet_get_cancel_progress (HdySwipeable *swipeable)
   return hdy_stackable_box_get_cancel_progress (HDY_GET_HELPER (swipeable));
 }
 
-static gboolean
-captured_event_cb (HdyLeaflet *self,
-                   GdkEvent   *event)
-{
-  return hdy_stackable_box_captured_event (HDY_GET_HELPER (self), event);
-}
-
 static void
 hdy_leaflet_class_init (HdyLeafletClass *klass)
 {
@@ -1185,14 +1176,6 @@ hdy_leaflet_init (HdyLeaflet *self)
   g_signal_connect_object (priv->box, "notify::can-swipe-back", G_CALLBACK (notify_can_swipe_back_cb), self, 
G_CONNECT_SWAPPED);
   g_signal_connect_object (priv->box, "notify::can-swipe-forward", G_CALLBACK (notify_can_swipe_forward_cb), 
self, G_CONNECT_SWAPPED);
   g_signal_connect_object (priv->box, "notify::orientation", G_CALLBACK (notify_orientation_cb), self, 
G_CONNECT_SWAPPED);
-
-  /*
-   * HACK: GTK3 has no other way to get events on capture phase.
-   * This is a reimplementation of _gtk_widget_set_captured_event_handler(),
-   * which is private. In GTK4 it can be replaced with GtkEventControllerLegacy
-   * with capture propagation phase
-   */
-  g_object_set_data (G_OBJECT (self), "captured-event-handler", captured_event_cb);
 }
 
 static void
diff --git a/src/hdy-stackable-box-private.h b/src/hdy-stackable-box-private.h
index 605e80b4..2c9674d3 100644
--- a/src/hdy-stackable-box-private.h
+++ b/src/hdy-stackable-box-private.h
@@ -85,8 +85,6 @@ void             hdy_stackable_box_set_child_allow_visible (HdyStackableBox *sel
                                                             GtkWidget       *widget,
                                                             gboolean         allow_visible);
 
-gboolean         hdy_stackable_box_captured_event (HdyStackableBox *self,
-                                                   GdkEvent        *event);
 void             hdy_stackable_box_switch_child (HdyStackableBox *self,
                                                  guint            index,
                                                  gint64           duration);
diff --git a/src/hdy-stackable-box.c b/src/hdy-stackable-box.c
index 33a7a846..579c36c7 100644
--- a/src/hdy-stackable-box.c
+++ b/src/hdy-stackable-box.c
@@ -3159,13 +3159,6 @@ hdy_stackable_box_get_cancel_progress (HdyStackableBox *self)
   return 0;
 }
 
-gboolean
-hdy_stackable_box_captured_event (HdyStackableBox *self,
-                                  GdkEvent        *event)
-{
-  return hdy_swipe_tracker_captured_event (self->tracker, event);
-}
-
 void
 hdy_stackable_box_switch_child (HdyStackableBox *self,
                                 guint            index,
diff --git a/src/hdy-swipe-tracker-private.h b/src/hdy-swipe-tracker-private.h
index 4e027d85..2f4fa541 100644
--- a/src/hdy-swipe-tracker-private.h
+++ b/src/hdy-swipe-tracker-private.h
@@ -33,9 +33,6 @@ gboolean         hdy_swipe_tracker_get_allow_mouse_drag (HdySwipeTracker *self);
 void             hdy_swipe_tracker_set_allow_mouse_drag (HdySwipeTracker *self,
                                                          gboolean         allow_mouse_drag);
 
-gboolean         hdy_swipe_tracker_captured_event (HdySwipeTracker *self,
-                                                   GdkEvent        *event);
-
 void             hdy_swipe_tracker_shift_position (HdySwipeTracker *self,
                                                    gdouble          delta);
 
diff --git a/src/hdy-swipe-tracker.c b/src/hdy-swipe-tracker.c
index 33f37683..9c880da6 100644
--- a/src/hdy-swipe-tracker.c
+++ b/src/hdy-swipe-tracker.c
@@ -535,6 +535,23 @@ handle_event_cb (HdySwipeTracker *self,
   return retval;
 }
 
+static gboolean
+captured_event_cb (HdySwipeable *swipeable,
+                   GdkEvent     *event)
+{
+  HdySwipeTracker *self = g_object_get_data (G_OBJECT (swipeable), "swipe-tracker");
+
+  g_assert (HDY_IS_SWIPE_TRACKER (self));
+
+  if (!self->enabled && self->state != HDY_SWIPE_TRACKER_STATE_SCROLLING)
+    return GDK_EVENT_PROPAGATE;
+
+  if (event->type != GDK_SCROLL)
+    return GDK_EVENT_PROPAGATE;
+
+  return handle_scroll_event (self, event, TRUE);
+}
+
 static void
 hdy_swipe_tracker_constructed (GObject *object)
 {
@@ -564,6 +581,14 @@ hdy_swipe_tracker_constructed (GObject *object)
 
   g_object_set_data (G_OBJECT (self->swipeable), "swipe-tracker", self);
 
+  /*
+   * HACK: GTK3 has no other way to get events on capture phase.
+   * This is a reimplementation of _gtk_widget_set_captured_event_handler(),
+   * which is private. In GTK4 it can be replaced with GtkEventControllerLegacy
+   * with capture propagation phase
+   */
+  g_object_set_data (G_OBJECT (self->swipeable), "captured-event-handler", captured_event_cb);
+
   G_OBJECT_CLASS (hdy_swipe_tracker_parent_class)->constructed (object);
 }
 
@@ -578,7 +603,8 @@ hdy_swipe_tracker_dispose (GObject *object)
   if (self->touch_gesture)
     g_signal_handlers_disconnect_by_data (self->touch_gesture, self);
 
-  g_object_steal_data (G_OBJECT (self->swipeable), "swipe-tracker");
+  g_object_set_data (G_OBJECT (self->swipeable), "swipe-tracker", NULL);
+  g_object_set_data (G_OBJECT (self->swipeable), "captured-event-handler", NULL);
 
   g_clear_object (&self->touch_gesture);
   g_clear_object (&self->swipeable);
@@ -908,33 +934,6 @@ hdy_swipe_tracker_set_allow_mouse_drag (HdySwipeTracker *self,
   g_object_notify_by_pspec (G_OBJECT (self), props[PROP_ALLOW_MOUSE_DRAG]);
 }
 
-/**
- * hdy_swipe_tracker_captured_event:
- * @self: a #HdySwipeTracker
- * @event: a captured #GdkEvent
- *
- * Handles an event. This must be called for events received at capture phase
- * only.
- *
- * Returns: %TRUE is the event was handled and must not be propagated
- *
- * Since: 0.0.11
- */
-gboolean
-hdy_swipe_tracker_captured_event (HdySwipeTracker *self,
-                                  GdkEvent        *event)
-{
-  g_return_val_if_fail (HDY_IS_SWIPE_TRACKER (self), GDK_EVENT_PROPAGATE);
-
-  if (!self->enabled && self->state != HDY_SWIPE_TRACKER_STATE_SCROLLING)
-    return GDK_EVENT_PROPAGATE;
-
-  if (event->type != GDK_SCROLL)
-    return GDK_EVENT_PROPAGATE;
-
-  return handle_scroll_event (self, event, TRUE);
-}
-
 void
 hdy_swipe_tracker_shift_position (HdySwipeTracker *self,
                                   gdouble          delta)


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