The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 16:30 UTC
fix(web): add copying terminal selection with ctrl+c in the web app (#5638)
Utkarsh Patil committed
Aug 14, 2026 at 23:22 UTC
1add47b322ab1dfb5010bb363613650176b88088
9 files changed
+159
-21
apps/web/src/components/ThreadTerminalDrawer.tsx
+6
-12
@@ -440,7 +440,6 @@ export function TerminalViewport({
440
onData: (data) => handleData(data),
441
onResize: (cols, rows) => void resizeTerminal(cols, rows),
442
onSelectionChange: () => handleSelectionChange(),
443
- onCopy: (text) => handleCopy(text),
443
beforeKey: (event) => handleBeforeKey(event),
444
onLinkActivate: (text, event) => handleLinkActivate(text, event),
445
};
@@ -668,17 +667,6 @@ export function TerminalViewport({
667
})();
668
}
669
671
- function handleCopy(text: string): void {
672
- void writeTextToClipboard(text, "terminal selection").catch((error: unknown) => {
673
- const activeTerminal = terminalRef.current;
674
- if (!activeTerminal) return;
675
- writeSystemMessage(
676
- activeTerminal,
677
- error instanceof Error ? error.message : "Unable to copy terminal selection",
678
- );
679
- });
680
- }
681
-
670
function handleData(data: string): void {
671
void (async () => {
672
const result = await writeTerminal(data);
@@ -696,6 +684,12 @@ export function TerminalViewport({
684
return;
685
}
686
clearSelectionAction();
687
+ // A copy shortcut that clears the selection (Ctrl+C) must also close
688
+ // the context menu that appears with the selection, but a clear that
689
+ // never opened a menu must not dismiss an unrelated one.
690
+ if (selectionActionMenuOpenRef.current) {
691
+ void localApi?.contextMenu.close();
692
+ }
693
}
694
695
const handleMouseUp = (event: MouseEvent) => {
apps/web/src/components/settings/SettingsFontPreviews.tsx
-1
@@ -238,7 +238,6 @@ export function TerminalFontPreview({ family, size }: { family: string; size: nu
238
onData: echo,
239
onResize: noop,
240
onSelectionChange: noop,
241
- onCopy: (text) => void navigator.clipboard?.writeText(text).catch(noop),
241
// Tab keeps walking the settings page instead of feeding the echo loop.
242
beforeKey: (event) => event.key !== "Tab",
243
onLinkActivate: noop,
apps/web/src/contextMenuFallback.test.ts
+35
-1
@@ -1,6 +1,6 @@
1
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2
3
-import { showContextMenuFallback } from "./contextMenuFallback";
3
+import { dismissContextMenu, showContextMenuFallback } from "./contextMenuFallback";
4
5
type FakeListener = (event: FakeDomEvent) => void;
6
@@ -236,3 +236,37 @@ describe("showContextMenuFallback", () => {
236
await expect(selectionPromise).resolves.toBe("rename:project-b");
237
});
238
});
239
+
240
+describe("dismissContextMenu", () => {
241
+ it("resolves an open menu with null", async () => {
242
+ const selectionPromise = showContextMenuFallback([
243
+ { id: "rename", label: "Rename" },
244
+ { id: "delete", label: "Delete" },
245
+ ]);
246
+ expect(findButton("Rename")).toBeTruthy();
247
+
248
+ dismissContextMenu();
249
+
250
+ await expect(selectionPromise).resolves.toBeNull();
251
+ expect(findButton("Rename")).toBeUndefined();
252
+ });
253
+
254
+ it("is a no-op when no menu is open", async () => {
255
+ dismissContextMenu();
256
+ expect(findButton("Rename")).toBeUndefined();
257
+ });
258
+
259
+ it("dismisses the prior menu when a new one opens", async () => {
260
+ const firstPromise = showContextMenuFallback([{ id: "first", label: "First" }]);
261
+ expect(findButton("First")).toBeTruthy();
262
+
263
+ const secondPromise = showContextMenuFallback([{ id: "second", label: "Second" }]);
264
+
265
+ await expect(firstPromise).resolves.toBeNull();
266
+ expect(findButton("First")).toBeUndefined();
267
+ expect(findButton("Second")).toBeTruthy();
268
+
269
+ dismissContextMenu();
270
+ await expect(secondPromise).resolves.toBeNull();
271
+ });
272
+});
apps/web/src/contextMenuFallback.ts
+27
@@ -101,6 +101,21 @@ function isNodeWithinMenuStack(target: EventTarget | null, menuStack: readonly H
101
return false;
102
}
103
104
+// Only one fallback menu exists at a time in the renderer; the active one is
105
+// tracked so a state change (for example a terminal selection clearing) can
106
+// dismiss it with the same result as an outside click or Escape.
107
+let activeContextMenuDismiss: (() => void) | null = null;
108
+
109
+/**
110
+ * Closes the currently open fallback context menu, resolving its show() with
111
+ * null (the same result as dismissing by outside click or Escape). No-op when
112
+ * no fallback menu is open.
113
+ */
114
+export function dismissContextMenu(): void {
115
+ activeContextMenuDismiss?.();
116
+ activeContextMenuDismiss = null;
117
+}
118
+
119
/**
120
* Imperative DOM-based context menu for non-Electron environments.
121
* Supports nested submenus and resolves with the clicked leaf item id.
@@ -114,11 +129,16 @@ export function showContextMenuFallback<T extends string>(
129
let isDisposed = false;
130
let canDismissFromPointer = false;
131
132
+ const dismiss = () => cleanup(null);
133
+
134
const cleanup = (result: T | null) => {
135
if (isDisposed) {
136
return;
137
}
138
isDisposed = true;
139
+ if (activeContextMenuDismiss === dismiss) {
140
+ activeContextMenuDismiss = null;
141
+ }
142
document.removeEventListener("keydown", onKeyDown);
143
document.removeEventListener("pointerdown", onPointerDown, true);
144
document.removeEventListener("contextmenu", onContextMenu, true);
@@ -299,6 +319,13 @@ export function showContextMenuFallback<T extends string>(
319
document.addEventListener("pointerdown", onPointerDown, true);
320
document.addEventListener("contextmenu", onContextMenu, true);
321
openMenu(items, position?.x ?? 0, position?.y ?? 0, 0);
322
+ // Only one fallback menu can be open at a time: a new show must dismiss
323
+ // any prior one, or its DOM and listeners leak and close() can only ever
324
+ // reach the newest menu.
325
+ if (activeContextMenuDismiss) {
326
+ activeContextMenuDismiss();
327
+ }
328
+ activeContextMenuDismiss = dismiss;
329
330
requestAnimationFrame(() => {
331
canDismissFromPointer = true;
apps/web/src/localApi.test.ts
+10
@@ -13,12 +13,14 @@ const showContextMenuFallbackMock =
13
position?: { x: number; y: number },
14
) => Promise<T | null>
15
>();
16
+const dismissContextMenuMock = vi.fn<() => void>();
17
18
const requestConfirmDialogMock =
19
vi.fn<(message: string, options?: ConfirmDialogOptions) => Promise<boolean> | undefined>();
20
21
vi.mock("./contextMenuFallback", () => ({
22
showContextMenuFallback: showContextMenuFallbackMock,
23
+ dismissContextMenu: dismissContextMenuMock,
24
}));
25
26
vi.mock("./confirmDialog", () => ({
@@ -85,6 +87,14 @@ describe("LocalApi", () => {
87
expect(showContextMenuFallbackMock).toHaveBeenCalledWith(items, { x: 4, y: 5 });
88
});
89
90
+ it("dismisses an open browser context menu without a desktop bridge", async () => {
91
+ const { createLocalApi } = await import("./localApi");
92
+
93
+ await createLocalApi().contextMenu.close();
94
+
95
+ expect(dismissContextMenuMock).toHaveBeenCalledOnce();
96
+ });
97
+
98
it("uses the themed confirmation host when it is available", async () => {
99
requestConfirmDialogMock.mockResolvedValue(true);
100
const { createLocalApi } = await import("./localApi");
apps/web/src/localApi.ts
+9
-1
@@ -1,7 +1,7 @@
1
import type { ConfirmDialogOptions, ContextMenuItem, LocalApi } from "@t3tools/contracts";
2
3
import { requestConfirmDialog } from "./confirmDialog";
4
-import { showContextMenuFallback } from "./contextMenuFallback";
4
+import { dismissContextMenu, showContextMenuFallback } from "./contextMenuFallback";
5
import { readBrowserClientSettings, writeBrowserClientSettings } from "./clientPersistenceStorage";
6
import { resetRequestLatencyStateForTests } from "./rpc/requestLatencyState";
7
@@ -41,6 +41,14 @@ function createBrowserLocalApi(): LocalApi {
41
}
42
return showContextMenuFallback(items, position);
43
},
44
+ // A native desktop menu blocks keyboard input and closes on outside
45
+ // interaction, so nothing to do there; the DOM fallback needs an explicit
46
+ // dismiss when the state behind it goes away.
47
+ close: async () => {
48
+ if (!window.desktopBridge) {
49
+ dismissContextMenu();
50
+ }
51
+ },
52
},
53
persistence: {
54
getClientSettings: async () => {
apps/web/src/terminal/ghostty/surface.test.ts
+3
-2
@@ -219,11 +219,12 @@ describe("isTerminalCopyShortcut", () => {
219
expect(isTerminalCopyShortcut(event({ metaKey: true }), "MacIntel")).toBe(true);
220
});
221
222
- it("uses the conventional Ctrl+Shift+C shortcut elsewhere", () => {
223
- expect(isTerminalCopyShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(false);
222
+ it("copies with Ctrl+C and Ctrl+Shift+C elsewhere", () => {
223
+ expect(isTerminalCopyShortcut(event({ ctrlKey: true }), "Linux x86_64")).toBe(true);
224
expect(isTerminalCopyShortcut(event({ ctrlKey: true, shiftKey: true }), "Linux x86_64")).toBe(
225
true,
226
);
227
+ expect(isTerminalCopyShortcut(event({}), "Linux x86_64")).toBe(false);
228
});
229
230
it("uses the produced character instead of the physical key position", () => {
apps/web/src/terminal/ghostty/surface.ts
+68
-4
@@ -333,7 +333,7 @@ export function isTerminalCopyShortcut(
333
platform = navigator.platform,
334
) {
335
if (event.key.toLowerCase() !== "c") return false;
336
- return isMacPlatform(platform) ? event.metaKey : event.ctrlKey && event.shiftKey;
336
+ return isMacPlatform(platform) ? event.metaKey : event.ctrlKey;
337
}
338
339
export function isTerminalPasteShortcut(
@@ -463,7 +463,6 @@ export interface GhosttyTerminalSurfaceOptions {
463
readonly onData: (data: string) => void;
464
readonly onResize: (cols: number, rows: number) => void;
465
readonly onSelectionChange: () => void;
466
- readonly onCopy: (text: string) => void;
466
readonly beforeKey: (event: KeyboardEvent) => boolean;
467
readonly onLinkActivate: (text: string, event: MouseEvent) => void;
468
}
@@ -531,6 +530,8 @@ export class GhosttyTerminalSurface {
530
private theme: GhosttyTheme;
531
private readonly suppressedKeyCodes = new Set<string>();
532
private pasteShortcutToken = 0;
533
+ private copyShortcutToken = 0;
534
+ private clearSelectionAfterCopy = false;
535
private wheelRemainder = 0;
536
private dprMedia: MediaQueryList | null = null;
537
// Read live on every blink decision, and watched so that dropping the
@@ -901,9 +902,58 @@ export class GhosttyTerminalSurface {
902
return;
903
}
904
if (isTerminalCopyShortcut(event) && this.hasSelection()) {
904
- event.preventDefault();
905
+ // A plain Ctrl+C/Cmd+C fires the browser's native copy event, caught in
906
+ // onCopyEvent; not preventing the default keeps that path alive. WebKit
907
+ // omits the keyboard copy event without a DOM selection, so race the
908
+ // clipboard write against it the same way paste races its read. The
909
+ // Shift variant has no native event (Chrome binds Ctrl+Shift+C to
910
+ // inspect), so synthesize one with execCommand("copy").
911
+ if (event.shiftKey) {
912
+ event.preventDefault();
913
+ document.execCommand("copy");
914
+ } else {
915
+ // A plain Ctrl+C is also SIGINT on non-mac: clear the selection once
916
+ // it copies so the next Ctrl+C reaches the shell. The Shift chord and
917
+ // Cmd+C are copy-only, so they keep the selection; resetting the flag
918
+ // up front also drops any clear owed by an earlier gesture that never
919
+ // completed.
920
+ this.clearSelectionAfterCopy = !event.shiftKey && !isMacPlatform(navigator.platform);
921
+ const clipboard = navigator.clipboard;
922
+ if (typeof clipboard?.writeText === "function") {
923
+ // Defer the write past the default action: the native copy event
924
+ // (dispatched synchronously with the default action) claims the
925
+ // token first when it fires, and the write covers browsers whose
926
+ // shortcut produces no copy event. Skipping a write the native
927
+ // event already handled stops a stale resolution from clobbering a
928
+ // clipboard the user filled after this copy.
929
+ const token = ++this.copyShortcutToken;
930
+ const selection = this.getSelection();
931
+ void Promise.resolve().then(() => {
932
+ if (this.disposed || this.copyShortcutToken !== token) return;
933
+ void clipboard.writeText(selection).then(
934
+ () => {
935
+ // The write may have been superseded while in flight; only
936
+ // touch the selection if this gesture still owns the token.
937
+ if (this.disposed || this.copyShortcutToken !== token) return;
938
+ if (this.clearSelectionAfterCopy) {
939
+ this.clearSelectionAfterCopy = false;
940
+ this.clearSelection();
941
+ }
942
+ },
943
+ () => {
944
+ // The write failed and the native event has already had its
945
+ // chance, so nothing copied and no clear is owed by this
946
+ // gesture; a newer one may have just set the flag, so only
947
+ // drop it if this gesture still owns the token.
948
+ if (this.copyShortcutToken === token) {
949
+ this.clearSelectionAfterCopy = false;
950
+ }
951
+ },
952
+ );
953
+ });
954
+ }
955
+ }
956
this.suppressedKeyCodes.add(event.code);
906
- this.options.onCopy(this.getSelection());
957
return;
958
}
959
if (isTerminalPasteShortcut(event)) {
@@ -989,6 +1039,18 @@ export class GhosttyTerminalSurface {
1039
this.dprMedia.addEventListener("change", this.onDevicePixelRatioChange);
1040
}
1041
1042
+ private readonly onCopyEvent = (event: ClipboardEvent) => {
1043
+ if (!this.hasSelection()) return;
1044
+ event.preventDefault();
1045
+ event.clipboardData?.setData("text/plain", this.getSelection());
1046
+ // The native event beat any deferred write; drop the in-flight fallback.
1047
+ this.copyShortcutToken += 1;
1048
+ if (this.clearSelectionAfterCopy) {
1049
+ this.clearSelectionAfterCopy = false;
1050
+ this.clearSelection();
1051
+ }
1052
+ };
1053
+
1054
private readonly onPaste = (event: ClipboardEvent) => {
1055
// Always suppress the browser's default insertion: content the textarea
1056
// would receive (for example an html-only clipboard converted to text)
@@ -1384,6 +1446,7 @@ export class GhosttyTerminalSurface {
1446
this.input.addEventListener("blur", this.onBlur);
1447
this.input.addEventListener("input", this.onInput);
1448
this.input.addEventListener("paste", this.onPaste);
1449
+ this.input.addEventListener("copy", this.onCopyEvent);
1450
this.input.addEventListener("compositionstart", this.onCompositionStart);
1451
this.input.addEventListener("compositionend", this.onCompositionEnd);
1452
this.canvas.addEventListener("pointerdown", this.onPointerDown);
@@ -1408,6 +1471,7 @@ export class GhosttyTerminalSurface {
1471
this.input.removeEventListener("blur", this.onBlur);
1472
this.input.removeEventListener("input", this.onInput);
1473
this.input.removeEventListener("paste", this.onPaste);
1474
+ this.input.removeEventListener("copy", this.onCopyEvent);
1475
this.input.removeEventListener("compositionstart", this.onCompositionStart);
1476
this.input.removeEventListener("compositionend", this.onCompositionEnd);
1477
this.canvas.removeEventListener("pointerdown", this.onPointerDown);
packages/contracts/src/ipc.ts
+1
@@ -1189,6 +1189,7 @@ export interface LocalApi {
1189
items: readonly ContextMenuItem<T>[],
1190
position?: { x: number; y: number },
1191
) => Promise<T | null>;
1192
+ close: () => Promise<void>;
1193
};
1194
persistence: {
1195
getClientSettings: () => Promise<ClientSettings | null>;