Skip to content

Commit d33caac

Browse files
committed
ENH: do a lot more clean up on figure close
1 parent 90c2a8b commit d33caac

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

mpl_gui/_promotion.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import matplotlib as mpl
77
from matplotlib import is_interactive
88
from matplotlib.cbook import _api
9-
9+
from matplotlib.backend_bases import FigureCanvasBase
1010
from ._manage_backend import current_backend_module
1111

1212

@@ -73,8 +73,26 @@ def promote_figure(fig, *, auto_draw=True):
7373
def _destroy(event):
7474

7575
if event.key in mpl.rcParams["keymap.quit"]:
76-
# TODO add notion of 'close' to managers
77-
event.canvas.manager.destroy()
76+
# grab the manager off the event
77+
mgr = event.canvas.manager
78+
if mgr is None:
79+
raise RuntimeError("Should never be here, please report a bug")
80+
fig = event.canvas.figure
81+
# remove this callback. Callbacks lives on the Figure so survive
82+
# the canvas being replaced.
83+
old_cid = getattr(mgr, "_destroy_cid", None)
84+
if old_cid is not None:
85+
fig.canvas.mpl_disconnect(old_cid)
86+
mgr._destroy_cid = None
87+
# close the window
88+
mgr.destroy()
89+
# disconnect the manager from the canvas
90+
fig.canvas.manager = None
91+
# reset the dpi
92+
fig.dpi = getattr(fig, "_original_dpi", fig.dpi)
93+
# Go back to "base" canvas
94+
# (this sets state on fig in the canvas init)
95+
FigureCanvasBase(fig)
7896

7997
manager._destroy_cid = fig.canvas.mpl_connect("key_press_event", _destroy)
8098

0 commit comments

Comments
 (0)