Skip to content

The agent that grows with you

fix(desktop): recover stale fullscreen normal bounds

Huklac5d6b96f005d
diff --git a/apps/desktop/electron/window-state.ts b/apps/desktop/electron/window-state.ts
index 2ece182bd98..364982929bc 100644
--- a/apps/desktop/electron/window-state.ts
+++ b/apps/desktop/electron/window-state.ts
@@ -13,6 +13,15 @@ const MIN_HEIGHT = 620
// a saved position, so the title bar stays grabbable after a monitor unplugs.
const MIN_VISIBLE = 48
+// A stale normal-bounds snapshot can be almost identical to the work area while
+// isMaximized=false (for example after a DPI/display transition). Restoring that
+// verbatim leaves Windows with no meaningful restore-down size. Treat a window
+// that covers at least 90% of both work-area dimensions and starts near that
+// work area's origin as the stuck-fullscreen shape, then recover to a centered
+// 80% windowed size.
+const STALE_FULLSCREEN_RATIO = 0.9
+const RECOVERED_WINDOW_RATIO = 0.8
+
const finite = v => typeof v === 'number' && Number.isFinite(v)
const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi))
@@ -99,7 +108,36 @@ function firstLaunchSize(workArea: WorkArea): WindowOptions {
}
}
-function computeWindowOptions(state: WindowOptions, displays): WindowOptions {
+function staleFullscreenWorkArea(state, displays) {
+ if (
+ !state ||
+ state.isMaximized ||
+ !finite(state.x) ||
+ !finite(state.y) ||
+ !finite(state.width) ||
+ !finite(state.height) ||
+ !Array.isArray(displays)
+ ) {
+ return null
+ }
+
+ return (
+ displays.find(({ workArea: a } = {}) => {
+ if (!a || !finite(a.x) || !finite(a.y) || !finite(a.width) || !finite(a.height)) {
+ return false
+ }
+
+ const nearOriginX = Math.abs(state.x - a.x) <= a.width * (1 - STALE_FULLSCREEN_RATIO)
+ const nearOriginY = Math.abs(state.y - a.y) <= a.height * (1 - STALE_FULLSCREEN_RATIO)
+ const fillsWidth = state.width >= a.width * STALE_FULLSCREEN_RATIO
+ const fillsHeight = state.height >= a.height * STALE_FULLSCREEN_RATIO
+
+ return nearOriginX && nearOriginY && fillsWidth && fillsHeight
+ })?.workArea ?? null
+ )
+}
+
+function computeWindowOptions(state: WindowOptions, displays, platform = process.platform): WindowOptions {
const opts: WindowOptions = { width: state.width, height: state.height }
const cap = (Array.isArray(displays) ? displays : []).reduce(
@@ -115,6 +153,17 @@ function computeWindowOptions(state: WindowOptions, displays): WindowOptions {
opts.height = clamp(opts.height, MIN_HEIGHT, cap.height)
}
+ // The motivating restore-down failure is Windows-specific. Keeping the
+ // geometry heuristic there avoids rewriting deliberate near-fullscreen
+ // layouts from tiling WMs or user placement on macOS/Linux. This early return
+ // intentionally omits stale x/y so Electron centers the recovered window.
+ const staleWorkArea = platform === 'win32' ? staleFullscreenWorkArea(state, displays) : null
+ if (staleWorkArea) {
+ opts.width = clamp(Math.round(staleWorkArea.width * RECOVERED_WINDOW_RATIO), MIN_WIDTH, staleWorkArea.width)
+ opts.height = clamp(Math.round(staleWorkArea.height * RECOVERED_WINDOW_RATIO), MIN_HEIGHT, staleWorkArea.height)
+ return opts
+ }
+
if (finite(state.x) && finite(state.y)) {
const workArea = matchingWorkArea({ x: state.x, y: state.y, width: opts.width, height: opts.height }, displays)