Skip to content

desktop: maximized state lost when saved bounds exceed the display #58247

Description

@asvcodes

Description

On Linux/X11, the desktop app does not restore a maximized window on displays shorter than the default 800px window height (for example 1366×768). It opens unmaximized at the default size on every launch, even though it was closed maximized.

resolveWindowState() discards the entire saved state — including isMaximized — when the saved normal bounds do not fit inside a display. manageWindowState.update() intentionally leaves the normal bounds untouched while maximized, so those bounds can remain at the 1280×800 default (or WM-adjusted values taller than the screen). The state is therefore rejected on every restart, and the maximized flag never survives.

Environment

  • OpenCode version: desktop app 2.1.3 (bundled CLI 2.1.3); opencode --version on PATH reports 2.1.0
  • OS: CachyOS (Arch-based), Linux 7.2.11-1-cachyos, x86_64, XFCE 4.20 / xfwm4 4.20.0, X11
  • Display: 1366×768; Electron reports bounds 1388×781 at scale 0.984375
  • Terminal: Unavailable: desktop app, launched from the desktop/Plank entry
  • Shell: /bin/fish
  • Install/channel: Homebrew cask opencode-desktop, latest (AppImage)
  • Plugins: none

Steps to reproduce

  1. Use a display whose height in Electron DIP is below 800 (e.g. 1366×768; Electron reports 1388×781 here).
  2. Launch the desktop app with a fresh profile (default window size is 1280×800).
  3. Maximize the window.
  4. Quit and relaunch the app.

Expected behavior

The window opens maximized again. If the saved normal bounds are unusable, the app may fall back to the default size but should still restore isMaximized / isFullScreen.

Actual behavior

The window opens unmaximized at 1280×800. This repeats on every launch. The state file stays invalid, so the maximized state is never restored; a workaround is to force maximize from outside after the window maps, which causes a visible resize.

Observed state before the workaround:

{"width":1284,"height":783,"x":2,"y":34,"displayBounds":{"x":0,"y":0,"width":1388,"height":781},"isMaximized":true,"isFullScreen":false}

y + height = 34 + 783 = 817 > 781, so within() fails.

Additional context

In packages/desktop/src/main/windows/window-state.ts (current main), the invalid-bounds fallback drops the maximize/fullscreen flags:

// resolveWindowState, lines 38-49
if (!state.displayBounds) return state
const visible = displays.all().some((bounds) => within(state, bounds))

if (visible) return state

return {
  width: defaults.width,
  height: defaults.height,
  x: 0,
  y: 0,
  displayBounds: displays.primary(),
} satisfies WindowState

The fallback's own default bounds (1280×800) also do not fit a 768px-tall display. Meanwhile manageWindowState.update() (lines 80-89) skips updating x/y/width/height while maximized but still writes isMaximized and displayBounds, so the stale oversized normal bounds persist.

Suggested fix: keep isMaximized / isFullScreen in the fallback branch, clamp the restored bounds to the display, and/or avoid persisting stale normal bounds while maximized.

Related: #56970 (remember window position and size), #12413 (restoring maximized state, closed), #15165 (window state reset, closed).

Workaround used locally: remove displayBounds from the state file (then resolveWindowState returns the state as-is) and set valid bounds with isMaximized: true; the app then maximizes while the window is still hidden, before first paint.

Activity

  1. opencode-agent commented on Oct 22, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report and the analysis. I could reproduce this with the window-state logic from v2.1.3, and it still happens on the latest dev (d93568f). I didn't run the desktop app itself here. Instead I ran the real window-state.ts with a stubbed window on a single 1388×781 display.

    What I saw:

    1. On a fresh profile, the window first maps unmaximized at the size from your report (x: 2, y: 34, 1284×783). It's then maximized and the app quits. The saved file has isMaximized: true along with those bounds, which are taller than the display (34 + 783 > 781).
    2. On every later launch, resolveWindowState() rejects the state and returns the 1280×800 fallback at 0,0 with no isMaximized, so the window never reopens maximized.
    3. Quitting while maximized writes the same oversized bounds back, so this repeats on every launch.
    4. Your exact saved state resolves to the same fallback, with isMaximized dropped.

    The code's own comment says a saved maximized or fullscreen state should be kept, so this looks like a bug, not intended behaviour. The fallback's 1280×800 size doesn't fit a 781px-tall display either.

    The script below reproduces it. Run it as REPO=<opencode checkout> ./repro.sh <git-ref>.

    Related: #56970 (window position and size not remembered on Windows) and PR #57088 (adds a tolerance to the bounds check and keeps the maximized/fullscreen flags only when the state file is invalid, so it doesn't cover this case).

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Usage: repro.sh <git-ref>   (run with REPO=/path/to/opencode checkout)
    set -euo pipefail
    REPO=${REPO:-/workspace/repo}; REF=${1:-v2.1.3}
    D=$(mktemp -d)
    git -C "$REPO" show "$REF:packages/desktop/src/main/windows/window-state.ts" > "$D/window-state.ts"
    cat > "$D/run.ts" <<'TS'
    import { resolveWindowState, manageWindowState } from "./window-state"
    import { readFileSync } from "node:fs"
    import { EventEmitter } from "node:events"
    const display = { x: 0, y: 0, width: 1388, height: 781 } // 1366x768 at scale 0.984375
    const displays = { all: () => [display], primary: () => display, matching: () => display }
    const defaults = { width: 1280, height: 800 }
    // Fake BrowserWindow: launched at defaults, user maximizes, then quits.
    function session(initial: any, file: string) {
      const w: any = new EventEmitter()
      let max = false, bounds = { x: initial.x ?? 0, y: initial.y ?? 0, width: initial.width, height: initial.height }
      Object.assign(w, { isDestroyed: () => false, getBounds: () => (max ? display : bounds), isMaximized: () => max,
        isMinimized: () => false, isFullScreen: () => false, maximize: () => { max = true }, setFullScreen() {} })
      manageWindowState(w, file, initial, displays)
      const startedMaximized = max
      if (!max) {
        // Window maps in normal state; xfwm4 places it below the panel (as in the report): y=34, height 783
        bounds = { x: 2, y: 34, width: 1284, height: 783 }
        w.emit("close") // runs update() once in normal state (same as a debounced move/resize event)
      }
      max = true          // user maximizes
      w.emit("close"); w.emit("closed")
      return startedMaximized
    }
    const file = `/tmp/opencode/ws-${process.pid}.json`
    let saved: unknown = undefined
    for (let launch = 1; launch <= 3; launch++) {
      const initial = resolveWindowState(saved, defaults, displays)
      const restored = session(initial, file)
      saved = JSON.parse(readFileSync(file, "utf8"))
      console.log(`launch ${launch}: resolved=${JSON.stringify(initial)} -> opened maximized: ${restored}; saved on quit=${JSON.stringify(saved)}`)
    }
    // Reporter's exact saved state
    const r = resolveWindowState({"width":1284,"height":783,"x":2,"y":34,"displayBounds":display,"isMaximized":true,"isFullScreen":false}, defaults, displays)
    console.log("reporter state resolves to:", JSON.stringify(r), "isMaximized kept:", r.isMaximized === true)
    TS
    cd "$D" && bun run.ts

    Run it: bash repro.sh

  2. tneemo commented on Oct 23, 2026

    @tneemo

    I'd like to take this one. The fallback in resolveWindowState drops isMaximized/isFullScreen when the saved bounds don't fit the display. I have a fix with regression tests ready and will open a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions