cocoa: When exiting a fullscreen space, wait for window state to normalize.
authorRyan C. Gordon
Fri, 21 Feb 2020 14:50:09 -0500
changeset 135416710fbe3b098
parent 13540 28fcb5ef7ff1
child 13542 27cf93d543ca
cocoa: When exiting a fullscreen space, wait for window state to normalize.

A good metric of this is when the titlebar's "minimize" button is reenabled,
which doesn't happen by the time windowDidExitFullscreen triggers.

This fixes minimizing a fullscreen window on macOS.

Fixes Bugzilla #4177.
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Wed Feb 19 08:26:00 2020 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Fri Feb 21 14:50:09 2020 -0500
     1.3 @@ -794,6 +794,7 @@
     1.4  {
     1.5      SDL_Window *window = _data->window;
     1.6      NSWindow *nswindow = _data->nswindow;
     1.7 +    NSButton *button = nil;
     1.8  
     1.9      inFullscreenTransition = NO;
    1.10  
    1.11 @@ -865,6 +866,22 @@
    1.12              Cocoa_ShowWindow(SDL_GetVideoDevice(), window);
    1.13          }
    1.14      }
    1.15 +
    1.16 +    /* There's some state that isn't quite back to normal when
    1.17 +        windowDidExitFullScreen triggers. For example, the minimize button on
    1.18 +        the titlebar doesn't actually enable for another 200 milliseconds or
    1.19 +        so on this MacBook. Camp here and wait for that to happen before
    1.20 +        going on, in case we're exiting fullscreen to minimize, which need
    1.21 +        that window state to be normal before it will work. */
    1.22 +    button = [nswindow standardWindowButton:NSWindowMiniaturizeButton];
    1.23 +    if (button) {
    1.24 +        int iterations = 0;
    1.25 +        while (![button isEnabled]) {
    1.26 +            SDL_Delay(10);
    1.27 +            SDL_PumpEvents();
    1.28 +            iterations++;
    1.29 +        }
    1.30 +    }
    1.31  }
    1.32  
    1.33  -(NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
    1.34 @@ -1724,6 +1741,7 @@
    1.35      SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    1.36      NSWindow *nswindow = data->nswindow;
    1.37  
    1.38 +printf("Cocoa_MinimizeWindow begin %u\n", (unsigned int) SDL_GetTicks());
    1.39      if ([data->listener isInFullscreenSpaceTransition]) {
    1.40          [data->listener addPendingWindowOperation:PENDING_OPERATION_MINIMIZE];
    1.41      } else {