[libadwaita] carousel-indicator: Correctly measure animations



commit ee898477dd47969acef258a1bd16d7c99bc8f14c
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Wed Feb 23 08:54:13 2022 +0100

    carousel-indicator: Correctly measure animations
    
    This takes the size of the animations into account when measuring the
    indicator.

 src/adw-carousel-indicator-dots.c  | 24 +++++++++++++++++++++---
 src/adw-carousel-indicator-lines.c | 24 +++++++++++++++++++++---
 2 files changed, 42 insertions(+), 6 deletions(-)
---
diff --git a/src/adw-carousel-indicator-dots.c b/src/adw-carousel-indicator-dots.c
index 4b1f2389..15436070 100644
--- a/src/adw-carousel-indicator-dots.c
+++ b/src/adw-carousel-indicator-dots.c
@@ -191,11 +191,29 @@ adw_carousel_indicator_dots_measure (GtkWidget      *widget,
   int size = 0;
 
   if (orientation == self->orientation) {
-    int n_pages = 0;
+    int i, n_points = 0;
+    double indicator_length, dot_size;
+    double *points, *sizes;
+
     if (self->carousel)
-      n_pages = adw_carousel_get_n_pages (self->carousel);
+      points = adw_swipeable_get_snap_points (ADW_SWIPEABLE (self->carousel), &n_points);
+
+    sizes = g_new0 (double, n_points);
+
+    if (n_points > 0)
+      sizes[0] = points[0] + 1;
+    for (i = 1; i < n_points; i++)
+      sizes[i] = points[i] - points[i - 1];
 
-    size = MAX (0, (2 * DOTS_RADIUS_SELECTED + DOTS_SPACING) * n_pages - DOTS_SPACING);
+    dot_size = 2 * DOTS_RADIUS_SELECTED + DOTS_SPACING;
+    indicator_length = 0;
+    for (i = 0; i < n_points; i++)
+      indicator_length += dot_size * sizes[i];
+
+    size = ceil (indicator_length);
+
+    g_free (points);
+    g_free (sizes);
   } else {
     size = 2 * DOTS_RADIUS_SELECTED;
   }
diff --git a/src/adw-carousel-indicator-lines.c b/src/adw-carousel-indicator-lines.c
index df78e9cd..c6574652 100644
--- a/src/adw-carousel-indicator-lines.c
+++ b/src/adw-carousel-indicator-lines.c
@@ -176,11 +176,29 @@ adw_carousel_indicator_lines_measure (GtkWidget      *widget,
   int size = 0;
 
   if (orientation == self->orientation) {
-    int n_pages = 0;
+    int i, n_points = 0;
+    double indicator_length, line_size;
+    double *points = NULL, *sizes;
+
     if (self->carousel)
-      n_pages = adw_carousel_get_n_pages (self->carousel);
+      points = adw_swipeable_get_snap_points (ADW_SWIPEABLE (self->carousel), &n_points);
+
+    sizes = g_new0 (double, n_points);
+
+    if (n_points > 0)
+      sizes[0] = points[0] + 1;
+    for (i = 1; i < n_points; i++)
+      sizes[i] = points[i] - points[i - 1];
 
-    size = MAX (0, (LINE_LENGTH + LINE_SPACING) * n_pages - LINE_SPACING);
+    line_size = LINE_LENGTH + LINE_SPACING;
+    indicator_length = 0;
+    for (i = 0; i < n_points; i++)
+      indicator_length += line_size * sizes[i];
+
+    size = ceil (indicator_length);
+
+    g_free (points);
+    g_free (sizes);
   } else {
     size = LINE_WIDTH;
   }


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