# Desktop window state

The main window stores its last normal size apart from maximize and fullscreen. A normal window never opens flush with the menu bar, Dock or screen sides, where no edge is left to grab.

| Failure | Guard |
| --- | --- |
| Quit while zoomed or filling the work area | Create a smaller normal window, then reapply maximize |
| Old flush `{{x,y,width,height}}` file | Migrated as maximized plus the inset default |
| Saved display unplugged | Centered default on the primary work area |
| Corrupt or tiny file | Minimum size and centered default |
| Tab row swallows the top edge | 5px `no-drag` strips |
| Torn-off window closed | Torn-off windows are not saved |

Code: `apps/desktop/main/windows/window-state.js`, `apps/desktop/main.js`, `apps/web/app/styles/base/desktop-chrome.css`, `apps/web/components/app-shell.tsx`. How many windows exist and who creates them: [Window lifecycle](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/desktop/window-lifecycle.html).

## Persisted schema

`window-state.json` in the desktop userData directory (`app.getPath("userData")`, desktop name `openprogram-desktop`).

```
{
  "version": 2,
  "x": 160, "y": 90, "width": 1280, "height": 800,
  "isMaximized": false,
  "isFullScreen": false,
  "displayId": 1,
  "displayWorkArea": { "x": 0, "y": 38, "width": 1512, "height": 851 }
}
```

| Field | Role |
| --- | --- |
| `x,y,width,height` | Last normal bounds; never a rectangle that fills the work area |
| `isMaximized` | Last zoom or maximize |
| `isFullScreen` | Last macOS native fullscreen |
| `displayId` | Electron display id, tried first while that screen is connected |
| `displayWorkArea` | Matches a reconnected display whose id changed |

| Limit | Value |
| --- | --- |
| Minimum | `800×500`; no maximum |
| First run | `1440×900`, centered on the primary work area |
| Inset default | 24px inside each edge when `1440×900` would fill the work area |
| Filling | Within 8 CSS pixels of the work area; checked on save and restore |
| Version 1 | The old `{{x,y,width,height}}` object; migrates on load |

## Restore

| Step | Action |
| --- | --- |
| 1. Read and migrate | Corrupt JSON, missing width or height, `NaN` or a tiny size: centered default |
| 2. Find the display | `displayId`, then work area (2px tolerance), then bounds overlapping a work area. None: primary work area, centered default, maximize and fullscreen dropped |
| 3. Check for a filled work area | Saved rectangle fills a connected work area: treat as maximized, normal size becomes the inset default. `BrowserWindow` is never constructed at work-area size |
| 4. Clamp | The normal rectangle onto the work area |
| 5. Create hidden | At normal size with `minWidth` and `minHeight`. If maximized, `setBounds` to the work area, then `maximize()` or `setFullScreen(true)`, still hidden |
| 6. Show | On `ready-to-show`, so macOS never animates from the small first frame |

The green Zoom button therefore returns to the remembered normal size.

## Stretched to the work area without Zoom

Saved as maximized. The normal size kept is the first that does not fill the work area: `getNormalBounds()`, the in-memory last good rectangle, the inset default.

## Save timing

| Event | Write |
| --- | --- |
| `resize`, `move` | Debounced 300ms |
| `maximize`, `unmaximize`, `enter-full-screen`, `leave-full-screen`, `close` | Written at once |
| While maximized, fullscreen or filling | Only flags and display identity change; the normal rectangle stays |

## Torn-off windows

Size `min(1100, normal width)` × `min(720, normal height)`, no parent `x/y`, no inherited maximize or fullscreen. Placed at the drop point by `centerHiddenWindowOnCursor`.

## Titlebar hit testing

| Area | Behaviour |
| --- | --- |
| 40px tab row | `-webkit-app-region: drag`, so empty space moves the window |
| Top, left and right edges | 5px `no-drag` strips (`.desktop-resize-edge`), corners included, for native resize |
| macOS traffic lights | `trafficLightPosition` centers them on the row: `{{x:13,y:12}}` at zoom 1 |
| Tabs | 6px below the top; the strips do not cover them, the lights, `+` or the main menu |

**Why a flush normal window cannot be resized**

Electron's window stays resizable, but on a 1512-wide display `{x:0,y:38,width:1512,height:851}` leaves nothing to grab, and Zoom has no size to return to. A drag region running edge to edge also takes the top-edge and corner resize targets. Setting `resizable: true` or deleting the file changes neither; the stored model has to separate normal bounds from maximize.

## Implementation status

Implemented, checked by `apps/desktop/tests/windows/check-window-state.js`. Window chrome colour follows the Web theme (`theme-chrome.js`, see [Unification work](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/visual-system/unification-work.html)); bounds persistence stores no background colour.
