Skip to content

Commit 9782d0f

Browse files
fix(web): reset terminal motion dedupe state
Co-authored-by: maria <[email protected]>
1 parent 5bcb285 commit 9782d0f

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
loadTerminalFontFamily,
1818
primeTerminalCopyInput,
1919
resolveTerminalMouseData,
20+
resolveTerminalMouseTrackingState,
2021
shouldBlinkTerminalCursor,
2122
shouldReportTerminalMouse,
2223
shouldShowTerminalLinkHover,
@@ -419,6 +420,25 @@ describe("application mouse reporting", () => {
419420
});
420421
});
421422

423+
it("clears the motion baseline when application mouse tracking changes", () => {
424+
expect(resolveTerminalMouseTrackingState(true, false, "\u001b[<35;8;4M")).toEqual({
425+
tracking: false,
426+
motionData: "",
427+
});
428+
expect(resolveTerminalMouseTrackingState(false, true, "\u001b[<35;8;4M")).toEqual({
429+
tracking: true,
430+
motionData: "",
431+
});
432+
expect(resolveTerminalMouseTrackingState(true, true, "\u001b[<35;8;4M")).toEqual({
433+
tracking: true,
434+
motionData: "\u001b[<35;8;4M",
435+
});
436+
expect(resolveTerminalMouseTrackingState(false, false, "\u001b[<35;8;4M")).toEqual({
437+
tracking: false,
438+
motionData: "\u001b[<35;8;4M",
439+
});
440+
});
441+
422442
it("only shows link hover during mouse tracking when the link modifier is held", () => {
423443
expect(shouldShowTerminalLinkHover(false, false)).toBe(true);
424444
expect(shouldShowTerminalLinkHover(false, true)).toBe(true);

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,17 @@ export function resolveTerminalMouseData(
432432
};
433433
}
434434

435+
export function resolveTerminalMouseTrackingState(
436+
previousTracking: boolean,
437+
tracking: boolean,
438+
motionData: string,
439+
): { readonly tracking: boolean; readonly motionData: string } {
440+
return {
441+
tracking,
442+
motionData: previousTracking === tracking ? motionData : "",
443+
};
444+
}
445+
435446
export function terminalWheelDeltaRows(
436447
event: Pick<WheelEvent, "deltaY" | "deltaMode">,
437448
cellHeight: number,
@@ -608,6 +619,7 @@ export class GhosttyTerminalSurface {
608619
private primedCopySelection = "";
609620
private wheelRemainder = 0;
610621
private lastMouseMotionData = "";
622+
private mouseAnyEventTracking = false;
611623
private dprMedia: MediaQueryList | null = null;
612624
// Read live on every blink decision, and watched so that dropping the
613625
// preference restarts a blink cycle that has no timer left to notice it.
@@ -634,6 +646,7 @@ export class GhosttyTerminalSurface {
634646
this.scrollbarThumb = scrollbarThumb;
635647
this.context = context;
636648
this.core = core;
649+
this.mouseAnyEventTracking = core.isMouseAnyEventTracking();
637650
this.metrics = metrics;
638651
this.options = options;
639652
this.theme = options.theme;
@@ -725,6 +738,7 @@ export class GhosttyTerminalSurface {
725738
write(data: string): void {
726739
if (this.disposed) return;
727740
this.core.write(data);
741+
this.synchronizeMouseTrackingState();
728742
// Restart the blink cycle from the visible phase so the cursor never sits
729743
// invisible through a stream of output or a burst of typing echo.
730744
this.cursorOn = true;
@@ -734,7 +748,9 @@ export class GhosttyTerminalSurface {
734748

735749
resetAndWrite(data: string): void {
736750
if (this.disposed) return;
751+
this.lastMouseMotionData = "";
737752
this.core.resetAndWrite(data);
753+
this.synchronizeMouseTrackingState();
738754
// A replayed session starts from the visible phase like any other write:
739755
// reattaching mid-blink must not open on an invisible cursor.
740756
this.cursorOn = true;
@@ -1287,9 +1303,10 @@ export class GhosttyTerminalSurface {
12871303
if (this.linkActivationPointerId === event.pointerId) return;
12881304
// Hover motion is only reportable in any-event tracking (DEC 1003); normal and
12891305
// button-event tracking never report motion without a captured pressed button.
1306+
const anyEventTracking = this.synchronizeMouseTrackingState();
12901307
if (
12911308
this.mouseReportingPointerId === event.pointerId ||
1292-
shouldReportTerminalMouse(this.core.isMouseAnyEventTracking(), event)
1309+
shouldReportTerminalMouse(anyEventTracking, event)
12931310
) {
12941311
event.preventDefault();
12951312
this.hoverPointer = { x: event.clientX, y: event.clientY };
@@ -1301,6 +1318,7 @@ export class GhosttyTerminalSurface {
13011318
this.sendMouse("motion", this.buttonFromButtons(event.buttons), event);
13021319
return;
13031320
}
1321+
this.lastMouseMotionData = "";
13041322
if (!this.selectionAnchorScreen || !this.canvas.hasPointerCapture(event.pointerId)) {
13051323
this.updateHoverCursor(event);
13061324
return;
@@ -1865,6 +1883,20 @@ export class GhosttyTerminalSurface {
18651883
if (resolution.send) this.options.onData(data);
18661884
}
18671885

1886+
private synchronizeMouseTrackingState(): boolean {
1887+
// Output writes can toggle DEC 1003 without moving the pointer. Keep the
1888+
// previous mode so the next same-cell motion starts a fresh tracking session.
1889+
const tracking = this.core.isMouseAnyEventTracking();
1890+
const state = resolveTerminalMouseTrackingState(
1891+
this.mouseAnyEventTracking,
1892+
tracking,
1893+
this.lastMouseMotionData,
1894+
);
1895+
this.mouseAnyEventTracking = state.tracking;
1896+
this.lastMouseMotionData = state.motionData;
1897+
return tracking;
1898+
}
1899+
18681900
private buttonFromButtons(buttons: number): number | null {
18691901
if ((buttons & 1) !== 0) return 1;
18701902
if ((buttons & 4) !== 0) return 3;

0 commit comments

Comments
 (0)