8000 Only set the wait cursor if the last draw was >1s ago. by anntzer · Pull Request #9672 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Only set the wait cursor if the last draw was >1s ago. #9672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2342,11 +2342,11 @@ def key_press_handler(event, canvas, toolbar=None):
# pan mnemonic (default key 'p')
elif event.key in pan_keys:
toolbar.pan()
toolbar._set_cursor(event)
toolbar._update_cursor(event)
# zoom mnemonic (default key 'o')
elif event.key in zoom_keys:
toolbar.zoom()
toolbar._set_cursor(event)
toolbar._update_cursor(event)
# saving current figure (default key 's')
elif event.key in save_keys:
toolbar.save_figure()
Expand Down Expand Up @@ -2700,7 +2700,10 @@ class implementation.
"""
raise NotImplementedError

def _set_cursor(self, event):
def _update_cursor(self, event):
"""
Update the cursor after a mouse move event or a tool (de)activation.
"""
if not event.inaxes or not self._active:
if self._lastCursor != cursors.POINTER:
self.set_cursor(cursors.POINTER)
Expand All @@ -2715,8 +2718,30 @@ def _set_cursor(self, event):
self.set_cursor(cursors.MOVE)
self._lastCursor = cursors.MOVE

@contextmanager
def _wait_cursor_for_draw_cm(self):
"""
Set the cursor to a wait cursor when drawing the canvas.

In order to avoid constantly changing the cursor when the canvas
changes frequently, do nothing if this context was triggered during the
last second. (Optimally we'd prefer only setting the wait cursor if
the *current* draw takes too long, but the current draw blocks the GUI
thread).
"""
self._draw_time, last_draw_time = (
time.time(), getattr(self, "_draw_time", -np.inf))
if self._draw_time - last_draw_time > 1:
try:
self.set_cursor(cursors.WAIT)
yield
finally:
self.set_cursor(self._lastCursor)
else:
yield

def mouse_move(self, event):
self._set_cursor(event)
self._update_cursor(event)

if event.inaxes and event.inaxes.get_navigate():

Expand Down
13 changes: 11 additions & 2 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@
import threading
except ImportError:
import dummy_threading as threading
import numpy as np
try:
from contextlib import nullcontext
except ImportError:
from contextlib import ExitStack as nullcontext # Py 3.6.
from math import radians, cos, sin

import numpy as np

from matplotlib import cbook, rcParams, __version__
from matplotlib.backend_bases import (
_Backend, FigureCanvasBase, FigureManagerBase, RendererBase)
Expand Down Expand Up @@ -383,7 +389,10 @@ def draw(self):
Draw the figure using the renderer.
"""
self.renderer = self.get_renderer(cleared=True)
with RendererAgg.lock:
# Acquire a lock on the shared font cache.
with RendererAgg.lock, \
(self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
else nullcontext()):
self.figure.draw(self.renderer)
# A GUI class may be need to update a window using this draw, so
# don't forget to call the superclass.
Expand Down
26 changes: 14 additions & 12 deletions lib/matplotlib/backends/backend_gtk3cairo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
try:
from contextlib import nullcontext
except ImportError:
from contextlib import ExitStack as nullcontext # Py 3.6.

from . import backend_cairo, backend_gtk3
from .backend_gtk3 import Gtk, _BackendGTK3
from matplotlib import cbook
Expand All @@ -22,18 +27,15 @@ def _render_figure(self, width, height):

def on_draw_event(self, widget, ctx):
"""GtkDrawable draw event."""
# toolbar = self.toolbar
# if toolbar:
# toolbar.set_cursor(cursors.WAIT)
self._renderer.set_context(ctx)
allocation = self.get_allocation()
Gtk.render_background(
self.get_style_context(), ctx,
allocation.x, allocation.y, allocation.width, allocation.height)
self._render_figure(allocation.width, allocation.height)
# if toolbar:
# toolbar.set_cursor(toolbar._lastCursor)
return False # finish event propagation?
with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
else nullcontext()):
self._renderer.set_context(ctx)
allocation = self.get_allocation()
Gtk.render_background(
self.get_style_context(), ctx,
allocation.x, allocation.y,
allocation.width, allocation.height)
self._render_figure(allocation.width, allocation.height)


@cbook.deprecated("3.1", alternative="backend_gtk3.FigureManagerGTK3")
Expand Down
0