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(chat): let the ⌘M chat's message text be selected and copied

Daniel Acevedoe743259d0676
diff --git a/CLAUDE.md b/CLAUDE.md
index 8a7146887..8a2e37222 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1718,7 +1718,11 @@ number: change the table in that one module, never in one emulator.
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. One document capture `pointerdown` does the rest: outside the node it releases activity
+ guard. A press on the ⌘M chat's MESSAGE TEXT (`.term-chat__msgs`) is the start of a drag-selection, so
+ its reclaim waits for the press to end (`reclaim-on-release` → `reclaimOnRelease`): a plain click
+ still hands the keyboard back to the composer, but a press that selected text leaves the
+ selection (and Cmd/Ctrl+C) alone — reclaiming at once moved the selection into the textarea and
+ nothing in the chat could be highlighted or copied. 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`), and a document capture `focusin` landing outside
the node (its own wrapper counts as inside) does the same for KEYBOARD focus moves — ⌘M open,
diff --git a/src/renderer/lib/terminalFocusMode.test.ts b/src/renderer/lib/terminalFocusMode.test.ts
index b54ed4ff0..9e32cd1ee 100644
--- a/src/renderer/lib/terminalFocusMode.test.ts
+++ b/src/renderer/lib/terminalFocusMode.test.ts
@@ -4,6 +4,7 @@ import { join } from 'path'
import { DEFAULT_SETTINGS } from '@shared/types'
import {
focusLossOutcome,
+ reclaimOnRelease,
bodyPressAcknowledges,
outsidePressReleases,
reclaimTarget,
@@ -104,6 +105,7 @@ describe('terminal focus mode (#757)', () => {
activeElement: body,
windowFocused: true,
pressedInOwnNode: false,
+ pressedInReadableText: false,
lostIsCoveredXterm: false,
...over
})
@@ -130,6 +132,17 @@ describe('terminal focus mode (#757)', () => {
expect(ev({ gained: null, activeElement: body, pressedInOwnNode: true })).toBe('reclaim')
})
+ it('defers the reclaim to the end of the press when it landed on readable text (⌘M chat selection)', () => {
+ // Reclaiming now would move the selection into the composer and cancel the drag.
+ expect(ev({ gained: null, activeElement: body, pressedInOwnNode: true, pressedInReadableText: true })).toBe(
+ 'reclaim-on-release'
+ )
+ // The flag alone (no press on this node in this task) changes nothing.
+ expect(ev({ gained: null, activeElement: body, pressedInReadableText: true })).toBe('release')
+ // Focus moving to another element inside the node still wins.
+ expect(ev({ gained: header, activeElement: header, pressedInOwnNode: true, pressedInReadableText: true })).toBe('keep')
+ })
+
it('keeps it when the WINDOW lost focus (Cmd+Tab away): the terminal still owns the keyboard', () => {
// Chromium fires blur/focusout on the focused element when the window deactivates, but the
// element stays `document.activeElement` and gets focus back on return. Releasing here would
@@ -250,4 +263,11 @@ describe('terminal focus mode (#757)', () => {
}
})
})
+ describe('reclaimOnRelease', () => {
+ it('gives the keyboard back only for a plain click, and only to an element still in the node', () => {
+ expect(reclaimOnRelease({ selectionEmpty: true, lostStillInNode: true })).toBe(true)
+ expect(reclaimOnRelease({ selectionEmpty: false, lostStillInNode: true })).toBe(false)
+ expect(reclaimOnRelease({ selectionEmpty: true, lostStillInNode: false })).toBe(false)
+ })
+ })
})
diff --git a/src/renderer/lib/terminalFocusMode.ts b/src/renderer/lib/terminalFocusMode.ts
index 438b4c59d..fc2ca0c71 100644
--- a/src/renderer/lib/terminalFocusMode.ts
+++ b/src/renderer/lib/terminalFocusMode.ts
@@ -100,6 +100,10 @@ export interface FocusLossEvent {
/** A press landed on THIS node (its React Flow wrapper, header included) in the same task — the
* focus change is that press's default action, not a click somewhere else. */
pressedInOwnNode: boolean
+ /** That press landed on text the user READS and may select — the ⌘M chat's message list. The
+ * press is the start of a drag-selection, and handing focus back to the composer in the same
+ * moment moves the document selection into the textarea and cancels the drag. */
+ pressedInReadableText: boolean
/** The element that lost focus is this node's xterm while its ⌘M view is open: the only way that
* happens is `useMdModeFocus` blurring it as the view opens (every other "take the keyboard"
* path goes through `focusXtermUnlessCovered`, which never focuses a covered xterm). */
@@ -112,6 +116,9 @@ export type FocusLossOutcome =
| 'keep'
/** The user clicked this node's own chrome: hand the keyboard straight back to its terminal. */
| 'reclaim'
+ /** The user pressed text they may be selecting: hand the keyboard back only when the press
+ * ends WITHOUT a selection (a plain click), so a drag-selection survives to be copied. */
+ | 'reclaim-on-release'
/** The keyboard really went somewhere else: release the node. */
| 'release'
@@ -135,6 +142,10 @@ export type FocusLossOutcome =
* way the keystroke after it would land on the canvas — where a bare Backspace is
* `canvas.deleteSelection`. #757 asks that the terminal keep the keyboard until the user clicks a
* DIFFERENT card, so this hands it back → `reclaim`.
+ * - **A press on text the user may select** (`pressedInReadableText`, the ⌘M chat's messages). The
+ * same press as above, but reclaiming NOW moves the selection into the composer and kills the
+ * drag — nothing could be highlighted or copied. So the reclaim waits for the press to end, and
+ * only happens if it ended without a selection → `reclaim-on-release` (see `reclaimOnRelease`).
*
* Everything else — focus on another node's xterm or a text field, or on `<body>` because a click
* landed on the empty canvas — is the user clicking elsewhere, which is exactly what #757 wants to
@@ -146,7 +157,7 @@ export function focusLossOutcome(e: FocusLossEvent): FocusLossOutcome {
if (e.lostIsCoveredXterm) return 'keep'
if (e.activeElement === e.lost) return 'keep'
if (e.gained && e.nodeRoot.contains(e.gained)) return 'keep'
- if (e.pressedInOwnNode) return 'reclaim'
+ if (e.pressedInOwnNode) return e.pressedInReadableText ? 'reclaim-on-release' : 'reclaim'
return 'release'
}
@@ -204,3 +215,13 @@ export function reclaimTarget(p: { lostIsXterm: boolean; lostStillInNode: boolea
export function bodyPressAcknowledges(p: { primary: boolean; inBody: boolean; onGuard: boolean }): boolean {
return p.primary && p.inBody && !p.onGuard
}
+
+/**
+ * `reclaim-on-release`, decided when the press ends: give the keyboard back to the element that
+ * lost it only for a plain click (nothing selected) and only if that element is still in the node.
+ * A press that selected text keeps the selection — and the keyboard off the composer, so Cmd/Ctrl+C
+ * copies it; clicking the composer (or anywhere) afterwards behaves as usual.
+ */
+export function reclaimOnRelease(p: { selectionEmpty: boolean; lostStillInNode: boolean }): boolean {
+ return p.selectionEmpty && p.lostStillInNode
+}
diff --git a/src/renderer/nodes/useClickToFocus.test.tsx b/src/renderer/nodes/useClickToFocus.test.tsx
index 302abcd6e..ccd13a0be 100644
--- a/src/renderer/nodes/useClickToFocus.test.tsx
+++ b/src/renderer/nodes/useClickToFocus.test.tsx
@@ -257,4 +257,56 @@ describe('useClickToFocus', () => {
expect(calls).toContain('release')
expect(calls).toContain('active:false')
})
+
+ describe('⌘M chat: selecting message text', () => {
+ let msgs: HTMLDivElement
+ let text: HTMLParagraphElement
+ let composer: HTMLTextAreaElement
+ const release = () =>
+ act(() => {
+ text.dispatchEvent(new MouseEvent('pointerup', { bubbles: true, cancelable: true, button: 0 }))
+ })
+ const settle = () => act(async () => { await new Promise((r) => setTimeout(r, 5)) })
+ beforeEach(() => {
+ host.md = true
+ const chat = document.createElement('div')
+ chat.className = 'term-chat'
+ msgs = document.createElement('div')
+ msgs.className = 'term-chat__msgs'
+ text = document.createElement('p')
+ text.textContent = 'Gate GREEN on f8453360.'
+ msgs.append(text)
+ composer = document.createElement('textarea')
+ chat.append(msgs, composer)
+ body.append(chat)
+ act(() => composer.focus())
+ window.getSelection()?.removeAllRanges()
+ })
+
+ it('does not pull focus back to the composer while the press is still selecting', async () => {
+ press(text)
+ act(() => composer.blur()) // the press's default action: messages are not focusable
+ await settle()
+ expect(document.activeElement).not.toBe(composer)
+ expect(calls).not.toContain('release')
+ })
+
+ it('a press that SELECTED text keeps the selection (and the keyboard off the composer, so Ctrl+C copies)', async () => {
+ press(text)
+ act(() => composer.blur())
+ await settle()
+ window.getSelection()?.selectAllChildren(text)
+ release()
+ expect(window.getSelection()?.toString()).toBe('Gate GREEN on f8453360.')
+ expect(document.activeElement).not.toBe(composer)
+ })
+
+ it('a plain click on the messages hands the keyboard back to the composer when the press ends', async () => {
+ press(text)
+ act(() => composer.blur())
+ await settle()
+ release()
+ expect(document.activeElement).toBe(composer)
+ })
+ })
})
diff --git a/src/renderer/nodes/useClickToFocus.ts b/src/renderer/nodes/useClickToFocus.ts
index 39efc8f56..d23476574 100644
--- a/src/renderer/nodes/useClickToFocus.ts
+++ b/src/renderer/nodes/useClickToFocus.ts
@@ -3,6 +3,7 @@ import {
bodyPressAcknowledges,
focusLossOutcome,
outsidePressReleases,
+ reclaimOnRelease,
reclaimTarget
} from '../lib/terminalFocusMode'
@@ -61,6 +62,9 @@ export function useClickToFocus(host: ClickToFocusHost): void {
const root = h().root()
if (!root) return
let pressedInOwnNode = false
+ let pressedInReadableText = false
+ // Set by a `reclaim-on-release` focusout; settled by the press's pointerup (see focusLossOutcome).
+ let reclaimOnUp: HTMLElement | null = null
let pressTimer: ReturnType<typeof setTimeout> | null = null
let reclaimTimer: ReturnType<typeof setTimeout> | null = null
@@ -76,9 +80,11 @@ export function useClickToFocus(host: ClickToFocusHost): void {
const inNode = !!target && ownNode.contains(target)
if (inNode) {
pressedInOwnNode = true
+ pressedInReadableText = !!target?.closest('.term-chat__msgs')
if (pressTimer) clearTimeout(pressTimer)
pressTimer = setTimeout(() => {
pressedInOwnNode = false
+ pressedInReadableText = false
pressTimer = null
}, 0)
}
@@ -124,6 +130,7 @@ export function useClickToFocus(host: ClickToFocusHost): void {
activeElement: document.activeElement,
windowFocused: document.hasFocus(),
pressedInOwnNode,
+ pressedInReadableText,
lostIsCoveredXterm: h().mdMode() && e.target === h().xtermTextarea()
})
if (outcome === 'keep') return
@@ -131,6 +138,10 @@ export function useClickToFocus(host: ClickToFocusHost): void {
release()
return
}
+ if (outcome === 'reclaim-on-release') {
+ reclaimOnUp = e.target instanceof HTMLElement ? e.target : null
+ return
+ }
// 'reclaim': once the press's own focus change has settled, give the keyboard back to what
// lost it (the ⌘M composer) or to the xterm. If nothing inside the node could take it (the
// xterm is covered or gone), the keyboard really did leave, so say so.
@@ -167,7 +178,22 @@ export function useClickToFocus(host: ClickToFocusHost): void {
}
}
+ const onPointerUp = () => {
+ const lost = reclaimOnUp
+ if (!lost) return
+ reclaimOnUp = null
+ const sel = window.getSelection()
+ if (
+ reclaimOnRelease({
+ selectionEmpty: !sel || sel.isCollapsed || sel.toString() === '',
+ lostStillInNode: lost.isConnected && root.contains(lost)
+ })
+ )
+ lost.focus()
+ }
+
document.addEventListener('pointerdown', onPointerDown, true)
+ document.addEventListener('pointerup', onPointerUp, true)
document.addEventListener('focusin', onDocFocusIn, true)
root.addEventListener('focusin', onFocusIn)
root.addEventListener('focusout', onFocusOut)
@@ -175,6 +201,7 @@ export function useClickToFocus(host: ClickToFocusHost): void {
if (pressTimer) clearTimeout(pressTimer)
if (reclaimTimer) clearTimeout(reclaimTimer)
document.removeEventListener('pointerdown', onPointerDown, true)
+ document.removeEventListener('pointerup', onPointerUp, true)
document.removeEventListener('focusin', onDocFocusIn, true)
root.removeEventListener('focusin', onFocusIn)
root.removeEventListener('focusout', onFocusOut)