Skip to content

Commit

Permalink
Merge pull request #2522 from cwensley/curtis/mac-window-double-close
Browse files Browse the repository at this point in the history
Mac: avoid exceptions from trying to close a form while it is already closing.
  • Loading branch information
cwensley authored Jul 14, 2023
2 parents 90dbad2 + 4888f1e commit cc4937d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Eto.Mac/Forms/MacWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -310,8 +311,10 @@ static void HandleWillClose(object sender, EventArgs e)
var handler = GetHandler(sender) as MacWindow<TControl, TWidget, TCallback>;
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)
Expand Down Expand Up @@ -842,6 +845,7 @@ public bool CloseWindow(Action<CancelEventArgs> closing = null)
if (!Widget.Loaded)
return true;

IsClosing = true;
var args = new CancelEventArgs();
Callback.OnClosing(Widget, args);
if (!args.Cancel && closing != null)
Expand All @@ -850,11 +854,21 @@ public bool CloseWindow(Action<CancelEventArgs> closing = null)
{
Callback.OnClosed(Widget, EventArgs.Empty);
}
IsClosing = false;
return !args.Cancel;
}

bool IsClosing
{
get => Widget.Properties.Get<bool>(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)
Expand Down

0 comments on commit cc4937d

Please sign in to comment.