[gnome-clocks/cherry-pick-354d1494] Merge branch 'timer-tick' into 'master'



commit 460ea779017037bc4cf25b289eca0c04adb6cf34
Author: Paolo Borelli <paolo borelli gmail com>
Date:   Sun Oct 28 20:21:35 2018 +0000

    Merge branch 'timer-tick' into 'master'
    
    timer: Decouple timer logic from the widget tick
    
    See merge request GNOME/gnome-clocks!8
    
    (cherry picked from commit 354d14943f10a2cede2c39d67ce9783c32aafd7d)
    
    14005c78 timer: Decouple timer logic from the widget tick
    00ea80dc timer: remove timeout on widget disposal

 src/timer.vala | 59 +++++++++++++++++++++++++---------------------------------
 1 file changed, 25 insertions(+), 34 deletions(-)
---
diff --git a/src/timer.vala b/src/timer.vala
index 7121b54..5846479 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -78,9 +78,9 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public State state { get; private set; default = State.STOPPED; }
 
     private GLib.Settings settings;
-    private uint tick_id;
     private double span;
     private GLib.Timer timer;
+    private uint timeout_id;
     private Utils.Bell bell;
     private GLib.Notification notification;
     [GtkChild]
@@ -119,10 +119,17 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         settings = new GLib.Settings ("org.gnome.clocks");
 
-        tick_id = 0;
         span = 0;
         timer = new GLib.Timer ();
 
+        timeout_id = 0;
+        destroy.connect(() => {
+            if (timeout_id != 0) {
+                GLib.Source.remove(timeout_id);
+                timeout_id = 0;
+            }
+        });
+
         bell = new Utils.Bell ("complete");
         notification = new GLib.Notification (_("Time is up!"));
         notification.set_body (_("Timer countdown finished"));
@@ -227,7 +234,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
     private void reset () {
         state = State.STOPPED;
         timer.reset ();
-        stop_ticking ();
         span = settings.get_uint ("timer");
         h_spinbutton.value = (int) span / 3600;
         m_spinbutton.value = (int) span % 3600 / 60;
@@ -242,7 +248,7 @@ public class Face : Gtk.Stack, Clocks.Clock {
     private void start () {
         countdown_frame.get_style_context ().remove_class ("clocks-paused");
 
-        if (state == State.STOPPED && tick_id == 0) {
+        if (state == State.STOPPED) {
             var h = h_spinbutton.get_value_as_int ();
             var m = m_spinbutton.get_value_as_int ();
             var s = s_spinbutton.get_value_as_int ();
@@ -257,46 +263,31 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         state = State.RUNNING;
         timer.start ();
-        start_ticking ();
+        timeout_id = GLib.Timeout.add(40, () => {
+           if (state != State.RUNNING) {
+                timeout_id = 0;
+                return false;
+            }
+            var e = timer.elapsed ();
+            if (e >= span) {
+                reset ();
+                ring ();
+                timeout_id = 0;
+                return false;
+            }
+            update_countdown (e);
+            return true;
+        });
     }
 
     private void pause () {
         state = State.PAUSED;
         timer.stop ();
-        stop_ticking ();
         span -= timer.elapsed ();
         countdown_frame.get_style_context ().add_class ("clocks-paused");
         countdown_frame.pause ();
     }
 
-    private void start_ticking () {
-        if (tick_id == 0) {
-            tick_id = add_tick_callback ((c) => {
-                return count ();
-            });
-        }
-    }
-
-    private void stop_ticking () {
-        if (tick_id != 0) {
-            remove_tick_callback (tick_id);
-            tick_id = 0;
-        }
-    }
-
-    private bool count () {
-        var e = timer.elapsed ();
-        if (e >= span) {
-            update_countdown_label (0, 0, 0);
-            ring ();
-            reset ();
-            return false;
-        }
-
-        update_countdown (e);
-        return true;
-    }
-
     private void update_countdown (double elapsed) {
         if (h_label.get_mapped ()) {
             // Math.ceil() because we count backwards:


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