Skip to content

Commit 2da4aa1

Browse files
committed
ENH: add singleshot timer to macosx draw_idle
This adds a singleshot timer to the draw_idle method. This has a callback that triggers the draw upon firing the timer if the draw has not occurred yet, otherwise it short-circuits out of the draw and removes the timer.
1 parent 3c2645b commit 2da4aa1

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

lib/matplotlib/backends/backend_macosx.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,38 @@ def draw(self):
4949

5050
def draw_idle(self):
5151
# docstring inherited
52-
if (getattr(self, '_draw_pending', False) or
52+
if not (getattr(self, '_draw_pending', False) or
5353
getattr(self, '_is_drawing', False)):
54-
return
55-
self._draw_pending = True
54+
self._draw_pending = True
55+
# Add a singleshot timer to the eventloop that will call back
56+
# into the Python method _draw_idle to take care of the draw
57+
self._single_shot_timer(self._draw_idle)
58+
59+
def _single_shot_timer(self, callback):
60+
"""Add a single shot timer with the given callback"""
61+
# We need to explicitly stop (called from delete) the timer after
62+
# firing, otherwise segfaults will occur when trying to deallocate
63+
# the singleshot timers.
64+
def callback_func(callback, timer):
65+
callback()
66+
del timer
67+
timer = self.new_timer(interval=0)
68+
timer.add_callback(callback_func, callback, timer)
69+
timer.start()
70+
71+
def _draw_idle(self):
72+
"""
73+
Draw method for singleshot timer
74+
75+
This draw method can be added to a singleshot timer, which can
76+
accumulate draws while the eventloop is spinning. This method will
77+
then only draw the first time and short-circuit the others.
78+
"""
5679
with self._idle_draw_cntx():
80+
if not self._draw_pending:
81+
# Short-circuit because our draw request has already been
82+
# taken care of
83+
return
5784
self._draw_pending = False
5885
self.draw()
5986

0 commit comments

Comments
 (0)