Skip to content

fix(terminal): stop native middle-click paste inserting twice on Linux - #8993

Merged
nwparker merged 3 commits into
stablyai:mainfrom
xianjianlf2:fix/middleclick-paste-double-x11-8860
Jul 24, 2026
Merged

nwparker merged 3 commits into
stablyai:mainfrom
xianjianlf2:fix/middleclick-paste-double-x11-8860

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes middle-click paste in the integrated terminal inserting the PRIMARY selection twice on Linux/X11 when "Middle-click Paste from Selection" is enabled.

ELI5

On Linux, middle-clicking the terminal to paste used to type your copied text twice. Now it pastes exactly once, like you'd expect.

Root cause & fix

The terminal reads the PRIMARY selection on mousedown and writes it to the PTY itself, calling preventDefault(). But Chromium's native X11 middle-click primary paste still fires on mouse release, landing in xterm's helper textarea, which forwards it to the PTY a second time. The global usePrimarySelectionPaste hook already suppresses this native follow-up, but only for editable targets it owns — it excludes xterm, leaving the terminal path unsuppressed. The fix arms a short shared suppression window (armPrimarySelectionNativePasteSuppression) when the terminal handles a middle-click, and has the hook's capture-phase beforeinput/paste suppressor honor it. The single native paste is swallowed; the selection reaches the PTY once. Ctrl+Shift+V and right-click paste (CLIPBOARD, no native primary-paste event) are unchanged.

Evidence

Validated & rebased onto latest main during a bug-bash triage on 2026-07-23.

  • All unit tests pass on the branch across the armed-window state (arm / window / expiry / disable-clears) and the hook suppression (armed native paste suppressed for a non-owned xterm-style target, plus a negative case).
Test Files  2 passed (2)
     Tests  20 passed (20)
  Duration  222ms
  • Oxlint clean (exit 0) on the changed source files.

Trade-offs

None — pure fix.

Regression risk

Low. The suppression only fires inside a briefly-armed window opened by a terminal middle-click; Ctrl+Shift+V and right-click paste take a different path and are unaffected. The new behavior is proven by the passing suppression and negative-case tests.

Credit to original author @xianjianlf2. Validated and rebased onto latest main via automated bug-bash review; original fix design preserved.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds timed primary-selection native-paste suppression. Terminal middle-click handling arms the state, while usePrimarySelectionPaste suppresses matching native paste events during the active window. Disabling or resetting primary selection clears the state. Tests cover arming conditions, expiry, reset behavior, event prevention, and non-suppressed paste handling.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes address #8860 by suppressing the native follow-up paste so middle-click paste inserts only once.
Out of Scope Changes check ✅ Passed The edits stay focused on primary-selection paste behavior and tests, with no clear unrelated scope creep.
Title check ✅ Passed The title accurately summarizes the main change: preventing duplicate native middle-click paste on Linux.
Description check ✅ Passed The description is detailed and covers summary, root cause, evidence, and regression risk, though it omits several template sections.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nezdelion

Copy link
Copy Markdown

second paste comes from CLIPBOARD, not PRIMARY
to reproduce: select A -> press ctrl+C -> select B -> middle click -> get "BA"
wayland, fedora

@AmethystLiang AmethystLiang assigned OrcaWin and unassigned Jinwoo-H Jul 20, 2026
@AmethystLiang
AmethystLiang requested review from OrcaWin and removed request for Jinwoo-H July 20, 2026 18:37
@xianjianlf2 xianjianlf2 changed the title fix(terminal): stop middle-click primary paste inserting twice on X11 fix(terminal): stop native middle-click paste inserting twice on Linux Jul 22, 2026
@xianjianlf2

Copy link
Copy Markdown
Contributor Author

Thanks for the repro details. I checked this path and updated the wording in the branch/title to avoid making the fix sound X11/PRIMARY-only.

The implementation intentionally suppresses the short-lived native paste follow-up after Orca has already handled the terminal middle-click paste. That suppression is based on the armed middle-click window rather than the clipboard source, because the renderer paste/beforeinput events do not reliably expose whether the text came from PRIMARY or CLIPBOARD. So the Wayland/Fedora case where the follow-up inserts CLIPBOARD text should be covered too.

Verified locally with:

pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/hooks/usePrimarySelectionPaste.test.tsx src/renderer/src/lib/primary-selection.test.ts

Result: 2 files / 18 tests passed.

xianjianlf2 and others added 3 commits July 23, 2026 04:38
When "Middle-click Paste from Selection" is enabled, the integrated
terminal reads the X11 PRIMARY selection on mousedown and writes it to
the PTY itself. It calls preventDefault on the mousedown, but Chromium's
native X11 middle-click primary paste fires on mouse release regardless,
landing in xterm's helper textarea, which xterm then forwards to the PTY
a second time. The result is the selection pasted twice.

The global primary-selection hook already suppresses the follow-up native
paste, but only for editable DOM targets it owns via a pending-target
handle; it deliberately excludes xterm, so the terminal path had no
suppression at all.

Arm a short shared suppression window when the terminal handles a
middle-click, and have the global hook's capture-phase beforeinput/paste
suppressor honor it. This swallows the single native paste event so the
selection reaches the PTY exactly once. Ctrl+Shift+V and right-click
paste are unaffected: they use the CLIPBOARD and never emit a native
primary-paste event.

Closes stablyai#8860
…nux xterm

Gate the armed native-paste suppression window to Linux (X11 primary-selection
path) and scope it to xterm's helper textarea so unrelated document pastes and
non-Linux platforms are never affected. Re-arm on auxclick for slow releases.

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the fix/middleclick-paste-double-x11-8860 branch from 040e2ee to 8d3eb62 Compare July 23, 2026 12:00
@nwparker
nwparker merged commit eefded2 into stablyai:main Jul 24, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Thanks so much for this, @xianjianlf2 — stop native middle-click paste inserting twice on Linux. Verified (fail-on-main/pass-on-branch), rebased on latest main, reviewed, and merged. Really appreciate it! 🙏

buf0-bot Bot added a commit that referenced this pull request Sep 20, 2026
…tracking TUIs (#21834)

* fix(terminal): arm native-paste suppression on middle-click in mouse-tracking TUIs (#21762)

Orca's own middle-click paste path bailed out entirely whenever the pane was
in mouse tracking mode (Claude Code, Codex, ...), skipping preventDefault()
and never arming the #8993 native-paste suppression window. Chromium's
native Linux middle-click paste then landed unsuppressed alongside the TUI's
own PRIMARY paste from the forwarded mouse report, pasting the selection
twice.

Split pane lookup from the tracking-mode gate: any terminal pane target now
arms suppression and blocks the native paste, while only the paste-to-PTY
(and the propagation stop that would swallow the click before xterm can
report it) stays gated on mouseTrackingMode === 'none'.

* fix(terminal): address review nits on the #21762 middle-click fix

- Fix a mis-attributing comment: the suppression window (not preventDefault,
  which only helps on mousedown while Chromium's native paste fires on
  mouseup) is what swallows the duplicate native paste.
- Drop the now-unused getPrimarySelectionMiddleClickPane.
- Assert stopPropagation is/isn't called per tracking mode in the repro test.

---------

Co-authored-by: bench <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#8993)

* fix(terminal): stop middle-click primary paste inserting twice on X11

When "Middle-click Paste from Selection" is enabled, the integrated
terminal reads the X11 PRIMARY selection on mousedown and writes it to
the PTY itself. It calls preventDefault on the mousedown, but Chromium's
native X11 middle-click primary paste fires on mouse release regardless,
landing in xterm's helper textarea, which xterm then forwards to the PTY
a second time. The result is the selection pasted twice.

The global primary-selection hook already suppresses the follow-up native
paste, but only for editable DOM targets it owns via a pending-target
handle; it deliberately excludes xterm, so the terminal path had no
suppression at all.

Arm a short shared suppression window when the terminal handles a
middle-click, and have the global hook's capture-phase beforeinput/paste
suppressor honor it. This swallows the single native paste event so the
selection reaches the PTY exactly once. Ctrl+Shift+V and right-click
paste are unaffected: they use the CLIPBOARD and never emit a native
primary-paste event.

Closes stablyai#8860

* docs(terminal): clarify native middle-click paste suppression

* fix(terminal): scope primary-selection native-paste suppression to Linux xterm

Gate the armed native-paste suppression window to Linux (X11 primary-selection
path) and scope it to xterm's helper textarea so unrelated document pastes and
non-Linux platforms are never affected. Re-arm on auxclick for slow releases.

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Neil <[email protected]>
Co-authored-by: Orca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants