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(plugin-views): scale CSS bounds to DIPs for non-100% zoom
ZxlDragonDoctor4754d9f25ac1
diff --git a/apps/desktop/electron/main/plugin-view-bounds.ts b/apps/desktop/electron/main/plugin-view-bounds.tsnew file mode 100644index 000000000..f018b45d6--- /dev/null+++ b/apps/desktop/electron/main/plugin-view-bounds.ts@@ -0,0 +1,23 @@+export type PluginViewBounds = {+ x: number;+ y: number;+ width: number;+ height: number;+};++/**+ * Convert a renderer CSS-pixel rect into the unscaled DIPs+ * `WebContentsView.setBounds` expects. At zoomFactor 1 the two spaces match.+ */+export function scaleBoundsToDip(+ bounds: PluginViewBounds,+ zoomFactor: number,+): PluginViewBounds {+ const zoom = Number.isFinite(zoomFactor) && zoomFactor > 0 ? zoomFactor : 1;+ return {+ x: Math.max(0, Math.round((Number(bounds.x) || 0) * zoom)),+ y: Math.max(0, Math.round((Number(bounds.y) || 0) * zoom)),+ width: Math.max(0, Math.round((Number(bounds.width) || 0) * zoom)),+ height: Math.max(0, Math.round((Number(bounds.height) || 0) * zoom)),+ };+}diff --git a/apps/desktop/electron/main/plugin-view-host.ts b/apps/desktop/electron/main/plugin-view-host.tsindex 94082b5e3..468f8228a 100644--- a/apps/desktop/electron/main/plugin-view-host.ts+++ b/apps/desktop/electron/main/plugin-view-host.ts@@ -9,6 +9,10 @@ import {planLocationDelivery,viewEntryUrl,} from "./plugin-view-location";+import {+ scaleBoundsToDip,+ type PluginViewBounds,+} from "./plugin-view-bounds";import {applyPluginEgressPolicy,pluginSessionPartition,@@ -70,12 +74,7 @@ export type PluginViewOpenRequest = {location?: string;};-export type PluginViewBounds = {- x: number;- y: number;- width: number;- height: number;-};+export type { PluginViewBounds };type LiveView = {key: string;@@ -105,6 +104,8 @@ export class PluginViewHost {/** The one view currently attached to the window, if any. */private visibleKey: string | null = null;private bounds: PluginViewBounds = { x: 0, y: 0, width: 0, height: 0 };+ /** Last CSS-pixel rect from the renderer, so zoom changes can rescale. */+ private lastCssBounds: PluginViewBounds | null = null;private clock = 0;private onBlockedRequest?: PluginPanelBlockedRequest;/**@@ -153,7 +154,41 @@ export class PluginViewHost {setWindow(window: BrowserWindow | null): void {if (this.window === window) return;this.detachVisible();+ this.unbindZoom();this.window = window;+ this.bindZoom();+ }++ private zoomListener: (() => void) | null = null;++ private bindZoom(): void {+ const contents = this.window && !this.window.isDestroyed() ? this.window.webContents : null;+ if (!contents || contents.isDestroyed()) return;+ const onChange = () => {+ if (this.lastCssBounds) {+ this.bounds = scaleBoundsToDip(this.lastCssBounds, this.currentZoomFactor());+ }+ const visible = this.visibleKey ? this.views.get(this.visibleKey) : null;+ visible?.view.setBounds(this.bounds);+ this.emitSurface();+ };+ try {+ contents.on("zoom-changed", onChange);+ } catch {+ // Older Electron may not emit zoom-changed; resize remeasures.+ }+ this.zoomListener = () => {+ try {+ contents.removeListener("zoom-changed", onChange);+ } catch {+ // contents already destroyed+ }+ };+ }++ private unbindZoom(): void {+ this.zoomListener?.();+ this.zoomListener = null;}/** Whether a live web contents exists for this view. */@@ -247,17 +282,26 @@ export class PluginViewHost {}setBounds(bounds: PluginViewBounds): void {- this.bounds = {- x: Math.max(0, Math.round(Number(bounds.x) || 0)),- y: Math.max(0, Math.round(Number(bounds.y) || 0)),- width: Math.max(0, Math.round(Number(bounds.width) || 0)),- height: Math.max(0, Math.round(Number(bounds.height) || 0)),+ // Renderer measures CSS pixels; WebContentsView.setBounds wants DIPs.+ this.lastCssBounds = {+ x: Number(bounds.x) || 0,+ y: Number(bounds.y) || 0,+ width: Number(bounds.width) || 0,+ height: Number(bounds.height) || 0,};+ this.bounds = scaleBoundsToDip(this.lastCssBounds, this.currentZoomFactor());const visible = this.visibleKey ? this.views.get(this.visibleKey) : null;visible?.view.setBounds(this.bounds);this.emitSurface();}+ private currentZoomFactor(): number {+ const contents = this.window && !this.window.isDestroyed() ? this.window.webContents : null;+ if (!contents || contents.isDestroyed()) return 1;+ const zoom = Number(contents.getZoomFactor());+ return Number.isFinite(zoom) && zoom > 0 ? zoom : 1;+ }+/*** Show exactly one view, or none.*diff --git a/apps/desktop/test/plugin-view-zoom-bounds.test.mjs b/apps/desktop/test/plugin-view-zoom-bounds.test.mjsnew file mode 100644index 000000000..135aa198e--- /dev/null+++ b/apps/desktop/test/plugin-view-zoom-bounds.test.mjs@@ -0,0 +1,37 @@+import test from "node:test";+import assert from "node:assert/strict";+import { scaleBoundsToDip } from "../electron/main/plugin-view-bounds.ts";++test("scaleBoundsToDip leaves a 100% zoom rect unchanged", () => {+ assert.deepEqual(+ scaleBoundsToDip({ x: 10, y: 20, width: 300, height: 200 }, 1),+ { x: 10, y: 20, width: 300, height: 200 },+ );+});++test("scaleBoundsToDip multiplies CSS pixels by zoomFactor", () => {+ // At 90% zoom a CSS x of 1333 maps to DIP 1200 — matching the ~148px+ // right-shift reported in #980 when CSS pixels were passed through.+ assert.deepEqual(+ scaleBoundsToDip({ x: 1333, y: 40, width: 400, height: 800 }, 0.9),+ { x: 1200, y: 36, width: 360, height: 720 },+ );+});++test("scaleBoundsToDip multiplies out for zoom-in and clamps negatives", () => {+ assert.deepEqual(+ scaleBoundsToDip({ x: -5, y: 10, width: 100, height: 50 }, 1.25),+ { x: 0, y: 13, width: 125, height: 63 },+ );+});++test("scaleBoundsToDip treats invalid zoom as 1", () => {+ assert.deepEqual(+ scaleBoundsToDip({ x: 8, y: 8, width: 8, height: 8 }, 0),+ { x: 8, y: 8, width: 8, height: 8 },+ );+ assert.deepEqual(+ scaleBoundsToDip({ x: 8, y: 8, width: 8, height: 8 }, Number.NaN),+ { x: 8, y: 8, width: 8, height: 8 },+ );+});