From 84ff954c35e9765b48c5bb5cde1f937deb7c691b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 8 Apr 2021 13:36:58 +0200 Subject: [PATCH 1/2] Switch logic of tool cursor setting. Putting the case where a tool is active first avoids negated conditions (e.g. re: event.inaxes), making things easier to follow. Also, let SetCursorBase keep a reference to the currently active tool, rather than its cursor, to avoid having the slightly confusing `._cursor` next to `._default_cursor` and `._last_cursor`. --- lib/matplotlib/backend_bases.py | 9 ++++----- lib/matplotlib/backend_tools.py | 25 ++++++++++--------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 5af60bddf9c9..aca72adfe9f2 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2976,11 +2976,7 @@ 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.mode: - if self._lastCursor != cursors.POINTER: - self.set_cursor(cursors.POINTER) - self._lastCursor = cursors.POINTER - else: + if self.mode and event.inaxes: if (self.mode == _Mode.ZOOM and self._lastCursor != cursors.SELECT_REGION): self.set_cursor(cursors.SELECT_REGION) @@ -2989,6 +2985,9 @@ def _update_cursor(self, event): and self._lastCursor != cursors.MOVE): self.set_cursor(cursors.MOVE) self._lastCursor = cursors.MOVE + elif self._lastCursor != cursors.POINTER: + self.set_cursor(cursors.POINTER) + self._lastCursor = cursors.POINTER @contextmanager def _wait_cursor_for_draw_cm(self): diff --git a/lib/matplotlib/backend_tools.py b/lib/matplotlib/backend_tools.py index 4b594442f899..17622a535d5e 100644 --- a/lib/matplotlib/backend_tools.py +++ b/lib/matplotlib/backend_tools.py @@ -223,12 +223,11 @@ class SetCursorBase(ToolBase): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._id_drag = None - self._cursor = None + self._current_tool = None self._default_cursor = cursors.POINTER self._last_cursor = self._default_cursor self.toolmanager.toolmanager_connect('tool_added_event', self._add_tool_cbk) - # process current tools for tool in self.toolmanager.tools.values(): self._add_tool(tool) @@ -243,10 +242,9 @@ def set_figure(self, figure): def _tool_trigger_cbk(self, event): if event.tool.toggled: - self._cursor = event.tool.cursor + self._current_tool = event.tool else: - self._cursor = None - + self._current_tool = None self._set_cursor_cbk(event.canvasevent) def _add_tool(self, tool): @@ -264,16 +262,13 @@ def _add_tool_cbk(self, event): def _set_cursor_cbk(self, event): if not event: return - - if not getattr(event, 'inaxes', False) or not self._cursor: - if self._last_cursor != self._default_cursor: - self.set_cursor(self._default_cursor) - self._last_cursor = self._default_cursor - elif self._cursor: - cursor = self._cursor - if cursor and self._last_cursor != cursor: - self.set_cursor(cursor) - self._last_cursor = cursor + if self._current_tool and getattr(event, "inaxes", None): + if self._last_cursor != self._current_tool.cursor: + self.set_cursor(self._current_tool.cursor) + self._last_cursor = self._current_tool.cursor + elif self._last_cursor != self._default_cursor: + self.set_cursor(self._default_cursor) + self._last_cursor = self._default_cursor def set_cursor(self, cursor): """ From 9e770364df8cfd3014228536b8c9dc8637d05746 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 8 Apr 2021 13:39:05 +0200 Subject: [PATCH 2/2] Don't set zoom/pan cursor for non-navigatable axes. Zoom/pan cannot be used e.g. on colorbars, which are "non-navigatable". So don't set the zoom/pan cursor when the mouse is over them, either. --- lib/matplotlib/backend_bases.py | 2 +- lib/matplotlib/backend_tools.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index aca72adfe9f2..70eff2cf566d 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2976,7 +2976,7 @@ def _update_cursor(self, event): """ Update the cursor after a mouse move event or a tool (de)activation. """ - if self.mode and event.inaxes: + if self.mode and event.inaxes and event.inaxes.get_navigate(): if (self.mode == _Mode.ZOOM and self._lastCursor != cursors.SELECT_REGION): self.set_cursor(cursors.SELECT_REGION) diff --git a/lib/matplotlib/backend_tools.py b/lib/matplotlib/backend_tools.py index 17622a535d5e..459e71e4f526 100644 --- a/lib/matplotlib/backend_tools.py +++ b/lib/matplotlib/backend_tools.py @@ -262,7 +262,8 @@ def _add_tool_cbk(self, event): def _set_cursor_cbk(self, event): if not event: return - if self._current_tool and getattr(event, "inaxes", None): + if (self._current_tool and getattr(event, "inaxes", None) + and event.inaxes.get_navigate()): if self._last_cursor != self._current_tool.cursor: self.set_cursor(self._current_tool.cursor) self._last_cursor = self._current_tool.cursor