8000 Combine common GTK timer code. · matplotlib/matplotlib@e135227 · GitHub
[go: up one dir, main page]

Skip to content

Commit e135227

Browse files
committed
Combine common GTK timer code.
1 parent 139a4db commit e135227

File tree

3 files changed

+44
-76
lines changed

3 files changed

+44
-76
lines changed

lib/matplotlib/backends/_backend_gtk.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
import matplotlib as mpl
88
from matplotlib import cbook
99
from matplotlib.backend_bases import (
10-
_Backend,
10+
_Backend, TimerBase,
1111
)
1212

1313
# The GTK3/GTK4 backends will have already called `gi.require_version` to set
1414
# the desired GTK.
15-
from gi.repository import Gio, Gtk
15+
from gi.repository import Gio, GLib, Gtk
1616

1717

1818
_log = logging.getLogger(__name__)
@@ -62,6 +62,42 @@ def _create_application():
6262
return _application
6363

6464

65+
class TimerGTK(TimerBase):
66+
"""Subclass of `.TimerBase` using GTK timer events."""
67+
68+
def __init__(self, *args, **kwargs):
69+
self._timer = None
70+
super().__init__(*args, **kwargs)
71+
72+
def _timer_start(self):
73+
# Need to stop it, otherwise we potentially leak a timer id that will
74+
# never be stopped.
75+
self._timer_stop()
76+
self._timer = GLib.timeout_add(self._interval, self._on_timer)
77+
78+
def _timer_stop(self):
79+
if self._timer is not None:
80+
GLib.source_remove(self._timer)
81+
self._timer = None
82+
83+
def _timer_set_interval(self):
84+
# Only stop and restart it if the timer has already been started.
85+
if self._timer is not None:
86+
self._timer_stop()
87+
self._timer_start()
88+
89+
def _on_timer(self):
90+
super()._on_timer()
91+
92+
# Gtk timeout_add() requires that the callback returns True if it
93+
# is to be called again.
94+
if self.callbacks and not self._single:
95+
return True
96+
else:
97+
self._timer = None
98+
return False
99+
100+
65101
class _BackendGTK(_Backend):
66102
@staticmethod
67103
def mainloop():

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
from gi.repository import Gio, GLib, GObject, Gtk, Gdk
3232
from ._backend_gtk import (
3333
_create_application, _shutdown_application,
34-
backend_version, _BackendGTK)
34+
backend_version, _BackendGTK,
35+
TimerGTK as TimerGTK3,
36+
)
3537

3638

3739
_log = logging.getLogger(__name__)
@@ -70,42 +72,6 @@ def _mpl_to_gtk_cursor(mpl_cursor):
7072
return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name)
7173

7274

73-
class TimerGTK3(TimerBase):
74-
"""Subclass of `.TimerBase` using GTK3 timer events."""
75-
76-
def __init__(self, *args, **kwargs):
77-
self._timer = None
78-
super().__init__(*args, **kwargs)
79-
80-
def _timer_start(self):
81-
# Need to stop it, otherwise we potentially leak a timer id that will
82-
# never be stopped.
83-
self._timer_stop()
84-
self._timer = GLib.timeout_add(self._interval, self._on_timer)
85-
86-
def _timer_stop(self):
87-
if self._timer is not None:
88-
GLib.source_remove(self._timer)
89-
self._timer = None
90-
91-
def _timer_set_interval(self):
92-
# Only stop and restart it if the timer has already been started
93-
628C if self._timer is not None:
94-
self._timer_stop()
95-
self._timer_start()
96-
97-
def _on_timer(self):
98-
super()._on_timer()
99-
100-
# Gtk timeout_add() requires that the callback returns True if it
101-
# is to be called again.
102-
if self.callbacks and not self._single:
103-
return True
104-
else:
105-
self._timer = None
106-
return False
107-
108-
10975
class FigureCanvasGTK3(Gtk.DrawingArea, FigureCanvasBase):
11076
required_interactive_framework = "gtk3"
11177
_timer_cls = TimerGTK3

lib/matplotlib/backends/backend_gtk4.py

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
from gi.repository import Gio, GLib, GObject, Gtk, Gdk, GdkPixbuf
3333
from ._backend_gtk import (
3434
_create_application, _shutdown_application,
35-
backend_version, _BackendGTK)
35+
backend_version, _BackendGTK,
36+
TimerGTK as TimerGTK4,
37+
)
3638

3739

3840
_log = logging.getLogger(__name__)
@@ -50,42 +52,6 @@ def _mpl_to_gtk_cursor(mpl_cursor):
5052
}, cursor=mpl_cursor)
5153

5254

53-
class TimerGTK4(TimerBase):
54-
"""Subclass of `.TimerBase` using GTK4 timer events."""
55-
56-
def __init__(self, *args, **kwargs):
57-
self._timer = None
58-
super().__init__(*args, **kwargs)
59-
60-
def _timer_start(self):
61-
# Need to stop it, otherwise we potentially leak a timer id that will
62-
# never be stopped.
63-
self._timer_stop()
64-
self._timer = GLib.timeout_add(self._interval, self._on_timer)
65-
66-
def _timer_stop(self):
67-
if self._timer is not None:
68-
GLib.source_remove(self._timer)
69-
self._timer = None
70-
71-
def _timer_set_interval(self):
72-
# Only stop and restart it if the timer has already been started
73-
if self._timer is not None:
74-
self._timer_stop()
75-
self._timer_start()
76-
77-
def _on_timer(self):
78-
super()._on_timer()
79-
80-
# Gtk timeout_add() requires that the callback returns True if it
81-
# is to be called again.
82-
if self.callbacks and not self._single:
83-
return True
84-
else:
85-
self._timer = None
86-
return False
87-
88-
8955
class FigureCanvasGTK4(Gtk.DrawingArea, FigureCanvasBase):
9056
required_interactive_framework = "gtk4"
9157
_timer_cls = TimerGTK4

0 commit comments

Comments
 (0)
0