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): stop the browser pasting the PRIMARY selection on middle click (#84)

enesb3879e953642
diff --git a/src/renderer/components/kanban/ModalTerminal.tsx b/src/renderer/components/kanban/ModalTerminal.tsx
index eaefb17c1..14af2c8d3 100644
--- a/src/renderer/components/kanban/ModalTerminal.tsx
+++ b/src/renderer/components/kanban/ModalTerminal.tsx
@@ -11,6 +11,7 @@ import { useSettings } from '../../state/settings'
import { useTerminalSearch } from '../../terminal/useTerminalSearch'
import { LocalTransport } from '../../terminal/local-transport'
import { clipboardImages, droppedPaths, pasteHasText, pastedFiles } from '../../terminal/file-drop'
+import { guardMiddleClickPaste } from '../../terminal/middle-click'
import { parseOsc52 } from '../../terminal/osc52'
import { activateUnicode11 } from '../../terminal/unicode-width'
import {
@@ -63,6 +64,17 @@ interface ModalTerminalProps {
export function ModalTerminal({ nodeId, spawn, searchOpen, onCloseSearch }: ModalTerminalProps) {
const { api } = useSession()
const hostRef = useRef<HTMLDivElement>(null)
+ const middleClickPaste = useSettings((st) => st.settings.terminalMiddleClickPaste)
+ // Chromium pastes the X PRIMARY selection into xterm's hidden textarea on middle click — a path
+ // this app never built and the user could not switch off (issue #84). Its own effect, keyed on
+ // the setting alone, so it applies to a PARKED terminal being re-adopted just as much as to a
+ // fresh one: the guard belongs to the host ELEMENT, not to the pty lifecycle.
+ useEffect(() => {
+ const host = hostRef.current
+ if (!host) return
+ return guardMiddleClickPaste(host, () => middleClickPaste)
+ }, [middleClickPaste])
+
const termRef = useRef<Terminal | null>(null)
const searchAddonRef = useRef<SearchAddon | null>(null)
// The live pty session + its fit addon, reachable from OUTSIDE the lifecycle effect's closure —
diff --git a/src/renderer/components/settings/sections/TerminalSection.tsx b/src/renderer/components/settings/sections/TerminalSection.tsx
index 107e193c5..9c0441d51 100644
--- a/src/renderer/components/settings/sections/TerminalSection.tsx
+++ b/src/renderer/components/settings/sections/TerminalSection.tsx
@@ -59,6 +59,10 @@ const ROWS = {
title: 'Cursor when unfocused',
keywords: ['cursor', 'inactive', 'unfocused', 'blurred', 'outline']
},
+ middleClickPaste: {
+ title: 'Middle-click paste',
+ keywords: ['middle', 'click', 'mouse', 'paste', 'primary', 'selection', 'x11', 'linux', 'wheel']
+ },
boldBright: {
title: 'Bold text uses bright colours',
keywords: ['bold', 'bright', 'color', 'colour', 'intense']
@@ -298,9 +302,25 @@ export function TerminalSection({ isActive }: { isActive: boolean }): React.JSX.
/>
</SearchableRow>
- <SearchableRow {...ROWS.boldBright}>
+ <SearchableRow {...ROWS.middleClickPaste}>
<div className="space-y-3">
<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."
+ control={
+ <Switch
+ checked={settings.terminalMiddleClickPaste}
+ onChange={(v) => update({ terminalMiddleClickPaste: v })}
+ ariaLabel="Middle click pastes the selection"
+ />
+ }
+ />
+ </div>
+ </SearchableRow>
+
+ <SearchableRow {...ROWS.boldBright}>
+ <div className="space-y-3">
<FieldRow
label="Bold text uses bright colours"
description="The historical terminal convention, and xterm's default. Turn it off to keep bold purely a weight, so a program's colour choice survives."
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 1be7b8595..0a06cc165 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -19,6 +19,7 @@ const ChatPanel = lazy(() => import('./ChatPanel').then((m) => ({ default: m.Cha
import { LocalTransport } from '../terminal/local-transport'
import { clipboardImages, droppedPaths, pasteHasText, pastedFiles } from '../terminal/file-drop'
import type { TerminalTransport } from '../terminal/transport'
+import { guardMiddleClickPaste } from '../terminal/middle-click'
import { patchTerminalScale } from '../terminal/scale-fix'
import { parseOsc52 } from '../terminal/osc52'
import { activateUnicode11 } from '../terminal/unicode-width'
@@ -602,6 +603,17 @@ export function TerminalNode({
const hiddenHeaderButtons = useSettings((s) => s.settings.hiddenHeaderButtons)
const accountChip = accountChipLabel(data.accountId, claudeAccounts)
const bodyRef = useRef<HTMLDivElement>(null)
+ const middleClickPaste = useSettings((st) => st.settings.terminalMiddleClickPaste)
+ // Chromium pastes the X PRIMARY selection into xterm's hidden textarea on middle click — a path
+ // this app never built and the user could not switch off (issue #84). Its own effect, keyed on
+ // the setting alone, so it applies to a PARKED terminal being re-adopted just as much as to a
+ // fresh one: the guard belongs to the host ELEMENT, not to the pty lifecycle.
+ useEffect(() => {
+ const host = bodyRef.current
+ if (!host) return
+ return guardMiddleClickPaste(host, () => middleClickPaste)
+ }, [middleClickPaste])
+
// OUR root (`.term-node`), not React Flow's wrapper — the element whose box changes when the
// node's CHROME changes (header chips, the find bar). Observed alongside the terminal host so a
// chrome change can never leave the glyph grid measured at its old offset; see the second
diff --git a/src/renderer/terminal/middle-click.test.ts b/src/renderer/terminal/middle-click.test.ts
new file mode 100644
index 000000000..6d34c29d6
--- /dev/null
+++ b/src/renderer/terminal/middle-click.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from 'vitest'
+import { 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.
+ */
+describe('suppressMiddleClickPaste', () => {
+ it('suppresses the middle button while the setting is off', () => {
+ expect(suppressMiddleClickPaste(1, false)).toBe(true)
+ })
+
+ it('lets the middle button through once the user opts in', () => {
+ expect(suppressMiddleClickPaste(1, true)).toBe(false)
+ })
+
+ 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.
+ for (const button of [0, 2, 3, 4]) {
+ expect(suppressMiddleClickPaste(button, false)).toBe(false)
+ expect(suppressMiddleClickPaste(button, true)).toBe(false)
+ }
+ })
+})
diff --git a/src/renderer/terminal/middle-click.ts b/src/renderer/terminal/middle-click.ts
new file mode 100644
index 000000000..6cdf91732
--- /dev/null
+++ b/src/renderer/terminal/middle-click.ts
@@ -0,0 +1,57 @@
+/** The MIDDLE mouse button, as `MouseEvent.button` numbers it (0 left, 1 middle, 2 right). */
+const MIDDLE_BUTTON = 1
+
+/**
+ * Should this mouse event's DEFAULT ACTION be suppressed?
+ *
+ * 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
+ * paste", so suppressing is what happens when it is OFF.
+ */
+export function suppressMiddleClickPaste(button: number, allow: boolean): boolean {
+ return button === MIDDLE_BUTTON && !allow
+}
+
+/**
+ * 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.
+ *
+ * `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()
+ }
+ host.addEventListener('mouseup', onEvent, true)
+ host.addEventListener('auxclick', onEvent, true)
+ return () => {
+ host.removeEventListener('mouseup', onEvent, true)
+ host.removeEventListener('auxclick', onEvent, true)
+ }
+}
diff --git a/src/shared/types.ts b/src/shared/types.ts
index a0cf9ac4a..68cd836d2 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -784,6 +784,21 @@ export interface Settings {
/** ms to dwell over a terminal before it takes pointer focus (pan-across guard). */
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).
+ *
+ * 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.
+ */
+ terminalMiddleClickPaste: boolean
/** Plain mouse wheel zooms the canvas (no Cmd/Ctrl needed). Trades away scroll-to-pan,
* so it's opt-in — best for mouse users; trackpads keep two-finger pan when off. */
wheelZoom: boolean
@@ -939,6 +954,7 @@ export const DEFAULT_SETTINGS: Settings = {
defaultProjectView: 'canvas',
panHoverDelay: 600,
doubleClickFocus: true,
+ terminalMiddleClickPaste: false,
wheelZoom: false,
canvasDragMode: 'select',
accent: '#0a84ff',