@@ -521,6 +521,7 @@ static void releaseMonitor(_GLFWwindow* window)
521521@interface GLFWWindowDelegate : NSObject
522522{
523523 _GLFWwindow* window;
524+ NSArray <NSDictionary *> *_lastScreenStates;
524525}
525526
526527- (instancetype )initWithGlfwWindow : (_GLFWwindow *)initWindow ;
@@ -533,12 +534,26 @@ @implementation GLFWWindowDelegate
533534- (instancetype )initWithGlfwWindow : (_GLFWwindow *)initWindow
534535{
535536 self = [super init ];
536- if (self != nil )
537+ if (self != nil ) {
537538 window = initWindow;
538-
539+ _lastScreenStates = [self captureScreenStates ];
540+ }
539541 return self;
540542}
541543
544+ - (NSArray <NSDictionary *> *)captureScreenStates {
545+ NSMutableArray *states = [NSMutableArray array ];
546+ for (NSScreen *screen in [NSScreen screens ]) {
547+ // Use the screen's deviceDescription, which contains a stable ID.
548+ [states addObject: screen.deviceDescription];
549+ }
550+ return [states copy ];
551+ }
552+
553+ - (void )cleanup {
554+ [_lastScreenStates release ]; _lastScreenStates = nil ;
555+ }
556+
542557- (BOOL )windowShouldClose : (id )sender
543558{
544559 (void )sender;
@@ -549,6 +564,16 @@ - (BOOL)windowShouldClose:(id)sender
549564- (void )windowDidResize : (NSNotification *)notification
550565{
551566 (void )notification;
567+ NSArray <NSDictionary *> *currentScreenStates = [self captureScreenStates ];
568+ const bool is_screen_change = ![_lastScreenStates isEqualToArray: currentScreenStates];
569+ debug_rendering (" windowDidResize() called, is_screen_change: %d \n " , is_screen_change);
570+ if (is_screen_change) {
571+ // This resize likely happened because a screen was added, removed, or changed resolution.
572+ [_lastScreenStates release ];
573+ _lastScreenStates = [currentScreenStates retain ];
574+ }
575+ [currentScreenStates release ];
576+
552577 if (window->context .client != GLFW_NO_API)
553578 [window->context.nsgl.object update ];
554579
@@ -580,7 +605,10 @@ - (void)windowDidResize:(NSNotification *)notification
580605 window->ns .height = (int )contentRect.size .height ;
581606 _glfwInputWindowSize (window, (int )contentRect.size .width , (int )contentRect.size .height );
582607 }
583- if (window->ns .resizeCallback ) window->ns .resizeCallback ((GLFWwindow*)window);
608+ // Because of a bug in macOS Tahoe we cannot redraw the window in response
609+ // to a resize event that was caused by a screen change as the OpenGL
610+ // context is not ready yet. See: https://github.com/kovidgoyal/kitty/issues/8983
611+ if (window->ns .resizeCallback && !is_screen_change) window->ns .resizeCallback ((GLFWwindow*)window);
584612}
585613
586614- (void )windowDidMove : (NSNotification *)notification
@@ -1913,6 +1941,7 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
19131941 window->context .destroy (window);
19141942
19151943 [window->ns.object setDelegate: nil ];
1944+ [window->ns.delegate cleanup ];
19161945 [window->ns.delegate release ];
19171946 window->ns .delegate = nil ;
19181947
0 commit comments