Skip to content

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

fix(window): restore pseudo-maximized windows to an explicit windowed rect

zhoulaihuib4bae22575cc
diff --git a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
index 7a9afbd41..0aed8406f 100644
--- a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
+++ b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
@@ -20,7 +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 { planMaximizeToggle, windowedRestoreBounds } from "../window-maximize";
import { createTraySessions } from "../tray-sessions";
import { createTaskbarUnreadBadge } from "../taskbar-unread-badge";
import { createWindow, type WindowLifecycleState } from "./window";
@@ -503,18 +503,31 @@ export function createApplicationLifecycle({
// 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 workArea = workAreaForWindow(target);
const maximizePlan = planMaximizeToggle({
isMaximized: target.isMaximized(),
isFullScreen: isWindowFullScreen(target),
platform: process.platform,
bounds: target.getBounds(),
- workArea: workAreaForWindow(target),
+ workArea,
});
if (maximizePlan === "unmaximize") {
target.unmaximize();
- } else if (maximizePlan === "resyncUnmaximize") {
- target.maximize();
- target.unmaximize();
+ } else if (maximizePlan === "restoreWindowed") {
+ // `restoreWindowed` implies a work area, but the types cannot carry
+ // that narrowing across the plan value.
+ if (!workArea) {
+ target.unmaximize();
+ } else {
+ target.setBounds(
+ windowedRestoreBounds({
+ workArea,
+ lastNormal: state.workPanelLastAppliedBounds,
+ minWidth: windowMinWidth,
+ minHeight: windowMinHeight,
+ }),
+ );
+ }
} else {
if (isWindowFullScreen(target)) {
setWindowFullScreen(
diff --git a/apps/desktop/electron/main/plugin-panel-host.ts b/apps/desktop/electron/main/plugin-panel-host.ts
index f671a4a30..50609d018 100644
--- a/apps/desktop/electron/main/plugin-panel-host.ts
+++ b/apps/desktop/electron/main/plugin-panel-host.ts
@@ -8,7 +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 { planMaximizeToggle, windowedRestoreBounds } from "./window-maximize";
import { getModuleDirectory } from "./module-path";
import { PanelSenders, panelReadyWithin, pageGoneWithin, resolvePanelInvocation } from "./plugin-panel-senders";
import { PanelOperationSerializer } from "./plugin-panel-senders";
@@ -401,19 +401,33 @@ export class PluginPanelHost {
// 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.
+ // fallback catches that pseudo-maximized state. Panel windows have
+ // no persisted normal bounds, so the restore target is computed.
+ const workArea = workAreaForWindow(window);
const plan = planMaximizeToggle({
isMaximized: window.isMaximized(),
isFullScreen: window.isFullScreen(),
platform: process.platform,
bounds: window.getBounds(),
- workArea: workAreaForWindow(window),
+ workArea,
});
if (plan === "unmaximize") {
window.unmaximize();
- } else if (plan === "resyncUnmaximize") {
- window.maximize();
- window.unmaximize();
+ } else if (plan === "restoreWindowed") {
+ // `restoreWindowed` implies a work area, but the types cannot carry
+ // that narrowing across the plan value.
+ if (!workArea) {
+ window.unmaximize();
+ } else {
+ window.setBounds(
+ windowedRestoreBounds({
+ workArea,
+ lastNormal: null,
+ minWidth: 0,
+ minHeight: 0,
+ }),
+ );
+ }
} else {
window.maximize();
}
diff --git a/apps/desktop/electron/main/window-maximize.ts b/apps/desktop/electron/main/window-maximize.ts
index 4a9ea2569..6af6b9260 100644
--- a/apps/desktop/electron/main/window-maximize.ts
+++ b/apps/desktop/electron/main/window-maximize.ts
@@ -4,13 +4,16 @@ 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
+ * - `restoreWindowed`: 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.
+ * quirk behind issue #1993). Electron has no maximized state to restore
+ * into here — and on the transparent Win10 main window the recorded restore
+ * bounds are the full rect itself, so `maximize()+unmaximize()` is a
+ * no-op (review feedback on PR #2420) — so the toggle sets an explicit
+ * windowed rect instead.
* - `maximize`: a plain windowed window; maximize it.
*/
-export type MaximizeTogglePlan = "unmaximize" | "resyncUnmaximize" | "maximize";
+export type MaximizeTogglePlan = "unmaximize" | "restoreWindowed" | "maximize";
/**
* Bounds and work areas are integer pixels, but display changes and DPI
@@ -55,7 +58,53 @@ export function planMaximizeToggle(input: {
input.workArea &&
boundsCoverWorkArea(input.bounds, input.workArea)
) {
- return "resyncUnmaximize";
+ return "restoreWindowed";
}
return "maximize";
}
+
+/**
+ * The windowed rect a pseudo-maximized window is restored to.
+ *
+ * `lastNormal` is trusted only when it is a real windowed size: fully inside
+ * the work area and not covering it. That matters because in the flag-lost
+ * state Win32 reports the full rect as the window's normal position, so any
+ * bounds recorded after the flag was lost are the very rect we are escaping.
+ * Without a trustworthy rect the window gets two thirds of the work area,
+ * centered.
+ */
+export function windowedRestoreBounds(input: {
+ workArea: Rectangle;
+ lastNormal: Rectangle | null;
+ minWidth: number;
+ minHeight: number;
+}): Rectangle {
+ const { workArea } = input;
+ const lastNormal = input.lastNormal;
+ if (
+ lastNormal &&
+ lastNormal.width >= input.minWidth &&
+ lastNormal.height >= input.minHeight &&
+ !boundsCoverWorkArea(lastNormal, workArea) &&
+ lastNormal.x >= workArea.x &&
+ lastNormal.y >= workArea.y &&
+ lastNormal.x + lastNormal.width <= workArea.x + workArea.width &&
+ lastNormal.y + lastNormal.height <= workArea.y + workArea.height
+ ) {
+ return lastNormal;
+ }
+ const width = Math.min(
+ Math.max(Math.round(workArea.width * 2 / 3), input.minWidth),
+ workArea.width,
+ );
+ const height = Math.min(
+ Math.max(Math.round(workArea.height * 2 / 3), input.minHeight),
+ workArea.height,
+ );
+ return {
+ x: Math.round(workArea.x + (workArea.width - width) / 2),
+ y: Math.round(workArea.y + (workArea.height - height) / 2),
+ width,
+ height,
+ };
+}
diff --git a/apps/desktop/test/window-maximize-toggle.test.mjs b/apps/desktop/test/window-maximize-toggle.test.mjs
index cee7f4b56..4a8691e2d 100644
--- a/apps/desktop/test/window-maximize-toggle.test.mjs
+++ b/apps/desktop/test/window-maximize-toggle.test.mjs
@@ -7,11 +7,15 @@
* `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.
+ * pseudo-maximized, and the toggle restores it to an explicit windowed rect
+ * (Electron's own restore bounds are the full rect in that state, so
+ * `maximize()+unmaximize()` is a no-op — review feedback on PR #2420).
*
* 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.
+ * interlock from issue #1655 stays intact; and the geometric fallback is
+ * Windows-only, because elsewhere a work-area-filling window is an ordinary
+ * user-resized window.
*/
import assert from "node:assert/strict";
import test from "node:test";
@@ -22,7 +26,7 @@ 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(
+const { boundsCoverWorkArea, planMaximizeToggle, windowedRestoreBounds } = await import(
"../electron/main/window-maximize.ts"
);
@@ -41,7 +45,7 @@ test("a maximized window always restores", () => {
);
});
-test("a pseudo-maximized window (flag lost, bounds cover the work area) resyncs and restores on Windows", () => {
+test("a pseudo-maximized window (flag lost, bounds cover the work area) restores to a windowed rect on Windows", () => {
assert.equal(
planMaximizeToggle({
isMaximized: false,
@@ -50,14 +54,14 @@ test("a pseudo-maximized window (flag lost, bounds cover the work area) resyncs
bounds: { x: -8, y: -8, width: 1936, height: 1088 },
workArea: WORK_AREA,
}),
- "resyncUnmaximize",
+ "restoreWindowed",
);
});
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.
+ // window: the maximize() flag sets fine there, and intercepting it would
+ // only regress the working toggle.
for (const platform of ["darwin", "linux"]) {
assert.equal(
planMaximizeToggle({
@@ -87,6 +91,7 @@ test("slack tolerates off-by-small bounds from DPI rounding and display changes"
WORK_AREA,
),
true,
+ "covering means the window reaches past the work area, not exact equality",
);
assert.equal(
boundsCoverWorkArea(
@@ -137,3 +142,67 @@ test("a missing work area falls back to the plain maximize path", () => {
"maximize",
);
});
+
+test("trustworthy last normal bounds win over the computed rect", () => {
+ const lastNormal = { x: 200, y: 150, width: 1100, height: 700 };
+ assert.deepEqual(
+ windowedRestoreBounds({
+ workArea: WORK_AREA,
+ lastNormal,
+ minWidth: 400,
+ minHeight: 300,
+ }),
+ lastNormal,
+ );
+});
+
+test("last normal bounds are rejected when they cover the work area (flag-loss pollution)", () => {
+ // In the flag-lost state Win32 reports the full rect as the normal
+ // position, so bounds recorded afterwards are the rect being escaped.
+ assert.deepEqual(
+ windowedRestoreBounds({
+ workArea: WORK_AREA,
+ lastNormal: { x: 0, y: 0, width: 1920, height: 1040 },
+ minWidth: 400,
+ minHeight: 300,
+ }),
+ { x: 320, y: 174, width: 1280, height: 693 },
+ "falls back to two thirds of the work area, centered",
+ );
+});
+
+test("last normal bounds are rejected when they leave the work area", () => {
+ assert.deepEqual(
+ windowedRestoreBounds({
+ workArea: WORK_AREA,
+ lastNormal: { x: -500, y: 0, width: 800, height: 600 },
+ minWidth: 400,
+ minHeight: 300,
+ }),
+ { x: 320, y: 174, width: 1280, height: 693 },
+ );
+});
+
+test("last normal bounds below the minimum size are rejected", () => {
+ assert.deepEqual(
+ windowedRestoreBounds({
+ workArea: WORK_AREA,
+ lastNormal: { x: 100, y: 100, width: 200, height: 100 },
+ minWidth: 400,
+ minHeight: 300,
+ }),
+ { x: 320, y: 174, width: 1280, height: 693 },
+ );
+});
+
+test("no last normal bounds yields two thirds of the work area, centered", () => {
+ assert.deepEqual(
+ windowedRestoreBounds({
+ workArea: WORK_AREA,
+ lastNormal: null,
+ minWidth: 0,
+ minHeight: 0,
+ }),
+ { x: 320, y: 174, width: 1280, height: 693 },
+ );
+});