Skip to content

Commit f2e6b6e

Browse files
committed
fix(web): sync terminal link hover modifiers
1 parent 138e0cf commit f2e6b6e

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isTerminalPasteShortcut,
1414
shouldBlinkTerminalCursor,
1515
shouldReportTerminalMouse,
16+
shouldShowTerminalLinkHover,
1617
terminalGridCellAt,
1718
terminalScrollbarGeometry,
1819
terminalScrollbarOffsetAtPointer,
@@ -284,6 +285,13 @@ describe("application mouse reporting", () => {
284285
it("maps browser buttons to Ghostty's button enum", () => {
285286
expect([0, 1, 2, 3, 4, 5].map(ghosttyMouseButton)).toEqual([1, 3, 2, 4, 5, null]);
286287
});
288+
289+
it("only shows link hover during mouse tracking when the link modifier is held", () => {
290+
expect(shouldShowTerminalLinkHover(false, false)).toBe(true);
291+
expect(shouldShowTerminalLinkHover(false, true)).toBe(true);
292+
expect(shouldShowTerminalLinkHover(true, false)).toBe(false);
293+
expect(shouldShowTerminalLinkHover(true, true)).toBe(true);
294+
});
287295
});
288296

289297
describe("terminal font resolution", () => {

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ export function isTerminalLinkPointerGesture(
403403
: event.ctrlKey && !event.metaKey;
404404
}
405405

406+
export function shouldShowTerminalLinkHover(
407+
mouseTracking: boolean,
408+
linkModifierActive: boolean,
409+
): boolean {
410+
return !mouseTracking || linkModifierActive;
411+
}
412+
406413
export function ghosttyMouseButton(button: number): number | null {
407414
switch (button) {
408415
case 0:
@@ -510,7 +517,8 @@ export class GhosttyTerminalSurface {
510517
private mouseReportingButton: number | null = null;
511518
private linkActivationPointerId: number | null = null;
512519
private hoveredLink: TerminalLinkWithRange | null = null;
513-
private hoverPointer: { x: number; y: number; allowInMouseTracking: boolean } | null = null;
520+
private hoverPointer: { x: number; y: number } | null = null;
521+
private linkModifierActive = false;
514522
private selectionClickSequence: TerminalSelectionClickSequence | null = null;
515523
private selectionMoved = false;
516524
private composing = false;
@@ -882,6 +890,7 @@ export class GhosttyTerminalSurface {
882890
}
883891

884892
private readonly onKeyDown = (event: KeyboardEvent) => {
893+
this.updateLinkModifier(event);
885894
// Presses handled outside the terminal must also swallow their release:
886895
// beforeKey runs side effects (keybindings, navigation sends), so it cannot
887896
// be consulted again on keyup, and Kitty report-event-types sessions would
@@ -933,6 +942,7 @@ export class GhosttyTerminalSurface {
933942
};
934943

935944
private readonly onKeyUp = (event: KeyboardEvent) => {
945+
this.updateLinkModifier(event);
936946
if (this.suppressedKeyCodes.delete(event.code)) return;
937947
if (event.isComposing || this.composing || event.key === "Process" || event.keyCode === 229) {
938948
return;
@@ -954,6 +964,8 @@ export class GhosttyTerminalSurface {
954964

955965
private readonly onBlur = () => {
956966
this.focused = false;
967+
this.linkModifierActive = false;
968+
this.refreshHoveredLink();
957969
// Suppressions survive blur deliberately: a shortcut that moves focus (for
958970
// example terminal-toggle) must still swallow its own keyup if focus comes
959971
// back before release. Stale entries are harmless — an encoding keydown
@@ -1098,7 +1110,10 @@ export class GhosttyTerminalSurface {
10981110
shouldReportTerminalMouse(this.core.isMouseAnyEventTracking(), event)
10991111
) {
11001112
event.preventDefault();
1101-
this.clearHoveredLink("default");
1113+
this.hoverPointer = { x: event.clientX, y: event.clientY };
1114+
this.linkModifierActive = isTerminalLinkPointerGesture(event);
1115+
this.setHoveredLink(null);
1116+
this.canvas.style.cursor = "default";
11021117
this.sendMouse("motion", this.buttonFromButtons(event.buttons), event);
11031118
return;
11041119
}
@@ -1167,11 +1182,15 @@ export class GhosttyTerminalSurface {
11671182
}
11681183

11691184
private updateHoverCursor(event: PointerEvent): void {
1170-
this.hoverPointer = {
1171-
x: event.clientX,
1172-
y: event.clientY,
1173-
allowInMouseTracking: isTerminalLinkPointerGesture(event),
1174-
};
1185+
this.hoverPointer = { x: event.clientX, y: event.clientY };
1186+
this.linkModifierActive = isTerminalLinkPointerGesture(event);
1187+
this.refreshHoveredLink();
1188+
}
1189+
1190+
private updateLinkModifier(event: Pick<KeyboardEvent, "ctrlKey" | "metaKey">): void {
1191+
const active = isTerminalLinkPointerGesture(event);
1192+
if (active === this.linkModifierActive) return;
1193+
this.linkModifierActive = active;
11751194
this.refreshHoveredLink();
11761195
}
11771196

@@ -1188,7 +1207,7 @@ export class GhosttyTerminalSurface {
11881207
private refreshHoveredLink(): void {
11891208
const pointer = this.hoverPointer;
11901209
const link =
1191-
pointer && (!this.core.isMouseTracking() || pointer.allowInMouseTracking)
1210+
pointer && shouldShowTerminalLinkHover(this.core.isMouseTracking(), this.linkModifierActive)
11921211
? this.linkAt(pointer.x, pointer.y)
11931212
: null;
11941213
this.setHoveredLink(link);

0 commit comments

Comments
 (0)