|
43 | 43 |
|
44 | 44 | /* Variables to keep track of state and window count for show() */ |
45 | 45 | static BOOL IsRunningFromShow = NO; |
46 | | -static long FigureWindowCount = 0; |
| 46 | +static NSHashTable<NSWindow *> *FigureWindowHashTable = nil; |
47 | 47 |
|
48 | 48 | /* Keep track of modifier key states for flagsChanged |
49 | 49 | to keep track of press vs release */ |
@@ -116,7 +116,7 @@ static int wait_for_stdin() { |
116 | 116 |
|
117 | 117 | // Short circuit if no windows are active |
118 | 118 | // 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 |
120 | 120 | // need to be processed to properly close the windows. |
121 | 121 | @autoreleasepool { |
122 | 122 | if (![[NSApp windows] count]) { |
@@ -711,7 +711,11 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name) |
711 | 711 | [view updateDevicePixelRatio: [window backingScaleFactor]]; |
712 | 712 |
|
713 | 713 | self->window = window; |
714 | | - ++FigureWindowCount; |
| 714 | + |
| 715 | + if (!FigureWindowHashTable) { |
| 716 | + FigureWindowHashTable = [NSHashTable weakObjectsHashTable]; |
| 717 | + } |
| 718 | + [FigureWindowHashTable addObject:window]; |
715 | 719 |
|
716 | 720 | END_OBJC_ENTRY |
717 | 721 | return 0; |
@@ -752,9 +756,10 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name) |
752 | 756 | [self->window close]; |
753 | 757 | [self->window setDelegate:nil]; |
754 | 758 | [self->window setManager:NULL]; |
| 759 | + [FigureWindowHashTable removeObject:self->window]; |
755 | 760 |
|
756 | 761 | self->window = nil; |
757 | | - if (--FigureWindowCount == 0 && IsRunningFromShow) { |
| 762 | + if ([FigureWindowHashTable count] == 0 && IsRunningFromShow) { |
758 | 763 | [NSApp stop:nil]; |
759 | 764 | } |
760 | 765 | } |
@@ -1816,14 +1821,12 @@ - (void)flagsChanged:(NSEvent *)event |
1816 | 1821 | { |
1817 | 1822 | BEGIN_OBJC_ENTRY |
1818 | 1823 |
|
1819 | | - // Iterating over -[NSApp windows] will add the windows to the topmost |
| 1824 | + // Iterating over FigureWindowHashTable will add the windows to the topmost |
1820 | 1825 | // autorelease pool, wrap in @autoreleasepool as -[NSApp run] is long-running. |
1821 | 1826 | @autoreleasepool { |
1822 | 1827 | [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]) { |
1827 | 1830 | [window orderFront:nil]; |
1828 | 1831 | } |
1829 | 1832 | } |
|
0 commit comments