Skip to content

Don't drag draggables on scroll events #29842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ def finalize_offset(self):
def __init__(self, ref_artist, use_blit=False):
self.ref_artist = ref_artist
if not ref_artist.pickable():
ref_artist.set_picker(True)
ref_artist.set_picker(self._picker)
self.got_artist = False
self._use_blit = use_blit and self.canvas.supports_blit
callbacks = self.canvas.callbacks
Expand All @@ -1500,6 +1500,11 @@ def __init__(self, ref_artist, use_blit=False):
]
]

@staticmethod
def _picker(artist, mouseevent):
# A custom picker to prevent dragging on mouse scroll events
return (artist.contains(mouseevent) and mouseevent.name != "scroll_event"), {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (artist.contains(mouseevent) and mouseevent.name != "scroll_event"), {}
"""
A custom picker to prevent dragging on scroll events.
Dragging is initialized through a pick events, which is typically a
mouse click. However, mouse scrolling also fires pick events and
we need to filter them out so that scrolling does not initalize
dragging.
"""
return (artist.contains(mouseevent) and mouseevent.name != "scroll_event"), {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 to adding a comment - I thought this was a bit verbose for a private helper function, so I added a one-liner instead - what do you think?


# A property, not an attribute, to maintain picklability.
canvas = property(lambda self: self.ref_artist.get_figure(root=True).canvas)
cids = property(lambda self: [
Expand Down
Loading