Skip to content

Commit cad4abe

Browse files
fix(web): preserve terminal link selection gestures
1 parent 1cbd513 commit cad4abe

2 files changed

Lines changed: 86 additions & 16 deletions

File tree

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,15 @@ describe("GhosttyTerminalSurface visibility", () => {
167167
resize() {
168168
for (const callback of resizeCallbacks) callback();
169169
},
170-
pointer(type: string, clientX: number, buttons: number) {
170+
pointer(type: string, clientX: number, buttons: number, shiftKey = false) {
171171
canvas.dispatchEvent(
172172
Object.assign(new Event(type, { cancelable: true }), {
173173
clientX,
174174
clientY: 5,
175175
pointerId: 1,
176176
button: 0,
177177
buttons,
178+
shiftKey,
178179
}),
179180
);
180181
},
@@ -294,6 +295,54 @@ describe("GhosttyTerminalSurface visibility", () => {
294295
expect(surface.getSelection()).toBe("https");
295296
});
296297

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+
297346
it("does not activate a link replaced before pointer release", async () => {
298347
const harness = createHarness();
299348
const onLinkActivate = vi.fn();

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,17 @@ export interface TerminalSelectionClickSequence {
500500
readonly y: number;
501501
}
502502

503+
const TERMINAL_LINK_DRAG_THRESHOLD_PX = 4;
504+
503505
export function advanceTerminalSelectionClickSequence(
504506
previous: TerminalSelectionClickSequence | null,
505507
event: Pick<PointerEvent, "clientX" | "clientY" | "timeStamp">,
506508
): TerminalSelectionClickSequence {
507509
const repeats =
508510
previous !== null &&
509511
event.timeStamp - previous.time <= SELECTION_MULTI_CLICK_INTERVAL_MS &&
510-
Math.hypot(event.clientX - previous.x, event.clientY - previous.y) <= 4;
512+
Math.hypot(event.clientX - previous.x, event.clientY - previous.y) <=
513+
TERMINAL_LINK_DRAG_THRESHOLD_PX;
511514
return {
512515
count: repeats ? (previous.count >= 3 ? 1 : previous.count + 1) : 1,
513516
time: event.timeStamp,
@@ -593,7 +596,11 @@ export class GhosttyTerminalSurface {
593596
private mouseReportingButton: number | null = null;
594597
private linkActivationPointerId: number | null = null;
595598
private linkActivationLink: TerminalLinkWithRange | null = null;
596-
private linkActivationOrigin: { x: number; y: number; time: number } | null = null;
599+
private linkActivationOrigin: {
600+
x: number;
601+
y: number;
602+
clickCount: number;
603+
} | null = null;
597604
private hoveredLink: TerminalLinkWithRange | null = null;
598605
private hoverPointer: { x: number; y: number } | null = null;
599606
private selectionClickSequence: TerminalSelectionClickSequence | null = null;
@@ -1262,33 +1269,39 @@ export class GhosttyTerminalSurface {
12621269
return;
12631270
}
12641271
if (event.button !== 0) return;
1272+
const clickCount = this.recordSelectionClick(event);
12651273
const link = this.linkAt(event.clientX, event.clientY);
1266-
if (link) {
1274+
if (link && !event.shiftKey && clickCount === 1) {
12671275
event.preventDefault();
12681276
event.stopPropagation();
12691277
this.linkActivationPointerId = event.pointerId;
12701278
this.linkActivationLink = link;
12711279
this.linkActivationOrigin = {
12721280
x: event.clientX,
12731281
y: event.clientY,
1274-
time: event.timeStamp,
1282+
clickCount,
12751283
};
12761284
this.canvas.setPointerCapture(event.pointerId);
12771285
return;
12781286
}
12791287
this.clearHoveredLink();
1280-
this.beginSelection(event);
1288+
this.beginSelection(event, clickCount);
12811289
this.canvas.setPointerCapture(event.pointerId);
12821290
};
12831291

1284-
private beginSelection(event: { clientX: number; clientY: number; timeStamp: number }): void {
1285-
const cell = this.cellAt(event.clientX, event.clientY);
1286-
this.selectionMoved = false;
1292+
private recordSelectionClick(
1293+
event: Pick<PointerEvent, "clientX" | "clientY" | "timeStamp">,
1294+
): number {
12871295
this.selectionClickSequence = advanceTerminalSelectionClickSequence(
12881296
this.selectionClickSequence,
12891297
event,
12901298
);
1291-
const clickCount = this.selectionClickSequence.count;
1299+
return this.selectionClickSequence.count;
1300+
}
1301+
1302+
private beginSelection(event: { clientX: number; clientY: number }, clickCount: number): void {
1303+
const cell = this.cellAt(event.clientX, event.clientY);
1304+
this.selectionMoved = false;
12921305
this.selectionMode = clickCount >= 3 ? "line" : clickCount === 2 ? "word" : "cell";
12931306
const range =
12941307
this.selectionMode === "line"
@@ -1322,16 +1335,24 @@ export class GhosttyTerminalSurface {
13221335
private readonly onPointerMove = (event: PointerEvent) => {
13231336
if (this.linkActivationPointerId === event.pointerId) {
13241337
const origin = this.linkActivationOrigin;
1325-
if (origin === null || (event.clientX === origin.x && event.clientY === origin.y)) return;
1338+
if (
1339+
origin === null ||
1340+
Math.hypot(event.clientX - origin.x, event.clientY - origin.y) <=
1341+
TERMINAL_LINK_DRAG_THRESHOLD_PX
1342+
) {
1343+
return;
1344+
}
13261345
this.linkActivationPointerId = null;
13271346
this.linkActivationLink = null;
13281347
this.linkActivationOrigin = null;
13291348
this.clearHoveredLink();
1330-
this.beginSelection({
1331-
clientX: origin.x,
1332-
clientY: origin.y,
1333-
timeStamp: origin.time,
1334-
});
1349+
this.beginSelection(
1350+
{
1351+
clientX: origin.x,
1352+
clientY: origin.y,
1353+
},
1354+
origin.clickCount,
1355+
);
13351356
}
13361357
// Hover motion is only reportable in any-event tracking (DEC 1003); normal and
13371358
// button-event tracking never report motion without a captured pressed button.

0 commit comments

Comments
 (0)