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

Skip to content

Commit 191cd49

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 191cd49

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

lib/matplotlib/widgets.py

Lines changed: 31 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,25 @@ 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+
self.connect_event('motion_notify_event', self._hover)
2210+
21952211
def _press(self, event):
21962212
"""Button press event handler."""
2213+
self._set_cursor(True)
21972214
if self._interactive and self._rect.get_visible():
21982215
self._set_active_handle(event)
21992216
else:
@@ -2248,6 +2265,7 @@ def direction(self, direction):
22482265

22492266
def _release(self, event):
22502267
"""Button release event handler."""
2268+
self._set_cursor(False)
22512269
if not self._interactive:
22522270
self._rect.set_visible(False)
22532271

@@ -2268,6 +2286,18 @@ def _release(self, event):
22682286

22692287
return False
22702288

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

0 commit comments

Comments
 (0)