[libhandy/wip/haecker-felix/flap-widget] Port to HdyAnimation



commit d9bf8c1325b877e9893f55444c641b6f28495682
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Tue Nov 17 15:31:56 2020 +0500

    Port to HdyAnimation

 src/hdy-flap.c | 175 +++++++++++++++++----------------------------------------
 1 file changed, 53 insertions(+), 122 deletions(-)
---
diff --git a/src/hdy-flap.c b/src/hdy-flap.c
index 04141169..2caf5f3f 100644
--- a/src/hdy-flap.c
+++ b/src/hdy-flap.c
@@ -34,17 +34,12 @@ struct _HdyFlap
   gboolean folded;
 
   gint overlay_duration;
-  gint reveal_duration;
-
-  gint64 overlay_start_time;
-  guint overlay_tick_cb_id;
   gdouble overlay_progress;
-  gint64 reveal_start_time;
-  guint reveal_tick_cb_id;
+  HdyAnimation *overlay_animation;
+
+  gint reveal_duration;
   gdouble reveal_progress;
-  gdouble reveal_start_progress;
-  gdouble reveal_end_progress;
-  gint64 reveal_current_duration;
+  HdyAnimation *reveal_animation;
 
   GtkOrientation orientation;
 
@@ -197,84 +192,53 @@ hdy_flap_finalize (GObject *object)
   G_OBJECT_CLASS (hdy_flap_parent_class)->finalize (object);
 }
 
-static gboolean
-reveal_tick_cb (HdyFlap       *self,
-                GdkFrameClock *frame_clock)
+static void
+overlay_animation_value_cb (gdouble  value,
+                            HdyFlap *self)
 {
-  guint64 frame_time = gdk_frame_clock_get_frame_time (frame_clock) / 1000;
-  gdouble t = (gdouble) (frame_time - self->reveal_start_time) / self->reveal_current_duration;
-
-  if (t >= 1) {
-    self->reveal_progress = self->reveal_end_progress;
-    self->reveal_tick_cb_id = 0;
-
-    gtk_widget_queue_resize (GTK_WIDGET (self));
-
-    return G_SOURCE_REMOVE;
-  }
-
-  self->reveal_progress = hdy_lerp (self->reveal_start_progress,
-                                    self->reveal_end_progress,
-                                    hdy_ease_out_cubic (t));
+  self->overlay_progress = value;
 
   gtk_widget_queue_resize (GTK_WIDGET (self));
-
-  return G_SOURCE_CONTINUE;
 }
 
-static gboolean
-overlay_tick_cb (HdyFlap       *self,
-                 GdkFrameClock *frame_clock)
+static void
+overlay_animation_done_cb (HdyFlap *self)
 {
-  gint64 frame_time = gdk_frame_clock_get_frame_time (frame_clock) / 1000;
-  gdouble t = (gdouble) (frame_time - self->overlay_start_time) / self->overlay_duration;
-
-  if (t >= 1) {
-    self->overlay_progress = self->folded ? 1 : 0;
-    self->overlay_tick_cb_id = 0;
-
-    gtk_widget_queue_resize (GTK_WIDGET (self));
-
-    return G_SOURCE_REMOVE;
-  }
-
-  self->overlay_progress = hdy_ease_out_cubic (t);
-
-  if (!self->folded)
-      self->overlay_progress = 1 - self->overlay_progress;
-
-  gtk_widget_queue_resize (GTK_WIDGET (self));
-
-  return G_SOURCE_CONTINUE;
+  g_clear_pointer (&self->overlay_animation, hdy_animation_unref);
 }
 
 static void
 animate_overlay (HdyFlap *self)
 {
-  GtkWidget *widget = GTK_WIDGET (self);
-  GdkFrameClock *frame_clock;
-
-  if (!hdy_get_enable_animations (widget) ||
-      !gtk_widget_get_mapped (widget)) {
-    self->overlay_progress = self->folded ? 1 : 0;
-    gtk_widget_queue_resize (widget);
-
-    return;
-  }
+  if (self->overlay_animation)
+    hdy_animation_stop (self->overlay_animation);
+
+  self->overlay_animation =
+    hdy_animation_new (GTK_WIDGET (self),
+                       self->overlay_progress,
+                       self->folded ? 1 : 0,
+                       self->overlay_duration,
+                       hdy_ease_out_cubic,
+                       (HdyAnimationValueCallback) overlay_animation_value_cb,
+                       (HdyAnimationDoneCallback) overlay_animation_done_cb,
+                       self);
+
+  hdy_animation_start (self->overlay_animation);
+}
 
-  if (self->overlay_tick_cb_id != 0) {
-    gtk_widget_remove_tick_callback (widget, self->overlay_tick_cb_id);
-    self->overlay_tick_cb_id = 0;
-  }
+static void
+reveal_animation_value_cb (gdouble  value,
+                           HdyFlap *self)
+{
+  self->reveal_progress = value;
 
-  frame_clock = gtk_widget_get_frame_clock (widget);
+  gtk_widget_queue_resize (GTK_WIDGET (self));
+}
 
-  self->overlay_start_time = gdk_frame_clock_get_frame_time (frame_clock) / 1000;
-  self->overlay_progress = self->folded ? 0 : 1;
-  self->overlay_tick_cb_id =
-    gtk_widget_add_tick_callback (widget,
-                                  (GtkTickCallback) overlay_tick_cb,
-                                  NULL, NULL);
+static void
+reveal_animation_done_cb (HdyFlap *self)
+{
+  g_clear_pointer (&self->reveal_animation, hdy_animation_unref);
 }
 
 static void
@@ -282,32 +246,20 @@ animate_reveal (HdyFlap *self,
                 gdouble  to,
                 gint64   duration)
 {
-  GtkWidget *widget = GTK_WIDGET (self);
-  GdkFrameClock *frame_clock;
-
-  if (!hdy_get_enable_animations (widget) ||
-      !gtk_widget_get_mapped (widget)) {
-    self->reveal_progress = to;
-    gtk_widget_queue_resize (widget);
-
-    return;
-  }
-
-  if (self->reveal_tick_cb_id != 0) {
-    gtk_widget_remove_tick_callback (widget, self->reveal_tick_cb_id);
-    self->reveal_tick_cb_id = 0;
-  }
-
-  frame_clock = gtk_widget_get_frame_clock (widget);
-
-  self->reveal_start_time = gdk_frame_clock_get_frame_time (frame_clock) / 1000;
-  self->reveal_start_progress = self->reveal_progress;
-  self->reveal_end_progress = to;
-  self->reveal_current_duration = duration;
-  self->reveal_tick_cb_id =
-    gtk_widget_add_tick_callback (widget,
-                                  (GtkTickCallback) reveal_tick_cb,
-                                  NULL, NULL);
+  if (self->reveal_animation)
+    hdy_animation_stop (self->reveal_animation);
+
+  self->reveal_animation =
+    hdy_animation_new (GTK_WIDGET (self),
+                       self->reveal_progress,
+                       to,
+                       duration,
+                       hdy_ease_out_cubic,
+                       (HdyAnimationValueCallback) reveal_animation_value_cb,
+                       (HdyAnimationDoneCallback) reveal_animation_done_cb,
+                       self);
+
+  hdy_animation_start (self->reveal_animation);
 }
 
 static gint
@@ -335,10 +287,8 @@ begin_swipe_cb (HdySwipeTracker        *tracker,
                 gboolean                direct,
                 HdyFlap                *self)
 {
-  if (self->reveal_tick_cb_id != 0) {
-    gtk_widget_remove_tick_callback (GTK_WIDGET (self), self->reveal_tick_cb_id);
-    self->overlay_tick_cb_id = 0;
-  }
+  if (self->reveal_animation)
+    hdy_animation_stop (self->reveal_animation);
 }
 
 static void
@@ -696,24 +646,6 @@ hdy_flap_direction_changed (GtkWidget        *widget,
                                                                previous_direction);
 }
 
-static void
-hdy_flap_unmap (GtkWidget *widget)
-{
-  HdyFlap *self = HDY_FLAP (widget);
-
-  if (self->overlay_tick_cb_id != 0) {
-    gtk_widget_remove_tick_callback (GTK_WIDGET (self), self->overlay_tick_cb_id);
-    self->overlay_tick_cb_id = 0;
-  }
-
-  if (self->reveal_tick_cb_id != 0) {
-    gtk_widget_remove_tick_callback (GTK_WIDGET (self), self->reveal_tick_cb_id);
-    self->overlay_tick_cb_id = 0;
-  }
-
-  GTK_WIDGET_CLASS (hdy_flap_parent_class)->unmap (widget);
-}
-
 static void
 hdy_flap_forall (GtkContainer *container,
                  gboolean      include_internals,
@@ -833,7 +765,6 @@ hdy_flap_class_init (HdyFlapClass *klass)
   widget_class->size_allocate = hdy_flap_size_allocate;
   widget_class->draw = hdy_flap_draw;
   widget_class->direction_changed = hdy_flap_direction_changed;
-  widget_class->unmap = hdy_flap_unmap;
 
   container_class->remove = hdy_flap_remove;
   container_class->forall = hdy_flap_forall;


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