From 4888f1e3f35292e051853f7e4d88a501b6d1a203 Mon Sep 17 00:00:00 2001 From: Curtis Wensley Date: Fri, 14 Jul 2023 12:32:54 -0700 Subject: [PATCH] Mac: avoid exceptions from trying to close a form while it is already closing. --- src/Eto.Mac/Forms/MacWindow.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Eto.Mac/Forms/MacWindow.cs b/src/Eto.Mac/Forms/MacWindow.cs index 8aeb33c90..31dedee61 100644 --- a/src/Eto.Mac/Forms/MacWindow.cs +++ b/src/Eto.Mac/Forms/MacWindow.cs @@ -186,6 +186,7 @@ static class MacWindow internal static readonly Selector selSetStyleMask = new Selector("setStyleMask:"); internal static IntPtr selMainMenu = Selector.GetHandle("mainMenu"); internal static IntPtr selSetMainMenu = Selector.GetHandle("setMainMenu:"); + internal static readonly object IsClosing_Key = new object(); internal static readonly object SetAsChildWindow_Key = new object(); internal static readonly IntPtr selWindows_Handle = Selector.GetHandle("windows"); internal static readonly IntPtr selCount_Handle = Selector.GetHandle("count"); @@ -310,8 +311,10 @@ static void HandleWillClose(object sender, EventArgs e) var handler = GetHandler(sender) as MacWindow; if (handler == null || !handler.Widget.Loaded) // could already be closed return; + handler.IsClosing = true; if (ApplicationHandler.Instance.ShouldCloseForm(handler.Widget, true)) handler.Callback.OnClosed(handler.Widget, EventArgs.Empty); + handler.IsClosing = false; } static bool HandleWindowShouldClose(NSObject sender) @@ -842,6 +845,7 @@ public bool CloseWindow(Action closing = null) if (!Widget.Loaded) return true; + IsClosing = true; var args = new CancelEventArgs(); Callback.OnClosing(Widget, args); if (!args.Cancel && closing != null) @@ -850,11 +854,21 @@ public bool CloseWindow(Action closing = null) { Callback.OnClosed(Widget, EventArgs.Empty); } + IsClosing = false; return !args.Cancel; } + bool IsClosing + { + get => Widget.Properties.Get(MacWindow.IsClosing_Key); + set => Widget.Properties.Set(MacWindow.IsClosing_Key, value); + } + public virtual void Close() { + // already closing, let's not cause problems by trying to close twice. + if (IsClosing) + return; var args = new CancelEventArgs(); Callback.OnClosing(Widget, args); if (!args.Cancel)