cairo-gtk-animation.py: add a more efficient animation strategy
[experiments/cairo-gtk.git] / cairo-gtk-animation.py
index d6d67fe..d85f986 100755 (executable)
@@ -24,6 +24,7 @@ import gi
 
 gi.require_version('Gtk', '3.0')
 from gi.repository import Gtk, GObject, Gio
+import time
 
 import math
 import colorsys
@@ -47,9 +48,27 @@ class AnimatedCanvas(Gtk.DrawingArea):
         self.set_size_request(width, height)
         self.connect('draw', self.do_draw_cb)
         self.connect('configure-event', self.do_configure_event_cb)
+        self.connect('destroy', self.on_destroy)
 
-        fps = 30
-        GObject.timeout_add(1000 / fps, self.update)
+        self.fps = 30
+        GObject.timeout_add(50, self.mainloop)
+
+    def on_destroy(self, event):
+        self.running = False
+
+    def tick(self):
+        self.update()
+        self.queue_draw()
+
+    def mainloop(self):
+        self.running = True
+        while self.running:
+            time.sleep(1.0 / self.fps)
+            self.tick()
+            while Gtk.events_pending():
+                Gtk.main_iteration()
+
+        return False
 
     def update(self):
         if self.x - self.radius - self.border < 0 or \
@@ -63,9 +82,6 @@ class AnimatedCanvas(Gtk.DrawingArea):
         self.x += self.x_offset
         self.y += self.y_offset
 
-        self.queue_draw()
-        return True
-
     def do_configure_event_cb(self, widget, event):
         self.width = event.width
         self.height = event.height