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(terminal): cut propagation on suppressed middle clicks — preventDefault had nothing to cancel

enesd8a922a7cb08
diff --git a/src/renderer/components/settings/sections/TerminalSection.tsx b/src/renderer/components/settings/sections/TerminalSection.tsx
index 2c30d26cd..5215fce55 100644
--- a/src/renderer/components/settings/sections/TerminalSection.tsx
+++ b/src/renderer/components/settings/sections/TerminalSection.tsx
@@ -311,7 +311,7 @@ export function TerminalSection({ isActive }: { isActive: boolean }): React.JSX.
<GroupHeading>Advanced</GroupHeading>
<FieldRow
label="Middle click pastes the selection"
- description="Linux only. Off by default: the paste comes from the browser rather than from the terminal, so it ignores the desktop's own middle-click setting and can drop text into a running agent's prompt by accident. tmux's own middle-click paste is unaffected either way."
+ description="Linux only. Off by default — and off makes the middle button do nothing at all inside a terminal, tmux's own middle-click paste included: the paste happens beyond the app (tmux, or the running program reading the selection), so the only way to stop it is to swallow the click before the terminal forwards it. On, a stray middle click can drop whatever was last selected into a running agent's prompt."
control={
<Switch
checked={settings.terminalMiddleClickPaste}
diff --git a/src/renderer/terminal/middle-click.test.ts b/src/renderer/terminal/middle-click.test.ts
index 6d34c29d6..ad4d232bf 100644
--- a/src/renderer/terminal/middle-click.test.ts
+++ b/src/renderer/terminal/middle-click.test.ts
@@ -1,14 +1,14 @@
-import { describe, expect, it } from 'vitest'
-import { suppressMiddleClickPaste } from './middle-click'
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { guardMiddleClickPaste, suppressMiddleClickPaste } from './middle-click'
/**
- * Issue #84: on Linux a middle click inside a terminal pasted the X PRIMARY selection into the pty,
- * with no way to switch it off — Chromium does it on the hidden textarea xterm keeps under the
- * cursor, and it honours the desktop's `gtk-enable-primary-paste` only in its own Views widgets.
- *
- * `guardMiddleClickPaste` itself needs a DOM and is verified on device; what is pinned here is the
- * DECISION, whose two halves are easy to invert — `allow` is the user's "middle click may paste",
- * so suppression is what happens when it is OFF.
+ * Issue #84: on Linux a middle click inside a terminal pasted text into the pty with no way to
+ * switch it off. Measured on the reporting machine, the paste happens DOWNSTREAM of the pty (tmux's
+ * `MouseDown2Pane` at a shell prompt; the agent TUI itself reading X PRIMARY), consuming the mouse
+ * report xterm forwards — so the fix is to stop the DOM event from ever reaching xterm's listeners,
+ * not to cancel a browser default that does not exist. `preventDefault` alone shipped once and was
+ * confirmed inert on device.
*/
describe('suppressMiddleClickPaste', () => {
it('suppresses the middle button while the setting is off', () => {
@@ -20,11 +20,103 @@ describe('suppressMiddleClickPaste', () => {
})
it('never touches the other buttons', () => {
- // Left is selection and focus; right is the context menu. Preventing either default would
- // break the terminal in a far more visible way than the bug being fixed.
+ // Left is selection and focus; right is the context menu. Swallowing either would break the
+ // terminal in a far more visible way than the bug being fixed.
for (const button of [0, 2, 3, 4]) {
expect(suppressMiddleClickPaste(button, false)).toBe(false)
expect(suppressMiddleClickPaste(button, true)).toBe(false)
}
})
})
+
+/**
+ * The guard's contract is that xterm — whose listeners live on DESCENDANTS of the host — never
+ * sees a suppressed event, because an unseen event produces no mouse report and therefore no
+ * paste anywhere downstream. That is a propagation fact, so it is pinned with a real DOM.
+ */
+describe('guardMiddleClickPaste', () => {
+ const cleanups: Array<() => void> = []
+ afterEach(() => {
+ while (cleanups.length > 0) cleanups.pop()?.()
+ })
+
+ function mount(allow: boolean): { child: HTMLElement; host: HTMLElement; seen: string[] } {
+ const host = document.createElement('div')
+ // Stands in for xterm's screen element, where its own mouse listeners are attached.
+ const child = document.createElement('div')
+ host.appendChild(child)
+ document.body.appendChild(host)
+ const off = guardMiddleClickPaste(host, () => allow)
+ const seen: string[] = []
+ for (const type of ['mousedown', 'mouseup', 'auxclick']) {
+ child.addEventListener(type, (e) => seen.push(`${type}:${(e as MouseEvent).button}`))
+ }
+ cleanups.push(() => {
+ off()
+ host.remove()
+ })
+ return { child, host, seen }
+ }
+
+ function press(target: HTMLElement, button: number): MouseEvent[] {
+ const events = ['mousedown', 'mouseup', 'auxclick'].map(
+ (type) => new MouseEvent(type, { bubbles: true, cancelable: true, button })
+ )
+ for (const e of events) target.dispatchEvent(e)
+ return events
+ }
+
+ it('a suppressed middle click never reaches listeners below the host', () => {
+ const { child, seen } = mount(false)
+ const events = press(child, 1)
+ expect(seen).toEqual([])
+ // preventDefault is kept for the (unmeasured) setups where a genuine browser paste exists.
+ for (const e of events) expect(e.defaultPrevented).toBe(true)
+ })
+
+ it('silences later listeners on the host itself, not only descendants', () => {
+ const { child, host } = mount(false)
+ // Registered AFTER the guard, same element, same capture phase — only
+ // stopImmediatePropagation covers this one.
+ const late = vi.fn()
+ host.addEventListener('mousedown', late, true)
+ cleanups.push(() => host.removeEventListener('mousedown', late, true))
+ press(child, 1)
+ expect(late).not.toHaveBeenCalled()
+ })
+
+ it('lets the middle button through once the user opts in', () => {
+ const { child, seen } = mount(true)
+ const events = press(child, 1)
+ expect(seen).toEqual(['mousedown:1', 'mouseup:1', 'auxclick:1'])
+ for (const e of events) expect(e.defaultPrevented).toBe(false)
+ })
+
+ it('never touches the other buttons', () => {
+ const { child, seen } = mount(false)
+ press(child, 0)
+ press(child, 2)
+ expect(seen).toEqual([
+ 'mousedown:0',
+ 'mouseup:0',
+ 'auxclick:0',
+ 'mousedown:2',
+ 'mouseup:2',
+ 'auxclick:2'
+ ])
+ })
+
+ it('the returned unsubscribe removes the guard', () => {
+ const host = document.createElement('div')
+ const child = document.createElement('div')
+ host.appendChild(child)
+ document.body.appendChild(host)
+ const off = guardMiddleClickPaste(host, () => false)
+ off()
+ const seen: string[] = []
+ child.addEventListener('mousedown', () => seen.push('mousedown'))
+ cleanups.push(() => host.remove())
+ press(child, 1)
+ expect(seen).toEqual(['mousedown'])
+ })
+})
diff --git a/src/renderer/terminal/middle-click.ts b/src/renderer/terminal/middle-click.ts
index 6cdf91732..7b5828d84 100644
--- a/src/renderer/terminal/middle-click.ts
+++ b/src/renderer/terminal/middle-click.ts
@@ -2,7 +2,7 @@
const MIDDLE_BUTTON = 1
/**
- * Should this mouse event's DEFAULT ACTION be suppressed?
+ * Should this mouse event be swallowed before xterm can see it?
*
* Pure, so the rule can be pinned without a DOM. It is one line and still worth naming, because
* the two halves are easy to get backwards: `allow` is the user's setting for "middle click may
@@ -13,44 +13,58 @@ export function suppressMiddleClickPaste(button: number, allow: boolean): boolea
}
/**
- * Stop Chromium pasting the X PRIMARY selection into a terminal on middle click.
- *
- * WHAT IS ACTUALLY HAPPENING (issue #84, Ubuntu/GNOME, nodeterm 0.2.37). xterm keeps a one-cell
- * hidden `textarea` positioned under the cursor. On Linux, Blink pastes the PRIMARY selection into
- * an editable element on middle-button release — so a middle click over that textarea injects
- * whatever was last selected ANYWHERE on the machine straight into the pty. Three things make it
- * worth suppressing rather than living with:
- *
- * - The user cannot turn it off. Chromium honours the desktop's `gtk-enable-primary-paste` in its
- * own Views widgets, not in web content, so the GNOME setting reading `false` changes nothing.
- * - It fires hardest exactly where it hurts. The textarea tracks the CURSOR, so in an agent TUI —
- * whose input box is where the pointer already is — a stray click lands text in a live prompt.
- * At a plain shell prompt the cursor is at the end of the line and the click usually misses.
- * - It is not a path this app ever built. Nothing here routes it, gates it or shows it to the
- * user; it is the browser reaching around the terminal.
- *
- * WHAT THIS DOES NOT TOUCH: tmux's own middle-click paste. That is a tmux root binding
- * (`MouseDown2Pane` → `paste-buffer`) pasting TMUX's buffer, and it is delivered through mouse
- * tracking — it never involved the browser, and preventing a default action does not stop xterm
- * forwarding the event. The issue reporter unbound it on a live server and the paste continued,
- * which is what identified Blink as the source.
- *
- * BOTH `mouseup` AND `auxclick`, in the CAPTURE phase. Blink triggers the paste on RELEASE, and the
- * two events are separate cancelation points on the same gesture; taking both is cheap and removes
- * any question of which one the platform actually honours. `mousedown` is deliberately NOT included
- * — nothing to gain, and preventing a default that early is the sort of thing that quietly breaks
- * focus handling.
+ * Stop a middle click inside a terminal pasting text into the pty (issue #84, Linux).
+ *
+ * WHAT IS ACTUALLY HAPPENING — measured on the reporting machine (Ubuntu/GNOME/Wayland, CDP probe
+ * with three distinct markers, issue #84 follow-up). The paste does NOT come from the browser. A
+ * middle click produces only `mousedown`/`mouseup`/`auxclick` on xterm's canvas — no `paste`, no
+ * `beforeinput`, no `input`, and the hidden textarea is never the target. xterm forwards a mouse
+ * report for the middle button, and something DOWNSTREAM of the pty consumes it:
+ *
+ * - at a plain shell prompt, tmux's root binding (`MouseDown2Pane` → `paste-buffer`) pastes
+ * TMUX's buffer — usually empty, which is why nothing visible happened there;
+ * - under an agent TUI, tmux passes the report through (`send-keys -M`) and the TUI itself reads
+ * the X PRIMARY selection — whatever was last selected anywhere on the machine lands in a live
+ * agent prompt, which is where the bug actually hurt.
+ *
+ * (The first version of this file told a different story — Blink pasting PRIMARY into the hidden
+ * textarea xterm keeps under the cursor. That mechanism was refuted by the measurement above: no
+ * paste event ever fires, and a CDP-synthesized middle click pastes nothing. It may still exist on
+ * setups we have not measured, which is the only reason `preventDefault` is kept below.)
+ *
+ * WHY `preventDefault` ALONE COULD NOT WORK: there is no browser default action in this path, so
+ * there was nothing to cancel — the first fix shipped exactly that and was confirmed inert on the
+ * reporting machine. The event's only role is to reach xterm's own listeners, which turn it into
+ * the mouse report. So the working lever is PROPAGATION: stop the event in the CAPTURE phase on
+ * the host, before xterm's listeners (attached on descendants) ever run, and no report is
+ * forwarded. There is no narrower hook — xterm.js exposes no public handler for mouse buttons
+ * (only `attachCustomKeyEventHandler` / `attachCustomWheelEventHandler`).
+ *
+ * THE CONSEQUENCE THE SETTING MUST OWN: tmux and the TUI consume the SAME forwarded report, so
+ * suppressing it kills tmux's own middle-click paste too. With the setting OFF, the middle button
+ * is fully INERT inside a terminal — not "the browser doesn't paste", but "a middle click does
+ * nothing here". That is the behaviour the reporter asked for; the Settings copy says the same.
+ *
+ * `mousedown` is in the list because that is where xterm emits the report. `mouseup`/`auxclick`
+ * stay so the release half of the gesture cannot leak either (and `preventDefault` covers a real
+ * Blink paste path if one exists elsewhere — it costs nothing). `stopImmediatePropagation` also
+ * silences later listeners on the host itself, not just descendants.
*
* `allow` is read at EVENT TIME, not at attach time, so flipping the setting takes effect on the
* next click instead of on the next terminal.
*/
export function guardMiddleClickPaste(host: HTMLElement, allow: () => boolean): () => void {
const onEvent = (e: MouseEvent): void => {
- if (suppressMiddleClickPaste(e.button, allow())) e.preventDefault()
+ if (!suppressMiddleClickPaste(e.button, allow())) return
+ e.preventDefault()
+ e.stopPropagation()
+ e.stopImmediatePropagation()
}
+ host.addEventListener('mousedown', onEvent, true)
host.addEventListener('mouseup', onEvent, true)
host.addEventListener('auxclick', onEvent, true)
return () => {
+ host.removeEventListener('mousedown', onEvent, true)
host.removeEventListener('mouseup', onEvent, true)
host.removeEventListener('auxclick', onEvent, true)
}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 7aded1991..e843095aa 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -1193,18 +1193,18 @@ export interface Settings {
panHoverDelay: number
doubleClickFocus: boolean
/**
- * Let a MIDDLE CLICK inside a terminal paste the X PRIMARY selection (Linux only — macOS and
- * Windows have no PRIMARY, so this changes nothing there).
+ * Let a MIDDLE CLICK inside a terminal paste (Linux in practice — macOS and Windows have no
+ * PRIMARY selection and no tmux middle-click habit, so the guard changes nothing visible there).
*
- * OFF by default, and that is a deliberate reversal of what the app shipped. The paste was never
- * ours: Chromium performs it on the hidden textarea xterm keeps under the cursor, which means it
- * ignored the desktop's own `gtk-enable-primary-paste` (Chromium applies that only to its Views
- * widgets, not to web content) and the user had NO way to switch it off — issue #84. It fires
- * hardest inside agent TUIs, whose input box sits exactly where the pointer is, so a stray click
- * drops whatever was last selected anywhere on the machine into a live agent prompt.
- *
- * tmux's own middle-click paste is UNAFFECTED either way: that one is a tmux root binding pasting
- * tmux's buffer, and it never reached the browser.
+ * OFF by default, and OFF means the middle button is fully INERT inside a terminal — tmux's own
+ * middle-click paste included. That is a consequence of the real mechanism (issue #84, measured
+ * on the reporting machine): the paste never happens in the browser. xterm forwards a mouse
+ * report for the middle button and something DOWNSTREAM of the pty consumes it — tmux's root
+ * `MouseDown2Pane` binding pastes tmux's buffer at a shell prompt, and an agent TUI reads the X
+ * PRIMARY selection itself. There is no browser default action to cancel, so the guard swallows
+ * the event before xterm can forward it (`guardMiddleClickPaste`), and tmux's paste necessarily
+ * goes with it. The default stays off because the paste fires hardest inside agent TUIs: a stray
+ * click drops whatever was last selected anywhere on the machine into a live agent prompt.
*/
terminalMiddleClickPaste: boolean
/** Plain mouse wheel zooms the canvas (no Cmd/Ctrl needed). On macOS a two-finger trackpad