[libhandy] animation: Call done() even if stopping before having started



commit 6dd5d783024d1282edc6984727a2addabf00d037
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Wed Mar 24 13:43:47 2021 +0500

    animation: Call done() even if stopping before having started
    
    Make sure it's only called once to avoid reentrancy.

 src/hdy-animation.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)
---
diff --git a/src/hdy-animation.c b/src/hdy-animation.c
index 01065e59..c54aff86 100644
--- a/src/hdy-animation.c
+++ b/src/hdy-animation.c
@@ -39,6 +39,8 @@ struct _HdyAnimation
   HdyAnimationValueCallback value_cb;
   HdyAnimationDoneCallback done_cb;
   gpointer user_data;
+
+  gboolean is_done;
 };
 
 static void
@@ -49,6 +51,16 @@ set_value (HdyAnimation *self,
   self->value_cb (value, self->user_data);
 }
 
+static void
+done (HdyAnimation *self)
+{
+  if (self->is_done)
+    return;
+
+  self->is_done = TRUE;
+  self->done_cb (self->user_data);
+}
+
 static gboolean
 tick_cb (GtkWidget     *widget,
          GdkFrameClock *frame_clock,
@@ -64,7 +76,7 @@ tick_cb (GtkWidget     *widget,
 
     g_signal_handlers_disconnect_by_func (self->widget, hdy_animation_stop, self);
 
-    self->done_cb (self->user_data);
+    done (self);
 
     return G_SOURCE_REMOVE;
   }
@@ -113,6 +125,7 @@ hdy_animation_new (GtkWidget                 *widget,
   self->user_data = user_data;
 
   self->value = from;
+  self->is_done = FALSE;
 
   return self;
 }
@@ -146,7 +159,7 @@ hdy_animation_start (HdyAnimation *self)
       self->duration <= 0) {
     set_value (self, self->value_to);
 
-    self->done_cb (self->user_data);
+    done (self);
 
     return;
   }
@@ -166,15 +179,14 @@ hdy_animation_stop (HdyAnimation *self)
 {
   g_return_if_fail (self != NULL);
 
-  if (!self->tick_cb_id)
-    return;
-
-  gtk_widget_remove_tick_callback (self->widget, self->tick_cb_id);
-  self->tick_cb_id = 0;
+  if (self->tick_cb_id) {
+    gtk_widget_remove_tick_callback (self->widget, self->tick_cb_id);
+    self->tick_cb_id = 0;
 
-  g_signal_handlers_disconnect_by_func (self->widget, hdy_animation_stop, self);
+    g_signal_handlers_disconnect_by_func (self->widget, hdy_animation_stop, self);
+  }
 
-  self->done_cb (self->user_data);
+  done (self);
 }
 
 gdouble


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