From 731f45460b5fd50b1490f028701400e2b460388d Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 22 May 2025 04:04:16 -0400 Subject: [PATCH] Fix OffsetBox custom picker As with the custom picker, `Artist.contains` returns a boolean and a dictionary in a tuple. This non-empty tuple is always true, so the custom picker would always return True for any non-scroll event. It would also lose the related dictionary. --- lib/matplotlib/offsetbox.py | 4 +++- lib/matplotlib/tests/test_offsetbox.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py index 6a3a122fc3e7..1e07125cdc2a 100644 --- a/lib/matplotlib/offsetbox.py +++ b/lib/matplotlib/offsetbox.py @@ -1504,7 +1504,9 @@ 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"), {} + if mouseevent.name == "scroll_event": + return False, {} + return artist.contains(mouseevent) # A property, not an attribute, to maintain picklability. canvas = property(lambda self: self.ref_artist.get_figure(root=True).canvas) diff --git a/lib/matplotlib/tests/test_offsetbox.py b/lib/matplotlib/tests/test_offsetbox.py index f18fa7c777d1..d9791ff5bc20 100644 --- a/lib/matplotlib/tests/test_offsetbox.py +++ b/lib/matplotlib/tests/test_offsetbox.py @@ -460,3 +460,13 @@ def test_draggable_in_subfigure(): fig.canvas.draw() # Texts are non-pickable until the first draw. MouseEvent("button_press_event", fig.canvas, 1, 1)._process() assert ann._draggable.got_artist + # Stop dragging the annotation. + MouseEvent("button_release_event", fig.canvas, 1, 1)._process() + assert not ann._draggable.got_artist + # A scroll event should not initiate a drag. + MouseEvent("scroll_event", fig.canvas, 1, 1)._process() + assert not ann._draggable.got_artist + # An event outside the annotation should not initiate a drag. + bbox = ann.get_window_extent() + MouseEvent("button_press_event", fig.canvas, bbox.x1+2, bbox.y1+2)._process() + assert not ann._draggable.got_artist