[libadwaita] carousel-indicator-dots: Use a single AdwAnimation



commit 4aa87e3db46c3c8a468074e5fda42215ed62cde9
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Nov 19 13:40:04 2021 +0500

    carousel-indicator-dots: Use a single AdwAnimation

 src/adw-carousel-indicator-dots.c | 58 ++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 37 deletions(-)
---
diff --git a/src/adw-carousel-indicator-dots.c b/src/adw-carousel-indicator-dots.c
index 6c3aae4e..17873b74 100644
--- a/src/adw-carousel-indicator-dots.c
+++ b/src/adw-carousel-indicator-dots.c
@@ -50,6 +50,7 @@ struct _AdwCarouselIndicatorDots
   GtkOrientation orientation;
 
   AdwAnimation *animation;
+  GBinding *duration_binding;
 };
 
 G_DEFINE_FINAL_TYPE_WITH_CODE (AdwCarouselIndicatorDots, adw_carousel_indicator_dots, GTK_TYPE_WIDGET,
@@ -66,32 +67,6 @@ enum {
 
 static GParamSpec *props[LAST_PROP];
 
-static void
-done_cb (AdwCarouselIndicatorDots *self)
-{
-  g_clear_object (&self->animation);
-}
-
-static void
-animate (AdwCarouselIndicatorDots *self,
-         gint64                    duration)
-{
-  AdwAnimationTarget *target;
-
-  if (self->animation)
-    adw_animation_skip (self->animation);
-
-  target = adw_callback_animation_target_new ((AdwAnimationTargetFunc)
-                                              gtk_widget_queue_draw,
-                                              self, NULL);
-  self->animation =
-    adw_animation_new (GTK_WIDGET (self), 0, 1, duration, target);
-
-  g_signal_connect_swapped (self->animation, "done", G_CALLBACK (done_cb), self);
-
-  adw_animation_play (self->animation);
-}
-
 static GdkRGBA
 get_color (GtkWidget *widget)
 {
@@ -191,12 +166,6 @@ snapshot_dots (GtkWidget      *widget,
   }
 }
 
-static void
-n_pages_changed_cb (AdwCarouselIndicatorDots *self)
-{
-  animate (self, adw_carousel_get_reveal_duration (self->carousel));
-}
-
 static void
 adw_carousel_indicator_dots_measure (GtkWidget      *widget,
                                      GtkOrientation  orientation,
@@ -273,6 +242,8 @@ adw_carousel_dispose (GObject *object)
 
   adw_carousel_indicator_dots_set_carousel (self, NULL);
 
+  g_clear_object (&self->animation);
+
   G_OBJECT_CLASS (adw_carousel_indicator_dots_parent_class)->dispose (object);
 }
 
@@ -366,6 +337,13 @@ adw_carousel_indicator_dots_class_init (AdwCarouselIndicatorDotsClass *klass)
 static void
 adw_carousel_indicator_dots_init (AdwCarouselIndicatorDots *self)
 {
+  AdwAnimationTarget *target
+    = adw_callback_animation_target_new ((AdwAnimationTargetFunc)
+                                         gtk_widget_queue_resize,
+                                         self, NULL);
+
+  self->animation =
+    adw_animation_new (GTK_WIDGET (self), 0, 1, 0, target);
 }
 
 /**
@@ -420,12 +398,14 @@ adw_carousel_indicator_dots_set_carousel (AdwCarouselIndicatorDots *self,
   if (self->carousel == carousel)
     return;
 
-  if (self->animation)
-    adw_animation_skip (self->animation);
+  adw_animation_reset (self->animation);
 
   if (self->carousel) {
-    g_signal_handlers_disconnect_by_func (self->carousel, gtk_widget_queue_draw, self);
-    g_signal_handlers_disconnect_by_func (self->carousel, n_pages_changed_cb, self);
+    g_signal_handlers_disconnect_by_func (self->carousel,
+                                          gtk_widget_queue_draw, self);
+    g_signal_handlers_disconnect_by_func (self->carousel,
+                                          adw_animation_play, self->animation);
+    g_clear_object (&self->duration_binding);
   }
 
   g_set_object (&self->carousel, carousel);
@@ -435,8 +415,12 @@ adw_carousel_indicator_dots_set_carousel (AdwCarouselIndicatorDots *self,
                              G_CALLBACK (gtk_widget_queue_draw), self,
                              G_CONNECT_SWAPPED);
     g_signal_connect_object (self->carousel, "notify::n-pages",
-                             G_CALLBACK (n_pages_changed_cb), self,
+                             G_CALLBACK (adw_animation_play), self->animation,
                              G_CONNECT_SWAPPED);
+    self->duration_binding =
+      g_object_bind_property (self->carousel, "reveal-duration",
+                              self->animation, "duration",
+                              G_BINDING_SYNC_CREATE);
   }
 
   gtk_widget_queue_draw (GTK_WIDGET (self));


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