Skip to content

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

fix(window): keep fullscreen and maximize mutually exclusive

AnJiaf74db71788f1
diff --git a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
index 6ca888118..d1998c03e 100644
--- a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
+++ b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
@@ -442,19 +442,57 @@ export function createApplicationLifecycle({
case "resetZoom":
contents.setZoomFactor(1);
break;
- case "toggleFullScreen":
+ case "toggleFullScreen": {
+ // Entering fullscreen from a maximized window leaves both flags set on
+ // Windows: `setFullScreen` does not clear the maximize state, and the
+ // maximized state is what `refitWindowToWorkArea` reads first to bail
+ // out. The result is a window that never fits itself back into the
+ // current work area and ends up stranded on another display (issue
+ // #1655). Unmaximize first, then toggle — and remember the windowed
+ // state so leaving fullscreen returns to what the user actually had
+ // rather than a restored-size window.
+ if (!isWindowFullScreen(target)) {
+ const wasMaximized = target.isMaximized();
+ if (wasMaximized) target.unmaximize();
+ if (wasMaximized) {
+ target.once("leave-full-screen", () => {
+ if (target.isDestroyed() || isWindowFullScreen(target)) return;
+ target.maximize();
+ });
+ }
+ setWindowFullScreen(
+ target,
+ true,
+ process.platform === "win32" && target === state.mainWindow,
+ );
+ break;
+ }
setWindowFullScreen(
target,
- !isWindowFullScreen(target),
+ false,
process.platform === "win32" && target === state.mainWindow,
);
break;
+ }
case "minimize":
target.minimize();
break;
case "toggleMaximize":
- if (target.isMaximized()) target.unmaximize();
- else target.maximize();
+ // 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()) {
+ target.unmaximize();
+ } else {
+ if (isWindowFullScreen(target)) {
+ setWindowFullScreen(
+ target,
+ false,
+ process.platform === "win32" && target === state.mainWindow,
+ );
+ }
+ target.maximize();
+ }
break;
case "close":
target.close();
diff --git a/apps/desktop/electron/main/bootstrap/window.ts b/apps/desktop/electron/main/bootstrap/window.ts
index 968e2d451..6337f325d 100644
--- a/apps/desktop/electron/main/bootstrap/window.ts
+++ b/apps/desktop/electron/main/bootstrap/window.ts
@@ -597,7 +597,7 @@ export async function createWindow({
window.on("enter-full-screen", sendFullScreen);
window.on("leave-full-screen", () => {
sendFullScreen();
- refitWindowToWorkArea();
+ refitWindowToWorkArea(true);
scheduleWorkPanelReservation();
});
window.webContents.on("did-finish-load", sendFullScreen);
@@ -614,9 +614,20 @@ export async function createWindow({
// area — e.g. a display-scale or accessibility "text size" change inflated a
// persisted rect past the screen (issue #544). Fit them back inside so the
// window never lands partly off-screen. macOS keeps its own restore behavior.
- const refitWindowToWorkArea = () => {
+ // A `leave-full-screen` fires while the window may still report the stale
+ // maximize flag from the pre-fullscreen state (issue #1655). The refit must
+ // still run there, or the window keeps bounds sized for a display it is no
+ // longer on. Callers that legitimately sit on a maximized window (unmaximize,
+ // restore) keep the original bail-out.
+ const refitWindowToWorkArea = (ignoreMaximized = false) => {
if (!isLiveWindow() || process.platform === "darwin") return;
- if (window.isMaximized() || isWindowFullScreen(window) || window.isMinimized()) return;
+ if (
+ (window.isMaximized() && !ignoreMaximized) ||
+ isWindowFullScreen(window) ||
+ window.isMinimized()
+ ) {
+ return;
+ }
const currentBounds = window.getBounds();
const workArea = screen.getDisplayMatching(currentBounds).workArea;
const minimum = clampMinimumSizeToWorkArea(
@@ -706,7 +717,11 @@ export async function createWindow({
// A scale or text-size change shrinks the DIP work area in place; re-cap the
// minimum so the OS never enforces one the display cannot show (issue #1175).
// Keep the open work panel's reservation in the minimum; only cap it.
- if (isLiveWindow() && !window.isFullScreen()) {
+ // `isWindowFullScreen` rather than `isFullScreen`: the borderless Win32
+ // fallback leaves Electron's own flag false while the window really is
+ // fullscreen, and resizing the minimum then fights the fullscreen state
+ // (issue #1655).
+ if (isLiveWindow() && !isWindowFullScreen(window)) {
const minimum = clampMinimumSizeToWorkArea(
{ width: workPanelMinimumWindowWidth(), height: windowMinHeight },
screen.getDisplayMatching(window.getBounds()).workArea,
diff --git a/apps/desktop/electron/main/ipc/window-ipc.ts b/apps/desktop/electron/main/ipc/window-ipc.ts
index a1d61cb1c..50a8b560b 100644
--- a/apps/desktop/electron/main/ipc/window-ipc.ts
+++ b/apps/desktop/electron/main/ipc/window-ipc.ts
@@ -171,13 +171,13 @@ export function registerWindowIpc({
case "minimize":
mainWindow.minimize();
break;
+ // `toggleMaximize` and the rest run through the shared native-menu
+ // action so the maximize/fullscreen interlock (issue #1655) has one
+ // implementation: calling `mainWindow.maximize()` directly would skip
+ // the fullscreen-first rule and leave both flags set.
case "toggleMaximize":
- if (mainWindow.isMaximized()) mainWindow.unmaximize();
- else mainWindow.maximize();
- break;
case "close":
- mainWindow.close();
- break;
+ return executeNativeMenuAction(input.action as NativeMenuAction);
}
return {
maximized: !mainWindow.isDestroyed() && mainWindow.isMaximized(),
diff --git a/apps/desktop/test/window-fullscreen-maximize.test.mjs b/apps/desktop/test/window-fullscreen-maximize.test.mjs
new file mode 100644
index 000000000..d2c6a81a4
--- /dev/null
+++ b/apps/desktop/test/window-fullscreen-maximize.test.mjs
@@ -0,0 +1,196 @@
+/**
+ * Fullscreen / maximize state interlock on Windows (issue #1655).
+ *
+ * Behavioral: the borderless fullscreen helper keeps the tracked flag in sync
+ * with Electron's own, and the native-menu actions never leave both the
+ * maximize and fullscreen flags set at once. Entering fullscreen from a
+ * maximized window unmaximizes first; maximizing a fullscreen window leaves
+ * fullscreen first.
+ */
+import assert from "node:assert/strict";
+import { EventEmitter } from "node:events";
+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 { isWindowFullScreen, setWindowFullScreen } = await import(
+ "../electron/main/window-fullscreen.ts"
+);
+const { readMainSource } = await import("./helpers/source-contracts.mjs");
+
+const lifecycleSource = await readMainSource();
+
+function makeWindow() {
+ const window = new EventEmitter();
+ const calls = [];
+ let maximized = false;
+ let fullScreen = false;
+ window.getBounds = () => ({ x: 0, y: 0, width: 1200, height: 800 });
+ window.isDestroyed = () => false;
+ window.isMinimized = () => false;
+ window.isFocused = () => true;
+ window.isVisible = () => true;
+ window.isMaximized = () => maximized;
+ // On the Win32 borderless path Electron's own flag stays false, which is the
+ // whole reason the tracked flag exists.
+ window.isFullScreen = () => fullScreen;
+ window.setFullScreen = (value) => {
+ fullScreen = value;
+ window.emit(value ? "enter-full-screen" : "leave-full-screen");
+ };
+ window.unmaximize = () => {
+ calls.push("unmaximize");
+ maximized = false;
+ };
+ window.maximize = () => {
+ calls.push("maximize");
+ maximized = true;
+ };
+ return {
+ window,
+ calls,
+ setMaximized(value) {
+ maximized = value;
+ },
+ };
+}
+
+test("the tracked flag follows setWindowFullScreen for the borderless fallback", () => {
+ const fixture = makeWindow();
+ // Electron's own flag stays false on the Win32 borderless path, so the
+ // tracked flag alone decides the toggle state.
+ assert.equal(isWindowFullScreen(fixture.window), false);
+
+ setWindowFullScreen(fixture.window, true, true);
+ assert.equal(isWindowFullScreen(fixture.window), true);
+
+ setWindowFullScreen(fixture.window, false, true);
+ assert.equal(isWindowFullScreen(fixture.window), false);
+});
+test("the tracked flag is not written when the borderless fallback does not apply", () => {
+ const fixture = makeWindow();
+ // Without the Win32 borderless path only Electron's real flag is consulted,
+ // so the tracked set stays empty and the helper mirrors `setFullScreen`.
+ assert.equal(fixture.window.isFullScreen(), false);
+ setWindowFullScreen(fixture.window, true, false);
+ assert.equal(fixture.window.isFullScreen(), true);
+ assert.equal(isWindowFullScreen(fixture.window), true);
+ setWindowFullScreen(fixture.window, false, false);
+ assert.equal(isWindowFullScreen(fixture.window), false);
+});
+
+test("entering fullscreen from a maximized window unmaximizes first", () => {
+ const fixture = makeWindow();
+ fixture.setMaximized(true);
+
+ const wasMaximized = fixture.window.isMaximized();
+ if (!isWindowFullScreen(fixture.window) && wasMaximized) {
+ fixture.window.unmaximize();
+ }
+ setWindowFullScreen(fixture.window, true, true);
+
+ assert.deepEqual(fixture.calls, ["unmaximize"]);
+ assert.equal(isWindowFullScreen(fixture.window), true);
+ // The window must never be maximized and fullscreen at the same time.
+ assert.equal(fixture.window.isMaximized(), false);
+});
+
+test("maximizing a fullscreen window leaves fullscreen first", () => {
+ const fixture = makeWindow();
+ setWindowFullScreen(fixture.window, true, true);
+ assert.equal(isWindowFullScreen(fixture.window), true);
+
+ if (fixture.window.isMaximized()) {
+ fixture.window.unmaximize();
+ } else {
+ if (isWindowFullScreen(fixture.window)) {
+ setWindowFullScreen(fixture.window, false, true);
+ }
+ fixture.window.maximize();
+ }
+
+ assert.deepEqual(fixture.calls, ["maximize"]);
+ assert.equal(isWindowFullScreen(fixture.window), false);
+ assert.equal(fixture.window.isMaximized(), true);
+});
+
+test("the fullscreen toggle unmaximizes before it toggles", () => {
+ const block = lifecycleSource.slice(
+ lifecycleSource.indexOf('case "toggleFullScreen"'),
+ lifecycleSource.indexOf('case "minimize"'),
+ );
+ assert.match(block, /if \(!isWindowFullScreen\(target\)\) \{/);
+ assert.match(block, /const wasMaximized = target\.isMaximized\(\)/);
+ assert.match(block, /if \(wasMaximized\) target\.unmaximize\(\)/);
+ assert.match(block, /setWindowFullScreen\(/);
+});
+
+test("leaving fullscreen returns a previously maximized window to maximized", () => {
+ const fixture = makeWindow();
+ fixture.setMaximized(true);
+
+ // Enter fullscreen from maximized, mirroring `executeNativeMenuAction`.
+ if (!isWindowFullScreen(fixture.window)) {
+ const wasMaximized = fixture.window.isMaximized();
+ if (wasMaximized) fixture.window.unmaximize();
+ if (wasMaximized) {
+ fixture.window.once("leave-full-screen", () => {
+ if (fixture.window.isDestroyed() || isWindowFullScreen(fixture.window)) return;
+ fixture.window.maximize();
+ });
+ }
+ setWindowFullScreen(fixture.window, true, true);
+ }
+ assert.equal(isWindowFullScreen(fixture.window), true);
+ assert.equal(fixture.window.isMaximized(), false);
+
+ // Leaving fullscreen must restore the pre-fullscreen maximized state.
+ setWindowFullScreen(fixture.window, false, true);
+ assert.equal(isWindowFullScreen(fixture.window), false);
+ assert.equal(fixture.window.isMaximized(), true);
+});
+
+test("leaving fullscreen keeps a normal window at its normal size", () => {
+ const fixture = makeWindow();
+ assert.equal(fixture.window.isMaximized(), false);
+
+ setWindowFullScreen(fixture.window, true, true);
+ setWindowFullScreen(fixture.window, false, true);
+
+ assert.equal(isWindowFullScreen(fixture.window), false);
+ assert.equal(fixture.window.isMaximized(), false);
+ assert.deepEqual(fixture.calls, []);
+});
+
+test("the fullscreen toggle restores maximized on leave", () => {
+ const block = lifecycleSource.slice(
+ lifecycleSource.indexOf('case "toggleFullScreen"'),
+ lifecycleSource.indexOf('case "minimize"'),
+ );
+ assert.match(block, /target\.once\("leave-full-screen"/);
+ assert.match(block, /target\.maximize\(\)/);
+});
+
+test("the maximize toggle leaves fullscreen first", () => {
+ const block = lifecycleSource.slice(
+ lifecycleSource.indexOf('case "toggleMaximize"'),
+ lifecycleSource.indexOf('case "close"'),
+ );
+ assert.match(block, /if \(target\.isMaximized\(\)\)/);
+ assert.match(block, /isWindowFullScreen\(target\)/);
+ assert.match(block, /setWindowFullScreen\(/);
+ assert.match(block, /target\.maximize\(\)/);
+});
+
+test("the display-topology reconcile uses the tracked fullscreen flag", () => {
+ // The borderless Win32 path leaves Electron's own flag false while the
+ // window really is fullscreen, so only the tracked flag is reliable there.
+ assert.match(
+ lifecycleSource,
+ /if \(isLiveWindow\(\) && !isWindowFullScreen\(window\)\) \{/,
+ );
+});