fix(window): clamp the restored window size to the display on every startup

The only startup clamp to the display's usable bounds lived inside the
`currentDpiScale > 1.01f` HiDPI branch, so on a standard-DPI (100%) cold start it
never ran. A window size saved on a larger or since-disconnected monitor then
opened oversized / partly off-screen (title bar under the taskbar), since the
window is also re-centered at the saved size.

Hoist the clamp-to-usable-bounds + re-center out of the HiDPI branch so it runs
on every startup; the DPI multiply and scaled minimum-size stay HiDPI-only. It
resizes only when the clamp actually changes the size, so a window that fits its
display is left untouched — only genuinely-oversized ones clamp. Also log when a
persisted size is rejected on load/save (below the 1024x720 floor) so "my window
keeps resetting" reports become traceable.

Root-caused via a multi-agent audit of the window-size/DPI logic, which also
confirmed the save/restore DPI round-trip is otherwise correct (the restore-side
auto-scale multiply was mistaken by the investigators for a unit bug). Verified:
a 9999x9999 saved size now clamps to the display at scale 1.00 (previously the
clamp was skipped on a same-DPI cold start).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-09 23:23:39 -05:00
parent 13ff20d791
commit a9f0d544cc

View File

@@ -703,6 +703,9 @@ int main(int argc, char* argv[])
savedWinW = sw;
savedWinH = sh;
DEBUG_LOGF("Restored window size from settings: %dx%d\n", sw, sh);
} else if (sw > 0 || sh > 0) {
DEBUG_LOGF("Ignored saved window size %dx%d (below 1024x720 minimum) — using default %dx%d\n",
sw, sh, savedWinW, savedWinH);
}
}
}
@@ -1062,13 +1065,21 @@ int main(int argc, char* argv[])
// (point) coordinates and the framebuffer handles pixel density, so
// we must NOT multiply the window size by the content scale.
#ifndef __APPLE__
if (currentDpiScale > 1.01f) {
{
int curW = 0, curH = 0;
SDL_GetWindowSize(window, &curW, &curH);
int newW = (int)(curW * currentDpiScale);
int newH = (int)(curH * currentDpiScale);
int newW = curW, newH = curH;
if (currentDpiScale > 1.01f) {
// On a HiDPI display the saved size is logical px — scale it up to physical so the UI
// appears the same physical size as on a 100% display, and scale the minimum too.
newW = (int)(curW * currentDpiScale);
newH = (int)(curH * currentDpiScale);
SDL_SetWindowMinimumSize(window, (int)(1024 * currentDpiScale), (int)(720 * currentDpiScale));
}
// Clamp to the display's work area so the window fits on screen.
// Clamp to the current display's work area — runs on EVERY startup (this clamp used to live
// inside the HiDPI branch, so a size saved on a larger/disconnected monitor could open the
// window off-screen or bigger than the screen on a same-DPI cold start).
SDL_DisplayID did = SDL_GetDisplayForWindow(window);
if (did) {
SDL_Rect usable;
@@ -1078,13 +1089,12 @@ int main(int argc, char* argv[])
}
}
SDL_SetWindowSize(window, newW, newH);
SDL_SetWindowMinimumSize(window,
(int)(1024 * currentDpiScale),
(int)(720 * currentDpiScale));
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
DEBUG_LOGF("HiDPI startup: window resized %dx%d -> %dx%d (scale %.2f)\n",
curW, curH, newW, newH, currentDpiScale);
if (newW != curW || newH != curH) {
SDL_SetWindowSize(window, newW, newH);
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
DEBUG_LOGF("Startup: window fitted %dx%d -> %dx%d (scale %.2f)\n",
curW, curH, newW, newH, currentDpiScale);
}
}
#endif
@@ -1853,6 +1863,8 @@ int main(int argc, char* argv[])
if (logW >= 1024 && logH >= 720) {
app.settings()->setWindowSize(logW, logH);
app.settings()->save();
} else {
DEBUG_LOGF("Not saving window size %dx%d (logical, below 1024x720 minimum)\n", logW, logH);
}
}