[recipes] Fix up timer state handling



commit 34bb2120b2bfb344cecb33e36a411835f25cfb33
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Feb 22 20:28:50 2017 -0500

    Fix up timer state handling
    
    Make it so that that !active and remaining == 0 indicates
    that the timer is expired.

 src/gr-cooking-view.c |   11 ++++++++-
 src/gr-time-widget.c  |   51 +++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 48 insertions(+), 14 deletions(-)
---
diff --git a/src/gr-cooking-view.c b/src/gr-cooking-view.c
index 375c8f6..64f07e5 100644
--- a/src/gr-cooking-view.c
+++ b/src/gr-cooking-view.c
@@ -195,12 +195,19 @@ setup_step (GrCookingView *view)
 
         if (s->timer) {
                 gboolean active;
+                guint64 remaining;
 
                 gtk_widget_show (view->cooking_stack);
                 gtk_widget_set_halign (view->text_box, GTK_ALIGN_START);
-                g_object_get (s->timer, "active", &active, NULL);
+
+                active = gr_timer_get_active (s->timer);
+                remaining = gr_timer_get_remaining (s->timer);
                 g_object_set (view->cooking_timer, "timer", s->timer, NULL);
-                gtk_stack_set_visible_child_name (GTK_STACK (view->cooking_stack), "timer");
+
+                if (active || remaining > 0)
+                        gtk_stack_set_visible_child_name (GTK_STACK (view->cooking_stack), "timer");
+                else
+                        gtk_stack_set_visible_child_name (GTK_STACK (view->cooking_stack), "complete");
         }
         else if (0 <= s->image && s->image < view->images->len) {
                 GrImage *ri = NULL;
diff --git a/src/gr-time-widget.c b/src/gr-time-widget.c
index 0c9aaba..6f667e9 100644
--- a/src/gr-time-widget.c
+++ b/src/gr-time-widget.c
@@ -33,7 +33,8 @@ struct _GrTimeWidget
 
         int size;
         GrTimer *timer;
-        gulong handler;
+        gulong remaining_handler;
+        gulong active_handler;
 
         GrTimerWidget *timer_widget;
         GtkWidget *time_remaining;
@@ -57,6 +58,28 @@ gr_time_widget_new (void)
 }
 
 static void
+update_buttons (GrTimeWidget *self)
+{
+        gboolean active;
+        guint64 duration;
+        guint64 remaining;
+
+        active = gr_timer_get_active (self->timer);
+        duration = gr_timer_get_duration (self->timer);
+        remaining = gr_timer_get_remaining (self->timer);
+
+        if (duration == remaining)
+                gtk_stack_set_visible_child_name (GTK_STACK (self->timer_button_stack), "start");
+        else {
+                gtk_stack_set_visible_child_name (GTK_STACK (self->timer_button_stack), "active");
+                if (active)
+                        gtk_stack_set_visible_child_name (GTK_STACK (self->pause_stack), "pause");
+                else
+                        gtk_stack_set_visible_child_name (GTK_STACK (self->pause_stack), "resume");
+        }
+}
+
+static void
 remaining_changed (GrTimeWidget *self)
 {
         guint64 remaining;
@@ -75,6 +98,8 @@ remaining_changed (GrTimeWidget *self)
         str = g_strdup_printf ("%02d∶%02d∶%02d", hours, minutes, seconds);
         gtk_label_set_label (GTK_LABEL (self->time_remaining), str);
         gtk_widget_queue_draw (GTK_WIDGET (self));
+
+        update_buttons (self);
 }
 
 static void
@@ -84,21 +109,21 @@ set_timer (GrTimeWidget *self,
         GrTimer *old = self->timer;
 
         if (g_set_object (&self->timer, timer)) {
-                gboolean active;
-
-                if (self->handler) {
-                        g_signal_handler_disconnect (old, self->handler);
-                        self->handler = 0;
+                if (self->remaining_handler) {
+                        g_signal_handler_disconnect (old, self->remaining_handler);
+                        self->remaining_handler = 0;
+                }
+                if (self->active_handler) {
+                        g_signal_handler_disconnect (old, self->active_handler);
+                        self->active_handler = 0;
                 }
                 if (timer) {
-                        self->handler = g_signal_connect_swapped (timer, "notify::remaining", G_CALLBACK 
(remaining_changed), self);
+                        self->remaining_handler = g_signal_connect_swapped (timer, "notify::remaining", 
G_CALLBACK (remaining_changed), self);
+                        self->active_handler = g_signal_connect_swapped (timer, "notify::active", G_CALLBACK 
(update_buttons), self);
                         remaining_changed (self);
                 }
                 g_object_set (self->timer_widget, "timer", timer, NULL);
                 g_object_notify (G_OBJECT (self), "timer");
-
-                g_object_get (timer, "active", &active, NULL);
-                gtk_stack_set_visible_child_name (GTK_STACK (self->timer_button_stack), active ? "active" : 
"start");
         }
 }
 
@@ -145,8 +170,10 @@ gr_time_widget_finalize (GObject *object)
 {
         GrTimeWidget *self = GR_TIME_WIDGET (object);
 
-        if (self->handler)
-                g_signal_handler_disconnect (self->timer, self->handler);
+        if (self->remaining_handler)
+                g_signal_handler_disconnect (self->timer, self->remaining_handler);
+        if (self->active_handler)
+                g_signal_handler_disconnect (self->timer, self->active_handler);
         g_clear_object (&self->timer);
 
         G_OBJECT_CLASS (gr_time_widget_parent_class)->finalize (object);


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