Skip to content

The agent that grows with you

fix(desktop): clamp restored window bounds to work area

KeelTrace35b94ca208a6
diff --git a/apps/desktop/electron/window-state.test.ts b/apps/desktop/electron/window-state.test.ts
index 6a5100d2d60..1109a0d8fec 100644
--- a/apps/desktop/electron/window-state.test.ts
+++ b/apps/desktop/electron/window-state.test.ts
@@ -94,8 +94,22 @@ test('computeWindowOptions falls back to defaults with no saved state', () => {
})
test('computeWindowOptions restores an on-screen position', () => {
- const saved = sanitizeWindowState({ x: 200, y: 150, width: 1400, height: 900 })
- assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 1400, height: 900, x: 200, y: 150 })
+ const saved = sanitizeWindowState({ x: 200, y: 100, width: 1400, height: 900 })
+ assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 1400, height: 900, x: 200, y: 100 })
+})
+
+test('computeWindowOptions clamps a trusted saved position fully inside its display work area', () => {
+ const saved = sanitizeWindowState({ x: -102, y: 175, width: 960, height: 1032 })
+ assert.deepEqual(computeWindowOptions(saved, PRIMARY), { width: 960, height: 1032, x: 0, y: 8 })
+})
+
+test('computeWindowOptions caps a positioned window to the display it overlaps', () => {
+ const dual = [
+ { workArea: { x: 0, y: 0, width: 2560, height: 1400 } },
+ { workArea: { x: 2560, y: 0, width: 1366, height: 728 } }
+ ]
+ const saved = sanitizeWindowState({ x: 2700, y: 100, width: 1400, height: 900 })
+ assert.deepEqual(computeWindowOptions(saved, dual), { width: 1366, height: 728, x: 2560, y: 0 })
})
test('computeWindowOptions keeps the size but drops an off-screen position', () => {
@@ -186,4 +200,4 @@ test('a window that never emits moved/resized still saves its geometry', () => {
win.emit('move')
assert.ok(saves > 0)
-})
+})
\ No newline at end of file
diff --git a/apps/desktop/electron/window-state.ts b/apps/desktop/electron/window-state.ts
index 78c53644492..a72e28e5d99 100644
--- a/apps/desktop/electron/window-state.ts
+++ b/apps/desktop/electron/window-state.ts
@@ -50,23 +50,42 @@ function sanitizeWindowState(raw?: any): SanitizedWindowState | null {
return state
}
-// True when `bounds` overlaps some display's work area by ≥ MIN_VISIBLE on both
-// axes. `displays` is Electron's screen.getAllDisplays() shape.
-function onScreen(bounds, displays) {
+// Return the work area with the largest meaningful overlap with `bounds`.
+// `displays` is Electron's screen.getAllDisplays() shape. A small sliver does
+// not count: the saved position is only trusted when at least MIN_VISIBLE is
+// reachable on both axes.
+function matchingWorkArea(bounds, displays) {
if (!Array.isArray(displays)) {
- return false
+ return null
}
- return displays.some(({ workArea: a } = {}) => {
+ let best = null
+ let bestArea = 0
+ for (const { workArea: a } of displays) {
if (!a) {
- return false
+ continue
}
const x = Math.min(bounds.x + bounds.width, a.x + a.width) - Math.max(bounds.x, a.x)
const y = Math.min(bounds.y + bounds.height, a.y + a.height) - Math.max(bounds.y, a.y)
+ if (x < MIN_VISIBLE || y < MIN_VISIBLE) {
+ continue
+ }
+
+ const area = x * y
+ if (area > bestArea) {
+ best = a
+ bestArea = area
+ }
+ }
- return x >= MIN_VISIBLE && y >= MIN_VISIBLE
- })
+ return best
+}
+
+// True when `bounds` overlaps some display's work area by ≥ MIN_VISIBLE on both
+// axes.
+function onScreen(bounds, displays) {
+ return matchingWorkArea(bounds, displays) !== null
}
interface WindowOptions {
@@ -78,8 +97,9 @@ interface WindowOptions {
// Sanitized state (or null) → BrowserWindow size/position options. Always sets
// width/height, capped to the largest current display so a size saved on a
-// since-disconnected bigger monitor can't exceed any screen the user now has.
-// Sets x/y only when still on-screen; otherwise Electron centers the window.
+// since-disconnected bigger monitor can't exceed every screen the user now has.
+// A trusted saved position is then capped and clamped to the work area it
+// meaningfully overlaps; otherwise Electron centers the window.
function computeWindowOptions(state, displays): WindowOptions {
const opts: WindowOptions = {
width: finite(state?.width) ? state.width : DEFAULT_WIDTH,
@@ -99,14 +119,17 @@ function computeWindowOptions(state, displays): WindowOptions {
opts.height = clamp(opts.height, MIN_HEIGHT, cap.height)
}
- if (
- state &&
- finite(state.x) &&
- finite(state.y) &&
- onScreen({ x: state.x, y: state.y, width: opts.width, height: opts.height }, displays)
- ) {
- opts.x = state.x
- opts.y = state.y
+ if (state && finite(state.x) && finite(state.y)) {
+ const workArea = matchingWorkArea(
+ { x: state.x, y: state.y, width: opts.width, height: opts.height },
+ displays
+ )
+ if (workArea) {
+ opts.width = clamp(opts.width, MIN_WIDTH, workArea.width)
+ opts.height = clamp(opts.height, MIN_HEIGHT, workArea.height)
+ opts.x = clamp(state.x, workArea.x, workArea.x + workArea.width - opts.width)
+ opts.y = clamp(state.y, workArea.y, workArea.y + workArea.height - opts.height)
+ }
}
return opts
@@ -166,4 +189,4 @@ export {
MIN_WIDTH,
onScreen,
sanitizeWindowState
-}
+}
\ No newline at end of file