[rcore][GLFW] Fix window ending up off-screen when larger than the monitor workarea

InitPlatform() centers the window with monitorX + (monitorWidth - width)/2,
with no check for the window being bigger than the monitor. If it is
(landscape window on a portrait primary monitor, or any window wider/taller
than the workarea), that division goes negative and the window gets placed
outside the virtual desktop. On Windows this doesn't just look off-center,
glfwSetWindowPos/GetWindowPos come back with garbage coordinates and the
window can end up invisible until you drag it back with win+shift+arrow.

SetWindowMonitor() already guards against this by anchoring to the workarea
origin when the window doesn't fit, so apply the same check here.

Repro: request InitWindow() with a size wider than the monitor and read
GetWindowPosition() right after. Before the fix this returns a huge bogus
x value instead of a small negative one. After the fix it's anchored to
the monitor origin. Verified on Windows 11 with a single monitor (couldn't
test the original portrait multi-monitor report from #6002 directly, don't
have that hardware, but the position math is the same code path).

Fixes #6002

Signed-off-by: Kyue <164024549+Gooh456@users.noreply.github.com>
This commit is contained in:
Kyue 2026-07-24 21:22:23 +01:00
commit 9bd4b30559

View file

@ -1791,12 +1791,28 @@ int InitPlatform(void)
// Center window into current monitor
#if defined(__APPLE__)
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.screen.width)/2;
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.screen.height)/2;
const int windowWidthForCentering = CORE.Window.screen.width;
const int windowHeightForCentering = CORE.Window.screen.height;
#else
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.render.width)/2;
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.render.height)/2;
const int windowWidthForCentering = CORE.Window.render.width;
const int windowHeightForCentering = CORE.Window.render.height;
#endif
// NOTE: If the window is as large or larger than the monitor workarea on either axis
// (e.g. a landscape window on a portrait primary monitor), centering it can push part
// of it past the edge of the virtual desktop, which some window managers (Windows in
// particular) then mishandle, causing the window to disappear or get misplaced entirely.
// Anchor it to the workarea origin instead, same as done in SetWindowMonitor()
if ((windowWidthForCentering >= monitorWidth) || (windowHeightForCentering >= monitorHeight))
{
CORE.Window.position.x = monitorX;
CORE.Window.position.y = monitorY;
}
else
{
CORE.Window.position.x = monitorX + (monitorWidth - windowWidthForCentering)/2;
CORE.Window.position.y = monitorY + (monitorHeight - windowHeightForCentering)/2;
}
SetWindowPosition(CORE.Window.position.x, CORE.Window.position.y);
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) MinimizeWindow();