Skip to content

Backport PR #25442 on branch v3.7.x (Fix disconnection of callbacks when draggable artist is deparented.) #25465

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
Show file tree
Hide file tree
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
31 changes: 14 additions & 17 deletions lib/matplotlib/offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,16 +1506,23 @@ def __init__(self, ref_artist, use_blit=False):
ref_artist.set_picker(True)
self.got_artist = False
self._use_blit = use_blit and self.canvas.supports_blit
self.cids = [
self.canvas.callbacks._connect_picklable(
'pick_event', self.on_pick),
self.canvas.callbacks._connect_picklable(
'button_release_event', self.on_release),
callbacks = ref_artist.figure._canvas_callbacks
self._disconnectors = [
functools.partial(
callbacks.disconnect, callbacks._connect_picklable(name, func))
for name, func in [
("pick_event", self.on_pick),
("button_release_event", self.on_release),
("motion_notify_event", self.on_motion),
]
]

# A property, not an attribute, to maintain picklability.
canvas = property(lambda self: self.ref_artist.figure.canvas)

cids = property(lambda self: [
disconnect.args[0] for disconnect in self._disconnectors[:2]])

def on_motion(self, evt):
if self._check_still_parented() and self.got_artist:
dx = evt.x - self.mouse_x
Expand All @@ -1542,16 +1549,12 @@ def on_pick(self, evt):
self.ref_artist.draw(
self.ref_artist.figure._get_renderer())
self.canvas.blit()
self._c1 = self.canvas.callbacks._connect_picklable(
"motion_notify_event", self.on_motion)
self.save_offset()

def on_release(self, event):
if self._check_still_parented() and self.got_artist:
self.finalize_offset()
self.got_artist = False
self.canvas.mpl_disconnect(self._c1)

if self._use_blit:
self.ref_artist.set_animated(False)

Expand All @@ -1564,14 +1567,8 @@ def _check_still_parented(self):

def disconnect(self):
"""Disconnect the callbacks."""
for cid in self.cids:
self.canvas.mpl_disconnect(cid)
try:
c1 = self._c1
except AttributeError:
pass
else:
self.canvas.mpl_disconnect(c1)
for disconnector in self._disconnectors:
disconnector()

def save_offset(self):
pass
Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_offsetbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,11 @@ def test_paddedbox():
pb = PaddedBox(at, patch_attrs={'facecolor': 'r'}, draw_frame=True)
ax.add_artist(pb)
fig.draw_without_rendering()


def test_remove_draggable():
fig, ax = plt.subplots()
an = ax.annotate("foo", (.5, .5))
an.draggable(True)
an.remove()
MouseEvent("button_release_event", fig.canvas, 1, 1)._process()