@devries-cameron20 / t3code-1 / commits / 4cd7a3eb1c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): stop opening a menu when a terminal selection ends (#24112)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

fixfon committed Nov 13, 2026 at 01:46 UTC 4cd7a3eb1c3ff00a037925728f30691b5dab05a0
3 files changed +4 -137
apps/web/src/components/ThreadTerminalDrawer.test.ts
-18
@@ -8,30 +8,12 @@ import {
8 } from "@t3tools/client-runtime/state/terminal";
9
10 import {
11 - shouldClearTerminalSelectionAction,
11 shouldHandleTerminalExit,
12 synchronizeTerminalOutput,
13 synchronizeTerminalSession,
14 } from "./ThreadTerminalDrawer";
15
16 describe("terminal selection actions", () => {
18 - it("does not let an old selection popup cancel its replacement right-click menu", () => {
19 - expect(
20 - shouldClearTerminalSelectionAction({
21 - actionPending: false,
22 - openMenuRequestId: 3,
23 - currentRequestId: 4,
24 - }),
25 - ).toBe(false);
26 - expect(
27 - shouldClearTerminalSelectionAction({
28 - actionPending: false,
29 - openMenuRequestId: null,
30 - currentRequestId: 4,
31 - }),
32 - ).toBe(false);
33 - });
34 -
17 it("handles an exit that lands while the terminal surface is still loading", () => {
18 expect(shouldHandleTerminalExit("exited", "running", false, 1)).toBe(true);
19 expect(shouldHandleTerminalExit("exited", "exited", false, 1)).toBe(false);
apps/web/src/components/ThreadTerminalDrawer.tsx
+4 -99
@@ -51,11 +51,6 @@ import { stackedThreadToast, toastManager } from "~/components/ui/toast";
51 import { readTextFromClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
52 import { cn } from "~/lib/utils";
53 import { type TerminalContextSelection } from "~/lib/terminalContext";
54 -import {
55 - observeSelectionActions,
56 - resolveSelectionActionPosition,
57 - type SelectionActionPoint,
58 -} from "~/lib/selectionActions";
54 import {
55 GhosttyTerminalSurface,
56 type GhosttyTerminalSurfaceOptions,
@@ -433,15 +428,6 @@ function terminalContextMenuItems(options: {
428 ];
429 }
430
436 -/** A superseded popup's unsettled promise must not cancel the newer right-click flow. */
437 -export function shouldClearTerminalSelectionAction(options: {
438 - actionPending: boolean;
439 - openMenuRequestId: number | null;
440 - currentRequestId: number;
441 -}): boolean {
442 - return options.actionPending || options.openMenuRequestId === options.currentRequestId;
443 -}
444 -
431 /**
432 * A writable session's output generation. A new one is a fresh PTY (or a
433 * reconnect) that has not seen the surface's grid yet.
@@ -587,7 +573,6 @@ function TerminalSessionViewport({
573 });
574 const hasHandledExitRef = useRef(false);
575 const selectionActionRequestIdRef = useRef(0);
590 - const openSelectionMenuRequestIdRef = useRef<number | null>(null);
576 const readKeybindings = useEffectEvent(() => keybindings);
577 const handleSessionExited = useEffectEvent(() => {
578 if (hasTerminalWriteAccess()) onSessionExited();
@@ -716,7 +701,6 @@ function TerminalSessionViewport({
701 let teardown: (() => void) | null = null;
702 let setupTerminal: GhosttyTerminalSurface | null = null;
703 let setupCleanups: Array<() => void> = [];
719 - let selectionActions: ReturnType<typeof observeSelectionActions> | null = null;
704
705 const setup = async (): Promise<(() => void) | null> => {
706 const setupFont = terminalFontRef.current;
@@ -725,7 +709,7 @@ function TerminalSessionViewport({
709 font: terminalFontOptions(setupFont.family, setupFont.size),
710 onData: (data) => handleData(data),
711 onResize: (cols, rows) => void resizeTerminal(cols, rows),
728 - onSelectionChange: () => handleSelectionChange(),
712 + onSelectionChange: () => {},
713 beforeKey: (event) => handleBeforeKey(event),
714 onLinkActivate: (text, event) => handleLinkActivate(text, event),
715 canActivateLink: (text) => canActivateTerminalLink(text),
@@ -798,28 +782,17 @@ function TerminalSessionViewport({
782 terminal.focus();
783 }
784
801 - const dismissSelectionAction = (supersede = false) => {
802 - const ownsMenu =
803 - openSelectionMenuRequestIdRef.current === selectionActionRequestIdRef.current;
804 - if (supersede || ownsMenu) selectionActionRequestIdRef.current += 1;
805 - if (ownsMenu) void localApi?.contextMenu.close();
806 - };
785 const clearSelectionAction = () => {
808 - selectionActions?.cancel();
809 - dismissSelectionAction(true);
786 + selectionActionRequestIdRef.current += 1;
787 };
788 setupCleanups.push(clearSelectionAction);
789
813 - const readSelectionAction = (
814 - pointer: SelectionActionPoint | null = null,
815 - ): {
816 - position: { x: number; y: number };
790 + const readSelectionAction = (): {
791 clipboardText: string;
792 selection: TerminalContextSelection;
793 } | null => {
794 const activeTerminal = terminalRef.current;
821 - const mountElement = containerRef.current;
822 - if (!activeTerminal || !mountElement || !activeTerminal.hasSelection()) {
795 + if (!activeTerminal || !activeTerminal.hasSelection()) {
796 return null;
797 }
798 const selectionText = activeTerminal.getSelection();
@@ -829,15 +802,7 @@ function TerminalSessionViewport({
802 return null;
803 }
804 const { lineStart, lineEnd } = terminalSelectionLineRange(selectionPosition);
832 - const bounds = mountElement.getBoundingClientRect();
833 - const position = resolveSelectionActionPosition({
834 - bounds,
835 - selectionRect: activeTerminal.getSelectionEndClientRect(),
836 - pointer,
837 - viewport: { width: window.innerWidth, height: window.innerHeight },
838 - });
805 return {
840 - position,
806 clipboardText: selectionText,
807 selection: {
808 terminalId,
@@ -947,44 +912,6 @@ function TerminalSessionViewport({
912 }
913 };
914
950 - const showSelectionAction = async (pointer: SelectionActionPoint | null) => {
951 - if (!localApi) {
952 - clearSelectionAction();
953 - return;
954 - }
955 - if (openSelectionMenuRequestIdRef.current === selectionActionRequestIdRef.current) {
956 - return;
957 - }
958 - const nextAction = readSelectionAction(pointer);
959 - if (!nextAction) {
960 - clearSelectionAction();
961 - return;
962 - }
963 - const requestId = ++selectionActionRequestIdRef.current;
964 - openSelectionMenuRequestIdRef.current = requestId;
965 - const clicked = await localApi.contextMenu
966 - .show(
967 - terminalSelectionMenuItems({ canAddToChat: canAddSelectionToChat() }),
968 - nextAction.position,
969 - )
970 - .finally(() => {
971 - if (openSelectionMenuRequestIdRef.current === requestId) {
972 - openSelectionMenuRequestIdRef.current = null;
973 - }
974 - });
975 - if (requestId !== selectionActionRequestIdRef.current || clicked === null) {
976 - return;
977 - }
978 - switch (clicked) {
979 - case "add-to-chat":
980 - if (canAddSelectionToChat()) addSelectionToChat(nextAction.clipboardText);
981 - return;
982 - case "copy":
983 - await copySelection(nextAction.clipboardText, requestId);
984 - return;
985 - }
986 - };
987 -
915 const sendTerminalInput = async (data: string, fallbackError: string) => {
916 if (!hasTerminalWriteAccess()) return;
917 const activeTerminal = terminalRef.current;
@@ -1103,28 +1030,6 @@ function TerminalSessionViewport({
1030 })();
1031 }
1032
1106 - function handleSelectionChange(): void {
1107 - if (terminalRef.current?.hasSelection()) {
1108 - return;
1109 - }
1110 - const shouldClear = shouldClearTerminalSelectionAction({
1111 - actionPending: selectionActions?.pending ?? false,
1112 - openMenuRequestId: openSelectionMenuRequestIdRef.current,
1113 - currentRequestId: selectionActionRequestIdRef.current,
1114 - });
1115 - if (!shouldClear) return;
1116 - clearSelectionAction();
1117 - }
1118 -
1119 - selectionActions = observeSelectionActions({
1120 - element: mount,
1121 - onSelection: (pointer) => {
1122 - void showSelectionAction(pointer);
1123 - },
1124 - onDismiss: (reason) => dismissSelectionAction(reason === "interaction"),
1125 - });
1126 - setupCleanups.push(() => selectionActions?.dispose());
1127 -
1033 const themeObserver = new MutationObserver(() => {
1034 const activeTerminal = terminalRef.current;
1035 if (!activeTerminal) return;
apps/web/src/terminal/ghostty/surface.ts
-20
@@ -1030,26 +1030,6 @@ export class GhosttyTerminalSurface {
1030 return before ? { start, end } : { start: end, end: start };
1031 }
1032
1033 - getSelectionEndClientRect(): { readonly right: number; readonly bottom: number } | null {
1034 - const position = this.getSelectionPosition();
1035 - if (!position) return null;
1036 - const viewportEnd = this.core.screenPointToViewport(position.end.x, position.end.y);
1037 - if (
1038 - !viewportEnd ||
1039 - viewportEnd.x < 0 ||
1040 - viewportEnd.x >= this.cols ||
1041 - viewportEnd.y < 0 ||
1042 - viewportEnd.y >= this.rows
1043 - ) {
1044 - return null;
1045 - }
1046 - const bounds = this.canvas.getBoundingClientRect();
1047 - return {
1048 - right: bounds.left + CONTENT_PADDING + (viewportEnd.x + 1) * this.metrics.width,
1049 - bottom: bounds.top + this.originY + (viewportEnd.y + 1) * this.metrics.height,
1050 - };
1051 - }
1052 -
1033 clearSelection({ source = "user" }: { source?: "user" | "output" } = {}): void {
1034 if (source === "user") this.linkActivationRequestId += 1;
1035 this.clearPrimedCopy();