Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(window): restore the toggle when the maximized flag is lost on Windows
zhoulaihui4451ddca1922
diff --git a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts b/apps/desktop/electron/main/bootstrap/app-lifecycle.tsindex 850dcd377..7a9afbd41 100644--- a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts+++ b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts@@ -1,5 +1,5 @@import {- app, BrowserWindow, Menu, nativeImage, nativeTheme, powerSaveBlocker, Tray,+ app, BrowserWindow, Menu, nativeImage, nativeTheme, powerSaveBlocker, screen, Tray,type MenuItemConstructorOptions,} from "electron";import { existsSync } from "node:fs";@@ -20,6 +20,7 @@ import {import { catalogs, resolveLocale } from "@pi-desktop/i18n";import { installApplicationMenu } from "../application-menu";import { isWindowFullScreen, setWindowFullScreen } from "../window-fullscreen";+import { planMaximizeToggle } from "../window-maximize";import { createTraySessions } from "../tray-sessions";import { createTaskbarUnreadBadge } from "../taskbar-unread-badge";import { createWindow, type WindowLifecycleState } from "./window";@@ -496,7 +497,23 @@ export function createApplicationLifecycle({// Maximizing a fullscreen window must leave fullscreen first, or the// window keeps both flags and the same stranding path above applies// (issue #1655).- if (target.isMaximized()) {+ //+ // `isMaximized()` alone cannot drive the toggle: on Windows the+ // native maximized flag can be lost while the window still fills+ // the work area, and then every click lands in the maximize branch+ // and changes nothing (issue #1993). The plan's geometric fallback+ // catches that pseudo-maximized state.+ const maximizePlan = planMaximizeToggle({+ isMaximized: target.isMaximized(),+ isFullScreen: isWindowFullScreen(target),+ platform: process.platform,+ bounds: target.getBounds(),+ workArea: workAreaForWindow(target),+ });+ if (maximizePlan === "unmaximize") {+ target.unmaximize();+ } else if (maximizePlan === "resyncUnmaximize") {+ target.maximize();target.unmaximize();} else {if (isWindowFullScreen(target)) {@@ -520,6 +537,16 @@ export function createApplicationLifecycle({};}+ /** Work area of the display a window is on, or null when it is gone. */+ function workAreaForWindow(target: BrowserWindow) {+ try {+ if (target.isDestroyed()) return null;+ return screen.getDisplayMatching(target.getBounds()).workArea;+ } catch {+ return null;+ }+ }+function dispatchNativeMenuAction(action: NativeMenuAction) {void executeNativeMenuAction(action);}diff --git a/apps/desktop/electron/main/plugin-panel-host.ts b/apps/desktop/electron/main/plugin-panel-host.tsindex de687224b..f671a4a30 100644--- a/apps/desktop/electron/main/plugin-panel-host.ts+++ b/apps/desktop/electron/main/plugin-panel-host.ts@@ -1,4 +1,4 @@-import { BrowserWindow, ipcMain, Menu, session, systemPreferences } from "electron";+import { BrowserWindow, ipcMain, Menu, screen, session, systemPreferences } from "electron";import { pathToFileURL } from "node:url";import { join, resolve } from "node:path";import { catalogs, resolveLocale } from "@pi-desktop/i18n";@@ -8,6 +8,7 @@ import {} from "@pi-desktop/plugin-sdk";import { builtinWindowBackground } from "@pi-desktop/shared";import { suppressLinuxFramelessSystemMenu } from "./frameless-system-menu";+import { planMaximizeToggle } from "./window-maximize";import { getModuleDirectory } from "./module-path";import { PanelSenders, panelReadyWithin, pageGoneWithin, resolvePanelInvocation } from "./plugin-panel-senders";import { PanelOperationSerializer } from "./plugin-panel-senders";@@ -179,6 +180,16 @@ export function pluginSessionPartition(pluginId: string): string {return `persist:pi-plugin-${pluginId.replace(/[^a-zA-Z0-9._-]/g, "_")}`;}+/** Work area of the display a window is on, or null when it is gone. */+function workAreaForWindow(window: BrowserWindow) {+ try {+ if (window.isDestroyed()) return null;+ return screen.getDisplayMatching(window.getBounds()).workArea;+ } catch {+ return null;+ }+}+/*** Hosts isolated plugin panel windows.* Each plugin panel gets a dedicated session partition and no Node integration.@@ -385,10 +396,29 @@ export class PluginPanelHost {case "minimize":window.minimize();break;- case "toggleMaximize":- if (window.isMaximized()) window.unmaximize();- else window.maximize();+ case "toggleMaximize": {+ // `isMaximized()` alone cannot drive the toggle: on Windows the+ // native maximized flag can be lost while the window still fills+ // the work area, and then every click maximizes a maximized-looking+ // window and changes nothing (issue #1993). The plan's geometric+ // fallback catches that pseudo-maximized state.+ const plan = planMaximizeToggle({+ isMaximized: window.isMaximized(),+ isFullScreen: window.isFullScreen(),+ platform: process.platform,+ bounds: window.getBounds(),+ workArea: workAreaForWindow(window),+ });+ if (plan === "unmaximize") {+ window.unmaximize();+ } else if (plan === "resyncUnmaximize") {+ window.maximize();+ window.unmaximize();+ } else {+ window.maximize();+ }break;+ }case "contextMenu":this.showWidgetMenu(window);break;diff --git a/apps/desktop/electron/main/window-maximize.ts b/apps/desktop/electron/main/window-maximize.tsnew file mode 100644index 000000000..4a9ea2569--- /dev/null+++ b/apps/desktop/electron/main/window-maximize.ts@@ -0,0 +1,61 @@+import type { Rectangle } from "electron";++/**+ * How `toggleMaximize` should act on a window.+ *+ * - `unmaximize`: the window reports maximized; restore it.+ * - `resyncUnmaximize`: the native maximized flag was lost while the window+ * still fills the work area (the Windows `frame:false` + `thickFrame:false`+ * quirk behind issue #1993). Re-assert the maximized state first so the+ * follow-up `unmaximize()` has a real maximized state to restore from.+ * - `maximize`: a plain windowed window; maximize it.+ */+export type MaximizeTogglePlan = "unmaximize" | "resyncUnmaximize" | "maximize";++/**+ * Bounds and work areas are integer pixels, but display changes and DPI+ * rounding leave off-by-small amounts, so "covers the work area" allows a+ * small slack instead of exact containment.+ */+const COVER_SLACK_PX = 8;++export function boundsCoverWorkArea(bounds: Rectangle, workArea: Rectangle): boolean {+ return (+ bounds.x <= workArea.x + COVER_SLACK_PX &&+ bounds.y <= workArea.y + COVER_SLACK_PX &&+ bounds.x + bounds.width >= workArea.x + workArea.width - COVER_SLACK_PX &&+ bounds.y + bounds.height >= workArea.y + workArea.height - COVER_SLACK_PX+ );+}++/**+ * Decide how `toggleMaximize` acts, from plain data so the Windows flag-loss+ * path is unit-testable without Electron.+ *+ * The geometric fallback is Windows-only: the flag loss it heals is the+ * win32 `frame:false` + `thickFrame:false` quirk (#1993). Elsewhere a+ * window that merely covers the work area is an ordinary user-resized+ * window, and maximizing it must keep the plain behavior.+ *+ * A fullscreen window never counts as pseudo-maximized: its bounds cover+ * the work area too, but its toggle must keep running through the existing+ * exit-fullscreen-then-maximize path (issue #1655).+ */+export function planMaximizeToggle(input: {+ isMaximized: boolean;+ isFullScreen: boolean;+ platform: string;+ bounds: Rectangle;+ workArea: Rectangle | null;+}): MaximizeTogglePlan {+ if (input.isMaximized) return "unmaximize";+ if (+ input.platform === "win32" &&+ !input.isFullScreen &&+ input.workArea &&+ boundsCoverWorkArea(input.bounds, input.workArea)+ ) {+ return "resyncUnmaximize";+ }+ return "maximize";+}diff --git a/apps/desktop/test/window-fullscreen-maximize.test.mjs b/apps/desktop/test/window-fullscreen-maximize.test.mjsindex 5d166b706..75d9d4f71 100644--- a/apps/desktop/test/window-fullscreen-maximize.test.mjs+++ b/apps/desktop/test/window-fullscreen-maximize.test.mjs@@ -183,8 +183,10 @@ test("the maximize toggle leaves fullscreen first", () => {lifecycleSource.indexOf('case "toggleMaximize"'),lifecycleSource.indexOf('case "close"'),);- assert.match(block, /if \(target\.isMaximized\(\)\)/);- assert.match(block, /isWindowFullScreen\(target\)/);+ // The toggle decision reads the tracked fullscreen flag, so the maximize+ // path still runs the exit-fullscreen-then-maximize interlock (#1655).+ assert.match(block, /isMaximized: target\.isMaximized\(\)/);+ assert.match(block, /isFullScreen: isWindowFullScreen\(target\)/);assert.match(block, /setWindowFullScreen\(/);assert.match(block, /target\.maximize\(\)/);});diff --git a/apps/desktop/test/window-maximize-toggle.test.mjs b/apps/desktop/test/window-maximize-toggle.test.mjsnew file mode 100644index 000000000..cee7f4b56--- /dev/null+++ b/apps/desktop/test/window-maximize-toggle.test.mjs@@ -0,0 +1,139 @@+/**+ * Maximize-toggle planning for the Windows flag-loss quirk (issue #1993).+ *+ * Behavioral: with `frame:false` + `thickFrame:false`, Windows can drop the+ * native maximized flag (IsZoomed) while the window keeps its work-area+ * size; `isMaximized()` then reads false and a plain toggle keeps calling+ * `maximize()` on a window that already fills the screen — the title-bar+ * button goes dead. The decision must fall back to geometry: a non-maximized+ * window whose bounds cover the work area of its display counts as+ * pseudo-maximized.+ *+ * Contract: fullscreen windows never count as pseudo-maximized (their+ * bounds cover the work area too), so the exit-fullscreen-then-maximize+ * interlock from issue #1655 stays intact.+ */+import assert from "node:assert/strict";+import test from "node:test";+import { register } from "node:module";+import { dirname, join } from "node:path";+import { fileURLToPath, pathToFileURL } from "node:url";++const here = dirname(fileURLToPath(import.meta.url));+register(pathToFileURL(join(here, "helpers/ts-import-hooks.mjs")));++const { boundsCoverWorkArea, planMaximizeToggle } = await import(+ "../electron/main/window-maximize.ts"+);++const WORK_AREA = { x: 0, y: 0, width: 1920, height: 1040 };++test("a maximized window always restores", () => {+ assert.equal(+ planMaximizeToggle({+ isMaximized: true,+ isFullScreen: false,+ platform: "win32",+ bounds: WORK_AREA,+ workArea: WORK_AREA,+ }),+ "unmaximize",+ );+});++test("a pseudo-maximized window (flag lost, bounds cover the work area) resyncs and restores on Windows", () => {+ assert.equal(+ planMaximizeToggle({+ isMaximized: false,+ isFullScreen: false,+ platform: "win32",+ bounds: { x: -8, y: -8, width: 1936, height: 1088 },+ workArea: WORK_AREA,+ }),+ "resyncUnmaximize",+ );+});++test("the geometric fallback is Windows-only", () => {+ // Outside win32 a work-area-filling window is an ordinary user-resized+ // window: the maximize() flag sets fine there, and a resync would only+ // risk regressing the working toggle.+ for (const platform of ["darwin", "linux"]) {+ assert.equal(+ planMaximizeToggle({+ isMaximized: false,+ isFullScreen: false,+ platform,+ bounds: { x: -8, y: -8, width: 1936, height: 1088 },+ workArea: WORK_AREA,+ }),+ "maximize",+ `${platform} keeps the plain maximize path`,+ );+ }+});++test("slack tolerates off-by-small bounds from DPI rounding and display changes", () => {+ assert.equal(+ boundsCoverWorkArea(+ { x: -6, y: -6, width: 1930, height: 1050 },+ WORK_AREA,+ ),+ true,+ );+ assert.equal(+ boundsCoverWorkArea(+ { x: -40, y: -40, width: 2000, height: 1120 },+ WORK_AREA,+ ),+ true,+ );+ assert.equal(+ boundsCoverWorkArea(+ { x: 0, y: 0, width: 1900, height: 1040 },+ WORK_AREA,+ ),+ false,+ "a window narrower than the work area does not cover it",+ );+});++test("an ordinary windowed window maximizes", () => {+ assert.equal(+ planMaximizeToggle({+ isMaximized: false,+ isFullScreen: false,+ platform: "win32",+ bounds: { x: 100, y: 100, width: 800, height: 600 },+ workArea: WORK_AREA,+ }),+ "maximize",+ );+});++test("a fullscreen window never takes the pseudo-maximized path", () => {+ assert.equal(+ planMaximizeToggle({+ isMaximized: false,+ isFullScreen: true,+ platform: "win32",+ bounds: { x: 0, y: 0, width: 1920, height: 1080 },+ workArea: WORK_AREA,+ }),+ "maximize",+ "fullscreen must keep the exit-fullscreen-then-maximize interlock (#1655)",+ );+});++test("a missing work area falls back to the plain maximize path", () => {+ assert.equal(+ planMaximizeToggle({+ isMaximized: false,+ isFullScreen: false,+ platform: "win32",+ bounds: WORK_AREA,+ workArea: null,+ }),+ "maximize",+ );+});