Skip to content

Where agent teams gather. Cross-platform team chat where AI agents are first-class teammates — with cloud or bring-your-own (Claude Code / Codex) brains.

TypeScript

fix(electron): the window must reopen at the size it was left (#204)

Xialie Zhuang0f0566327f83
diff --git a/electron/main.cjs b/electron/main.cjs
index c82c485..ff3be67 100644
--- a/electron/main.cjs
+++ b/electron/main.cjs
@@ -5,6 +5,7 @@ const fs = require('node:fs')
const http = require('node:http')
const crypto = require('node:crypto')
const { pathToFileURL } = require('node:url')
+const { initialWindowSize } = require('./window-size.cjs')
const autoUpdater = require('./autoUpdater.cjs')
const isDev = !app.isPackaged
@@ -1089,16 +1090,18 @@ function attachDisplayListeners() {
}
function createWindow() {
- const saved = readWindowState() ?? DEFAULT_WINDOW_STATE
+ const savedState = readWindowState()
+ const saved = savedState ?? DEFAULT_WINDOW_STATE
const rect = visibleRect(saved)
- // Cap initial size to fit comfortably inside the primary display's
- // work area — 90% of work area, with the configured default as the
- // upper ceiling. Without this, the 1480×920 default would exceed
- // smaller laptop displays and macOS would clamp on launch, making
- // every first-run feel "fullscreen".
- const wa = screen.getPrimaryDisplay().workArea
- const initW = Math.min(saved.width ?? DEFAULT_WINDOW_STATE.width, Math.round(wa.width * 0.9))
- const initH = Math.min(saved.height ?? DEFAULT_WINDOW_STATE.height, Math.round(wa.height * 0.9))
+ // The 90% cap is for FIRST RUN — it stops the 1480×920 default exceeding a
+ // small laptop, which macOS would clamp on launch so the app felt like it
+ // opened fullscreen. A size the user chose is not capped, only fitted, and
+ // fitted against the display it is actually landing on rather than the
+ // primary one. See window-size.cjs for what the old expression cost.
+ const wa = (rect ? screen.getDisplayMatching(rect) : screen.getPrimaryDisplay()).workArea
+ const { width: initW, height: initH } = initialWindowSize(
+ saved, DEFAULT_WINDOW_STATE, wa, Boolean(savedState),
+ )
mainWindow = new BrowserWindow({
width: initW,
height: initH,
diff --git a/electron/window-size.cjs b/electron/window-size.cjs
new file mode 100644
index 0000000..644257e
--- /dev/null
+++ b/electron/window-size.cjs
@@ -0,0 +1,44 @@
+/**
+ * How large the main window opens.
+ *
+ * Two different questions were being answered by one expression:
+ *
+ * FIRST RUN — the 1480×920 default has to fit a small laptop, or macOS
+ * clamps it on launch and the app feels like it opened fullscreen. Capping
+ * at 90% of the work area is the fix, and is what the comment describes.
+ *
+ * RESTORING — the saved size is the size the user chose. Shaving 10% off it
+ * on every launch is not a cap, it is a slow shrink, and because
+ * `persistState` writes the new bounds back on the first resize event, the
+ * size they picked is overwritten and gone.
+ *
+ * The old code applied the 90% cap to both, against the PRIMARY display's work
+ * area regardless of which display the window was being restored onto. On a
+ * laptop plus a large external, a window sized 2300×1300 on the external came
+ * back at the laptop's 90% — roughly a third of its area — and that third was
+ * then saved over the original.
+ *
+ * So: cap the default, fit the saved size. "Fit" still bounds it to the work
+ * area of the display it is actually landing on, because a monitor that was
+ * unplugged since last quit should not produce a window larger than what is
+ * left — it just no longer takes a 10% bite out of a size that already fits.
+ *
+ * Pure and electron-free so the arithmetic can be tested, the same split as
+ * cli-path.cjs.
+ */
+
+/**
+ * @param {{width?: number, height?: number}} saved last-quit size, or the defaults on first run
+ * @param {{width: number, height: number}} defaults DEFAULT_WINDOW_STATE
+ * @param {{width: number, height: number}} workArea work area of the display the window will open on
+ * @param {boolean} restoring true when `saved` came from disk
+ */
+function initialWindowSize(saved, defaults, workArea, restoring) {
+ const cap = restoring ? 1 : 0.9
+ return {
+ width: Math.min(saved?.width ?? defaults.width, Math.round(workArea.width * cap)),
+ height: Math.min(saved?.height ?? defaults.height, Math.round(workArea.height * cap)),
+ }
+}
+
+module.exports = { initialWindowSize }
diff --git a/server/src/__tests__/electron-window-size.test.ts b/server/src/__tests__/electron-window-size.test.ts
new file mode 100644
index 0000000..d2edaf9
--- /dev/null
+++ b/server/src/__tests__/electron-window-size.test.ts
@@ -0,0 +1,103 @@
+/**
+ * The window opens at the size the user left it.
+ *
+ * One expression answered two different questions:
+ *
+ * const wa = screen.getPrimaryDisplay().workArea
+ * const initW = Math.min(saved.width ?? DEFAULT_WINDOW_STATE.width, Math.round(wa.width * 0.9))
+ *
+ * The 90% cap is described, correctly, as protecting FIRST RUN: the 1480×920
+ * default would exceed a small laptop and macOS would clamp it, so the app felt
+ * like it opened fullscreen. But it was applied to the SAVED size too, so every
+ * launch shaved 10% off the size the user chose — and `persistState` writes the
+ * new bounds back on the first resize event, so the original was overwritten.
+ *
+ * Multi-monitor made it worse: the cap read the PRIMARY display's work area
+ * regardless of which display the window was restored onto. A window sized
+ * 2300×1300 on an external came back at the laptop's 90%, roughly a third of
+ * its area, and that third was then saved over the original.
+ *
+ * Run: node --import tsx --test server/src/__tests__/electron-window-size.test.ts
+ */
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { createRequire } from 'node:module'
+
+const require = createRequire(import.meta.url)
+const { initialWindowSize } = require('../../../electron/window-size.cjs') as {
+ initialWindowSize: (
+ saved: { width?: number; height?: number } | null,
+ defaults: { width: number; height: number },
+ workArea: { width: number; height: number },
+ restoring: boolean,
+ ) => { width: number; height: number }
+}
+
+const DEFAULTS = { width: 1480, height: 920 }
+/** MacBook Air 13", after the menu bar. */
+const LAPTOP = { width: 1470, height: 931 }
+const EXTERNAL = { width: 2560, height: 1440 }
+
+// ── restoring: the user's size, not 90% of it ──────────────────────────────
+
+test('a size that fits is restored exactly', () => {
+ const saved = { width: 1460, height: 925 }
+ assert.deepEqual(initialWindowSize(saved, DEFAULTS, LAPTOP, true), saved)
+})
+
+test('a large size on the external display survives', () => {
+ // The old code capped this against the LAPTOP's work area even though the
+ // window was being restored onto the external, and persistState then wrote
+ // the shrunken size back.
+ const saved = { width: 2300, height: 1300 }
+ assert.deepEqual(initialWindowSize(saved, DEFAULTS, EXTERNAL, true), saved)
+})
+
+test('restoring is idempotent — the size does not creep down', () => {
+ // Relaunch three times; the size the user chose must not decay.
+ let size = { width: 1460, height: 925 }
+ for (let i = 0; i < 3; i++) size = initialWindowSize(size, DEFAULTS, LAPTOP, true)
+ assert.deepEqual(size, { width: 1460, height: 925 })
+})
+
+test('a saved size larger than the display it lands on is fitted, not shrunk further', () => {
+ // The external was unplugged. Bound it to what is left — but to the whole
+ // work area, not 90% of it.
+ assert.deepEqual(
+ initialWindowSize({ width: 2300, height: 1300 }, DEFAULTS, LAPTOP, true),
+ { width: 1470, height: 931 },
+ )
+})
+
+// ── first run: the cap the comment describes ───────────────────────────────
+
+test('first run is capped to 90% so it does not feel fullscreen', () => {
+ assert.deepEqual(
+ initialWindowSize(DEFAULTS, DEFAULTS, LAPTOP, false),
+ { width: 1323, height: 838 },
+ )
+})
+
+test('first run on a large display gets the configured default, not 90% of the screen', () => {
+ // The default is the ceiling; the cap only ever lowers it.
+ assert.deepEqual(initialWindowSize(DEFAULTS, DEFAULTS, EXTERNAL, false), DEFAULTS)
+})
+
+test('a saved record missing its size falls back to the default', () => {
+ assert.deepEqual(
+ initialWindowSize({}, DEFAULTS, EXTERNAL, true),
+ DEFAULTS,
+ )
+ assert.deepEqual(initialWindowSize(null, DEFAULTS, EXTERNAL, true), DEFAULTS)
+})
+
+// ── the regression, stated as the difference ───────────────────────────────
+
+test('restoring and first-run no longer produce the same number', () => {
+ // This equality IS the bug: the old code could not tell the two apart.
+ const saved = { width: 1460, height: 925 }
+ const restored = initialWindowSize(saved, DEFAULTS, LAPTOP, true)
+ const firstRun = initialWindowSize(DEFAULTS, DEFAULTS, LAPTOP, false)
+ assert.notDeepEqual(restored, firstRun)
+ assert.ok(restored.width > firstRun.width)
+})