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
fix(canvas): reclaim focus to the element that lost it in click-to-focus (#757)
Erik Zwepdf57dddf0443
diff --git a/CLAUDE.md b/CLAUDE.mdindex 1c8f271a9..8cdaa7d97 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -1267,7 +1267,8 @@ session.keyboard, and the node's active flag, presence focus AND guard then follow DOM focus through a`focusin`/`focusout` listener on the node root (`lib/terminalFocusMode.ts` `focusLossOutcome`):focus moving inside the node or the WINDOW blurring (Cmd+Tab) keeps it, a press on the node's own- chrome (header drag — React Flow focuses its wrapper) hands it back to the xterm, anything else —+ chrome (header drag — React Flow focuses its wrapper, MEASURED in Electron 42) hands it back to+ the element that lost it (the ⌘M composer) or the xterm (`reclaimTarget`), anything else —another node, a field, the empty canvas (`onPaneClick` blurs the xterm textarea,`shouldReleasePaneFocus`) — releases it and re-arms the guard. Activity claimed WITHOUT focus(go-to-node under the ⌘M view, Canvas's own `setActive` on a jump) has no focusout, so adiff --git a/src/renderer/lib/terminalFocusMode.test.ts b/src/renderer/lib/terminalFocusMode.test.tsindex c095abba1..5d0a6556c 100644--- a/src/renderer/lib/terminalFocusMode.test.ts+++ b/src/renderer/lib/terminalFocusMode.test.ts@@ -5,6 +5,7 @@ import { DEFAULT_SETTINGS } from '@shared/types'import {focusLossOutcome,outsidePressReleases,+ reclaimTarget,hoverTakesKeyboard,pointerLeaveReleases,resolveFocusFollowsPointer@@ -127,6 +128,21 @@ describe('terminal focus mode (#757)', () => {})})+ describe('reclaimTarget', () => {+ // MEASURED in Electron 42 (Blink): a press on the node's own header moves focus from whatever+ // held it to the React Flow wrapper (tabindex 0). When the ⌘M view is open, what held it was the+ // composer, and the covered xterm cannot take focus back — so reclaiming only to the xterm left+ // the keyboard on the wrapper, where Backspace is the canvas's delete-selection.+ it('hands focus back to the element inside the node that lost it (the ⌘M composer)', () => {+ expect(reclaimTarget({ lostIsXterm: false, lostStillInNode: true })).toBe('lost')+ })++ it('uses the xterm when the xterm lost it, or when the lost element is gone (rename field closed)', () => {+ expect(reclaimTarget({ lostIsXterm: true, lostStillInNode: true })).toBe('xterm')+ expect(reclaimTarget({ lostIsXterm: false, lostStillInNode: false })).toBe('xterm')+ })+ })+// The decisions above are only worth something if the node and the settings page use them. A// source check, like `focusRestore.test.ts`: TerminalNode cannot be mounted without an xterm.describe('wiring', () => {diff --git a/src/renderer/lib/terminalFocusMode.ts b/src/renderer/lib/terminalFocusMode.tsindex c71d7ced0..8d40c960c 100644--- a/src/renderer/lib/terminalFocusMode.ts+++ b/src/renderer/lib/terminalFocusMode.ts@@ -130,3 +130,13 @@ export interface OutsidePress {export function outsidePressReleases(p: OutsidePress): boolean {return p.isActive && !p.pressInsideNode && !p.focusInsideNode}++/**+ * Click to focus, `reclaim`: which element gets the keyboard back after a press on the node's own+ * chrome? The one that lost it, when it is still inside the node and is not the xterm (the ⌘M+ * composer — the covered xterm cannot take focus, so falling back to it strands the keyboard on the+ * React Flow wrapper); otherwise the xterm, through `focusXtermUnlessCovered`.+ */+export function reclaimTarget(p: { lostIsXterm: boolean; lostStillInNode: boolean }): 'lost' | 'xterm' {+ return !p.lostIsXterm && p.lostStillInNode ? 'lost' : 'xterm'+}diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsxindex 87a6bc0df..7a2c44b58 100644--- a/src/renderer/nodes/TerminalNode.tsx+++ b/src/renderer/nodes/TerminalNode.tsx@@ -214,6 +214,7 @@ import {hoverTakesKeyboard,outsidePressReleases,pointerLeaveReleases,+ reclaimTarget,resolveFocusFollowsPointer} from '../lib/terminalFocusMode'import { isHidden } from '../lib/ui-visibility'@@ -5344,14 +5345,20 @@ export function TerminalNode({release()return}- // 'reclaim': after the press's own focus change has settled. If the xterm cannot take it (the- // ⌘M view covers it, or it is gone) the keyboard really did leave, so say so.+ // 'reclaim': after the press's own focus change has settled, give the keyboard back to what+ // lost it (the ⌘M composer) or to the xterm (`reclaimTarget`). If nothing inside the node+ // could take it (the xterm is covered or gone) the keyboard really did leave, so say so.+ const lost = e.targetif (reclaimTimer) clearTimeout(reclaimTimer)reclaimTimer = setTimeout(() => {reclaimTimer = null- focusXtermUnlessCovered(termRef.current, mdModeRef.current)- const ta = termRef.current?.textarea- if (!ta || document.activeElement !== ta) release()+ const target = reclaimTarget({+ lostIsXterm: lost === termRef.current?.textarea,+ lostStillInNode: lost instanceof HTMLElement && lost.isConnected && root.contains(lost)+ })+ if (target === 'lost') (lost as HTMLElement).focus()+ else focusXtermUnlessCovered(termRef.current, mdModeRef.current)+ if (!root.contains(document.activeElement)) release()}, 0)}// Activity claimed WITHOUT focus (a go-to-node under the ⌘M view, Canvas's own setActive on a