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(desktop): keep the main window inside the work area on restore
vastsa527bec7084f2
diff --git a/apps/desktop/electron/main/bootstrap/window.ts b/apps/desktop/electron/main/bootstrap/window.tsindex a9c3e78e9..511d629d3 100644--- a/apps/desktop/electron/main/bootstrap/window.ts+++ b/apps/desktop/electron/main/bootstrap/window.ts@@ -17,6 +17,7 @@ import type { PluginViewHost } from "../plugin-view-host";import {baseWindowBounds,clampBoundsOriginToWorkArea,+ clampBoundsToWorkArea,displayWorkAreaKey,emptyWorkPanelReservationState,isWorkPanelOuterResizeEdge,@@ -151,10 +152,24 @@ export async function createWindow({windowMinWidth,windowMinHeight,);+ // A persisted rect can exceed the current work area when the display scale or+ // the Windows accessibility "text size" changed since it was saved: fit it+ // back inside the target display so the window never restores off-screen+ // (issue #544). The minimum size is capped to the work area for the same+ // reason — a minimum wider than the screen would defeat the clamp.+ const restoreDisplay = screen.getDisplayMatching(+ savedState ?? { x: 0, y: 0, width: 1200, height: 800 },+ );+ const restoreWorkArea = restoreDisplay.workArea;+ const restoredBounds = savedState+ ? clampBoundsToWorkArea(savedState, restoreWorkArea)+ : null;+ const initialMinWidth = Math.min(windowMinWidth, restoreWorkArea.width);+ const initialMinHeight = Math.min(windowMinHeight, restoreWorkArea.height);windowState.mainWindow = new BrowserWindow({- ...(savedState ?? { width: 1200, height: 800 }),- minWidth: windowMinWidth,- minHeight: windowMinHeight,+ ...(restoredBounds ?? { width: 1200, height: 800 }),+ minWidth: initialMinWidth,+ minHeight: initialMinHeight,title: APP_NAME,show: false,// Keep native edge/corner resizing explicit. Frameless chrome owns the@@ -199,7 +214,9 @@ export async function createWindow({});const window = windowState.mainWindow;const initialBounds = window.getBounds();- windowState.workPanelBaseBounds = savedState ? { ...savedState } : { ...initialBounds };+ windowState.workPanelBaseBounds = restoredBounds+ ? { ...restoredBounds }+ : { ...initialBounds };windowState.workPanelLastAppliedBounds = { ...initialBounds };resetMenuRendererReady(window);const isLiveWindow = () =>@@ -334,7 +351,13 @@ export async function createWindow({windowState.workPanelNativeResizeActive = true;// Let the right edge reach the panel minimum while the base chat width// remains fixed. The normal minimum is restored after the gesture settles.- window.setMinimumSize(baseBounds.width + WORK_PANEL_MIN_WIDTH, windowMinHeight);+ // Never demand more width than the current work area offers, or a narrow+ // display could no longer shrink the window back on-screen (issue #544).+ const resizeWorkArea = screen.getDisplayMatching(currentBounds).workArea;+ window.setMinimumSize(+ Math.min(baseBounds.width + WORK_PANEL_MIN_WIDTH, resizeWorkArea.width),+ Math.min(windowMinHeight, resizeWorkArea.height),+ );return nativeWorkPanelResize;};@@ -499,6 +522,7 @@ export async function createWindow({window.on("enter-full-screen", sendFullScreen);window.on("leave-full-screen", () => {sendFullScreen();+ refitWindowToWorkArea();scheduleWorkPanelReservation();});window.webContents.on("did-finish-load", sendFullScreen);@@ -509,6 +533,29 @@ export async function createWindow({maximized: window.isMaximized(),});};++ // 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+ // 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 = () => {+ if (!isLiveWindow() || process.platform === "darwin") return;+ if (window.isMaximized() || window.isFullScreen() || window.isMinimized()) return;+ const currentBounds = window.getBounds();+ const workArea = screen.getDisplayMatching(currentBounds).workArea;+ window.setMinimumSize(+ Math.min(windowMinWidth, workArea.width),+ Math.min(windowMinHeight, workArea.height),+ );+ const fitted = clampBoundsToWorkArea(currentBounds, workArea);+ if (windowBoundsEqual(fitted, currentBounds)) return;+ windowState.expectedWorkPanelBounds = fitted;+ window.setBounds(fitted, false);+ const appliedBounds = window.getBounds();+ windowState.workPanelBaseBounds = { ...appliedBounds };+ windowState.workPanelLastAppliedBounds = { ...appliedBounds };+ };// Custom window controls (Windows/Linux) need maximize state to swap the// maximize/restore glyph.if (process.platform !== "darwin") {@@ -519,6 +566,7 @@ export async function createWindow({}window.on("unmaximize", () => {if (process.platform !== "darwin") sendMaximized();+ refitWindowToWorkArea();scheduleWorkPanelReservation();});diff --git a/apps/desktop/electron/main/work-panel-window.ts b/apps/desktop/electron/main/work-panel-window.tsindex 545684185..bd0f415bd 100644--- a/apps/desktop/electron/main/work-panel-window.ts+++ b/apps/desktop/electron/main/work-panel-window.ts@@ -126,6 +126,28 @@ export function clampBoundsOriginToWorkArea(};}+/**+ * Fits a rect entirely inside a work area for a restore/unmaximize path:+ * first shrinks the size so it never exceeds the work area, then clamps the+ * origin so no edge falls off-screen.+ *+ * Unlike {@link clampBoundsOriginToWorkArea}, this intentionally shrinks an+ * oversized rect. A persisted rect can exceed the current work area when the+ * display scale or the Windows accessibility "text size" changed since it was+ * saved (issue #544): the OS-reported work area and the window bounds both+ * live in the same scaled coordinate space, so clamping here keeps the window+ * fully visible regardless of which scale source inflated it. When the work+ * area is smaller than the rect, the fitted size wins over any minimum.+ */+export function clampBoundsToWorkArea(+ bounds: WindowBounds,+ workArea: WindowBounds,+): WindowBounds {+ const width = Math.min(bounds.width, workArea.width);+ const height = Math.min(bounds.height, workArea.height);+ return clampBoundsOriginToWorkArea({ ...bounds, width, height }, workArea);+}+export function planWorkPanelReservation({baseBounds,workArea,diff --git a/apps/desktop/test/work-panel-window.test.mjs b/apps/desktop/test/work-panel-window.test.mjsindex 721ef52bb..18523468f 100644--- a/apps/desktop/test/work-panel-window.test.mjs+++ b/apps/desktop/test/work-panel-window.test.mjs@@ -3,6 +3,7 @@ import test from "node:test";import {baseWindowBounds,clampBoundsOriginToWorkArea,+ clampBoundsToWorkArea,displayWorkAreaKey,emptyWorkPanelReservationState,isWorkPanelOuterResizeEdge,@@ -397,6 +398,30 @@ test("normalizing a dropped window moves its origin without resizing it", () =>);});+test("restoring a window shrinks an oversized rect to fit the work area", () => {+ // Regression for issue #544: a display-scale or accessibility "text size"+ // change can leave a persisted rect wider/taller than the current work area.+ // The restore path must fit it back on-screen, size included.+ const restoreArea = { x: 0, y: 0, width: 1280, height: 912 };+ assert.deepEqual(+ clampBoundsToWorkArea(+ { x: -37, y: 22, width: 1572, height: 1056 },+ restoreArea,+ ),+ { x: 0, y: 0, width: 1280, height: 912 },+ );+ // A rect that already fits keeps its size and only its origin is clamped.+ assert.deepEqual(+ clampBoundsToWorkArea({ x: 900, y: 700, width: 800, height: 600 }, restoreArea),+ { x: 480, y: 312, width: 800, height: 600 },+ );+ // A rect fully inside the work area is returned unchanged.+ assert.deepEqual(+ clampBoundsToWorkArea({ x: 40, y: 30, width: 1000, height: 700 }, restoreArea),+ { x: 40, y: 30, width: 1000, height: 700 },+ );+});+test("a drag onto a smaller display keeps the base size restorable", () => {const smallWorkArea = { x: 1920, y: 0, width: 1280, height: 800 };const base = { x: 100, y: 80, width: 1600, height: 900 };