Skip to content

Commit 772ea14

Browse files
fix(web): honor terminal link browser overrides (pingdotgg#10060)
1 parent 5d14c0e commit 772ea14

6 files changed

Lines changed: 184 additions & 57 deletions

File tree

‎apps/web/src/components/ThreadTerminalDrawer.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
} from "~/terminal/ghostty/surface";
5858
import { type GhosttyColor, type GhosttyTheme } from "~/terminal/ghostty/core";
5959
import { useOpenInPreferredEditor } from "../editorPreferences";
60-
import { isTerminalLinkActivation, isTerminalUrl, resolvePathLinkTarget } from "../terminal-links";
60+
import { isTerminalUrl, resolvePathLinkTarget } from "../terminal-links";
6161
import {
6262
isDiffToggleShortcut,
6363
isTerminalClearShortcut,
@@ -782,7 +782,6 @@ export function TerminalViewport({
782782
}
783783

784784
function handleLinkActivate(text: string, event: MouseEvent): void {
785-
if (!isTerminalLinkActivation(event)) return;
786785
const latestTerminal = terminalRef.current;
787786
if (!latestTerminal) return;
788787
if (isTerminalUrl(text)) {
@@ -803,6 +802,7 @@ export function TerminalViewport({
803802
threadRef,
804803
openPreview,
805804
fallbackToBrowser,
805+
forceBrowser: event.metaKey || event.ctrlKey,
806806
}).catch((error: unknown) => {
807807
toastManager.add(
808808
stackedThreadToast({

‎apps/web/src/components/preview/openTerminalLinkInPreview.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ describe("openTerminalLinkInPreview", () => {
8888
threadRef,
8989
openPreview,
9090
fallbackToBrowser,
91+
forceBrowser: false,
9192
}),
9293
).rejects.toBe(failure);
9394
expect(fallbackToBrowser).not.toHaveBeenCalled();
@@ -105,6 +106,7 @@ describe("openTerminalLinkInPreview", () => {
105106
threadRef,
106107
openPreview,
107108
fallbackToBrowser,
109+
forceBrowser: false,
108110
});
109111

110112
expect(fallbackToBrowser).toHaveBeenCalledOnce();
@@ -120,6 +122,7 @@ describe("openTerminalLinkInPreview", () => {
120122
threadRef,
121123
openPreview,
122124
fallbackToBrowser,
125+
forceBrowser: false,
123126
});
124127

125128
expect(openPreview).toHaveBeenCalledOnce();
@@ -141,6 +144,7 @@ describe("openTerminalLinkInPreview", () => {
141144
threadRef,
142145
openPreview,
143146
fallbackToBrowser: vi.fn(),
147+
forceBrowser: false,
144148
});
145149

146150
await vi.waitFor(() => expect(browserDefaultsMocks.resolve).toHaveBeenCalledOnce());
@@ -170,6 +174,7 @@ describe("openTerminalLinkInPreview", () => {
170174
threadRef,
171175
openPreview: async () => AsyncResult.failure(cause),
172176
fallbackToBrowser,
177+
forceBrowser: false,
173178
});
174179

175180
expect(fallbackToBrowser).toHaveBeenCalledOnce();
@@ -194,9 +199,26 @@ describe("openTerminalLinkInPreview", () => {
194199
threadRef,
195200
openPreview: async () => AsyncResult.failure(Cause.interrupt()),
196201
fallbackToBrowser,
202+
forceBrowser: false,
197203
});
198204

199205
expect(reportError).not.toHaveBeenCalled();
200206
expect(fallbackToBrowser).not.toHaveBeenCalled();
201207
});
208+
209+
it("opens in the system browser when Ctrl or Command is held", async () => {
210+
const fallbackToBrowser = vi.fn();
211+
const openPreview = vi.fn(async () => AsyncResult.success(snapshot));
212+
213+
await openTerminalLinkInPreview({
214+
url: "https://example.com/docs",
215+
threadRef,
216+
openPreview,
217+
fallbackToBrowser,
218+
forceBrowser: true,
219+
});
220+
221+
expect(fallbackToBrowser).toHaveBeenCalledOnce();
222+
expect(openPreview).not.toHaveBeenCalled();
223+
});
202224
});

‎apps/web/src/components/preview/openTerminalLinkInPreview.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ interface OpenTerminalLinkInPreviewInput<E> {
3434
readonly threadRef: ScopedThreadRef;
3535
readonly openPreview: OpenPreviewMutation<E>;
3636
readonly fallbackToBrowser: () => void;
37+
/** Cmd/Ctrl-click bypasses the preference and opens in the system browser. */
38+
readonly forceBrowser: boolean;
3739
}
3840

3941
/**
40-
* Opens a terminal hyperlink where the "Open links in" setting says. Terminal
41-
* links are activated with the platform modifier already held, so unlike chat
42-
* links the modifier cannot double as the system-browser override; the setting
43-
* alone decides, and the system browser is the fallback whenever the in-app
44-
* one cannot take the URL.
42+
* Opens a terminal hyperlink where the "Open links in" setting says, unless a
43+
* Cmd/Ctrl-click explicitly requests the system browser.
4544
*/
4645
export async function openTerminalLinkInPreview<E>(
4746
input: OpenTerminalLinkInPreviewInput<E>,
4847
): Promise<void> {
4948
const supportsPreview =
49+
!input.forceBrowser &&
5050
isWebUrl(input.url) &&
5151
isPreviewSupportedInRuntime() &&
5252
input.threadRef.threadId.length > 0 &&

‎apps/web/src/components/settings/IntegrationsSettings.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ function BrowserLinkTargetSetting({ disabled }: { readonly disabled: boolean })
516516
return (
517517
<SettingsRow
518518
{...searchableSetting("browser-link-target")}
519-
description="Where links in the chat and terminal open. Hold ⌘ or Ctrl while clicking a chat link to open it in your default browser either way."
519+
description="Where links in the chat and terminal open. Hold ⌘ or Ctrl while clicking a link to open it in your default browser either way."
520520
resetAction={
521521
!disabled && linkTarget !== DEFAULT_BROWSER_LINK_TARGET ? (
522522
<SettingResetButton

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

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
isTerminalCompositionCommitInput,
1313
isTerminalCompositionKey,
1414
isTerminalCopyShortcut,
15-
isTerminalLinkPointerGesture,
1615
isTerminalPasteShortcut,
1716
loadTerminalFontFamily,
1817
primeTerminalCopyInput,
@@ -168,14 +167,15 @@ describe("GhosttyTerminalSurface visibility", () => {
168167
resize() {
169168
for (const callback of resizeCallbacks) callback();
170169
},
171-
pointer(type: string, clientX: number, buttons: number) {
170+
pointer(type: string, clientX: number, buttons: number, shiftKey = false) {
172171
canvas.dispatchEvent(
173172
Object.assign(new Event(type, { cancelable: true }), {
174173
clientX,
175174
clientY: 5,
176175
pointerId: 1,
177176
button: 0,
178177
buttons,
178+
shiftKey,
179179
}),
180180
);
181181
},
@@ -280,6 +280,84 @@ describe("GhosttyTerminalSurface visibility", () => {
280280
expect(harness.renderedSnapshot.rowData[0]?.cells.some((cell) => cell.selected)).toBe(false);
281281
});
282282

283+
it("starts a selection when dragging from a link", async () => {
284+
const harness = createHarness();
285+
const onLinkActivate = vi.fn();
286+
const surface = await harness.create({ onLinkActivate });
287+
surface.write("https://example.com");
288+
harness.flushFrame();
289+
290+
harness.pointer("pointerdown", 5, 1);
291+
harness.pointer("pointermove", 37, 1);
292+
harness.pointer("pointerup", 37, 0);
293+
294+
expect(onLinkActivate).not.toHaveBeenCalled();
295+
expect(surface.getSelection()).toBe("https");
296+
});
297+
298+
it("keeps a link click active through slight pointer movement", async () => {
299+
const harness = createHarness();
300+
const onLinkActivate = vi.fn();
301+
const surface = await harness.create({ onLinkActivate });
302+
surface.write("https://example.com");
303+
harness.flushFrame();
304+
305+
harness.pointer("pointerdown", 5, 1);
306+
harness.pointer("pointermove", 6, 1);
307+
harness.pointer("pointerup", 6, 0);
308+
309+
expect(onLinkActivate).toHaveBeenCalledOnce();
310+
});
311+
312+
it("uses repeated link clicks for word and line selection", async () => {
313+
const harness = createHarness();
314+
const onLinkActivate = vi.fn();
315+
const surface = await harness.create({ onLinkActivate });
316+
surface.write("https://example.com tail");
317+
harness.flushFrame();
318+
319+
harness.pointer("pointerdown", 5, 1);
320+
harness.pointer("pointerup", 5, 0);
321+
harness.pointer("pointerdown", 5, 1);
322+
harness.pointer("pointerup", 5, 0);
323+
expect(onLinkActivate).toHaveBeenCalledOnce();
324+
expect(surface.getSelection()).not.toBe("");
325+
326+
harness.pointer("pointerdown", 5, 1);
327+
harness.pointer("pointerup", 5, 0);
328+
expect(onLinkActivate).toHaveBeenCalledOnce();
329+
expect(surface.getSelection()).toBe("https://example.com tail");
330+
});
331+
332+
it("uses Shift drags over links for selection", async () => {
333+
const harness = createHarness();
334+
const onLinkActivate = vi.fn();
335+
const surface = await harness.create({ onLinkActivate });
336+
surface.write("https://example.com");
337+
harness.flushFrame();
338+
339+
harness.pointer("pointerdown", 5, 1, true);
340+
harness.pointer("pointermove", 37, 1, true);
341+
harness.pointer("pointerup", 37, 0, true);
342+
expect(onLinkActivate).not.toHaveBeenCalled();
343+
expect(surface.getSelection()).toBe("https");
344+
});
345+
346+
it("does not activate a link replaced before pointer release", async () => {
347+
const harness = createHarness();
348+
const onLinkActivate = vi.fn();
349+
const surface = await harness.create({ onLinkActivate });
350+
surface.write("https://first.example");
351+
harness.flushFrame();
352+
353+
harness.pointer("pointerdown", 5, 1);
354+
surface.write("\x1b[2J\x1b[Hhttps://second.example");
355+
harness.flushFrame();
356+
harness.pointer("pointerup", 5, 0);
357+
358+
expect(onLinkActivate).not.toHaveBeenCalled();
359+
});
360+
283361
it("stops zero-size mounts and repaints when the same size returns", async () => {
284362
const harness = createHarness();
285363
const surface = await harness.create();
@@ -890,19 +968,6 @@ describe("terminalWheelArrowData", () => {
890968
});
891969
});
892970

893-
describe("isTerminalLinkPointerGesture", () => {
894-
it("uses Command on macOS and Control elsewhere", () => {
895-
expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "MacIntel")).toBe(true);
896-
expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "MacIntel")).toBe(false);
897-
expect(isTerminalLinkPointerGesture({ ctrlKey: true, metaKey: false }, "Linux x86_64")).toBe(
898-
true,
899-
);
900-
expect(isTerminalLinkPointerGesture({ ctrlKey: false, metaKey: true }, "Linux x86_64")).toBe(
901-
false,
902-
);
903-
});
904-
});
905-
906971
describe("advanceTerminalSelectionClickSequence", () => {
907972
it("recognizes stationary double and triple pointer presses without PointerEvent.detail", () => {
908973
const first = advanceTerminalSelectionClickSequence(null, {

0 commit comments

Comments
 (0)