diff --git a/packages/desktop/src/main/plugin/embeds.ts b/packages/desktop/src/main/plugin/embeds.ts index d0cc439d906..a5dfe2cd07f 100644 --- a/packages/desktop/src/main/plugin/embeds.ts +++ b/packages/desktop/src/main/plugin/embeds.ts @@ -127,7 +127,11 @@ export function createEmbeds() { if (!resizing.has(window)) { resizing.add(window) - window.on("resize", () => entries.forEach((entry) => entry.window === window && apply(entry))) + const refresh = () => entries.forEach((entry) => entry.window === window && apply(entry)) + window.on("resize", refresh) + window.on("move", refresh) + screen.on("display-metrics-changed", refresh) + window.once("closed", () => screen.off("display-metrics-changed", refresh)) } const corners = [new ImageView(), new ImageView()] diff --git a/packages/desktop/src/main/windows/early.ts b/packages/desktop/src/main/windows/early.ts index 8864c770188..99b750ca65d 100644 --- a/packages/desktop/src/main/windows/early.ts +++ b/packages/desktop/src/main/windows/early.ts @@ -11,7 +11,13 @@ import { platformFrame, storedBackgroundColor, storedZoomLevel, tone } from "./d import { registerRendererProtocol } from "./protocol" import { loadWindow } from "./scheme" import { allowRendererPermissions, wireNavigationPolicy, wireRendererHeaders } from "./security" -import { manageWindowState, readWindowState, resolveWindowState, windowStateFile, type WindowState } from "./window-state" +import { + manageWindowState, + readWindowState, + resolveWindowState, + windowStateFile, + type WindowState, +} from "./window-state" export type EarlyWindow = { id: string @@ -43,12 +49,16 @@ export function createEarlyWindow() { const zoom = storedZoomLevel() const icons = app.isPackaged ? path.join(process.resourcesPath, "icons") : path.join(root, "../../resources/icons") + // On Windows, maximize() on a hidden window shows it with SW_SHOWMAXIMIZED, so it never paints at + // its normal bounds first. macOS does not reliably enter fullscreen while hidden, so it shows first. + const deferReveal = process.platform === "win32" && state.isMaximized + const win = new BrowserWindow({ x: state.x, y: state.y, width: state.width, height: state.height, - show: true, + show: !deferReveal, autoHideMenuBar: true, title: "OpenCode", icon: path.join(icons, `icon.${process.platform === "win32" ? "ico" : "png"}`), @@ -66,6 +76,8 @@ export function createEarlyWindow() { }) manageWindowState(win, file, state, displays) + + if (deferReveal) win.show() // Closing the only window before the rest of the app has adopted it is a quit. win.once("closed", () => { if (pending?.win !== win) return diff --git a/packages/desktop/src/main/windows/window-state.test.ts b/packages/desktop/src/main/windows/window-state.test.ts index 0cce959b8e4..295b690385c 100644 --- a/packages/desktop/src/main/windows/window-state.test.ts +++ b/packages/desktop/src/main/windows/window-state.test.ts @@ -8,34 +8,85 @@ const defaults = { width: 1280, height: 800 } const primary = { x: 0, y: 0, width: 1920, height: 1080 } +const upper = { x: -102, y: -1152, width: 2752, height: 1152 } + const displays = { - all: () => [primary, { x: 1920, y: 0, width: 2560, height: 1440 }], + all: () => [primary, { x: 1920, y: 0, width: 2560, height: 1440 }, upper], primary: () => primary, matching: () => primary, } describe("window state", () => { - test("keeps bounds that still fit on a display", () => { - const saved = { x: 2000, y: 100, width: 1000, height: 700, displayBounds: { x: 1920, y: 0, width: 2560, height: 1440 } } + test("keeps bounds that still fit on a display, including negative-coordinate monitors", () => { + const saved = { + x: 2000, + y: 100, + width: 1000, + height: 700, + displayBounds: { x: 1920, y: 0, width: 2560, height: 1440 }, + } + expect(resolveWindowState(saved, defaults, displays)).toEqual(saved) + + const negative = { x: -50, y: -1000, width: 1280, height: 800, displayBounds: upper } + expect(resolveWindowState(negative, defaults, displays)).toEqual(negative) + }) + + test.each([ + { + name: "it is above the primary and the normal bounds stayed on the primary", + all: displays.all(), + displayBounds: upper, + expected: { x: 634, y: -976, width: 1280, height: 800 }, + }, + { + // #58247: Electron reports a 1366x768 panel as 1388x781 DIP, shorter than the 800px default. + name: "it is shorter than the normal bounds", + all: [{ x: 0, y: 0, width: 1388, height: 781 }], + displayBounds: { x: 0, y: 0, width: 1388, height: 781 }, + expected: { x: 54, y: 0, width: 1280, height: 781 }, + }, + ])("maximizes on the saved display when $name", (input) => { + const saved = { x: 0, y: 0, width: 1280, height: 800, displayBounds: input.displayBounds, isMaximized: true } + + expect(resolveWindowState(saved, defaults, { ...displays, all: () => [...input.all] })).toEqual({ + ...input.expected, + displayBounds: input.displayBounds, + isMaximized: true, + }) }) test("falls back to the default size on the primary display when the saved display is gone", () => { - const saved = { x: 5000, y: 100, width: 1000, height: 700, displayBounds: { x: 4480, y: 0, width: 1920, height: 1080 } } + const saved = { + x: 5000, + y: 100, + width: 1000, + height: 700, + displayBounds: { x: 4480, y: 0, width: 1920, height: 1080 }, + } + expect(resolveWindowState(saved, defaults, displays)).toEqual({ ...defaults, x: 0, y: 0, displayBounds: primary }) + + expect(resolveWindowState({ ...saved, isMaximized: true }, defaults, displays)).toEqual({ + ...defaults, + x: 0, + y: 0, + displayBounds: primary, + isMaximized: true, + }) }) test("uses defaults without a position when nothing usable was saved", () => { expect(resolveWindowState(undefined, defaults, displays)).toEqual(defaults) expect(resolveWindowState({ width: 0, height: -1 }, defaults, displays)).toEqual(defaults) - expect(resolveWindowState("junk", defaults, displays)).toEqual(defaults) + expect(resolveWindowState({}, defaults, displays)).toEqual(defaults) }) test("keeps a maximized flag even without bounds", () => { expect(resolveWindowState({ isMaximized: true }, defaults, displays)).toEqual({ ...defaults, isMaximized: true }) }) - test("reads the file electron-window-state wrote and ignores an unreadable one", () => { + test("reads the file electron-window-state wrote and ignores an unreadable or non-object one", () => { const dir = mkdtempSync(path.join(tmpdir(), "opencode-window-state-")) const file = path.join(dir, "window-state-a.json") writeFileSync( @@ -51,6 +102,8 @@ describe("window state", () => { isMaximized: false, isFullScreen: false, }) + writeFileSync(file, '"junk"') + expect(readWindowState(file)).toBeUndefined() writeFileSync(file, "{oops") expect(readWindowState(file)).toBeUndefined() expect(readWindowState(path.join(dir, "missing.json"))).toBeUndefined() diff --git a/packages/desktop/src/main/windows/window-state.ts b/packages/desktop/src/main/windows/window-state.ts index 63bc3d6a7f9..cce428c6cbe 100644 --- a/packages/desktop/src/main/windows/window-state.ts +++ b/packages/desktop/src/main/windows/window-state.ts @@ -1,5 +1,6 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs" import path from "node:path" +import { Option, Schema } from "effect" import type { BrowserWindow, Rectangle } from "electron" // Remembers a window's bounds and maximized / fullscreen flags across launches in the same JSON @@ -21,46 +22,82 @@ export type Displays = { matching: (bounds: Rectangle) => Rectangle } +const StateFile = Schema.fromJsonString( + Schema.Struct({ + x: Schema.optional(Schema.Int), + y: Schema.optional(Schema.Int), + width: Schema.optional(Schema.Int), + height: Schema.optional(Schema.Int), + displayBounds: Schema.optional( + Schema.Struct({ x: Schema.Int, y: Schema.Int, width: Schema.Int, height: Schema.Int }), + ), + isMaximized: Schema.optional(Schema.Boolean), + isFullScreen: Schema.optional(Schema.Boolean), + }), +) + // Bounds are only trusted when they still fit on one of the current displays; otherwise the window -// falls back to the default size on the primary display. A saved maximized or fullscreen state -// without bounds is kept so manage() can restore it. -export function resolveWindowState(saved: unknown, defaults: { width: number; height: number }, displays: Displays) { - const state = isState(saved) ? saved : undefined +// falls back to the default size on the primary display. A saved maximized or fullscreen state is +// kept in every fallback so manage() can restore it. +export function resolveWindowState( + saved: Partial | undefined, + defaults: { width: number; height: number }, + displays: Displays, +): WindowState { + if (!saved) return { width: defaults.width, height: defaults.height } - if (!state) return { width: defaults.width, height: defaults.height } satisfies WindowState - - if (!hasBounds(state)) { - return state.isMaximized || state.isFullScreen - ? ({ ...state, width: defaults.width, height: defaults.height } satisfies WindowState) - : ({ width: defaults.width, height: defaults.height } satisfies WindowState) + if (!hasBounds(saved)) { + return saved.isMaximized || saved.isFullScreen + ? { ...saved, width: defaults.width, height: defaults.height } + : { width: defaults.width, height: defaults.height } } - if (!state.displayBounds) return state - const visible = displays.all().some((bounds) => within(state, bounds)) + const savedDisplay = saved.displayBounds - if (visible) return state + if (!savedDisplay) return saved + const all = displays.all() + + // Normal bounds are only recorded while the window is restored, so a window maximized on another + // display keeps x/y on the display it was restored on. maximize() uses the display under the normal + // bounds, so move them onto the saved display while it is still connected. + const target = + saved.isMaximized || saved.isFullScreen ? all.find((bounds) => sameBounds(bounds, savedDisplay)) : undefined + + if (target && !within(saved, target)) { + const width = Math.min(saved.width, target.width) + const height = Math.min(saved.height, target.height) + + return { + ...saved, + x: target.x + Math.floor((target.width - width) / 2), + y: target.y + Math.floor((target.height - height) / 2), + width, + height, + } + } + + if (all.some((bounds) => within(saved, bounds))) return saved + const primary = displays.primary() return { width: defaults.width, height: defaults.height, - x: 0, - y: 0, - displayBounds: displays.primary(), - } satisfies WindowState + x: primary.x, + y: primary.y, + displayBounds: primary, + isMaximized: saved.isMaximized, + isFullScreen: saved.isFullScreen, + } } export function windowStateFile(id: string) { return `window-state-${id.replace(/[^a-zA-Z0-9._-]/g, "-")}.json` } -export function readWindowState(file: string): unknown { +export function readWindowState(file: string) { if (!existsSync(file)) return undefined - try { - return JSON.parse(readFileSync(file, "utf8")) - } catch { - return undefined - } + return Option.getOrUndefined(Schema.decodeUnknownOption(StateFile)(readFileSync(file, "utf8"))) } // Tracks the window until it closes and writes the final state then. Bounds are recorded while @@ -111,19 +148,8 @@ export function manageWindowState(win: BrowserWindow, file: string, initial: Win win.on("closed", closed) } -function isState(value: unknown): value is WindowState { - return typeof value === "object" && value !== null -} - -function hasBounds(state: WindowState): state is WindowState & Required> { - return ( - Number.isInteger(state.x) && - Number.isInteger(state.y) && - Number.isInteger(state.width) && - state.width > 0 && - Number.isInteger(state.height) && - state.height > 0 - ) +function hasBounds(state: Partial): 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) { @@ -134,3 +160,7 @@ function within(state: WindowState & { x: number; y: number }, bounds: Rectangle state.y + state.height <= bounds.y + bounds.height ) } + +function sameBounds(a: Rectangle, b: Rectangle) { + return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height +}