[libhandy/wip/exalm/paginator-animate: 65/78] carousel-box: Add swipe state accessors



commit b0627a67796718f0d9594f42d64639243e3e3745
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Thu Dec 26 14:56:29 2019 +0500

    carousel-box: Add swipe state accessors
    
    Move the implementation from HdyCarousel. This will allow to have more
    precise values later without exposing the internals to HdyCarousel.
    
    Signed-off-by: Alexander Mikhaylenko <alexm gnome org>

 src/hdy-carousel-box-private.h |  7 ++++
 src/hdy-carousel-box.c         | 75 ++++++++++++++++++++++++++++++++++++++++++
 src/hdy-carousel.c             | 30 +++--------------
 3 files changed, 86 insertions(+), 26 deletions(-)
---
diff --git a/src/hdy-carousel-box-private.h b/src/hdy-carousel-box-private.h
index f0de619e..7859f471 100644
--- a/src/hdy-carousel-box-private.h
+++ b/src/hdy-carousel-box-private.h
@@ -51,4 +51,11 @@ void            hdy_carousel_box_set_spacing (HdyCarouselBox *self,
 GtkWidget      *hdy_carousel_box_get_nth_child (HdyCarouselBox *self,
                                                 guint           n);
 
+gdouble        *hdy_carousel_box_get_snap_points        (HdyCarouselBox *self,
+                                                         gint           *n_snap_points);
+void            hdy_carousel_box_get_range              (HdyCarouselBox *self,
+                                                         gdouble        *lower,
+                                                         gdouble        *upper);
+gdouble         hdy_carousel_box_get_closest_snap_point (HdyCarouselBox *self);
+
 G_END_DECLS
diff --git a/src/hdy-carousel-box.c b/src/hdy-carousel-box.c
index 7a835b3c..d3d291c1 100644
--- a/src/hdy-carousel-box.c
+++ b/src/hdy-carousel-box.c
@@ -1200,3 +1200,78 @@ hdy_carousel_box_get_nth_child (HdyCarouselBox *self,
   return info->widget;
 }
 
+/**
+ * hdy_carousel_box_get_snap_points:
+ * @self: a #HdyCarouselBox
+ * @n_snap_points: (out)
+ *
+ * Gets the snap points of @self, representing the points between each page,
+ * before the first page and after the last page.
+ *
+ * Returns: (array length=n_snap_points) (transfer full): the snap points of @self
+ *
+ * Since: 1.0
+ */
+gdouble *
+hdy_carousel_box_get_snap_points (HdyCarouselBox *self,
+                                  gint           *n_snap_points)
+{
+  guint i, n_pages;
+  gdouble *points;
+
+  g_return_val_if_fail (HDY_IS_CAROUSEL_BOX (self), NULL);
+
+  n_pages = hdy_carousel_box_get_n_pages (self);
+
+  points = g_new (gdouble, n_pages);
+  for (i = 0; i < n_pages; i++)
+    points[i] = i;
+
+  if (n_snap_points)
+    *n_snap_points = n_pages;
+
+  return points;
+}
+
+/**
+ * hdy_carousel_box_get_range:
+ * @self: a #HdyCarouselBox
+ * @lower: (out) (allow-none): location to store the lowest possible position, or %NULL
+ * @upper: (out) (allow-none): location to store the maximum possible position, or %NULL
+ *
+ * Gets the range of possible positions.
+ *
+ * Since: 1.0
+ */
+void
+hdy_carousel_box_get_range (HdyCarouselBox *self,
+                            gdouble        *lower,
+                            gdouble        *upper)
+{
+  g_return_if_fail (HDY_IS_CAROUSEL_BOX (self));
+
+  if (lower)
+    *lower = 0;
+
+  if (upper)
+    *upper = hdy_carousel_box_get_n_pages (self) - 1;
+}
+
+/**
+ * hdy_carousel_box_get_closest_snap_point:
+ * @self: a #HdyCarouselBox
+ *
+ * Gets the snap point closest to the current position.
+ *
+ * Returns: the closest snap point.
+ *
+ * Since: 1.0
+ */
+gdouble
+hdy_carousel_box_get_closest_snap_point (HdyCarouselBox *self)
+{
+  g_return_val_if_fail (HDY_IS_CAROUSEL_BOX (self), 0);
+
+  return CLAMP (round (self->position), 0,
+                hdy_carousel_box_get_n_pages (self) - 1);
+}
diff --git a/src/hdy-carousel.c b/src/hdy-carousel.c
index 4ea54c3c..48d05910 100644
--- a/src/hdy-carousel.c
+++ b/src/hdy-carousel.c
@@ -172,11 +172,7 @@ hdy_carousel_get_range (HdySwipeable *swipeable,
 {
   HdyCarousel *self = HDY_CAROUSEL (swipeable);
 
-  if (lower)
-    *lower = 0;
-
-  if (upper)
-    *upper = hdy_carousel_get_n_pages (self) - 1;
+  hdy_carousel_box_get_range (self->scrolling_box, lower, upper);
 }
 
 static gdouble *
@@ -184,19 +180,9 @@ hdy_carousel_get_snap_points (HdySwipeable *swipeable,
                               gint         *n_snap_points)
 {
   HdyCarousel *self = HDY_CAROUSEL (swipeable);
-  guint i, n_pages;
-  gdouble *points;
-
-  n_pages = hdy_carousel_get_n_pages (self);
-
-  points = g_new (gdouble, n_pages);
-  for (i = 0; i < n_pages; i++)
-    points[i] = i;
 
-  if (n_snap_points)
-    *n_snap_points = n_pages;
-
-  return points;
+  return hdy_carousel_box_get_snap_points (self->scrolling_box,
+                                           n_snap_points);
 }
 
 static gdouble
@@ -210,17 +196,9 @@ hdy_carousel_get_progress (HdySwipeable *swipeable)
 static gdouble
 hdy_carousel_get_cancel_progress (HdySwipeable *swipeable)
 {
-  gdouble position;
-  guint n_pages;
-
   HdyCarousel *self = HDY_CAROUSEL (swipeable);
 
-  g_object_get (self->scrolling_box,
-                "position", &position,
-                "n-pages", &n_pages,
-                NULL);
-
-  return CLAMP (round (position), 0, n_pages - 1);
+  return hdy_carousel_box_get_closest_snap_point (self->scrolling_box);
 }
 
 static void


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