Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(desktop): resync maximize state after fullscreen

Tioit-Wang660cce17fb14
diff --git a/apps/desktop/electron/main/bootstrap/window.ts b/apps/desktop/electron/main/bootstrap/window.ts
index 02e39159f..a97d52758 100644
--- a/apps/desktop/electron/main/bootstrap/window.ts
+++ b/apps/desktop/electron/main/bootstrap/window.ts
@@ -57,6 +57,7 @@ import {
mainWindowBackgroundOptions,
} from "../window-background";
import { recoverRendererAfterGone } from "../renderer-recovery";
+import { installWindowStateSync } from "../window-state-sync";
function windowsIconPath(): string | undefined {
if (process.platform !== "win32") return undefined;
@@ -685,6 +686,7 @@ export async function createWindow({
maximized: window.isMaximized(),
});
};
+ installWindowStateSync(window, sendMaximized, windowBoundsSettleMs);
// After a restore/unmaximize (or leaving fullscreen) the OS puts the window
// back to its normal bounds. Those bounds can sit outside the current work
@@ -722,7 +724,6 @@ export async function createWindow({
};
// Custom window controls (Windows/Linux) need maximize state to swap the
// maximize/restore glyph.
- window.on("maximize", sendMaximized);
if (process.platform !== "darwin") {
// Native-runner E2E fixture: establish the initial native state before
// the renderer mounts, then let WindowControls query it through IPC.
diff --git a/apps/desktop/electron/main/window-state-sync.ts b/apps/desktop/electron/main/window-state-sync.ts
new file mode 100644
index 000000000..31aa0ec4f
--- /dev/null
+++ b/apps/desktop/electron/main/window-state-sync.ts
@@ -0,0 +1,48 @@
+import type { BrowserWindow } from "electron";
+
+type WindowStateScheduler = {
+ setTimeout(callback: () => void, delayMs: number): ReturnType<typeof setTimeout>;
+ clearTimeout(timer: ReturnType<typeof setTimeout>): void;
+};
+
+const scheduler: WindowStateScheduler = {
+ setTimeout: (callback, delayMs) => setTimeout(callback, delayMs),
+ clearTimeout: (timer) => clearTimeout(timer),
+};
+
+/** Keep renderer window chrome aligned with native state after fullscreen settles. */
+export function installWindowStateSync(
+ window: Pick<BrowserWindow, "on" | "removeListener">,
+ sendMaximized: () => void,
+ settleMs: number,
+ timers: WindowStateScheduler = scheduler,
+): void {
+ let settleTimer: ReturnType<typeof setTimeout> | null = null;
+ let disposed = false;
+ const reportMaximized = () => sendMaximized();
+ const scheduleMaximizedResync = () => {
+ if (settleTimer) timers.clearTimeout(settleTimer);
+ settleTimer = timers.setTimeout(() => {
+ settleTimer = null;
+ if (!disposed) sendMaximized();
+ }, settleMs);
+ };
+ const dispose = () => {
+ disposed = true;
+ if (settleTimer) {
+ timers.clearTimeout(settleTimer);
+ settleTimer = null;
+ }
+ window.removeListener("maximize", reportMaximized);
+ window.removeListener("unmaximize", reportMaximized);
+ window.removeListener("enter-full-screen", scheduleMaximizedResync);
+ window.removeListener("leave-full-screen", scheduleMaximizedResync);
+ window.removeListener("closed", dispose);
+ };
+
+ window.on("maximize", reportMaximized);
+ window.on("unmaximize", reportMaximized);
+ window.on("enter-full-screen", scheduleMaximizedResync);
+ window.on("leave-full-screen", scheduleMaximizedResync);
+ window.on("closed", dispose);
+}
diff --git a/apps/desktop/test/window-state-sync.test.mjs b/apps/desktop/test/window-state-sync.test.mjs
new file mode 100644
index 000000000..8fd5e1f71
--- /dev/null
+++ b/apps/desktop/test/window-state-sync.test.mjs
@@ -0,0 +1,127 @@
+import assert from "node:assert/strict";
+import { EventEmitter } from "node:events";
+import test from "node:test";
+import { installWindowStateSync } from "../electron/main/window-state-sync.ts";
+import { installMaximizedWindowChrome } from "../src/lib/maximized-window-chrome.ts";
+import { readMainSource } from "./helpers/source-contracts.mjs";
+
+const mainSource = await readMainSource();
+class Caption { constructor(region = "drag") { this.region = region; } }
+globalThis.Element = Caption;
+const settle = () => new Promise(setImmediate);
+
+function fixture() {
+ const window = new EventEmitter();
+ const timers = new Map();
+ let timerId = 0;
+ let maximized = true;
+ let maximizeListener;
+ const scheduler = {
+ setTimeout(callback, delayMs) {
+ assert.equal(delayMs, 300);
+ const id = ++timerId;
+ timers.set(id, callback);
+ return id;
+ },
+ clearTimeout(id) { timers.delete(id); },
+ };
+ const documentListeners = new Map();
+ const document = {
+ documentElement: { dataset: {} },
+ addEventListener: (name, listener) => documentListeners.set(name, listener),
+ removeEventListener: (name) => documentListeners.delete(name),
+ };
+ const api = {
+ windowControl: async () => ({ maximized }),
+ onWindowMaximized(listener) {
+ maximizeListener = listener;
+ return () => { maximizeListener = null; };
+ },
+ };
+ const disposeChrome = installMaximizedWindowChrome("darwin", document, {
+ getComputedStyle: (target) => ({ getPropertyValue: () => target.region }),
+ }, api);
+ installWindowStateSync(
+ window,
+ () => maximizeListener?.({ maximized }),
+ 300,
+ scheduler,
+ );
+ return {
+ window,
+ document,
+ timers,
+ setMaximized: (value) => { maximized = value; },
+ runTimers() {
+ const callbacks = [...timers.values()];
+ timers.clear();
+ for (const callback of callbacks) callback();
+ },
+ clickCaption() {
+ const event = {
+ target: new Caption(),
+ button: 0,
+ defaultPrevented: false,
+ preventDefault() { this.defaultPrevented = true; },
+ };
+ documentListeners.get("dblclick")?.(event);
+ return event;
+ },
+ dispose() { disposeChrome(); window.emit("closed"); },
+ };
+}
+
+test("native fullscreen transitions resync stale macOS maximize chrome after settling", async () => {
+ const f = fixture();
+ await settle();
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "true");
+ assert.equal(f.clickCaption().defaultPrevented, true);
+
+ // AppKit can deliver the fullscreen event before isMaximized() reflects the
+ // settled native state, and may not emit a separate unmaximize event.
+ f.window.emit("enter-full-screen");
+ f.setMaximized(false);
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "true");
+ f.runTimers();
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "false");
+ assert.equal(f.clickCaption().defaultPrevented, false);
+
+ f.setMaximized(true);
+ f.window.emit("maximize");
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "true");
+ f.window.emit("leave-full-screen");
+ f.setMaximized(false);
+ f.runTimers();
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "false");
+ assert.equal(f.clickCaption().defaultPrevented, false);
+ f.dispose();
+});
+
+test("maximize and unmaximize events report native state immediately", async () => {
+ const f = fixture();
+ await settle();
+ f.setMaximized(false);
+ f.window.emit("unmaximize");
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "false");
+ f.setMaximized(true);
+ f.window.emit("maximize");
+ assert.equal(f.document.documentElement.dataset.windowMaximized, "true");
+ f.dispose();
+});
+
+test("fullscreen state resync timers are replaced and disposed on window close", () => {
+ const f = fixture();
+ f.window.emit("enter-full-screen");
+ f.window.emit("leave-full-screen");
+ assert.equal(f.timers.size, 1);
+ f.dispose();
+ assert.equal(f.timers.size, 0);
+ f.runTimers();
+ assert.equal(f.document.documentElement.dataset.windowMaximized, undefined);
+});
+
+test("Main retains fullscreen broadcasts and installs maximize resync", () => {
+ assert.match(mainSource, /window\.on\("enter-full-screen", sendFullScreen\)/);
+ assert.match(mainSource, /window\.on\("leave-full-screen", \(\) => \{\s*sendFullScreen\(\)/);
+ assert.match(mainSource, /installWindowStateSync\(window, sendMaximized, windowBoundsSettleMs\)/);
+});
diff --git a/docs/spec/04-ux/09-interaction-patterns.md b/docs/spec/04-ux/09-interaction-patterns.md
index ebcfa5963..a51f22338 100644
--- a/docs/spec/04-ux/09-interaction-patterns.md
+++ b/docs/spec/04-ux/09-interaction-patterns.md
@@ -135,8 +135,9 @@ recency only breaks ties between equally relevant matches.
resets this handshake.
- Frameless minimize, maximize/restore, and close controls remain outside the
drag region. Maximize state is queried on mount and updated from native
- window events, so the restore affordance never depends only on optimistic
- renderer state.
+ maximize/unmaximize events. Fullscreen enter/exit also rechecks native
+ maximize state after the transition settles, so macOS caption dragging and
+ the restore affordance reflect the actual window state.
- Windows/Linux explicit minimize actions use native minimize and keep the
taskbar entry. On Windows, clicking the focused window's taskbar button also
uses native minimize and keeps the taskbar entry; clicking it again
diff --git a/docs/spec/06-delivery/e2e/ui-shell.md b/docs/spec/06-delivery/e2e/ui-shell.md
index a982ddad5..f45df9edd 100644
--- a/docs/spec/06-delivery/e2e/ui-shell.md
+++ b/docs/spec/06-delivery/e2e/ui-shell.md
@@ -2694,8 +2694,11 @@ position and size, and AppKit must be able to resize its edges.
`node scripts/e2e-window-controls.mjs`
then covers the built shell: maximized caption regions must compute to no-drag,
and a double-click must restore through the real preload/IPC and unlock the
-normal caption. `maximized-window-chrome.test.mjs` covers late-query/event races,
-repeated clicks, no-drag controls and unmount cleanup.
+normal caption. Native fullscreen enter/exit must also leave renderer maximize
+state synchronized after the AppKit transition settles, including when the
+native event precedes the final maximize flag. `maximized-window-chrome.test.mjs`
+and `window-state-sync.test.mjs` cover late-query/event races, fullscreen
+resync, repeated clicks, no-drag controls and cleanup.
Windows covers maximize/restore with the same production guard; Linux keeps
its compositor-owned geometry. Unit tests cover cancellable edge events and
listener cleanup (`window-native-behavior.test.mjs`).