Skip to content

Commit 2c6000e

Browse files
maria-rcksclaude
andcommitted
fix(web): middle-click pastes in the terminal on Linux
The terminal canvas ignored the middle button: onPointerDown returned for any button but 0, so the standard Linux middle-click paste did nothing and Chromium started autoscroll instead. Middle-click now pastes through the same bracketed-paste path as every other paste. A browser cannot read the X11/Wayland PRIMARY buffer, so the terminal's own selection is the source when the user highlighted here and the system clipboard covers the rest. Applications that track the mouse still receive the click. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent b7b3ef1 commit 2c6000e

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ describe("GhosttyTerminalSurface visibility", () => {
167167
resize() {
168168
for (const callback of resizeCallbacks) callback();
169169
},
170-
pointer(type: string, clientX: number, buttons: number, shiftKey = false) {
170+
pointer(type: string, clientX: number, buttons: number, shiftKey = false, button = 0) {
171171
canvas.dispatchEvent(
172172
Object.assign(new Event(type, { cancelable: true }), {
173173
clientX,
174174
clientY: 5,
175175
pointerId: 1,
176-
button: 0,
176+
button,
177177
buttons,
178178
shiftKey,
179179
}),
@@ -280,6 +280,38 @@ 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 () => {
284+
const harness = createHarness();
285+
vi.stubGlobal("navigator", { platform: "Linux x86_64" });
286+
const surface = await harness.create();
287+
surface.write("hello world");
288+
harness.flushFrame();
289+
harness.pointer("pointerdown", 5, 1);
290+
harness.pointer("pointermove", 37, 1);
291+
harness.pointer("pointerup", 37, 0);
292+
expect(surface.getSelection()).toBe("hello");
293+
294+
harness.onData.mockClear();
295+
harness.pointer("pointerdown", 5, 4, false, 1);
296+
await vi.waitFor(() => expect(harness.onData).toHaveBeenCalled());
297+
expect(harness.onData.mock.calls.at(-1)?.[0]).toBe("hello");
298+
expect(surface.getSelection()).toBe("hello");
299+
});
300+
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();
309+
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!!!");
313+
});
314+
283315
it("starts a selection when dragging from a link", async () => {
284316
const harness = createHarness();
285317
const onLinkActivate = vi.fn();

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ export function isTerminalPasteShortcut(
395395
return isMacPlatform(platform) ? event.metaKey : event.ctrlKey && event.shiftKey;
396396
}
397397

398+
/**
399+
* Middle-click paste is an X11/Wayland convention. macOS and Windows have no
400+
* primary selection and use the button for autoscroll, so only desktops that
401+
* expect the gesture get it.
402+
*/
403+
export function isTerminalMiddleClickPastePlatform(platform = navigator.platform): boolean {
404+
return /linux|bsd|x11/i.test(platform);
405+
}
406+
398407
export function isTerminalCompositionCommitInput(event: Pick<InputEvent, "inputType">): boolean {
399408
return (
400409
event.inputType === "" ||
@@ -938,6 +947,26 @@ export class GhosttyTerminalSurface {
938947
if (encoded.length > 0) this.options.onData(encoded);
939948
}
940949

950+
/**
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.
956+
*/
957+
private pasteFromSelectionBuffer(): void {
958+
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+
});
968+
}
969+
941970
hasSelection(): boolean {
942971
return this.core.selectionText().length > 0;
943972
}
@@ -1272,6 +1301,14 @@ export class GhosttyTerminalSurface {
12721301
this.canvas.setPointerCapture(event.pointerId);
12731302
return;
12741303
}
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();
1310+
return;
1311+
}
12751312
if (event.button !== 0) return;
12761313
const clickCount = this.recordSelectionClick(event);
12771314
const link = this.linkAt(event.clientX, event.clientY);
@@ -1551,7 +1588,12 @@ export class GhosttyTerminalSurface {
15511588
};
15521589

15531590
private readonly onMouseDown = (event: MouseEvent) => {
1554-
if (event.button === 0) event.preventDefault();
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())) {
1595+
event.preventDefault();
1596+
}
15551597
this.focus();
15561598
};
15571599

0 commit comments

Comments
 (0)