Skip to content

Commit 84ff954

Browse files
committed
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`.
1 parent 07fd38f commit 84ff954

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2976,11 +2976,7 @@ def _update_cursor(self, event):
29762976
"""
29772977
Update the cursor after a mouse move event or a tool (de)activation.
29782978
"""
2979-
if not event.inaxes or not self.mode:
2980-
if self._lastCursor != cursors.POINTER:
2981-
self.set_cursor(cursors.POINTER)
2982-
self._lastCursor = cursors.POINTER
2983-
else:
2979+
if self.mode and event.inaxes:
29842980
if (self.mode == _Mode.ZOOM
29852981
and self._lastCursor != cursors.SELECT_REGION):
29862982
self.set_cursor(cursors.SELECT_REGION)
@@ -2989,6 +2985,9 @@ def _update_cursor(self, event):
29892985
and self._lastCursor != cursors.MOVE):
29902986
self.set_cursor(cursors.MOVE)
29912987
self._lastCursor = cursors.MOVE
2988+
elif self._lastCursor != cursors.POINTER:
2989+
self.set_cursor(cursors.POINTER)
2990+
self._lastCursor = cursors.POINTER
29922991

29932992
@contextmanager
29942993
def _wait_cursor_for_draw_cm(self):

lib/matplotlib/backend_tools.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,11 @@ class SetCursorBase(ToolBase):
223223
def __init__(self, *args, **kwargs):
224224
super().__init__(*args, **kwargs)
225225
self._id_drag = None
226-
self._cursor = None
226+
self._current_tool = None
227227
self._default_cursor = cursors.POINTER
228228
self._last_cursor = self._default_cursor
229229
self.toolmanager.toolmanager_connect('tool_added_event',
230230
self._add_tool_cbk)
231-
232231
# process current tools
233232
for tool in self.toolmanager.tools.values():
234233
self._add_tool(tool)
@@ -243,10 +242,9 @@ def set_figure(self, figure):
243242

244243
def _tool_trigger_cbk(self, event):
245244
if event.tool.toggled:
246-
self._cursor = event.tool.cursor
245+
self._current_tool = event.tool
247246
else:
248-
self._cursor = None
249-
247+
self._current_tool = None
250248
self._set_cursor_cbk(event.canvasevent)
251249

252250
def _add_tool(self, tool):
@@ -264,16 +262,13 @@ def _add_tool_cbk(self, event):
264262
def _set_cursor_cbk(self, event):
265263
if not event:
266264
return
267-
268-
if not getattr(event, 'inaxes', False) or not self._cursor:
269-
if self._last_cursor != self._default_cursor:
270-
self.set_cursor(self._default_cursor)
271-
self._last_cursor = self._default_cursor
272-
elif self._cursor:
273-
cursor = self._cursor
274-
if cursor and self._last_cursor != cursor:
275-
self.set_cursor(cursor)
276-
self._last_cursor = cursor
265+
if self._current_tool and getattr(event, "inaxes", None):
266+
if self._last_cursor != self._current_tool.cursor:
267+
self.set_cursor(self._current_tool.cursor)
268+
self._last_cursor = self._current_tool.cursor
269+
elif self._last_cursor != self._default_cursor:
270+
self.set_cursor(self._default_cursor)
271+
self._last_cursor = self._default_cursor
277272

278273
def set_cursor(self, cursor):
279274
"""

0 commit comments

Comments
 (0)