Skip to content

Commit 9bd4b30

Browse files
committed
[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>
1 parent c15f321 commit 9bd4b30

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/platforms/rcore_desktop_glfw.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,12 +1791,28 @@ int InitPlatform(void)
17911791

17921792
// Center window into current monitor
17931793
#if defined(__APPLE__)
1794-
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.screen.width)/2;
1795-
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.screen.height)/2;
1794+
const int windowWidthForCentering = CORE.Window.screen.width;
1795+
const int windowHeightForCentering = CORE.Window.screen.height;
17961796
#else
1797-
CORE.Window.position.x = monitorX + (monitorWidth - CORE.Window.render.width)/2;
1798-
CORE.Window.position.y = monitorY + (monitorHeight - CORE.Window.render.height)/2;
1797+
const int windowWidthForCentering = CORE.Window.render.width;
1798+
const int windowHeightForCentering = CORE.Window.render.height;
17991799
#endif
1800+
1801+
// NOTE: If the window is as large or larger than the monitor workarea on either axis
1802+
// (e.g. a landscape window on a portrait primary monitor), centering it can push part
1803+
// of it past the edge of the virtual desktop, which some window managers (Windows in
1804+
// particular) then mishandle, causing the window to disappear or get misplaced entirely.
1805+
// Anchor it to the workarea origin instead, same as done in SetWindowMonitor()
1806+
if ((windowWidthForCentering >= monitorWidth) || (windowHeightForCentering >= monitorHeight))
1807+
{
1808+
CORE.Window.position.x = monitorX;
1809+
CORE.Window.position.y = monitorY;
1810+
}
1811+
else
1812+
{
1813+
CORE.Window.position.x = monitorX + (monitorWidth - windowWidthForCentering)/2;
1814+
CORE.Window.position.y = monitorY + (monitorHeight - windowHeightForCentering)/2;
1815+
}
18001816
SetWindowPosition(CORE.Window.position.x, CORE.Window.position.y);
18011817

18021818
if (FLAG_IS_SET(CORE.Window.flags, FLAG_WINDOW_MINIMIZED)) MinimizeWindow();

0 commit comments

Comments
 (0)