[libhandy] stackable-box: Implement get_snap_points()



commit 0eb007d6167b84ceef3444a379e67c7741cf470c
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sun Jun 28 19:50:23 2020 +0500

    stackable-box: Implement get_snap_points()
    
    Currently get_range() is mandatory, get_snap_points() is optional. However,
    bindings don't have optional methods, and so essentially both are required,
    which is redundant in many cases. Instead, let's make get_snap_points()
    mandatory and remove get_range(). But first, we need to actually implement
    it everywhere.
    
    Signed-off-by: Alexander Mikhaylenko <alexm gnome org>

 src/hdy-deck.c                  |  8 ++++++
 src/hdy-leaflet.c               |  8 ++++++
 src/hdy-stackable-box-private.h |  2 ++
 src/hdy-stackable-box.c         | 54 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+)
---
diff --git a/src/hdy-deck.c b/src/hdy-deck.c
index 7de3ec1a..f3bfac28 100644
--- a/src/hdy-deck.c
+++ b/src/hdy-deck.c
@@ -783,6 +783,13 @@ hdy_deck_get_range (HdySwipeable *swipeable,
   hdy_stackable_box_get_range (HDY_GET_HELPER (swipeable), lower, upper);
 }
 
+static gdouble *
+hdy_deck_get_snap_points (HdySwipeable *swipeable,
+                          gint         *n_snap_points)
+{
+  return hdy_stackable_box_get_snap_points (HDY_GET_HELPER (swipeable), n_snap_points);
+}
+
 static gdouble
 hdy_deck_get_progress (HdySwipeable *swipeable)
 {
@@ -1048,6 +1055,7 @@ hdy_deck_swipeable_init (HdySwipeableInterface *iface)
   iface->switch_child = hdy_deck_switch_child;
   iface->get_distance = hdy_deck_get_distance;
   iface->get_range = hdy_deck_get_range;
+  iface->get_snap_points = hdy_deck_get_snap_points;
   iface->get_progress = hdy_deck_get_progress;
   iface->get_cancel_progress = hdy_deck_get_cancel_progress;
 }
diff --git a/src/hdy-leaflet.c b/src/hdy-leaflet.c
index eea2fc45..c6e113ef 100644
--- a/src/hdy-leaflet.c
+++ b/src/hdy-leaflet.c
@@ -856,6 +856,13 @@ hdy_leaflet_get_range (HdySwipeable *swipeable,
   hdy_stackable_box_get_range (HDY_GET_HELPER (swipeable), lower, upper);
 }
 
+static gdouble *
+hdy_leaflet_get_snap_points (HdySwipeable *swipeable,
+                             gint         *n_snap_points)
+{
+  return hdy_stackable_box_get_snap_points (HDY_GET_HELPER (swipeable), n_snap_points);
+}
+
 static gdouble
 hdy_leaflet_get_progress (HdySwipeable *swipeable)
 {
@@ -1153,6 +1160,7 @@ hdy_leaflet_swipeable_init (HdySwipeableInterface *iface)
   iface->switch_child = hdy_leaflet_switch_child;
   iface->get_distance = hdy_leaflet_get_distance;
   iface->get_range = hdy_leaflet_get_range;
+  iface->get_snap_points = hdy_leaflet_get_snap_points;
   iface->get_progress = hdy_leaflet_get_progress;
   iface->get_cancel_progress = hdy_leaflet_get_cancel_progress;
 }
diff --git a/src/hdy-stackable-box-private.h b/src/hdy-stackable-box-private.h
index 688630ae..143a29ea 100644
--- a/src/hdy-stackable-box-private.h
+++ b/src/hdy-stackable-box-private.h
@@ -89,6 +89,8 @@ void             hdy_stackable_box_switch_child (HdyStackableBox *self,
                                                  gint64           duration);
 
 gdouble          hdy_stackable_box_get_distance (HdyStackableBox *self);
+gdouble         *hdy_stackable_box_get_snap_points (HdyStackableBox *self,
+                                                    gint            *n_snap_points);
 void             hdy_stackable_box_get_range (HdyStackableBox *self,
                                               gdouble         *lower,
                                               gdouble         *upper);
diff --git a/src/hdy-stackable-box.c b/src/hdy-stackable-box.c
index 211442e8..0832e578 100644
--- a/src/hdy-stackable-box.c
+++ b/src/hdy-stackable-box.c
@@ -2559,6 +2559,60 @@ can_swipe_in_direction (HdyStackableBox        *self,
   }
 }
 
+gdouble *
+hdy_stackable_box_get_snap_points (HdyStackableBox *self,
+                                   gint            *n_snap_points)
+{
+  gint n;
+  gdouble *points, lower, upper;
+
+  if (self->child_transition.tick_id > 0 ||
+      self->child_transition.is_gesture_active) {
+    gint current_direction;
+    gboolean is_rtl = gtk_widget_get_direction (GTK_WIDGET (self->container)) == GTK_TEXT_DIR_RTL;
+
+    switch (self->child_transition.active_direction) {
+    case GTK_PAN_DIRECTION_UP:
+      current_direction = 1;
+      break;
+    case GTK_PAN_DIRECTION_DOWN:
+      current_direction = -1;
+      break;
+    case GTK_PAN_DIRECTION_LEFT:
+      current_direction = is_rtl ? -1 : 1;
+      break;
+    case GTK_PAN_DIRECTION_RIGHT:
+      current_direction = is_rtl ? 1 : -1;
+      break;
+    default:
+      g_assert_not_reached ();
+    }
+
+    lower = MIN (0, current_direction);
+    upper = MAX (0, current_direction);
+  } else {
+    HdyStackableBoxChildInfo *child = NULL;
+
+    if ((can_swipe_in_direction (self, self->child_transition.swipe_direction) ||
+         !self->child_transition.is_direct_swipe) && self->folded)
+      child = find_swipeable_child (self, self->child_transition.swipe_direction);
+
+    lower = MIN (0, child ? self->child_transition.swipe_direction : 0);
+    upper = MAX (0, child ? self->child_transition.swipe_direction : 0);
+  }
+
+  n = (lower != upper) ? 2 : 1;
+
+  points = g_new0 (gdouble, n);
+  points[0] = lower;
+  points[n - 1] = upper;
+
+  if (n_snap_points)
+    *n_snap_points = n;
+
+  return points;
+}
+
 void
 hdy_stackable_box_get_range (HdyStackableBox *self,
                              gdouble         *lower,


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