Skip to content

Commit 80a4327

Browse files
committed
Adding additional fix for #32019
- Add a weak-objects hash table to keep track of active `Window` objects. - Check `[FigureWindowHashTable count]` instead of `FigureWindowCount` - Iterate over said hash table in `show()` rather than using `NSApplication.windows`
1 parent d9fd9ba commit 80a4327

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

‎src/_macosx.m‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
/* Variables to keep track of state and window count for show() */
4545
static BOOL IsRunningFromShow = NO;
46-
static long FigureWindowCount = 0;
46+
static NSHashTable<NSWindow *> *FigureWindowHashTable = nil;
4747

4848
/* Keep track of modifier key states for flagsChanged
4949
to keep track of press vs release */
@@ -116,7 +116,7 @@ static int wait_for_stdin() {
116116

117117
// Short circuit if no windows are active
118118
// Rely on Python's input handling to manage CPU usage
119-
// This queries the NSApp, rather than using our FigureWindowCount because that is decremented when events still
119+
// This queries the NSApp, rather than using our FigureWindowHashTable because that is modified when events still
120120
// need to be processed to properly close the windows.
121121
@autoreleasepool {
122122
if (![[NSApp windows] count]) {
@@ -711,7 +711,11 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
711711
[view updateDevicePixelRatio: [window backingScaleFactor]];
712712

713713
self->window = window;
714-
++FigureWindowCount;
714+
715+
if (!FigureWindowHashTable) {
716+
FigureWindowHashTable = [NSHashTable weakObjectsHashTable];
717+
}
718+
[FigureWindowHashTable addObject:window];
715719

716720
END_OBJC_ENTRY
717721
return 0;
@@ -752,9 +756,10 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
752756
[self->window close];
753757
[self->window setDelegate:nil];
754758
[self->window setManager:NULL];
759+
[FigureWindowHashTable removeObject:self->window];
755760

756761
self->window = nil;
757-
if (--FigureWindowCount == 0 && IsRunningFromShow) {
762+
if ([FigureWindowHashTable count] == 0 && IsRunningFromShow) {
758763
[NSApp stop:nil];
759764
}
760765
}
@@ -1816,14 +1821,12 @@ - (void)flagsChanged:(NSEvent *)event
18161821
{
18171822
BEGIN_OBJC_ENTRY
18181823

1819-
// Iterating over -[NSApp windows] will add the windows to the topmost
1824+
// Iterating over FigureWindowHashTable will add the windows to the topmost
18201825
// autorelease pool, wrap in @autoreleasepool as -[NSApp run] is long-running.
18211826
@autoreleasepool {
18221827
[NSApp activateIgnoringOtherApps: YES];
1823-
NSArray *windowsArray = [NSApp windows];
1824-
NSEnumerator *enumerator = [windowsArray objectEnumerator];
1825-
NSWindow *window;
1826-
while ((window = [enumerator nextObject])) {
1828+
1829+
for (NSWindow *window in [FigureWindowHashTable allObjects]) {
18271830
[window orderFront:nil];
18281831
}
18291832
}

0 commit comments

Comments
 (0)