Skip to content

Commit da464e9

Browse files
committed
win32: Keep the window on the last maximized display when leaving fullscreen
A window may have been maximized by dragging it to the top of another display, in which case the floating position may be out-of-date. If the window is being restored to maximized, and the maximized and floating position are on different displays, try to center the window on the maximized display for restoration, which mimics native Windows behavior.
1 parent bf01cc8 commit da464e9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/video/windows/SDL_windowswindow.c

+29
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,35 @@ SDL_FullscreenResult WIN_SetWindowFullscreen(SDL_VideoDevice *_this, SDL_Window
13911391
&w, &h,
13921392
SDL_WINDOWRECT_FLOATING);
13931393
data->windowed_mode_was_maximized = false;
1394+
1395+
/* A window may have been maximized by dragging it to the top of another display, in which case the floating
1396+
* position may be out-of-date. If the window is being restored to maximized, and the maximized and floating
1397+
* position are on different displays, try to center the window on the maximized display for restoration, which
1398+
* mimics native Windows behavior.
1399+
*/
1400+
if (enterMaximized) {
1401+
const SDL_Point windowed_point = { window->windowed.x, window->windowed.y };
1402+
const SDL_Point floating_point = { window->floating.x, window->floating.y };
1403+
const SDL_DisplayID floating_display = SDL_GetDisplayForPoint(&floating_point);
1404+
const SDL_DisplayID windowed_display = SDL_GetDisplayForPoint(&windowed_point);
1405+
1406+
if (floating_display != windowed_display) {
1407+
SDL_Rect bounds;
1408+
1409+
SDL_zero(bounds);
1410+
SDL_GetDisplayUsableBounds(windowed_display, &bounds);
1411+
if (w < bounds.w) {
1412+
x = bounds.x + (bounds.w - w) / 2;
1413+
} else {
1414+
x = bounds.x;
1415+
}
1416+
if (h < bounds.h) {
1417+
y = bounds.y + (bounds.h - h) / 2;
1418+
} else {
1419+
y = bounds.y;
1420+
}
1421+
}
1422+
}
13941423
}
13951424

13961425
/* Always reset the window to the base floating size before possibly re-applying the maximized state,

0 commit comments

Comments
 (0)