hermes-agent/apps/desktop/electron/window-state.ts
Siddharth Balyan 666f313d1d
Desktop opens new installs at a screen-sized window and 110% zoom (#127448)
* feat(desktop): open new installs at a display-sized window and 110% zoom

A fresh install opened at a fixed 1220x800 and 90% zoom, so body text sat
below the Apple and Windows minimums and users pressed Cmd+ repeatedly.

The first window now takes 75% x 80% of the primary display's work area,
floored at 1220x800, capped at 1600x1000, and never larger than the work
area. A saved window-state.json still wins. The default zoom is the 110%
UI Scale preset; Cmd+0 / Actual Size resets to it. Existing installs keep
their saved zoom (first launch has always written the default to
zoom-state.json).

* chore: retrigger CI (zero-job dispatch failure, auto-heal)

* test(desktop-e2e): let a full-window overlay cover the viewport within 1 px

waitForInteractive treats a fixed element as blocking only when its rect
reaches innerWidth/innerHeight. innerWidth is rounded: at 110% zoom a
1280x860 window is 1163.6 CSS px wide and innerWidth reads 1164, so the boot
overlay (inset-0, 1163.6 px) no longer counted as covering the window and the
update E2E declared the app interactive while it was still booting.
2026-09-29 06:55:56 +00:00

185 lines
5.4 KiB
TypeScript

/**
* Pure geometry helpers for window-state.json — restoring the main window's
* size, position, and maximized flag across launches. Side-effect-free so the
* part that actually matters (rejecting garbage + off-screen bounds) is
* unit-testable without booting Electron; main.ts owns the file I/O and the
* live `screen` displays.
*/
const MIN_WIDTH = 400
const MIN_HEIGHT = 620
// Keep at least this much of the window over a display work area before we trust
// a saved position, so the title bar stays grabbable after a monitor unplugs.
const MIN_VISIBLE = 48
const finite = v => typeof v === 'number' && Number.isFinite(v)
const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi))
interface SanitizedWindowState {
width: number
height: number
isMaximized: boolean
x?: number
y?: number
}
// Parse raw JSON → clean state, or null if garbage. width/height are required
// and floored; x/y survive only as a finite pair; isMaximized is strict.
function sanitizeWindowState(raw?: any): SanitizedWindowState | null {
if (!raw || typeof raw !== 'object' || !finite(raw.width) || !finite(raw.height)) {
return null
}
const state: SanitizedWindowState = {
width: Math.max(MIN_WIDTH, Math.round(raw.width)),
height: Math.max(MIN_HEIGHT, Math.round(raw.height)),
isMaximized: raw.isMaximized === true
}
if (finite(raw.x) && finite(raw.y)) {
state.x = Math.round(raw.x)
state.y = Math.round(raw.y)
}
return state
}
// Return the work area with the largest meaningful overlap with `bounds`.
// `displays` is Electron's screen.getAllDisplays() shape. A small sliver does
// not count: the saved position is only trusted when at least `minVisible` is
// reachable on both axes.
function matchingWorkArea(bounds, displays, minVisible = MIN_VISIBLE) {
if (!Array.isArray(displays)) {
return null
}
let best = null
let bestArea = 0
for (const { workArea: a } of displays) {
if (!a) {
continue
}
const x = Math.min(bounds.x + bounds.width, a.x + a.width) - Math.max(bounds.x, a.x)
const y = Math.min(bounds.y + bounds.height, a.y + a.height) - Math.max(bounds.y, a.y)
if (x < minVisible || y < minVisible) {
continue
}
const area = x * y
if (area > bestArea) {
best = a
bestArea = area
}
}
return best
}
interface WindowOptions {
width: number
height: number
x?: number
y?: number
}
interface WorkArea {
width: number
height: number
}
function firstLaunchSize(workArea: WorkArea): WindowOptions {
return {
width: Math.min(clamp(Math.round(workArea.width * 0.75), 1220, 1600), workArea.width),
height: Math.min(clamp(Math.round(workArea.height * 0.8), 800, 1000), workArea.height)
}
}
function computeWindowOptions(state: WindowOptions, displays): WindowOptions {
const opts: WindowOptions = { width: state.width, height: state.height }
const cap = (Array.isArray(displays) ? displays : []).reduce(
(m, { workArea: a } = {}) =>
a && finite(a.width) && finite(a.height)
? { width: Math.max(m.width, a.width), height: Math.max(m.height, a.height) }
: m,
{ width: 0, height: 0 }
)
if (cap.width && cap.height) {
opts.width = clamp(opts.width, MIN_WIDTH, cap.width)
opts.height = clamp(opts.height, MIN_HEIGHT, cap.height)
}
if (finite(state.x) && finite(state.y)) {
const workArea = matchingWorkArea({ x: state.x, y: state.y, width: opts.width, height: opts.height }, displays)
if (workArea) {
opts.width = clamp(opts.width, MIN_WIDTH, workArea.width)
opts.height = clamp(opts.height, MIN_HEIGHT, workArea.height)
opts.x = clamp(state.x, workArea.x, workArea.x + workArea.width - opts.width)
opts.y = clamp(state.y, workArea.y, workArea.y + workArea.height - opts.height)
}
}
return opts
}
// Trailing debounce: collapse a burst of resize/move events (Linux fires many
// mid-drag) into a single run `delayMs` after the last. `.flush()` runs now and
// cancels the pending timer — used on close, before the window is gone.
function debounce(fn, delayMs) {
let timer = null
const debounced = () => {
clearTimeout(timer)
timer = setTimeout(() => {
timer = null
fn()
}, delayMs)
}
debounced.flush = () => {
clearTimeout(timer)
timer = null
fn()
}
return debounced
}
// The geometry events worth persisting from. `moved` and `resized` — the
// settled-once-per-drag pair — are macOS/Windows only (Electron tags them
// `@platform darwin,win32`), so a window bound to those alone never saves its
// place on Linux: the events simply never arrive. `move` and `resize` carry no
// platform tag and fire everywhere. They also fire continuously mid-drag, which
// is what the trailing debounce above is for — and once a burst collapses to a
// single trailing run, the settled events add nothing the debounce hasn't
// already given us.
const GEOMETRY_EVENTS = ['move', 'resize']
// Bind `schedule` to every geometry event, on a BrowserWindow or any emitter
// with `.on`. One call site per window so the platform reasoning above can't be
// half-applied to one window and not the other.
function bindGeometryPersistence(win, schedule) {
for (const event of GEOMETRY_EVENTS) {
win.on(event, schedule)
}
}
export {
bindGeometryPersistence,
computeWindowOptions,
debounce,
firstLaunchSize,
GEOMETRY_EVENTS,
matchingWorkArea,
MIN_HEIGHT,
MIN_VISIBLE,
MIN_WIDTH,
sanitizeWindowState
}