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

Skip to content

Commit 49590bb

Browse files
committed
Change cursor when hovering over draggable artists
1 parent 5f650dd commit 49590bb

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import matplotlib.path as mpath
3333
import matplotlib.text as mtext
3434
import matplotlib.transforms as mtransforms
35+
from matplotlib.backend_tools import Cursors
3536
from matplotlib.font_manager import FontProperties
3637
from matplotlib.image import BboxImage
3738
from matplotlib.patches import (
@@ -1489,6 +1490,7 @@ def __init__(self, ref_artist, use_blit=False):
14891490
if not ref_artist.pickable():
14901491
ref_artist.set_picker(True)
14911492
self.got_artist = False
1493+
self._hover = False
14921494
self._use_blit = use_blit and self.canvas.supports_blit
14931495
callbacks = ref_artist.figure._canvas_callbacks
14941496
self._disconnectors = [
@@ -1508,7 +1510,31 @@ def __init__(self, ref_artist, use_blit=False):
15081510
disconnect.args[0] for disconnect in self._disconnectors[:2]])
15091511

15101512
def on_motion(self, evt):
1511-
if self._check_still_parented() and self.got_artist:
1513+
# Only check if the widget lock is available, setting it would prevent
1514+
# picking.
1515+
if not (
1516+
self._check_still_parented() and self.canvas.widgetlock.available(self)
1517+
):
1518+
return
1519+
1520+
picker = self.ref_artist.get_picker()
1521+
if callable(picker):
1522+
inside, _ = picker(self, evt)
1523+
else:
1524+
inside, _ = self.ref_artist.contains(evt)
1525+
1526+
# If the mouse is moving quickly while dragging, it may leave the artist,
1527+
# but should still use the move cursor.
1528+
if inside or self.got_artist:
1529+
self._hover = True
1530+
self.canvas.set_cursor(Cursors.MOVE)
1531+
elif self._hover:
1532+
# Only change the cursor back if this is the widget that set it, to
1533+
# avoid multiple draggable widgets fighting over the cursor.
1534+
self._hover = False
1535+
self.canvas.set_cursor(Cursors.POINTER)
1536+
1537+
if self.got_artist:
15121538
dx = evt.x - self.mouse_x
15131539
dy = evt.y - self.mouse_y
15141540
self.update_offset(dx, dy)
@@ -1554,6 +1580,9 @@ def disconnect(self):
15541580
for disconnector in self._disconnectors:
15551581
disconnector()
15561582

1583+
if self._hover:
1584+
self.canvas.set_cursor(Cursors.POINTER)
1585+
15571586
def save_offset(self):
15581587
pass
15591588

0 commit comments

Comments
 (0)