Mac: Send a window resize event when the window's backing scale factor changes.
authorAlex Szpakowski
Tue, 26 May 2015 21:51:47 -0300
changeset 9643413b222c4b64
parent 9642 cd237569d0f6
child 9644 2bd2c601df72
Mac: Send a window resize event when the window's backing scale factor changes.

The backing scale factor can change when the window moves between retina and non-retina displays.

The only other way to detect such a change is to compare the output of SDL_GL_GetDrawableSize or SDL_GetRendererOutputSize every frame, which is less than desirable, especially since the necessary app logic is likely already being executed when a window resize event is received.
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Tue May 26 20:49:27 2015 -0400
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Tue May 26 21:51:47 2015 -0300
     1.3 @@ -70,6 +70,7 @@
     1.4  -(void) windowDidDeminiaturize:(NSNotification *) aNotification;
     1.5  -(void) windowDidBecomeKey:(NSNotification *) aNotification;
     1.6  -(void) windowDidResignKey:(NSNotification *) aNotification;
     1.7 +-(void) windowDidChangeBackingProperties:(NSNotification *) aNotification;
     1.8  -(void) windowWillEnterFullScreen:(NSNotification *) aNotification;
     1.9  -(void) windowDidEnterFullScreen:(NSNotification *) aNotification;
    1.10  -(void) windowWillExitFullScreen:(NSNotification *) aNotification;
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue May 26 20:49:27 2015 -0400
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Tue May 26 21:51:47 2015 -0300
     2.3 @@ -255,6 +255,7 @@
     2.4          [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:window];
     2.5          [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:window];
     2.6          [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:window];
     2.7 +        [center addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window];
     2.8          [center addObserver:self selector:@selector(windowWillEnterFullScreen:) name:NSWindowWillEnterFullScreenNotification object:window];
     2.9          [center addObserver:self selector:@selector(windowDidEnterFullScreen:) name:NSWindowDidEnterFullScreenNotification object:window];
    2.10          [center addObserver:self selector:@selector(windowWillExitFullScreen:) name:NSWindowWillExitFullScreenNotification object:window];
    2.11 @@ -385,6 +386,7 @@
    2.12          [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:window];
    2.13          [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:window];
    2.14          [center removeObserver:self name:NSWindowDidResignKeyNotification object:window];
    2.15 +        [center removeObserver:self name:NSWindowDidChangeBackingPropertiesNotification object:window];
    2.16          [center removeObserver:self name:NSWindowWillEnterFullScreenNotification object:window];
    2.17          [center removeObserver:self name:NSWindowDidEnterFullScreenNotification object:window];
    2.18          [center removeObserver:self name:NSWindowWillExitFullScreenNotification object:window];
    2.19 @@ -584,6 +586,22 @@
    2.20      }
    2.21  }
    2.22  
    2.23 +- (void)windowDidChangeBackingProperties:(NSNotification *)aNotification
    2.24 +{
    2.25 +    NSNumber *oldscale = [[aNotification userInfo] objectForKey:NSBackingPropertyOldScaleFactorKey];
    2.26 +
    2.27 +    if (inFullscreenTransition) {
    2.28 +        return;
    2.29 +    }
    2.30 +
    2.31 +    if ([oldscale doubleValue] != [_data->nswindow backingScaleFactor]) {
    2.32 +        /* Force a resize event when the backing scale factor changes. */
    2.33 +        _data->window->w = 0;
    2.34 +        _data->window->h = 0;
    2.35 +        [self windowDidResize:aNotification];
    2.36 +    }
    2.37 +}
    2.38 +
    2.39  - (void)windowWillEnterFullScreen:(NSNotification *)aNotification
    2.40  {
    2.41      SDL_Window *window = _data->window;