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(canvas): release a focusless active node when keyboard focus leaves it in click-to-focus (#757)

Erik Zwep735a8faab049
diff --git a/CLAUDE.md b/CLAUDE.md
index ff1edd211..5e24d7139 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1277,8 +1277,9 @@ session.
(`onPaneClick` blurs the xterm textarea, `shouldReleasePaneFocus`) — releases it and re-arms the
guard. One document capture `pointerdown` does the rest: outside the node it releases activity
claimed WITHOUT focus (go-to-node under the ⌘M view, Canvas's own `setActive` on a jump — no
- focusout ever comes, `outsidePressReleases`), else the stale `activeId` suppresses that node's
- unread dot; inside the BODY, any deliberate primary press that is not on the guard runs `enterNow`
+ focusout ever comes, `outsidePressReleases`), and a document capture `focusin` landing outside
+ the node (its own wrapper counts as inside) does the same for KEYBOARD focus moves — ⌘M open,
+ then ⌘K's autofocus — else the stale `activeId` suppresses that node's unread dot; inside the BODY, any deliberate primary press that is not on the guard runs `enterNow`
(`bodyPressAcknowledges`) — guard down, xterm focused, ⌘M view open, all the same — so an unread
finish is cleared by clicking the terminal, not only by clicking the guard. A focus RESTORE that
no press caused (window activation) never acknowledges. The xterm blur that OPENING the ⌘M view
diff --git a/src/renderer/nodes/useClickToFocus.test.tsx b/src/renderer/nodes/useClickToFocus.test.tsx
index 392f2fba4..ec8f6b374 100644
--- a/src/renderer/nodes/useClickToFocus.test.tsx
+++ b/src/renderer/nodes/useClickToFocus.test.tsx
@@ -169,6 +169,50 @@ describe('useClickToFocus', () => {
expect(calls).toContain('armed:true')
})
+ it('releases a focusless active node when keyboard focus lands outside it (⌘M open, then ⌘K)', () => {
+ // Opening the ⌘M view blurs the xterm but keeps the node active; the palette's autofocus then
+ // arrives with no root focusout and no pointerdown.
+ host.md = true
+ host.active = true
+ const palette = document.createElement('input')
+ outside.append(palette)
+ act(() => palette.focus())
+ expect(calls).toContain('release')
+ expect(calls).toContain('active:false')
+ expect(calls).toContain('armed:true')
+ })
+
+ it('keeps a focusless active node when focus moves to the composer inside it', () => {
+ host.md = true
+ host.active = true
+ const composer = document.createElement('textarea')
+ body.append(composer)
+ act(() => composer.focus())
+ expect(calls).not.toContain('release')
+ })
+
+ it('keeps a focusless active node when a press on its own header focuses the wrapper', () => {
+ host.md = true
+ host.active = true
+ press(header)
+ act(() => wrapper.focus())
+ expect(calls).not.toContain('release')
+ })
+
+ it('does not release on window re-activation restoring focus to the xterm', () => {
+ host.active = true
+ act(() => xterm.focus())
+ expect(calls).not.toContain('release')
+ expect(host.active).toBe(true)
+ })
+
+ it('leaves an inactive node alone when focus lands elsewhere', () => {
+ const field = document.createElement('input')
+ outside.append(field)
+ act(() => field.focus())
+ expect(calls).not.toContain('release')
+ })
+
it('hands the keyboard back to the xterm after a press on the node’s own chrome moved it to the wrapper', async () => {
act(() => xterm.focus())
press(header)
diff --git a/src/renderer/nodes/useClickToFocus.ts b/src/renderer/nodes/useClickToFocus.ts
index eece5a5ca..af4c05aa4 100644
--- a/src/renderer/nodes/useClickToFocus.ts
+++ b/src/renderer/nodes/useClickToFocus.ts
@@ -142,13 +142,34 @@ export function useClickToFocus(enabled: boolean, host: ClickToFocusHost): void
}, 0)
}
+ // Activity claimed WITHOUT focus (the ⌘M view blurs the xterm and keeps the node active) never
+ // sees a root focusout, and keyboard focus moving elsewhere (⌘K's autofocus) sends no
+ // pointerdown. So a focus landing outside this node is the same "the user went elsewhere" as an
+ // outside press. The node's own wrapper counts as inside: a header press focuses it on the way
+ // to `reclaim`.
+ const onDocFocusIn = (e: FocusEvent) => {
+ const target = e.target instanceof Node ? e.target : null
+ const ownNode = root.closest('.react-flow__node') ?? root
+ if (
+ outsidePressReleases({
+ isActive: h().isActive(),
+ pressInsideNode: !!target && ownNode.contains(target),
+ focusInsideNode: !!target && root.contains(target)
+ })
+ ) {
+ release()
+ }
+ }
+
document.addEventListener('pointerdown', onPointerDown, true)
+ document.addEventListener('focusin', onDocFocusIn, true)
root.addEventListener('focusin', onFocusIn)
root.addEventListener('focusout', onFocusOut)
return () => {
if (pressTimer) clearTimeout(pressTimer)
if (reclaimTimer) clearTimeout(reclaimTimer)
document.removeEventListener('pointerdown', onPointerDown, true)
+ document.removeEventListener('focusin', onDocFocusIn, true)
root.removeEventListener('focusin', onFocusIn)
root.removeEventListener('focusout', onFocusOut)
}