Skip to content

Commit 3aff591

Browse files
authored
fix(desktop): reopen snapped, moved and minimized windows where they were on Windows (#65323)
1 parent b310a05 commit 3aff591

2 files changed

Lines changed: 107 additions & 10 deletions

File tree

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { describe, expect, test } from "bun:test"
2-
import { mkdtempSync, writeFileSync } from "node:fs"
1+
import { afterEach, describe, expect, test } from "bun:test"
2+
import { EventEmitter } from "node:events"
3+
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"
34
import { tmpdir } from "node:os"
45
import path from "node:path"
5-
import { readWindowState, resolveWindowState } from "./window-state"
6+
import type { Rectangle } from "electron"
7+
import { manageWindowState, readWindowState, resolveWindowState, type ManagedWindow } from "./window-state"
8+
9+
const originalPlatform = process.platform
10+
11+
afterEach(() => {
12+
Object.defineProperty(process, "platform", { value: originalPlatform })
13+
})
614

715
const defaults = { width: 1280, height: 800 }
816

@@ -108,4 +116,59 @@ describe("window state", () => {
108116
expect(readWindowState(file)).toBeUndefined()
109117
expect(readWindowState(path.join(dir, "missing.json"))).toBeUndefined()
110118
})
119+
120+
test.each([
121+
{ platform: "win32" as const, expected: "kept" },
122+
{ platform: "linux" as const, expected: "reset" },
123+
])("on $platform a window snapped to the primary display's left edge is $expected", ({ platform, expected }) => {
124+
Object.defineProperty(process, "platform", { value: platform })
125+
// A left-half snap at 100% scale: the invisible resize borders put the frame 7px past the left and bottom edges.
126+
const snapped = { x: -7, y: 0, width: 974, height: 1047, displayBounds: primary }
127+
const resolved = resolveWindowState(snapped, defaults, displays)
128+
129+
expect(resolved).toEqual(expected === "kept" ? snapped : { ...defaults, x: 0, y: 0, displayBounds: primary })
130+
})
131+
132+
test.each([
133+
{
134+
name: "a maximized window keeps the frame Windows restores it to",
135+
reading: { maximized: true, minimized: false, bounds: { x: 1912, y: -8, width: 2576, height: 1456 } },
136+
expected: { x: 2100, y: 200, width: 1100, height: 700, isMaximized: true, isFullScreen: false },
137+
},
138+
{
139+
name: "a minimized window keeps how it was shown",
140+
reading: { maximized: false, minimized: true, bounds: { x: -32000, y: -32000, width: 160, height: 28 } },
141+
expected: { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary },
142+
},
143+
])("on win32 $name", ({ reading, expected }) => {
144+
Object.defineProperty(process, "platform", { value: "win32" })
145+
const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-"))
146+
const file = path.join(dir, "window-state-a.json")
147+
const second = { x: 1920, y: 0, width: 2560, height: 1440 }
148+
const events = new EventEmitter()
149+
150+
const fake: ManagedWindow = {
151+
maximize: () => {},
152+
setFullScreen: () => {},
153+
isDestroyed: () => false,
154+
isMaximized: () => reading.maximized,
155+
isMinimized: () => reading.minimized,
156+
isFullScreen: () => false,
157+
getBounds: () => reading.bounds,
158+
getNormalBounds: () => ({ x: 2100, y: 200, width: 1100, height: 700 }),
159+
on: (event, listener) => events.on(event, listener),
160+
off: (event, listener) => events.off(event, listener),
161+
}
162+
163+
const initial = { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary }
164+
165+
const matching = (bounds: Rectangle) => (bounds.x >= second.x - 16 ? second : primary)
166+
167+
manageWindowState(fake, file, initial, { ...displays, matching })
168+
events.emit("close")
169+
events.emit("closed")
170+
171+
const written: unknown = JSON.parse(readFileSync(file, "utf8"))
172+
expect(written).toEqual(expected.displayBounds ? expected : { ...expected, displayBounds: second })
173+
})
111174
})

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export function resolveWindowState(
7676
}
7777
}
7878

79-
if (all.some((bounds) => within(saved, bounds))) return saved
79+
// Windows counts a window's invisible resize borders in its bounds, so a window snapped to a display's edge hangs
80+
// about 7px off it at 100% scale and more at higher scales.
81+
const slack = process.platform === "win32" ? 16 : 0
82+
83+
if (all.some((bounds) => within(saved, bounds, slack))) return saved
8084
const primary = displays.primary()
8185

8286
return {
@@ -100,9 +104,26 @@ export function readWindowState(file: string) {
100104
return Option.getOrUndefined(Schema.decodeUnknownOption(StateFile)(readFileSync(file, "utf8")))
101105
}
102106

107+
type ManagedEvent = "resize" | "move" | "close" | "closed"
108+
109+
export type ManagedWindow = Pick<
110+
BrowserWindow,
111+
| "maximize"
112+
| "setFullScreen"
113+
| "isDestroyed"
114+
| "isMaximized"
115+
| "isMinimized"
116+
| "isFullScreen"
117+
| "getBounds"
118+
| "getNormalBounds"
119+
> & {
120+
on(event: ManagedEvent, listener: () => void): void
121+
off(event: ManagedEvent, listener: () => void): void
122+
}
123+
103124
// Tracks the window until it closes and writes the final state then. Bounds are recorded while
104125
// the window is in its normal state so a maximized window restores to its previous size.
105-
export function manageWindowState(win: BrowserWindow, file: string, initial: WindowState, displays: Displays) {
126+
export function manageWindowState(win: ManagedWindow, file: string, initial: WindowState, displays: Displays) {
106127
const state = { ...initial }
107128

108129
if (state.isMaximized) win.maximize()
@@ -112,6 +133,9 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
112133

113134
const update = () => {
114135
if (win.isDestroyed()) return
136+
137+
// A minimized window on Windows reports no maximized state and a frame at -32000, so it keeps how it was shown.
138+
if (process.platform === "win32" && win.isMinimized()) return
115139
const bounds = win.getBounds()
116140

117141
if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) {
@@ -121,6 +145,16 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
121145
state.height = bounds.height
122146
}
123147

148+
// Windows keeps the frame a maximized window restores to and moves it with the window (Win+Shift+Arrow), which
149+
// the bounds read while restored can never see.
150+
if (process.platform === "win32" && win.isMaximized() && !win.isFullScreen()) {
151+
const normal = win.getNormalBounds()
152+
state.x = normal.x
153+
state.y = normal.y
154+
state.width = normal.width
155+
state.height = normal.height
156+
}
157+
124158
state.isMaximized = win.isMaximized()
125159
state.isFullScreen = win.isFullScreen()
126160
state.displayBounds = displays.matching(bounds)
@@ -152,12 +186,12 @@ function hasBounds(state: Partial<WindowState>): state is WindowState & Required
152186
return state.x !== undefined && state.y !== undefined && (state.width ?? 0) > 0 && (state.height ?? 0) > 0
153187
}
154188

155-
function within(state: WindowState & { x: number; y: number }, bounds: Rectangle) {
189+
function within(state: WindowState & { x: number; y: number }, bounds: Rectangle, slack = 0) {
156190
return (
157-
state.x >= bounds.x &&
158-
state.y >= bounds.y &&
159-
state.x + state.width <= bounds.x + bounds.width &&
160-
state.y + state.height <= bounds.y + bounds.height
191+
state.x >= bounds.x - slack &&
192+
state.y >= bounds.y - slack &&
193+
state.x + state.width <= bounds.x + bounds.width + slack &&
194+
state.y + state.height <= bounds.y + bounds.height + slack
161195
)
162196
}
163197

0 commit comments

Comments
 (0)