Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6176b7

Browse files
committed
Set the canvas cursor when using a SpanSelector.
This sets either a horizontal or vertcal resize cursor when initiating a selection, moving the span, or when hovering over an end handle.
1 parent c2d4061 commit e6176b7

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/matplotlib/widgets.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import matplotlib as mpl
1919
from matplotlib import docstring
20-
from . import _api, cbook, colors, ticker
20+
from . import _api, backend_tools, cbook, colors, ticker
2121
from .lines import Line2D
2222
from .patches import Circle, Rectangle, Ellipse
2323

@@ -2192,8 +2192,26 @@ def _setup_edge_handle(self, props):
21922192
useblit=self.useblit)
21932193
self.artists.extend([line for line in self._edge_handles.artists])
21942194

2195+
def _set_cursor(self, enabled):
2196+
"""Update the canvas cursor based on direction of the selector."""
2197+
if enabled:
2198+
cursor = (backend_tools.Cursors.RESIZE_HORIZONTAL
2199+
if self.direction == 'horizontal' else
2200+
backend_tools.Cursors.RESIZE_VERTICAL)
2201+
else:
2202+
cursor = backend_tools.Cursors.POINTER
2203+
2204+
self.ax.figure.canvas.set_cursor(cursor)
2205+
2206+
def connect_default_events(self):
2207+
# docstring inherited
2208+
super().connect_default_events()
2209+
if getattr(self, '_interactive', False):
2210+
self.connect_event('motion_notify_event', self._hover)
2211+
21952212
def _press(self, event):
21962213
"""Button press event handler."""
2214+
self._set_cursor(True)
21972215
if self._interactive and self._rect.get_visible():
21982216
self._set_active_handle(event)
21992217
else:
@@ -2248,6 +2266,7 @@ def direction(self, direction):
22482266

22492267
def _release(self, event):
22502268
"""Button release event handler."""
2269+
self._set_cursor(False)
22512270
if not self._interactive:
22522271
self._rect.set_visible(False)
22532272

@@ -2268,6 +2287,19 @@ def _release(self, event):
22682287

22692288
return False
22702289

2290+
def _hover(self, event):
2291+
"""Update the canvas cursor if it's over a handle."""
2292+
if self.ignore(event):
2293+
return
2294+
2295+
if self._active_handle is not None:
2296+
# Do nothing if button is pressed and a handle is active, which may
2297+
# occur with drag_from_anywhere=True.
2298+
return
2299+
2300+
_, e_dist = self._edge_handles.closest(event.x, event.y)
2301+
self._set_cursor(e_dist <= self.grab_range)
2302+
22712303
def _onmove(self, event):
22722304
"""Motion notify event handler."""
22732305

0 commit comments

Comments
 (0)