Fixed bug 2073 - Mac: window moves unexpectedly when exiting SDL_WINDOW_FULLSCREEN_DESKTOP mode
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Oct 2013 02:37:03 -0700
changeset 7873e88af17231bd
parent 7872 ca36ab2a8179
child 7874 446846276cb0
Fixed bug 2073 - Mac: window moves unexpectedly when exiting SDL_WINDOW_FULLSCREEN_DESKTOP mode

Alex Szpakowski

In Mac OS X, when SDL_SetWindowFullscreen(window, 0) is called on a window which was in SDL_WINDOW_FULLSCREEN_DESKTOP mode, its original size is restored but its position is moved to the bottom of the screen.

I tracked down the issue to these two lines: http://hg.libsdl.org/SDL/file/66b5b8446275/src/video/cocoa/SDL_cocoawindow.m#l1034

I believe [nswindow setFrameOrigin:rect.origin] implicitly calls [nswindow constrainFrameRect:rect toScreen:screen], which will attempt to constrain the window to the screen, but at that point the window size is still full-screen rather than the restored window size, so the constrainFrameRect function operates on the wrong window size.

https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/classes/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow/constrainFrameRect:toScreen:

I resolved the issue by swapping the order of the function calls, like so:
[nswindow setContentSize:rect.size];
[nswindow setFrameOrigin:rect.origin];
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Oct 21 02:32:34 2013 -0700
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Oct 21 02:37:03 2013 -0700
     1.3 @@ -1058,8 +1058,8 @@
     1.4      }
     1.5  
     1.6      s_moveHack = 0;
     1.7 +    [nswindow setContentSize:rect.size];
     1.8      [nswindow setFrameOrigin:rect.origin];
     1.9 -    [nswindow setContentSize:rect.size];
    1.10      s_moveHack = SDL_GetTicks();
    1.11  
    1.12      /* When the window style changes the title is cleared */