[gnome-clocks: 1/3] timer: Decouple timer logic from the widget tick



commit 14005c78289239b7fd78ce8c9694ccd92a15f0cd
Author: Yanko Kaneti <yaneti declera com>
Date:   Sat Oct 27 02:03:50 2018 +0300

    timer: Decouple timer logic from the widget tick
    
    Moving the timer logic in a separate processing timeout
    outside the widget frame update tick.
    
     - fixes timer firing when the window is not being repainted
       https://bugzilla.redhat.com/show_bug.cgi?id=1525025
    
     - the countdown frame is updated at most 25 times a second
       which should slightly reduce CPU usage

 src/timer.vala | 48 ++++++++++++++----------------------------------
 1 file changed, 14 insertions(+), 34 deletions(-)
---
diff --git a/src/timer.vala b/src/timer.vala
index 7121b54..73ea374 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -78,7 +78,6 @@ 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 Utils.Bell bell;
@@ -119,7 +118,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         settings = new GLib.Settings ("org.gnome.clocks");
 
-        tick_id = 0;
         span = 0;
         timer = new GLib.Timer ();
 
@@ -227,7 +225,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 +239,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 +254,29 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         state = State.RUNNING;
         timer.start ();
-        start_ticking ();
+        GLib.Timeout.add(40, () => {
+           if (state != State.RUNNING) {
+                return false;
+            }
+            var e = timer.elapsed ();
+            if (e >= span) {
+                reset ();
+                ring ();
+                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]