Skip to content

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.

fix(presence): clear cursor chat on exit, anchor the input, zoom-invariant cursors

enesab298d61800b
diff --git a/src/renderer/components/PresenceLayer.tsx b/src/renderer/components/PresenceLayer.tsx
index 78de20d38..565418b02 100644
--- a/src/renderer/components/PresenceLayer.tsx
+++ b/src/renderer/components/PresenceLayer.tsx
@@ -1,10 +1,17 @@
-import { useEffect, useRef, useState } from 'react'
+import { useCallback, useEffect, useRef, useState } from 'react'
import { useShallow } from 'zustand/react/shallow'
-import { ViewportPortal, useReactFlow } from '@xyflow/react'
+import { ViewportPortal, useReactFlow, useStore } from '@xyflow/react'
import { CHAT_MAX_LEN } from '@shared/presence'
import { selectVisible, usePresence } from '../state/presence'
import { useProjects } from '../state/projects'
-import { CURSOR_MIN_INTERVAL_MS, canOpenCursorChat, type KeyTarget } from '../lib/presenceKeys'
+import {
+ CHAT_ANCHOR_OFFSET_PX,
+ CURSOR_HOTSPOT_PX,
+ CURSOR_MIN_INTERVAL_MS,
+ canOpenCursorChat,
+ counterScale,
+ type KeyTarget
+} from '../lib/presenceKeys'
// A sent chat line lingers this long after Enter, then clears itself.
const CHAT_LINGER_MS = 5000
@@ -20,8 +27,11 @@ const CHAT_LINGER_MS = 5000
*
* PERF: this is the only component subscribed to cursor traffic (see state/presence.ts). The
* sampler is a single rAF loop throttled to ~20 Hz that runs ONLY while another peer shares this
- * canvas — alone, this component renders null and installs no listeners. Smoothing between the
- * 20 Hz updates is a CSS transition on `transform`, not a JS lerp loop.
+ * canvas AND the local pointer is actually moving (it stops itself when nothing is pending and the
+ * pointer-move handler restarts it) — alone, this component renders null and installs no
+ * listeners. Smoothing between the 20 Hz updates is a CSS transition on `transform`, not a JS lerp
+ * loop. Zoom is read via React Flow's own store, so a zoom tick re-renders THIS layer only, never
+ * Canvas.
*/
export function PresenceLayer(): JSX.Element | null {
const activeProjectId = useProjects((s) => s.activeProjectId)
@@ -32,12 +42,50 @@ export function PresenceLayer(): JSX.Element | null {
const others = usePresence(useShallow((s) => selectVisible(s, activeProjectId || null)))
const hasOthers = others.length > 0
const { screenToFlowPosition } = useReactFlow()
+ // Live viewport zoom, straight from React Flow's store: this subscribes THIS component, so a
+ // zoom tick re-renders the cursor layer (cheap) and leaves Canvas.tsx untouched.
+ const zoom = useStore((s) => s.transform[2])
// Cursor-chat input (local). `null` = closed.
const [chat, setChat] = useState<string | null>(null)
- // Where to anchor the local chat input on screen (client coords of the last pointer position).
+ // Where the input is pinned on screen: a SNAPSHOT of the pointer taken when the chat opened, not
+ // the live pointer. Read during render, so it must be state — reading the live pointer ref here
+ // would make the "anchor" chase the mouse whenever a peer's cursor happened to re-render us.
+ const [anchor, setAnchor] = useState<{ x: number; y: number } | null>(null)
+ // The last pointer position (client coords), updated at pointer rate. Never read during render.
const screenRef = useRef<{ x: number; y: number }>({ x: 0, y: 0 })
const lingerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
+ // True while peers are showing a chat line of ours that we have not retracted yet — so we know
+ // whether a `chat(null)` is owed (on close, on the last peer leaving, on unmount).
+ const publishedRef = useRef(false)
+
+ const sendChat = useCallback((text: string | null): void => {
+ if (text === null && !publishedRef.current) return // nothing to retract — don't spam the wire
+ publishedRef.current = text !== null
+ window.nodeTerminal.presence.chat(text)
+ }, [])
+
+ /** Close the input and drop the local state. `linger`: keep the bubble up for the readers for a
+ * few seconds (Enter), then retract it. Safe to call when nothing is open. */
+ const closeChat = useCallback(
+ (linger: boolean): void => {
+ setChat(null)
+ setAnchor(null)
+ if (lingerRef.current) {
+ clearTimeout(lingerRef.current)
+ lingerRef.current = null
+ }
+ if (linger && publishedRef.current) {
+ lingerRef.current = setTimeout(() => {
+ lingerRef.current = null
+ sendChat(null)
+ }, CHAT_LINGER_MS)
+ } else {
+ sendChat(null)
+ }
+ },
+ [sendChat]
+ )
// ---- local cursor sampling (rAF + ~20 Hz), only while someone else is here ----
useEffect(() => {
@@ -46,19 +94,19 @@ export function PresenceLayer(): JSX.Element | null {
let last = 0
let pending: { x: number; y: number } | null = null
- const onPointerMove = (e: PointerEvent): void => {
- pending = { x: e.clientX, y: e.clientY }
- screenRef.current = pending
- }
- const onPointerLeave = (): void => {
- pending = null
- window.nodeTerminal.presence.cursor(null)
+ // The loop runs ONLY while a sample is pending: it stops itself when the pointer goes still
+ // (an idle canvas must not burn a 60 Hz rAF forever) and onPointerMove restarts it.
+ const schedule = (): void => {
+ if (!raf) raf = requestAnimationFrame(tick)
}
const tick = (): void => {
- raf = requestAnimationFrame(tick)
- if (!pending) return
+ raf = 0
+ if (!pending) return // idle — the next pointermove will restart the loop
const now = performance.now()
- if (now - last < CURSOR_MIN_INTERVAL_MS) return
+ if (now - last < CURSOR_MIN_INTERVAL_MS) {
+ schedule() // throttled: come back next frame, the sample is still pending
+ return
+ }
last = now
const p = pending
pending = null
@@ -66,13 +114,22 @@ export function PresenceLayer(): JSX.Element | null {
window.nodeTerminal.presence.cursor({ x: Math.round(flow.x), y: Math.round(flow.y) })
}
+ const onPointerMove = (e: PointerEvent): void => {
+ pending = { x: e.clientX, y: e.clientY }
+ screenRef.current = pending
+ schedule()
+ }
+ const onPointerLeave = (): void => {
+ pending = null
+ window.nodeTerminal.presence.cursor(null)
+ }
+
window.addEventListener('pointermove', onPointerMove)
document.addEventListener('pointerleave', onPointerLeave)
- raf = requestAnimationFrame(tick)
return () => {
window.removeEventListener('pointermove', onPointerMove)
document.removeEventListener('pointerleave', onPointerLeave)
- cancelAnimationFrame(raf)
+ if (raf) cancelAnimationFrame(raf)
window.nodeTerminal.presence.cursor(null)
}
}, [hasOthers, screenToFlowPosition])
@@ -85,33 +142,42 @@ export function PresenceLayer(): JSX.Element | null {
if (chat !== null) return
if (!canOpenCursorChat(document.activeElement as unknown as KeyTarget | null)) return
e.preventDefault()
- if (lingerRef.current) clearTimeout(lingerRef.current)
+ if (lingerRef.current) {
+ clearTimeout(lingerRef.current)
+ lingerRef.current = null
+ }
+ // Snapshot the pointer NOW: the input is anchored where you opened it and stays there.
+ setAnchor({ ...screenRef.current })
setChat('')
- window.nodeTerminal.presence.chat('')
+ sendChat('')
}
window.addEventListener('keydown', onKey)
return () => window.removeEventListener('keydown', onKey)
- }, [hasOthers, chat])
+ }, [hasOthers, chat, sendChat])
+ // The last peer left: below, this component renders null, so an open chat would become an
+ // INVISIBLE open chat — "/" would keep early-returning, the hub would keep our last line, and a
+ // peer joining later would re-mount the input (autoFocus!) with stale text. Close it for real.
+ useEffect(() => {
+ if (!hasOthers) closeChat(false)
+ }, [hasOthers, closeChat])
+
+ // Unmount: retract whatever peers can still see (an open input, or a lingering sent line) —
+ // the mirror of the sampler's cursor(null).
useEffect(
() => () => {
if (lingerRef.current) clearTimeout(lingerRef.current)
+ if (publishedRef.current) window.nodeTerminal.presence.chat(null)
},
[]
)
if (!hasOthers) return null
- const closeChat = (linger: boolean): void => {
- setChat(null)
- if (lingerRef.current) clearTimeout(lingerRef.current)
- if (linger) {
- // Enter: the bubble stays up for the readers, then clears itself.
- lingerRef.current = setTimeout(() => window.nodeTerminal.presence.chat(null), CHAT_LINGER_MS)
- } else {
- window.nodeTerminal.presence.chat(null)
- }
- }
+ // Cursors are positioned in FLOW space (inside the viewport transform) but sized in SCREEN
+ // space: the counter-scale cancels the zoom, pivoting on the arrow tip.
+ const scale = counterScale(zoom)
+ const hotspot = `${CURSOR_HOTSPOT_PX}px ${CURSOR_HOTSPOT_PX}px`
return (
<>
@@ -123,34 +189,39 @@ export function PresenceLayer(): JSX.Element | null {
className="presence-cursor"
style={{ transform: `translate(${p.cursor.x}px, ${p.cursor.y}px)`, color: p.color }}
>
- <svg className="presence-cursor__arrow" width="18" height="18" viewBox="0 0 18 18">
- <path
- d="M2 2 L2 14 L5.5 10.6 L8 15.6 L10.4 14.4 L7.9 9.5 L12.6 9.5 Z"
- fill="currentColor"
- stroke="#000"
- strokeWidth="0.75"
- />
- </svg>
- <span className="presence-cursor__name" style={{ background: p.color }}>
- {p.name}
- </span>
- {p.chat ? <span className="presence-cursor__chat">{p.chat}</span> : null}
+ <div
+ className="presence-cursor__scale"
+ style={{ transform: scale, transformOrigin: hotspot }}
+ >
+ <svg className="presence-cursor__arrow" width="18" height="18" viewBox="0 0 18 18">
+ <path
+ d="M2 2 L2 14 L5.5 10.6 L8 15.6 L10.4 14.4 L7.9 9.5 L12.6 9.5 Z"
+ fill="currentColor"
+ stroke="#000"
+ strokeWidth="0.75"
+ />
+ </svg>
+ <span className="presence-cursor__name" style={{ background: p.color }}>
+ {p.name}
+ </span>
+ {p.chat ? <span className="presence-cursor__chat">{p.chat}</span> : null}
+ </div>
</div>
) : null
)}
</ViewportPortal>
- {chat !== null && (
+ {chat !== null && anchor && (
<input
className="presence-chat-input nodrag nowheel"
autoFocus
value={chat}
maxLength={CHAT_MAX_LEN}
placeholder="Say something…"
- style={{ left: screenRef.current.x + 16, top: screenRef.current.y + 16 }}
+ style={{ left: anchor.x + CHAT_ANCHOR_OFFSET_PX, top: anchor.y + CHAT_ANCHOR_OFFSET_PX }}
onChange={(e) => {
setChat(e.target.value)
- window.nodeTerminal.presence.chat(e.target.value)
+ sendChat(e.target.value)
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
diff --git a/src/renderer/lib/presenceKeys.test.ts b/src/renderer/lib/presenceKeys.test.ts
index 2e662db6f..b0387f45b 100644
--- a/src/renderer/lib/presenceKeys.test.ts
+++ b/src/renderer/lib/presenceKeys.test.ts
@@ -1,14 +1,28 @@
import { describe, it, expect } from 'vitest'
-import { canOpenCursorChat, type KeyTarget } from './presenceKeys'
+import { canOpenCursorChat, counterScale, type KeyTarget } from './presenceKeys'
-/** A stand-in for document.activeElement (an HTMLElement structurally satisfies KeyTarget). */
+/**
+ * A stand-in for document.activeElement (an HTMLElement structurally satisfies KeyTarget).
+ * `inside` lists the selectors of the element's ancestors (plus itself).
+ *
+ * The stub parses the selector list the way a real `closest` does — split on commas, match WHOLE
+ * selectors — deliberately: a substring match (`sel.includes('.xterm')`) would also report a hit
+ * for a compound/descendant selector like `.xterm .monaco-editor`, i.e. an implementation that is
+ * broken in a real DOM would pass here. vitest runs in the node environment (no jsdom), so this
+ * stub is the only DOM contract the guard is tested against — it has to be an honest one.
+ */
function target(tagName: string, opts: { editable?: boolean; inside?: string[] } = {}): KeyTarget {
const inside = opts.inside ?? []
return {
tagName,
isContentEditable: !!opts.editable,
- closest: (sel: string) =>
- inside.some((cls) => sel.includes(cls)) ? ({} as unknown as KeyTarget) : null
+ closest: (sel: string) => {
+ const selectors = sel
+ .split(',')
+ .map((s) => s.trim())
+ .filter(Boolean)
+ return selectors.some((s) => inside.includes(s)) ? ({} as unknown as KeyTarget) : null
+ }
}
}
@@ -31,4 +45,33 @@ describe('canOpenCursorChat', () => {
expect(canOpenCursorChat(target('DIV', { inside: ['.monaco-editor'] }))).toBe(false)
expect(canOpenCursorChat(target('DIV', { inside: ['.chat-node'] }))).toBe(false)
})
+
+ it('opens inside ordinary canvas chrome (an unrelated ancestor must not block it)', () => {
+ expect(canOpenCursorChat(target('DIV', { inside: ['.react-flow__pane'] }))).toBe(true)
+ // Each typing zone must be its OWN selector in the list: an element inside `.monaco-editor`
+ // alone (never inside `.xterm`) still owns the keyboard.
+ expect(canOpenCursorChat(target('DIV', { inside: ['.monaco-editor', '.react-flow'] }))).toBe(
+ false
+ )
+ })
+})
+
+describe('counterScale', () => {
+ it('cancels the viewport zoom so the cursor chrome keeps a constant on-screen size', () => {
+ expect(counterScale(1)).toBe('scale(1)')
+ expect(counterScale(0.5)).toBe('scale(2)')
+ expect(counterScale(2)).toBe('scale(0.5)')
+ expect(counterScale(0.2)).toBe('scale(5)')
+ })
+
+ it('rounds, so a jittery zoom does not produce a 17-digit transform string', () => {
+ expect(counterScale(3)).toBe('scale(0.3333)')
+ })
+
+ it('falls back to 1× on a zoom React Flow can never actually report (0, negative, NaN)', () => {
+ expect(counterScale(0)).toBe('scale(1)')
+ expect(counterScale(-1)).toBe('scale(1)')
+ expect(counterScale(Number.NaN)).toBe('scale(1)')
+ expect(counterScale(Number.POSITIVE_INFINITY)).toBe('scale(1)')
+ })
})
diff --git a/src/renderer/lib/presenceKeys.ts b/src/renderer/lib/presenceKeys.ts
index 54be44afd..b16d14257 100644
--- a/src/renderer/lib/presenceKeys.ts
+++ b/src/renderer/lib/presenceKeys.ts
@@ -1,3 +1,7 @@
+// Pure helpers for the presence cursor layer (PresenceLayer.tsx): the cursor-chat key guard and
+// the zoom counter-scale. Kept out of the component because vitest runs in the node environment
+// (no jsdom), so a React component cannot be unit-tested — these can.
+//
// Cursor-chat key guard. "/" opens the cursor-chat input ONLY when the user is on the canvas —
// never while typing into a terminal (xterm focuses a hidden <textarea> inside .xterm), Monaco
// (.inputarea inside .monaco-editor), a chat node, or any input/contentEditable. Mirrors the
@@ -25,3 +29,28 @@ export function canOpenCursorChat(active: KeyTarget | null): boolean {
if (active.isContentEditable) return false
return active.closest(TYPING_ZONES) === null
}
+
+/**
+ * The arrow tip's offset (px, at 1×) inside the cursor svg — the `transform-origin` of the
+ * counter-scale, so scaling pivots on the TIP and it keeps pointing at the peer's exact flow
+ * coordinate at any zoom. Must track the `M2 2 …` start of the arrow path in PresenceLayer.
+ */
+export const CURSOR_HOTSPOT_PX = 2
+
+/** How far below-right of the anchor the local cursor-chat input sits (px, screen space). */
+export const CHAT_ANCHOR_OFFSET_PX = 16
+
+/**
+ * The transform that cancels the canvas zoom for a peer's cursor chrome (arrow, name label, chat
+ * bubble). Cursors live inside the ViewportPortal so their POSITION is in flow space (they stick
+ * to the node they point at) — but their SIZE must stay constant on screen, Figma-style: at 0.2×
+ * an un-scaled name label is unreadable, at 2× it is oversized. Hence 1/zoom.
+ *
+ * Rounded to 4 decimals so a zoom tick can't emit a 17-digit style string, and defensive about a
+ * zoom of 0 / negative / non-finite (React Flow never reports one; a `scale(Infinity)` would blow
+ * the layer up to nothing).
+ */
+export function counterScale(zoom: number): string {
+ const z = Number.isFinite(zoom) && zoom > 0 ? zoom : 1
+ return `scale(${Math.round((1 / z) * 1e4) / 1e4})`
+}
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index 32d22063f..43a0a6fb8 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -5181,13 +5181,21 @@ body,
left: 0;
pointer-events: none;
z-index: 4;
- display: flex;
- align-items: flex-start;
/* Positions arrive at ~20 Hz — the transition is the "lerp" (no JS animation loop). */
transition: transform 80ms linear;
will-change: transform;
}
+/* The cursor lives inside the viewport transform, so its POSITION follows the canvas — but its
+ SIZE must not (a name label at 0.2x is unreadable, at 2x it is huge). This wrapper carries the
+ 1/zoom counter-scale (see counterScale in lib/presenceKeys.ts), pivoted on the arrow tip
+ (transform-origin, set inline from CURSOR_HOTSPOT_PX) so the tip keeps pointing at the peer's
+ exact flow coordinate. No transition here: zoom must apply instantly, only position lerps. */
+.presence-cursor__scale {
+ display: flex;
+ align-items: flex-start;
+}
+
.presence-cursor__arrow {
display: block;
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));