[gnome-clocks] Pause expensive UI updates when the UI is hidden



commit ba7779242055af921569c06bf3eb122a72e1a1b7
Author: Volker Sobek <reklov live com>
Date:   Sat Nov 10 16:42:47 2012 +0100

    Pause expensive UI updates when the UI is hidden
    
    The timer and the stopwatch use a lot of CPU time because
    they update their UI frequently. To improve this, catch the map/unmap
    signals of their widgets and stop updating them while they are not
    visible (mapped).
    
    Also add methods for adding/removing the timeout to Stopwatch, to
    unify the code of Stopwatch and Timer.

 gnomeclocks/clocks.py    |   26 ++++++++++++++++++++++++++
 gnomeclocks/stopwatch.py |   23 ++++++++++++++++++++---
 gnomeclocks/timer.py     |   12 +++++++++++-
 3 files changed, 57 insertions(+), 4 deletions(-)
---
diff --git a/gnomeclocks/clocks.py b/gnomeclocks/clocks.py
index 1f3884d..5a6aca1 100644
--- a/gnomeclocks/clocks.py
+++ b/gnomeclocks/clocks.py
@@ -31,6 +31,13 @@ class Clock(Gtk.EventBox):
 
     def __init__(self, label, new_label=None, has_selection_mode=False):
         Gtk.EventBox.__init__(self)
+
+        # We catch map/unmap here to allow pausing of expensive UI
+        # updates, like for the stopwatch, when corresponding UI is not
+        # visible.
+        self.connect('map', self._ui_thaw)
+        self.connect('unmap', self._ui_freeze)
+
         self.label = label
         self.new_label = new_label
         self.has_selection_mode = has_selection_mode
@@ -48,3 +55,22 @@ class Clock(Gtk.EventBox):
 
     def delete_selected(self):
         pass
+
+    def _ui_freeze(self, widget):
+        """
+        Called when the clock widget is unmapped.
+
+        Derived classes can implement this method to remove timeouts
+        in order to save CPU time while the clock widget is not
+        visible.
+        """
+        pass
+
+    def _ui_thaw(self, widget):
+        """
+        Called when the clock widget is mapped.
+
+        Derived clock classes can implement this method to re-add
+        timeouts when the clock widget becomes visible again.
+        """
+        pass
diff --git a/gnomeclocks/stopwatch.py b/gnomeclocks/stopwatch.py
index 38ffdc5..5898816 100644
--- a/gnomeclocks/stopwatch.py
+++ b/gnomeclocks/stopwatch.py
@@ -164,17 +164,25 @@ class Stopwatch(Clock):
         else:
             self.time_label.set_markup(Stopwatch.LABEL_MARKUP % (m, s))
 
+    def _add_timeout(self):
+        if self.timeout_id == 0:
+            self.timeout_id = GLib.timeout_add(100, self.count)
+
+    def _remove_timeout(self):
+        if self.timeout_id != 0:
+            GLib.source_remove(self.timeout_id)
+        self.timeout_id = 0
+
     def start(self):
         if self.timeout_id == 0:
             curr = time.time()
             self.start_time = curr
             self.lap_start_time = curr
-            self.timeout_id = GLib.timeout_add(100, self.count)
+            self._add_timeout()
 
     def stop(self):
-        GLib.source_remove(self.timeout_id)
-        self.timeout_id = 0
         curr = time.time()
+        self._remove_timeout()
         self.time_diff = self.time_diff + (curr - self.start_time)
         self.lap_time_diff = self.lap_time_diff + (curr - self.lap_start_time)
 
@@ -187,3 +195,12 @@ class Stopwatch(Clock):
         (h, m, s) = self.get_time()
         self.set_time_label(h, m, s)
         return True
+
+    def _ui_freeze(self, widget):
+        if self.state == Stopwatch.State.RUNNING:
+            self._remove_timeout()
+
+    def _ui_thaw(self, widget):
+        if self.state == Stopwatch.State.RUNNING:
+            self.count()
+            self._add_timeout()
diff --git a/gnomeclocks/timer.py b/gnomeclocks/timer.py
index 83ecb90..950cbf1 100644
--- a/gnomeclocks/timer.py
+++ b/gnomeclocks/timer.py
@@ -186,7 +186,8 @@ class Timer(Clock):
         self.notebook.set_current_page(1)
 
     def _add_timeout(self):
-        self.timeout_id = GLib.timeout_add(250, self.count)
+        if self.timeout_id == 0:
+            self.timeout_id = GLib.timeout_add(250, self.count)
 
     def _remove_timeout(self):
         if self.timeout_id != 0:
@@ -233,3 +234,12 @@ class Timer(Clock):
             h, m = divmod(m, 60)
             self.timer_screen.set_time(h, m, s)
             return True
+
+    def _ui_freeze(self, widget):
+        if self.state == Timer.State.RUNNING:
+            self._remove_timeout()
+
+    def _ui_thaw(self, widget):
+        if self.state == Timer.State.RUNNING:
+            self.count()
+            self._add_timeout()



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