Skip to content

Commit d3cf911

Browse files
maria-rcksclaude
andcommitted
fix(terminal): paste only the terminal selection on middle click
Middle click no longer falls back to the system clipboard: with nothing selected in the terminal there is no primary-selection-like buffer to read, and CLIPBOARD holds text the user only ever copied. The gesture also stops cancelling pointerdown, which used to drop the compatibility mousedown that activates an inactive split pane, and now cancels the middle mouseup so Chromium cannot paste PRIMARY into the hidden textarea on top of the paste already sent. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 2c6000e commit d3cf911

2 files changed

Lines changed: 40 additions & 41 deletions

File tree

‎apps/web/src/terminal/ghostty/surface.test.ts‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,10 @@ describe("GhosttyTerminalSurface visibility", () => {
280280
expect(harness.renderedSnapshot.rowData[0]?.cells.some((cell) => cell.selected)).toBe(false);
281281
});
282282

283-
it("pastes the current selection on a Linux middle click", async () => {
283+
it("pastes the terminal selection, and only that, on a Linux middle click", async () => {
284284
const harness = createHarness();
285-
vi.stubGlobal("navigator", { platform: "Linux x86_64" });
285+
const readText = vi.fn(async () => "clipboard text");
286+
vi.stubGlobal("navigator", { platform: "Linux x86_64", clipboard: { readText } });
286287
const surface = await harness.create();
287288
surface.write("hello world");
288289
harness.flushFrame();
@@ -296,20 +297,12 @@ describe("GhosttyTerminalSurface visibility", () => {
296297
await vi.waitFor(() => expect(harness.onData).toHaveBeenCalled());
297298
expect(harness.onData.mock.calls.at(-1)?.[0]).toBe("hello");
298299
expect(surface.getSelection()).toBe("hello");
299-
});
300300

301-
it("forwards a middle click to an application that tracks the mouse", async () => {
302-
const harness = createHarness();
303-
vi.stubGlobal("navigator", { platform: "Linux x86_64" });
304-
const surface = await harness.create();
305-
surface.write("\x1b[?1000hhello world");
306-
harness.flushFrame();
307-
308-
harness.onData.mockClear();
301+
// Without a selection there is no primary buffer to paste; the clipboard
302+
// holds what the user copied and must not be substituted.
303+
surface.clearSelection();
309304
harness.pointer("pointerdown", 5, 4, false, 1);
310-
// Legacy X10 report for a middle-button press at 1,1: the application gets
311-
// the click instead of a paste.
312-
expect(harness.onData.mock.calls.at(-1)?.[0]).toBe("\x1b[M!!!");
305+
expect(readText).not.toHaveBeenCalled();
313306
});
314307

315308
it("starts a selection when dragging from a link", async () => {

‎apps/web/src/terminal/ghostty/surface.ts‎

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ export function isTerminalPasteShortcut(
400400
* primary selection and use the button for autoscroll, so only desktops that
401401
* expect the gesture get it.
402402
*/
403-
export function isTerminalMiddleClickPastePlatform(platform = navigator.platform): boolean {
404-
return /linux|bsd|x11/i.test(platform);
403+
function isMiddleClickPastePlatform(): boolean {
404+
return /linux|bsd/i.test(navigator.platform);
405405
}
406406

407407
export function isTerminalCompositionCommitInput(event: Pick<InputEvent, "inputType">): boolean {
@@ -948,23 +948,17 @@ export class GhosttyTerminalSurface {
948948
}
949949

950950
/**
951-
* The middle-click paste source. A browser cannot read the X11/Wayland
952-
* PRIMARY buffer, so the terminal's own selection stands in for it when the
953-
* user highlighted here, and the system clipboard covers the rest. Both go
954-
* through pasteFromClipboard, so a middle click joins the same paste race as
955-
* every other paste path.
951+
* Middle-click pastes the terminal's own selection, which is the only
952+
* primary-selection-like buffer a browser can read. It goes through
953+
* pasteFromClipboard so it joins the same paste race as every other path.
954+
* With nothing selected here there is no buffer to paste, and CLIPBOARD is
955+
* deliberately not substituted: middle-click must never emit text the user
956+
* only ever copied.
956957
*/
957-
private pasteFromSelectionBuffer(): void {
958+
private pasteTerminalSelection(): void {
958959
const selection = this.getSelection();
959-
if (selection.length > 0) {
960-
void this.pasteFromClipboard(() => Promise.resolve(selection));
961-
return;
962-
}
963-
const clipboard = navigator.clipboard;
964-
if (typeof clipboard?.readText !== "function") return;
965-
void this.pasteFromClipboard(() => clipboard.readText()).catch(() => {
966-
// Clipboard read denied; middle-click has no other source to fall back to.
967-
});
960+
if (selection.length === 0) return;
961+
void this.pasteFromClipboard(() => Promise.resolve(selection));
968962
}
969963

970964
hasSelection(): boolean {
@@ -1301,12 +1295,10 @@ export class GhosttyTerminalSurface {
13011295
this.canvas.setPointerCapture(event.pointerId);
13021296
return;
13031297
}
1304-
if (event.button === 1 && isTerminalMiddleClickPastePlatform()) {
1305-
// Cancelling the pointer event also suppresses the compatibility
1306-
// mousedown, and with it Chromium's middle-click autoscroll.
1307-
event.preventDefault();
1308-
event.stopPropagation();
1309-
this.pasteFromSelectionBuffer();
1298+
if (event.button === 1 && isMiddleClickPastePlatform()) {
1299+
// Left uncancelled on purpose: cancelling pointerdown drops the
1300+
// compatibility mousedown, which is what activates a split pane.
1301+
this.pasteTerminalSelection();
13101302
return;
13111303
}
13121304
if (event.button !== 0) return;
@@ -1552,6 +1544,10 @@ export class GhosttyTerminalSurface {
15521544
if (this.canvas.hasPointerCapture(event.pointerId)) {
15531545
this.canvas.releasePointerCapture(event.pointerId);
15541546
}
1547+
if (event.button === 1 && isMiddleClickPastePlatform()) {
1548+
event.preventDefault();
1549+
return;
1550+
}
15551551
if (event.button !== 0) return;
15561552
if (!this.selectionMoved && this.selectionMode === "cell") {
15571553
this.clearSelection();
@@ -1588,15 +1584,23 @@ export class GhosttyTerminalSurface {
15881584
};
15891585

15901586
private readonly onMouseDown = (event: MouseEvent) => {
1591-
// onPointerDown answers the middle button; cancelling it here too keeps
1592-
// Chromium's autoscroll from starting in browsers that still deliver the
1593-
// compatibility mousedown after a cancelled pointerdown.
1594-
if (event.button === 0 || (event.button === 1 && isTerminalMiddleClickPastePlatform())) {
1587+
// Cancelling the middle button here stops autoscroll while still letting
1588+
// the event bubble to the drawer handler that activates a split pane.
1589+
if (event.button === 0 || (event.button === 1 && isMiddleClickPastePlatform())) {
15951590
event.preventDefault();
15961591
}
15971592
this.focus();
15981593
};
15991594

1595+
/**
1596+
* Chromium pastes PRIMARY into the focused editable on a middle mouseup, and
1597+
* the hidden textarea is focused, so leaving the default alive would deliver
1598+
* a second paste through onPaste on top of the one onPointerDown sent.
1599+
*/
1600+
private readonly onMouseUp = (event: MouseEvent) => {
1601+
if (event.button === 1 && isMiddleClickPastePlatform()) event.preventDefault();
1602+
};
1603+
16001604
private readonly onContextMenu = (event: MouseEvent) => {
16011605
if (shouldReportTerminalMouse(this.core.isMouseTracking(), event)) {
16021606
event.preventDefault();
@@ -1686,6 +1690,7 @@ export class GhosttyTerminalSurface {
16861690
this.canvas.addEventListener("pointercancel", this.onPointerUp);
16871691
this.canvas.addEventListener("wheel", this.onWheel, { passive: false });
16881692
this.canvas.addEventListener("mousedown", this.onMouseDown);
1693+
this.canvas.addEventListener("mouseup", this.onMouseUp);
16891694
this.canvas.addEventListener("contextmenu", this.onContextMenu);
16901695
this.scrollbar.addEventListener("pointerdown", this.onScrollbarPointerDown);
16911696
this.scrollbar.addEventListener("pointermove", this.onScrollbarPointerMove);
@@ -1711,6 +1716,7 @@ export class GhosttyTerminalSurface {
17111716
this.canvas.removeEventListener("pointercancel", this.onPointerUp);
17121717
this.canvas.removeEventListener("wheel", this.onWheel);
17131718
this.canvas.removeEventListener("mousedown", this.onMouseDown);
1719+
this.canvas.removeEventListener("mouseup", this.onMouseUp);
17141720
this.canvas.removeEventListener("contextmenu", this.onContextMenu);
17151721
this.scrollbar.removeEventListener("pointerdown", this.onScrollbarPointerDown);
17161722
this.scrollbar.removeEventListener("pointermove", this.onScrollbarPointerMove);

0 commit comments

Comments
 (0)