[libhandy] swipeable: Add get_swipe_tracker()



commit 7346ecc2c76ffbd96d05a9bd37233dab4226a042
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Jun 27 01:26:35 2020 +0500

    swipeable: Add get_swipe_tracker()
    
    Stop using g_object_get_data() for this, and make the API clearer by
    actually requiring HdySwipeable implementation to have a swipe tracker.
    
    Signed-off-by: Alexander Mikhaylenko <alexm gnome org>

 src/hdy-carousel.c              |  9 +++++++++
 src/hdy-deck.c                  |  7 +++++++
 src/hdy-leaflet.c               |  7 +++++++
 src/hdy-stackable-box-private.h |  2 ++
 src/hdy-stackable-box.c         |  7 ++++++-
 src/hdy-swipe-group.c           | 16 +++++-----------
 src/hdy-swipe-tracker.c         |  5 +----
 src/hdy-swipeable.c             | 23 +++++++++++++++++++++++
 src/hdy-swipeable.h             | 33 +++++++++++++++++++--------------
 9 files changed, 79 insertions(+), 30 deletions(-)
---
diff --git a/src/hdy-carousel.c b/src/hdy-carousel.c
index e63e9408..ff793d24 100644
--- a/src/hdy-carousel.c
+++ b/src/hdy-carousel.c
@@ -208,6 +208,14 @@ end_swipe_cb (HdySwipeTracker *tracker,
   hdy_carousel_box_scroll_to (self->scrolling_box, child, duration);
 }
 
+static HdySwipeTracker *
+hdy_carousel_get_swipe_tracker (HdySwipeable *swipeable)
+{
+  HdyCarousel *self = HDY_CAROUSEL (swipeable);
+
+  return self->tracker;
+}
+
 static gdouble
 hdy_carousel_get_distance (HdySwipeable *swipeable)
 {
@@ -905,6 +913,7 @@ static void
 hdy_carousel_swipeable_init (HdySwipeableInterface *iface)
 {
   iface->switch_child = hdy_carousel_switch_child;
+  iface->get_swipe_tracker = hdy_carousel_get_swipe_tracker;
   iface->get_distance = hdy_carousel_get_distance;
   iface->get_snap_points = hdy_carousel_get_snap_points;
   iface->get_progress = hdy_carousel_get_progress;
diff --git a/src/hdy-deck.c b/src/hdy-deck.c
index 8ca77abf..d887af46 100644
--- a/src/hdy-deck.c
+++ b/src/hdy-deck.c
@@ -769,6 +769,12 @@ hdy_deck_switch_child (HdySwipeable *swipeable,
   hdy_stackable_box_switch_child (HDY_GET_HELPER (swipeable), index, duration);
 }
 
+static HdySwipeTracker *
+hdy_deck_get_swipe_tracker (HdySwipeable *swipeable)
+{
+  return hdy_stackable_box_get_swipe_tracker (HDY_GET_HELPER (swipeable));
+}
+
 static gdouble
 hdy_deck_get_distance (HdySwipeable *swipeable)
 {
@@ -1045,6 +1051,7 @@ static void
 hdy_deck_swipeable_init (HdySwipeableInterface *iface)
 {
   iface->switch_child = hdy_deck_switch_child;
+  iface->get_swipe_tracker = hdy_deck_get_swipe_tracker;
   iface->get_distance = hdy_deck_get_distance;
   iface->get_snap_points = hdy_deck_get_snap_points;
   iface->get_progress = hdy_deck_get_progress;
diff --git a/src/hdy-leaflet.c b/src/hdy-leaflet.c
index d60d4266..c91891db 100644
--- a/src/hdy-leaflet.c
+++ b/src/hdy-leaflet.c
@@ -842,6 +842,12 @@ hdy_leaflet_switch_child (HdySwipeable *swipeable,
   hdy_stackable_box_switch_child (HDY_GET_HELPER (swipeable), index, duration);
 }
 
+static HdySwipeTracker *
+hdy_leaflet_get_swipe_tracker (HdySwipeable *swipeable)
+{
+  return hdy_stackable_box_get_swipe_tracker (HDY_GET_HELPER (swipeable));
+}
+
 static gdouble
 hdy_leaflet_get_distance (HdySwipeable *swipeable)
 {
@@ -1150,6 +1156,7 @@ static void
 hdy_leaflet_swipeable_init (HdySwipeableInterface *iface)
 {
   iface->switch_child = hdy_leaflet_switch_child;
+  iface->get_swipe_tracker = hdy_leaflet_get_swipe_tracker;
   iface->get_distance = hdy_leaflet_get_distance;
   iface->get_snap_points = hdy_leaflet_get_snap_points;
   iface->get_progress = hdy_leaflet_get_progress;
diff --git a/src/hdy-stackable-box-private.h b/src/hdy-stackable-box-private.h
index f2497803..9d50298d 100644
--- a/src/hdy-stackable-box-private.h
+++ b/src/hdy-stackable-box-private.h
@@ -12,6 +12,7 @@
 
 #include <gtk/gtk.h>
 #include "hdy-navigation-direction.h"
+#include "hdy-swipe-tracker.h"
 
 G_BEGIN_DECLS
 
@@ -88,6 +89,7 @@ void             hdy_stackable_box_switch_child (HdyStackableBox *self,
                                                  guint            index,
                                                  gint64           duration);
 
+HdySwipeTracker *hdy_stackable_box_get_swipe_tracker (HdyStackableBox *self);
 gdouble          hdy_stackable_box_get_distance (HdyStackableBox *self);
 gdouble         *hdy_stackable_box_get_snap_points (HdyStackableBox *self,
                                                     gint            *n_snap_points);
diff --git a/src/hdy-stackable-box.c b/src/hdy-stackable-box.c
index 9b48dcd4..ac799704 100644
--- a/src/hdy-stackable-box.c
+++ b/src/hdy-stackable-box.c
@@ -14,7 +14,6 @@
 #include "hdy-stackable-box-private.h"
 #include "hdy-shadow-helper-private.h"
 #include "hdy-swipeable.h"
-#include "hdy-swipe-tracker.h"
 
 /**
  * PRIVATE:hdy-stackable-box
@@ -2536,6 +2535,12 @@ hdy_stackable_box_unmap (HdyStackableBox *self)
   GTK_WIDGET_CLASS (self->klass)->unmap (GTK_WIDGET (self->container));
 }
 
+HdySwipeTracker *
+hdy_stackable_box_get_swipe_tracker (HdyStackableBox *self)
+{
+  return self->tracker;
+}
+
 gdouble
 hdy_stackable_box_get_distance (HdyStackableBox *self)
 {
diff --git a/src/hdy-swipe-group.c b/src/hdy-swipe-group.c
index 51d236ba..99d58344 100644
--- a/src/hdy-swipe-group.c
+++ b/src/hdy-swipe-group.c
@@ -103,12 +103,6 @@ hdy_swipe_group_new (void)
   return g_object_new (HDY_TYPE_SWIPE_GROUP, NULL);
 }
 
-static HdySwipeTracker *
-get_swipe_tracker (HdySwipeable *swipeable)
-{
-  return g_object_get_data (G_OBJECT (swipeable), "swipe-tracker");
-}
-
 static void
 child_switched_cb (HdySwipeGroup *self,
                    uint           index,
@@ -155,7 +149,7 @@ begin_swipe_cb (HdySwipeGroup          *self,
 
   for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
     if (swipeables->data != swipeable)
-      hdy_swipe_tracker_emit_begin_swipe (get_swipe_tracker (swipeables->data),
+      hdy_swipe_tracker_emit_begin_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
                                           direction, FALSE);
 
   self->block = FALSE;
@@ -181,7 +175,7 @@ update_swipe_cb (HdySwipeGroup   *self,
 
   for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
     if (swipeables->data != swipeable)
-      hdy_swipe_tracker_emit_update_swipe (get_swipe_tracker (swipeables->data),
+      hdy_swipe_tracker_emit_update_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
                                            progress);
 
   self->block = FALSE;
@@ -208,7 +202,7 @@ end_swipe_cb (HdySwipeGroup   *self,
 
   for (swipeables = self->swipeables; swipeables != NULL; swipeables = swipeables->next)
     if (swipeables->data != swipeable)
-      hdy_swipe_tracker_emit_end_swipe (get_swipe_tracker (swipeables->data),
+      hdy_swipe_tracker_emit_end_swipe (hdy_swipeable_get_swipe_tracker (swipeables->data),
                                         duration, to);
 
   self->current = NULL;
@@ -235,7 +229,7 @@ hdy_swipe_group_add_swipeable (HdySwipeGroup *self,
   g_return_if_fail (HDY_IS_SWIPE_GROUP (self));
   g_return_if_fail (HDY_IS_SWIPEABLE (swipeable));
 
-  tracker = get_swipe_tracker (swipeable);
+  tracker = hdy_swipeable_get_swipe_tracker (swipeable);
 
   g_return_if_fail (HDY_IS_SWIPE_TRACKER (tracker));
 
@@ -271,7 +265,7 @@ hdy_swipe_group_remove_swipeable (HdySwipeGroup *self,
   g_return_if_fail (HDY_IS_SWIPEABLE (swipeable));
   g_return_if_fail (contains (self, swipeable));
 
-  tracker = get_swipe_tracker (swipeable);
+  tracker = hdy_swipeable_get_swipe_tracker (swipeable);
 
   self->swipeables = g_slist_remove (self->swipeables, swipeable);
 
diff --git a/src/hdy-swipe-tracker.c b/src/hdy-swipe-tracker.c
index 15469f3e..f6f6d916 100644
--- a/src/hdy-swipe-tracker.c
+++ b/src/hdy-swipe-tracker.c
@@ -591,7 +591,7 @@ static gboolean
 captured_event_cb (HdySwipeable *swipeable,
                    GdkEvent     *event)
 {
-  HdySwipeTracker *self = g_object_get_data (G_OBJECT (swipeable), "swipe-tracker");
+  HdySwipeTracker *self = hdy_swipeable_get_swipe_tracker (swipeable);
 
   g_assert (HDY_IS_SWIPE_TRACKER (self));
 
@@ -631,8 +631,6 @@ hdy_swipe_tracker_constructed (GObject *object)
 
   g_signal_connect_object (self->swipeable, "event", G_CALLBACK (handle_event_cb), self, G_CONNECT_SWAPPED);
 
-  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(),
@@ -655,7 +653,6 @@ hdy_swipe_tracker_dispose (GObject *object)
   if (self->touch_gesture)
     g_signal_handlers_disconnect_by_data (self->touch_gesture, self);
 
-  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);
diff --git a/src/hdy-swipeable.c b/src/hdy-swipeable.c
index 0bfe2bf8..d2d4b9b4 100644
--- a/src/hdy-swipeable.c
+++ b/src/hdy-swipeable.c
@@ -105,6 +105,29 @@ hdy_swipeable_emit_child_switched (HdySwipeable *self,
   g_signal_emit (self, signals[SIGNAL_CHILD_SWITCHED], 0, index, duration);
 }
 
+/**
+ * hdy_swipeable_get_swipe_tracker:
+ * @self: a #HdySwipeable
+ *
+ * Gets the #HdySwipeTracker used by this swipeable widget.
+ *
+ * Returns: (transfer none): the swipe tracker
+ *
+ * Since: 1.0
+ */
+HdySwipeTracker *
+hdy_swipeable_get_swipe_tracker (HdySwipeable *self)
+{
+  HdySwipeableInterface *iface;
+
+  g_return_val_if_fail (HDY_IS_SWIPEABLE (self), NULL);
+
+  iface = HDY_SWIPEABLE_GET_IFACE (self);
+  g_return_val_if_fail (iface->get_swipe_tracker != NULL, NULL);
+
+  return (* iface->get_swipe_tracker) (self);
+}
+
 /**
  * hdy_swipeable_get_distance:
  * @self: a #HdySwipeable
diff --git a/src/hdy-swipeable.h b/src/hdy-swipeable.h
index 74b1e585..893c37dc 100644
--- a/src/hdy-swipeable.h
+++ b/src/hdy-swipeable.h
@@ -15,6 +15,8 @@
 
 G_BEGIN_DECLS
 
+typedef struct _HdySwipeTracker HdySwipeTracker;
+
 #define HDY_TYPE_SWIPEABLE (hdy_swipeable_get_type ())
 
 G_DECLARE_INTERFACE (HdySwipeable, hdy_swipeable, HDY, SWIPEABLE, GtkWidget)
@@ -23,6 +25,7 @@ G_DECLARE_INTERFACE (HdySwipeable, hdy_swipeable, HDY, SWIPEABLE, GtkWidget)
  * HdySwipeableInterface:
  * @parent: The parent interface.
  * @switch_child: Switches visible child.
+ * @get_swipe_tracker: Gets the swipe tracker.
  * @get_distance: Gets the swipe distance.
  * @get_snap_points: Gets the snap points
  * @get_progress: Gets the current progress.
@@ -41,13 +44,14 @@ struct _HdySwipeableInterface
                         guint         index,
                         gint64        duration);
 
-  gdouble   (*get_distance)        (HdySwipeable *self);
-  gdouble * (*get_snap_points)     (HdySwipeable *self,
-                                    gint         *n_snap_points);
-  gdouble   (*get_progress)        (HdySwipeable *self);
-  gdouble   (*get_cancel_progress) (HdySwipeable *self);
-  void      (*get_swipe_area)      (HdySwipeable *self,
-                                    GdkRectangle *rect);
+  HdySwipeTracker * (*get_swipe_tracker)   (HdySwipeable *self);
+  gdouble           (*get_distance)        (HdySwipeable *self);
+  gdouble *         (*get_snap_points)     (HdySwipeable *self,
+                                            gint         *n_snap_points);
+  gdouble           (*get_progress)        (HdySwipeable *self);
+  gdouble           (*get_cancel_progress) (HdySwipeable *self);
+  void              (*get_swipe_area)      (HdySwipeable *self,
+                                            GdkRectangle *rect);
 };
 
 void hdy_swipeable_switch_child (HdySwipeable *self,
@@ -58,12 +62,13 @@ void hdy_swipeable_emit_child_switched (HdySwipeable *self,
                                         guint         index,
                                         gint64        duration);
 
-gdouble  hdy_swipeable_get_distance        (HdySwipeable *self);
-gdouble *hdy_swipeable_get_snap_points     (HdySwipeable *self,
-                                            gint         *n_snap_points);
-gdouble  hdy_swipeable_get_progress        (HdySwipeable *self);
-gdouble  hdy_swipeable_get_cancel_progress (HdySwipeable *self);
-void     hdy_swipeable_get_swipe_area      (HdySwipeable *self,
-                                            GdkRectangle *rect);
+HdySwipeTracker *hdy_swipeable_get_swipe_tracker   (HdySwipeable *self);
+gdouble          hdy_swipeable_get_distance        (HdySwipeable *self);
+gdouble         *hdy_swipeable_get_snap_points     (HdySwipeable *self,
+                                                    gint         *n_snap_points);
+gdouble          hdy_swipeable_get_progress        (HdySwipeable *self);
+gdouble          hdy_swipeable_get_cancel_progress (HdySwipeable *self);
+void             hdy_swipeable_get_swipe_area      (HdySwipeable *self,
+                                                    GdkRectangle *rect);
 
 G_END_DECLS


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