Skip to content

Commit 1456e7d

Browse files
committed
fix(desktop): restore edge-snapped window bounds
1 parent d814879 commit 1456e7d

2 files changed

Lines changed: 85 additions & 30 deletions

File tree

‎packages/desktop/src/main/windows/window-state.test.ts‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,45 @@ describe("window state", () => {
2020
expect(resolveWindowState(saved, defaults, displays)).toEqual(saved)
2121
})
2222

23+
test("keeps edge-snapped bounds within the platform border tolerance", () => {
24+
const saved = { x: -7, y: -7, width: 1000, height: 700, displayBounds: primary }
25+
expect(resolveWindowState(saved, defaults, displays)).toEqual(saved)
26+
})
27+
28+
test("falls back when the saved window is far outside a display", () => {
29+
const saved = { x: -200, y: 100, width: 1000, height: 700, displayBounds: primary }
30+
expect(resolveWindowState(saved, defaults, displays)).toEqual({ ...defaults, x: 0, y: 0, displayBounds: primary })
31+
})
32+
2333
test("falls back to the default size on the primary display when the saved display is gone", () => {
2434
const saved = { x: 5000, y: 100, width: 1000, height: 700, displayBounds: { x: 4480, y: 0, width: 1920, height: 1080 } }
2535
expect(resolveWindowState(saved, defaults, displays)).toEqual({ ...defaults, x: 0, y: 0, displayBounds: primary })
2636
})
2737

2838
test("uses defaults without a position when nothing usable was saved", () => {
2939
expect(resolveWindowState(undefined, defaults, displays)).toEqual(defaults)
30-
expect(resolveWindowState({ width: 0, height: -1 }, defaults, displays)).toEqual(defaults)
31-
expect(resolveWindowState("junk", defaults, displays)).toEqual(defaults)
3240
})
3341

3442
test("keeps a maximized flag even without bounds", () => {
3543
expect(resolveWindowState({ isMaximized: true }, defaults, displays)).toEqual({ ...defaults, isMaximized: true })
3644
})
3745

46+
test("keeps maximized and fullscreen flags when saved bounds are unusable", () => {
47+
const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-"))
48+
const file = path.join(dir, "window-state-a.json")
49+
50+
const cases = [
51+
['{"isMaximized":true,"width":0,"height":0}', { ...defaults, isMaximized: true }],
52+
['{"isMaximized":true,"x":10.5,"y":0,"width":1000,"height":700}', { ...defaults, isMaximized: true }],
53+
['{"isFullScreen":true,"x":null,"y":null,"width":1000,"height":700}', { ...defaults, isFullScreen: true }],
54+
] as const
55+
56+
for (const [contents, expected] of cases) {
57+
writeFileSync(file, contents)
58+
expect(resolveWindowState(readWindowState(file), defaults, displays)).toEqual(expected)
59+
}
60+
})
61+
3862
test("reads the file electron-window-state wrote and ignores an unreadable one", () => {
3963
const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-"))
4064
const file = path.join(dir, "window-state-a.json")
@@ -51,6 +75,8 @@ describe("window state", () => {
5175
isMaximized: false,
5276
isFullScreen: false,
5377
})
78+
writeFileSync(file, '{"width":0,"height":-1}')
79+
expect(readWindowState(file)).toBeUndefined()
5480
writeFileSync(file, "{oops")
5581
expect(readWindowState(file)).toBeUndefined()
5682
expect(readWindowState(path.join(dir, "missing.json"))).toBeUndefined()

‎packages/desktop/src/main/windows/window-state.ts‎

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"
22
import path from "node:path"
33
import type { BrowserWindow, Rectangle } from "electron"
4+
import { Option, Schema } from "effect"
45

56
// Remembers a window's bounds and maximized / fullscreen flags across launches in the same JSON
67
// file electron-window-state wrote, so existing profiles restore where they were.
78

9+
const Bounds = Schema.Struct({
10+
x: Schema.Int,
11+
y: Schema.Int,
12+
width: Schema.Int,
13+
height: Schema.Int,
14+
})
15+
16+
const SavedWindowState = Schema.Struct({
17+
x: Schema.optional(Schema.Int),
18+
y: Schema.optional(Schema.Int),
19+
width: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
20+
height: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
21+
displayBounds: Schema.optional(Bounds),
22+
isMaximized: Schema.optional(Schema.Boolean),
23+
isFullScreen: Schema.optional(Schema.Boolean),
24+
})
25+
26+
const SavedWindowFlags = Schema.Struct({
27+
isMaximized: Schema.optional(Schema.Boolean),
28+
isFullScreen: Schema.optional(Schema.Boolean),
29+
})
30+
31+
export type SavedWindowState = typeof SavedWindowState.Type
32+
833
export type WindowState = {
934
x?: number
1035
y?: number
@@ -24,21 +49,19 @@ export type Displays = {
2449
// Bounds are only trusted when they still fit on one of the current displays; otherwise the window
2550
// falls back to the default size on the primary display. A saved maximized or fullscreen state
2651
// without bounds is kept so manage() can restore it.
27-
export function resolveWindowState(saved: unknown, defaults: { width: number; height: number }, displays: Displays) {
28-
const state = isState(saved) ? saved : undefined
29-
30-
if (!state) return { width: defaults.width, height: defaults.height } satisfies WindowState
52+
export function resolveWindowState(saved: SavedWindowState | undefined, defaults: { width: number; height: number }, displays: Displays) {
53+
if (!saved) return { width: defaults.width, height: defaults.height } satisfies WindowState
3154

32-
if (!hasBounds(state)) {
33-
return state.isMaximized || state.isFullScreen
34-
? ({ ...state, width: defaults.width, height: defaults.height } satisfies WindowState)
55+
if (!hasBounds(saved)) {
56+
return saved.isMaximized || saved.isFullScreen
57+
? ({ ...saved, width: defaults.width, height: defaults.height } satisfies WindowState)
3558
: ({ width: defaults.width, height: defaults.height } satisfies WindowState)
3659
}
3760

38-
if (!state.displayBounds) return state
39-
const visible = displays.all().some((bounds) => within(state, bounds))
61+
if (!saved.displayBounds) return saved
62+
const visible = displays.all().some((bounds) => within(saved, bounds))
4063

41-
if (visible) return state
64+
if (visible) return saved
4265

4366
return {
4467
width: defaults.width,
@@ -53,11 +76,24 @@ export function windowStateFile(id: string) {
5376
return `window-state-${id.replace(/[^a-zA-Z0-9._-]/g, "-")}.json`
5477
}
5578

56-
export function readWindowState(file: string): unknown {
79+
const decodeWindowState = Schema.decodeUnknownOption(Schema.fromJsonString(SavedWindowState))
80+
81+
const decodeWindowFlags = Schema.decodeUnknownOption(Schema.fromJsonString(SavedWindowFlags))
82+
83+
export function readWindowState(file: string): SavedWindowState | undefined {
5784
if (!existsSync(file)) return undefined
5885

5986
try {
60-
return JSON.parse(readFileSync(file, "utf8"))
87+
const contents = readFileSync(file, "utf8")
88+
const state = Option.getOrUndefined(decodeWindowState(contents))
89+
90+
if (state) return state
91+
92+
// Keep the flags when only the bounds are unusable; resolveWindowState already falls back
93+
// to the default size for those, and the window should still restore maximized/fullscreen.
94+
const flags = Option.getOrUndefined(decodeWindowFlags(contents))
95+
96+
return flags && (flags.isMaximized !== undefined || flags.isFullScreen !== undefined) ? flags : undefined
6197
} catch {
6298
return undefined
6399
}
@@ -111,26 +147,19 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
111147
win.on("closed", closed)
112148
}
113149

114-
function isState(value: unknown): value is WindowState {
115-
return typeof value === "object" && value !== null
150+
function hasBounds(state: SavedWindowState): state is WindowState & { x: number; y: number } {
151+
return state.x !== undefined && state.y !== undefined && state.width !== undefined && state.height !== undefined
116152
}
117153

118-
function hasBounds(state: WindowState): state is WindowState & Required<Pick<WindowState, "x" | "y">> {
119-
return (
120-
Number.isInteger(state.x) &&
121-
Number.isInteger(state.y) &&
122-
Number.isInteger(state.width) &&
123-
state.width > 0 &&
124-
Number.isInteger(state.height) &&
125-
state.height > 0
126-
)
127-
}
154+
// Windows reports a window snapped to a screen edge slightly outside the display because of its
155+
// invisible resize borders. Keep those saved bounds instead of falling back to the default window.
156+
const EDGE_TOLERANCE = 16
128157

129158
function within(state: WindowState & { x: number; y: number }, bounds: Rectangle) {
130159
return (
131-
state.x >= bounds.x &&
132-
state.y >= bounds.y &&
133-
state.x + state.width <= bounds.x + bounds.width &&
134-
state.y + state.height <= bounds.y + bounds.height
160+
state.x >= bounds.x - EDGE_TOLERANCE &&
161+
state.y >= bounds.y - EDGE_TOLERANCE &&
162+
state.x + state.width <= bounds.x + bounds.width + EDGE_TOLERANCE &&
163+
state.y + state.height <= bounds.y + bounds.height + EDGE_TOLERANCE
135164
)
136165
}

0 commit comments

Comments
 (0)