Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Handles disposed windows when they are closed while checking windows loop #9109

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enum JIS_VKS {

#if MAC
Foundation.NSObject keyMonitor;
Foundation.NSObject closeWindowMonitor;
uint throttleLastEventTime = 0;
#endif

Expand Down Expand Up @@ -124,6 +125,12 @@ public CommandManager (Window root)
ActionCommand c = new ActionCommand (CommandSystemCommands.ToolbarList, "Toolbar List", null, null, ActionType.Check);
c.CommandArray = true;
RegisterCommand (c);

closeWindowMonitor = Foundation.NSNotificationCenter.DefaultCenter.AddObserver (AppKit.NSWindow.WillCloseNotification, (s) => {
if (topLevelWindows.Any (h => h.nativeWidget == s.Object)) {
TopLevelDestroyed (s.Object, EventArgs.Empty);
}
});
}

/// <summary>
Expand Down Expand Up @@ -869,13 +876,18 @@ void TopLevelDestroyed (object o, EventArgs args)
{
RegisterUserInteraction ();

Gtk.Window w = (Gtk.Window)o;
w.Destroyed -= TopLevelDestroyed;
w.KeyPressEvent -= OnKeyPressed;
w.KeyReleaseEvent -= OnKeyReleased;
w.ButtonPressEvent -= HandleButtonPressEvent;
topLevelWindows.Remove (w);
if (o is Gtk.Window w) {
w.Destroyed -= TopLevelDestroyed;
w.KeyPressEvent -= OnKeyPressed;
w.KeyReleaseEvent -= OnKeyReleased;
w.ButtonPressEvent -= HandleButtonPressEvent;
topLevelWindows.Remove (w);
}
#if MAC
else if (o is AppKit.NSWindow nsWindow) {
topLevelWindows.Remove (nsWindow);
}

if (topLevelWindows.Count == 0) {
if (keyMonitor != null) {
AppKit.NSEvent.RemoveMonitor (keyMonitor);
Expand All @@ -884,7 +896,7 @@ void TopLevelDestroyed (object o, EventArgs args)
}
#endif

if (w == lastFocused?.nativeWidget)
if (o == lastFocused?.nativeWidget)
lastFocused = null;
}

Expand All @@ -898,6 +910,11 @@ public void Dispose ()
AppKit.NSEvent.RemoveMonitor (keyMonitor);
keyMonitor = null;
}

if (closeWindowMonitor != null) {
AppKit.NSEvent.RemoveMonitor (closeWindowMonitor);
closeWindowMonitor = null;
}
#endif

if (bindings != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,26 +253,39 @@ internal class OpenWindowListHandler : CommandHandler
protected override void Update (CommandArrayInfo info)
{
foreach (Components.Window window in IdeApp.CommandService.TopLevelWindowStack) {
netonjm marked this conversation as resolved.
Show resolved Hide resolved

string title = null;
bool hasTopLevel = false;

try {
title = window.Title;
hasTopLevel = window.HasTopLevelFocus;
#if !WINDOWS
//we don't want include hidden windows
if (!window.IsRealized || !window.IsVisible || Components.Mac.GtkMacInterop.IsGdkQuartzWindow (window))
continue;
//we don't want include hidden windows
if (!window.IsRealized || !window.IsVisible || Components.Mac.GtkMacInterop.IsGdkQuartzWindow (window))
continue;
#endif
} catch (System.Exception ex) {
LoggingService.LogInternalError (ex);
//we log the issue
continue;
}


//Create CommandInfo object
var commandInfo = new CommandInfo ();
commandInfo.Text = window.Title.Replace ("_", "__").Replace ("-", "\u2013").Replace (" \u2013 " + BrandingService.ApplicationName, "");
commandInfo.Text = title.Replace ("_", "__").Replace ("-", "\u2013").Replace (" \u2013 " + BrandingService.ApplicationName, "");

if (string.IsNullOrEmpty (commandInfo.Text)) {
commandInfo.Text = GettextCatalog.GetString ("No description");
}

if (window.HasTopLevelFocus)
if (hasTopLevel)
commandInfo.Checked = true;
commandInfo.Description = GettextCatalog.GetString ("Activate window '{0}'", commandInfo.Text);

//Add menu item
info.Add (commandInfo, window.Title);
info.Add (commandInfo, title);
}
}

Expand Down