Properly handle toggling fullscreen state when another fullscreen state change is in progress.
authorSam Lantinga <slouken@libsdl.org>
Mon, 11 Nov 2013 20:42:59 -0800
changeset 7961c629882e0a81
parent 7960 904d473b8845
child 7962 e4d2f3375868
Properly handle toggling fullscreen state when another fullscreen state change is in progress.
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Mon Nov 11 19:17:32 2013 -0300
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Mon Nov 11 20:42:59 2013 -0800
     1.3 @@ -34,12 +34,19 @@
     1.4      BOOL wasVisible;
     1.5      BOOL isFullscreen;
     1.6      BOOL inFullscreenTransition;
     1.7 +
     1.8 +    enum
     1.9 +    {
    1.10 +        PENDING_TRANSITION_NONE,
    1.11 +        PENDING_TRANSITION_ENTER_FULLSCREEN,
    1.12 +        PENDING_TRANSITION_LEAVE_FULLSCREEN
    1.13 +    } pendingFullscreenTransition;
    1.14  }
    1.15  
    1.16  -(void) listen:(SDL_WindowData *) data;
    1.17  -(void) pauseVisibleObservation;
    1.18  -(void) resumeVisibleObservation;
    1.19 --(BOOL) isToggledFullscreen;
    1.20 +-(BOOL) setFullscreenState:(BOOL) state;
    1.21  -(void) close;
    1.22  
    1.23  /* Window delegate functionality */
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Nov 11 19:17:32 2013 -0300
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Nov 11 20:42:59 2013 -0800
     2.3 @@ -103,6 +103,7 @@
     2.4      wasVisible = [window isVisible];
     2.5      isFullscreen = NO;
     2.6      inFullscreenTransition = NO;
     2.7 +    pendingFullscreenTransition = PENDING_TRANSITION_NONE;
     2.8  
     2.9      center = [NSNotificationCenter defaultCenter];
    2.10  
    2.11 @@ -183,9 +184,52 @@
    2.12      }
    2.13  }
    2.14  
    2.15 -- (BOOL) isToggledFullscreen
    2.16 +-(BOOL) canSetFullscreenState:(BOOL) state;
    2.17  {
    2.18 -    return isFullscreen;
    2.19 +}
    2.20 +
    2.21 +-(BOOL) setFullscreenState:(BOOL) state;
    2.22 +{
    2.23 +    SDL_Window *window = _data->window;
    2.24 +    NSWindow *nswindow = _data->nswindow;
    2.25 +    BOOL canSetState = NO;
    2.26 +
    2.27 +    /* Make sure we can support this fullscreen style */
    2.28 +    if (![nswindow respondsToSelector: @selector(toggleFullScreen:)]) {
    2.29 +        return NO;
    2.30 +    }
    2.31 +
    2.32 +    pendingFullscreenTransition = PENDING_TRANSITION_NONE;
    2.33 +
    2.34 +    /* We can enter new style fullscreen mode for "fullscreen desktop" */
    2.35 +    if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
    2.36 +        canSetState = YES;
    2.37 +    }
    2.38 +
    2.39 +    /* We can always leave new style fullscreen mode */
    2.40 +    if (!state && isFullscreen) {
    2.41 +        canSetState = YES;
    2.42 +    }
    2.43 +
    2.44 +    if (!canSetState) {
    2.45 +        return NO;
    2.46 +    }
    2.47 +
    2.48 +    if (state == isFullscreen) {
    2.49 +        return YES;
    2.50 +    }
    2.51 +
    2.52 +    if (inFullscreenTransition) {
    2.53 +        if (state) {
    2.54 +            pendingFullscreenTransition = PENDING_TRANSITION_ENTER_FULLSCREEN;
    2.55 +        } else {
    2.56 +            pendingFullscreenTransition = PENDING_TRANSITION_LEAVE_FULLSCREEN;
    2.57 +        }
    2.58 +        return YES;
    2.59 +    }
    2.60 +
    2.61 +    [nswindow performSelector: @selector(toggleFullScreen:) withObject:nswindow];
    2.62 +    return YES;
    2.63  }
    2.64  
    2.65  - (void)close
    2.66 @@ -215,8 +259,7 @@
    2.67          [window setDelegate:nil];
    2.68      }
    2.69  
    2.70 -    [window removeObserver:self
    2.71 -                forKeyPath:@"visible"];
    2.72 +    [window removeObserver:self forKeyPath:@"visible"];
    2.73  
    2.74      if ([window nextResponder] == self) {
    2.75          [window setNextResponder:nil];
    2.76 @@ -385,11 +428,18 @@
    2.77  - (void)windowDidEnterFullScreen:(NSNotification *)aNotification
    2.78  {
    2.79      inFullscreenTransition = NO;
    2.80 -    [self windowDidResize:aNotification];
    2.81 +
    2.82 +    if (pendingFullscreenTransition != PENDING_TRANSITION_NONE) {
    2.83 +        pendingFullscreenTransition = PENDING_TRANSITION_NONE;
    2.84 +        [self setFullscreenState:NO];
    2.85 +    } else {
    2.86 +        [self windowDidResize:aNotification];
    2.87 +    }
    2.88  }
    2.89  
    2.90  - (void)windowWillExitFullScreen:(NSNotification *)aNotification
    2.91  {
    2.92 +    isFullscreen = NO;
    2.93      inFullscreenTransition = YES;
    2.94  }
    2.95  
    2.96 @@ -401,9 +451,14 @@
    2.97      if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
    2.98          [nswindow setStyleMask:GetWindowStyle(window)];
    2.99      }
   2.100 -    isFullscreen = NO;
   2.101      inFullscreenTransition = NO;
   2.102 -    [self windowDidResize:aNotification];
   2.103 +
   2.104 +    if (pendingFullscreenTransition != PENDING_TRANSITION_NONE) {
   2.105 +        pendingFullscreenTransition = PENDING_TRANSITION_NONE;
   2.106 +        [self setFullscreenState:YES];
   2.107 +    } else {
   2.108 +        [self windowDidResize:aNotification];
   2.109 +    }
   2.110  }
   2.111  
   2.112  /* We'll respond to key events by doing nothing so we don't beep.
   2.113 @@ -1094,41 +1149,6 @@
   2.114      [pool release];
   2.115  }
   2.116  
   2.117 -static SDL_bool
   2.118 -Cocoa_CanToggleFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   2.119 -{
   2.120 -    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   2.121 -    NSWindow *nswindow = data->nswindow;
   2.122 -
   2.123 -    if (![nswindow respondsToSelector: @selector(toggleFullScreen:)]) {
   2.124 -        return SDL_FALSE;
   2.125 -    }
   2.126 -
   2.127 -    /* We can enter new style fullscreen mode for "fullscreen desktop" */
   2.128 -    if ((window->flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) {
   2.129 -        return SDL_TRUE;
   2.130 -    }
   2.131 -
   2.132 -    /* We can always leave new style fullscreen mode */
   2.133 -    if (!fullscreen && [data->listener isToggledFullscreen]) {
   2.134 -        return SDL_TRUE;
   2.135 -    }
   2.136 -
   2.137 -    /* Requesting a mode switched fullscreen mode */
   2.138 -    return SDL_FALSE;
   2.139 -}
   2.140 -
   2.141 -static void
   2.142 -Cocoa_SetWindowFullscreen_NewStyle(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   2.143 -{
   2.144 -    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   2.145 -    NSWindow *nswindow = data->nswindow;
   2.146 - 
   2.147 -    if (fullscreen != [data->listener isToggledFullscreen]) {
   2.148 -        [nswindow performSelector: @selector(toggleFullScreen:) withObject:nswindow];
   2.149 -    }
   2.150 -    ScheduleContextUpdates(data);
   2.151 -}
   2.152  
   2.153  static void
   2.154  Cocoa_SetWindowFullscreen_OldStyle(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   2.155 @@ -1212,10 +1232,9 @@
   2.156  Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   2.157  {
   2.158      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   2.159 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   2.160  
   2.161 -    if (Cocoa_CanToggleFullscreen(_this, window, display, fullscreen)) {
   2.162 -        Cocoa_SetWindowFullscreen_NewStyle(_this, window, display, fullscreen);
   2.163 -    } else {
   2.164 +    if (![data->listener setFullscreenState:(fullscreen ? YES : NO)]) {
   2.165          Cocoa_SetWindowFullscreen_OldStyle(_this, window, display, fullscreen);
   2.166      }
   2.167