mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(desktop): reopen snapped, moved and minimized windows where they were on Windows (#65323)
This commit is contained in:
parent
b310a05b54
commit
3aff591323
2 changed files with 107 additions and 10 deletions
|
|
@ -1,8 +1,16 @@
|
|||
import { describe, expect, test } from "bun:test"
|
||||
import { mkdtempSync, writeFileSync } from "node:fs"
|
||||
import { afterEach, describe, expect, test } from "bun:test"
|
||||
import { EventEmitter } from "node:events"
|
||||
import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import path from "node:path"
|
||||
import { readWindowState, resolveWindowState } from "./window-state"
|
||||
import type { Rectangle } from "electron"
|
||||
import { manageWindowState, readWindowState, resolveWindowState, type ManagedWindow } from "./window-state"
|
||||
|
||||
const originalPlatform = process.platform
|
||||
|
||||
afterEach(() => {
|
||||
Object.defineProperty(process, "platform", { value: originalPlatform })
|
||||
})
|
||||
|
||||
const defaults = { width: 1280, height: 800 }
|
||||
|
||||
|
|
@ -108,4 +116,59 @@ describe("window state", () => {
|
|||
expect(readWindowState(file)).toBeUndefined()
|
||||
expect(readWindowState(path.join(dir, "missing.json"))).toBeUndefined()
|
||||
})
|
||||
|
||||
test.each([
|
||||
{ platform: "win32" as const, expected: "kept" },
|
||||
{ platform: "linux" as const, expected: "reset" },
|
||||
])("on $platform a window snapped to the primary display's left edge is $expected", ({ platform, expected }) => {
|
||||
Object.defineProperty(process, "platform", { value: platform })
|
||||
// A left-half snap at 100% scale: the invisible resize borders put the frame 7px past the left and bottom edges.
|
||||
const snapped = { x: -7, y: 0, width: 974, height: 1047, displayBounds: primary }
|
||||
const resolved = resolveWindowState(snapped, defaults, displays)
|
||||
|
||||
expect(resolved).toEqual(expected === "kept" ? snapped : { ...defaults, x: 0, y: 0, displayBounds: primary })
|
||||
})
|
||||
|
||||
test.each([
|
||||
{
|
||||
name: "a maximized window keeps the frame Windows restores it to",
|
||||
reading: { maximized: true, minimized: false, bounds: { x: 1912, y: -8, width: 2576, height: 1456 } },
|
||||
expected: { x: 2100, y: 200, width: 1100, height: 700, isMaximized: true, isFullScreen: false },
|
||||
},
|
||||
{
|
||||
name: "a minimized window keeps how it was shown",
|
||||
reading: { maximized: false, minimized: true, bounds: { x: -32000, y: -32000, width: 160, height: 28 } },
|
||||
expected: { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary },
|
||||
},
|
||||
])("on win32 $name", ({ reading, expected }) => {
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-"))
|
||||
const file = path.join(dir, "window-state-a.json")
|
||||
const second = { x: 1920, y: 0, width: 2560, height: 1440 }
|
||||
const events = new EventEmitter()
|
||||
|
||||
const fake: ManagedWindow = {
|
||||
maximize: () => {},
|
||||
setFullScreen: () => {},
|
||||
isDestroyed: () => false,
|
||||
isMaximized: () => reading.maximized,
|
||||
isMinimized: () => reading.minimized,
|
||||
isFullScreen: () => false,
|
||||
getBounds: () => reading.bounds,
|
||||
getNormalBounds: () => ({ x: 2100, y: 200, width: 1100, height: 700 }),
|
||||
on: (event, listener) => events.on(event, listener),
|
||||
off: (event, listener) => events.off(event, listener),
|
||||
}
|
||||
|
||||
const initial = { x: 100, y: 100, width: 1000, height: 600, isMaximized: true, displayBounds: primary }
|
||||
|
||||
const matching = (bounds: Rectangle) => (bounds.x >= second.x - 16 ? second : primary)
|
||||
|
||||
manageWindowState(fake, file, initial, { ...displays, matching })
|
||||
events.emit("close")
|
||||
events.emit("closed")
|
||||
|
||||
const written: unknown = JSON.parse(readFileSync(file, "utf8"))
|
||||
expect(written).toEqual(expected.displayBounds ? expected : { ...expected, displayBounds: second })
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -76,7 +76,11 @@ export function resolveWindowState(
|
|||
}
|
||||
}
|
||||
|
||||
if (all.some((bounds) => within(saved, bounds))) return saved
|
||||
// Windows counts a window's invisible resize borders in its bounds, so a window snapped to a display's edge hangs
|
||||
// about 7px off it at 100% scale and more at higher scales.
|
||||
const slack = process.platform === "win32" ? 16 : 0
|
||||
|
||||
if (all.some((bounds) => within(saved, bounds, slack))) return saved
|
||||
const primary = displays.primary()
|
||||
|
||||
return {
|
||||
|
|
@ -100,9 +104,26 @@ export function readWindowState(file: string) {
|
|||
return Option.getOrUndefined(Schema.decodeUnknownOption(StateFile)(readFileSync(file, "utf8")))
|
||||
}
|
||||
|
||||
type ManagedEvent = "resize" | "move" | "close" | "closed"
|
||||
|
||||
export type ManagedWindow = Pick<
|
||||
BrowserWindow,
|
||||
| "maximize"
|
||||
| "setFullScreen"
|
||||
| "isDestroyed"
|
||||
| "isMaximized"
|
||||
| "isMinimized"
|
||||
| "isFullScreen"
|
||||
| "getBounds"
|
||||
| "getNormalBounds"
|
||||
> & {
|
||||
on(event: ManagedEvent, listener: () => void): void
|
||||
off(event: ManagedEvent, listener: () => void): void
|
||||
}
|
||||
|
||||
// Tracks the window until it closes and writes the final state then. Bounds are recorded while
|
||||
// the window is in its normal state so a maximized window restores to its previous size.
|
||||
export function manageWindowState(win: BrowserWindow, file: string, initial: WindowState, displays: Displays) {
|
||||
export function manageWindowState(win: ManagedWindow, file: string, initial: WindowState, displays: Displays) {
|
||||
const state = { ...initial }
|
||||
|
||||
if (state.isMaximized) win.maximize()
|
||||
|
|
@ -112,6 +133,9 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
|
|||
|
||||
const update = () => {
|
||||
if (win.isDestroyed()) return
|
||||
|
||||
// A minimized window on Windows reports no maximized state and a frame at -32000, so it keeps how it was shown.
|
||||
if (process.platform === "win32" && win.isMinimized()) return
|
||||
const bounds = win.getBounds()
|
||||
|
||||
if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) {
|
||||
|
|
@ -121,6 +145,16 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win
|
|||
state.height = bounds.height
|
||||
}
|
||||
|
||||
// Windows keeps the frame a maximized window restores to and moves it with the window (Win+Shift+Arrow), which
|
||||
// the bounds read while restored can never see.
|
||||
if (process.platform === "win32" && win.isMaximized() && !win.isFullScreen()) {
|
||||
const normal = win.getNormalBounds()
|
||||
state.x = normal.x
|
||||
state.y = normal.y
|
||||
state.width = normal.width
|
||||
state.height = normal.height
|
||||
}
|
||||
|
||||
state.isMaximized = win.isMaximized()
|
||||
state.isFullScreen = win.isFullScreen()
|
||||
state.displayBounds = displays.matching(bounds)
|
||||
|
|
@ -152,12 +186,12 @@ function hasBounds(state: Partial<WindowState>): state is WindowState & Required
|
|||
return state.x !== undefined && state.y !== undefined && (state.width ?? 0) > 0 && (state.height ?? 0) > 0
|
||||
}
|
||||
|
||||
function within(state: WindowState & { x: number; y: number }, bounds: Rectangle) {
|
||||
function within(state: WindowState & { x: number; y: number }, bounds: Rectangle, slack = 0) {
|
||||
return (
|
||||
state.x >= bounds.x &&
|
||||
state.y >= bounds.y &&
|
||||
state.x + state.width <= bounds.x + bounds.width &&
|
||||
state.y + state.height <= bounds.y + bounds.height
|
||||
state.x >= bounds.x - slack &&
|
||||
state.y >= bounds.y - slack &&
|
||||
state.x + state.width <= bounds.x + bounds.width + slack &&
|
||||
state.y + state.height <= bounds.y + bounds.height + slack
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue