Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
perf(terminal): one rect read per frame in the zoom mouse fix
enese43fe37a0ece
diff --git a/src/renderer/terminal/scale-fix.test.ts b/src/renderer/terminal/scale-fix.test.tsnew file mode 100644index 000000000..7a3e41bba--- /dev/null+++ b/src/renderer/terminal/scale-fix.test.ts@@ -0,0 +1,49 @@+import { describe, it, expect } from 'vitest'+import type { Terminal } from '@xterm/xterm'+import { patchTerminalScale } from './scale-fix'++function fixture() {+ const seen: number[] = []+ const ms = {+ getCoords: (e: MouseEvent, ..._rest: unknown[]) => (seen.push(e.clientX), [0, 0]),+ getMouseReportCoords: (e: MouseEvent, ..._rest: unknown[]) => (seen.push(e.clientX), undefined)+ }+ const term = { _core: { _mouseService: ms } } as unknown as Terminal+ let rectReads = 0+ const el = { getBoundingClientRect: () => (rectReads++, { left: 100, top: 50 }) } as unknown as HTMLElement+ const frames: Array<() => void> = []+ patchTerminalScale(term, () => 0.5, (cb) => frames.push(cb))+ return { ms, el, seen, reads: () => rectReads, flushFrame: () => frames.splice(0).forEach((f) => f()) }+}++const ev = (x: number) => ({ clientX: x, clientY: 60 }) as MouseEvent++describe('patchTerminalScale', () => {+ it('still converts coordinates by the zoom', () => {+ const f = fixture()+ f.ms.getCoords(ev(120), f.el, 80, 24, false)+ expect(f.seen[0]).toBe(140) // 100 + (120-100)/0.5+ })++ it('reads the rect once per frame across calls', () => {+ const f = fixture()+ f.ms.getCoords(ev(120), f.el, 80, 24, false)+ f.ms.getMouseReportCoords(ev(130), f.el)+ f.ms.getCoords(ev(140), f.el, 80, 24, false)+ expect(f.reads()).toBe(1)+ f.flushFrame()+ f.ms.getCoords(ev(150), f.el, 80, 24, false)+ expect(f.reads()).toBe(2) // the next frame re-reads (the canvas may have moved)+ })++ it('does not read the rect at scale 1', () => {+ const seen: number[] = []+ const ms = { getCoords: (e: MouseEvent, ..._rest: unknown[]) => (seen.push(e.clientX), [0, 0]) }+ const term = { _core: { _mouseService: ms } } as unknown as Terminal+ let reads = 0+ const el = { getBoundingClientRect: () => (reads++, { left: 0, top: 0 }) } as unknown as HTMLElement+ patchTerminalScale(term, () => 1, () => 0)+ ms.getCoords(ev(10), el, 80, 24, false)+ expect(reads).toBe(0)+ })+})diff --git a/src/renderer/terminal/scale-fix.ts b/src/renderer/terminal/scale-fix.tsindex 44ca80544..ce15e7b9e 100644--- a/src/renderer/terminal/scale-fix.ts+++ b/src/renderer/terminal/scale-fix.ts@@ -15,16 +15,42 @@ import type { Terminal } from '@xterm/xterm'* which makes `fakeClientX - rect.left = (clientX - rect.left) / scale` — the correct* unscaled offset. No-ops at scale 1.*/-export function patchTerminalScale(term: Terminal, getScale: () => number): void {+export function patchTerminalScale(+ term: Terminal,+ getScale: () => number,+ raf: (cb: () => void) => unknown = (cb) => requestAnimationFrame(cb)+): void {// eslint-disable-next-line @typescript-eslint/no-explicit-anyconst ms: any = (term as unknown as { _core?: { _mouseService?: unknown } })._core?._mouseServiceif (!ms || ms.__scalePatched) returnms.__scalePatched = true+ // One rect read per animation frame: a mouse move runs getCoords AND getMouseReportCoords (and+ // the link providers call getCoords again), and each fresh getBoundingClientRect() forced a+ // style/layout read. The rect only changes when the canvas moves, which happens on a frame+ // boundary, so a frame-scoped cache is exact.+ let rectCache = new WeakMap<HTMLElement, DOMRect>()+ let clearQueued = false+ const rectOf = (el: HTMLElement): DOMRect => {+ let r = rectCache.get(el)+ if (!r) {+ r = el.getBoundingClientRect()+ rectCache.set(el, r)+ if (!clearQueued) {+ clearQueued = true+ raf(() => {+ clearQueued = false+ rectCache = new WeakMap()+ })+ }+ }+ return r+ }+const adjust = (event: MouseEvent, element: HTMLElement): MouseEvent => {const s = getScale() || 1if (Math.abs(s - 1) < 0.001) return event- const rect = element.getBoundingClientRect()+ const rect = rectOf(element)return new Proxy(event, {get(target, prop) {if (prop === 'clientX') return rect.left + (event.clientX - rect.left) / s