The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 15:15 UTC
perf(web): dedupe terminal mouse motion reports (#7845)
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: maria <254055478+maria-rcks@users.noreply.github.com>
t3-code[bot] committed
Aug 21, 2026 at 18:08 UTC
592c5983c14d248aa3cfddb8e6c7372f12cd1ab6
2 files changed
+90
-7
apps/web/src/terminal/ghostty/surface.test.ts
+37
@@ -16,6 +16,8 @@ import {
16
isTerminalPasteShortcut,
17
loadTerminalFontFamily,
18
primeTerminalCopyInput,
19
+ resolveTerminalMouseData,
20
+ resolveTerminalMouseTrackingState,
21
shouldBlinkTerminalCursor,
22
shouldReportTerminalMouse,
23
shouldShowTerminalLinkHover,
@@ -402,6 +404,41 @@ describe("application mouse reporting", () => {
404
expect([0, 1, 2, 3, 4, 5].map(ghosttyMouseButton)).toEqual([1, 3, 2, 4, 5, null]);
405
});
406
407
+ it("drops repeated motion reports until another mouse action resets the cell", () => {
408
+ const first = resolveTerminalMouseData("motion", "\u001b[<35;8;4M", "");
409
+ expect(first).toEqual({ send: true, nextMotionData: "\u001b[<35;8;4M" });
410
+
411
+ const duplicate = resolveTerminalMouseData("motion", "\u001b[<35;8;4M", first.nextMotionData);
412
+ expect(duplicate).toEqual({ send: false, nextMotionData: "\u001b[<35;8;4M" });
413
+
414
+ const press = resolveTerminalMouseData("press", "\u001b[<0;8;4M", duplicate.nextMotionData);
415
+ expect(press).toEqual({ send: true, nextMotionData: "" });
416
+
417
+ expect(resolveTerminalMouseData("motion", "\u001b[<35;8;4M", press.nextMotionData)).toEqual({
418
+ send: true,
419
+ nextMotionData: "\u001b[<35;8;4M",
420
+ });
421
+ });
422
+
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
+
442
it("only shows link hover during mouse tracking when the link modifier is held", () => {
443
expect(shouldShowTerminalLinkHover(false, false)).toBe(true);
444
expect(shouldShowTerminalLinkHover(false, true)).toBe(true);
apps/web/src/terminal/ghostty/surface.ts
+53
-7
@@ -418,6 +418,31 @@ export function shouldReportTerminalMouse(
418
return tracking && !event.shiftKey && !event.ctrlKey && !event.metaKey;
419
}
420
421
+type TerminalMouseAction = "press" | "release" | "motion";
422
+
423
+export function resolveTerminalMouseData(
424
+ action: TerminalMouseAction,
425
+ data: string,
426
+ previousMotionData: string,
427
+): { readonly send: boolean; readonly nextMotionData: string } {
428
+ const nextMotionData = action === "motion" ? data : "";
429
+ return {
430
+ send: data.length > 0 && (action !== "motion" || data !== previousMotionData),
431
+ nextMotionData,
432
+ };
433
+}
434
+
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
+
446
export function terminalWheelDeltaRows(
447
event: Pick<WheelEvent, "deltaY" | "deltaMode">,
448
cellHeight: number,
@@ -593,6 +618,8 @@ export class GhosttyTerminalSurface {
618
private clearSelectionAfterCopy = false;
619
private primedCopySelection = "";
620
private wheelRemainder = 0;
621
+ private lastMouseMotionData = "";
622
+ private mouseAnyEventTracking = false;
623
private dprMedia: MediaQueryList | null = null;
624
// Read live on every blink decision, and watched so that dropping the
625
// preference restarts a blink cycle that has no timer left to notice it.
@@ -619,6 +646,7 @@ export class GhosttyTerminalSurface {
646
this.scrollbarThumb = scrollbarThumb;
647
this.context = context;
648
this.core = core;
649
+ this.mouseAnyEventTracking = core.isMouseAnyEventTracking();
650
this.metrics = metrics;
651
this.options = options;
652
this.theme = options.theme;
@@ -710,6 +738,7 @@ export class GhosttyTerminalSurface {
738
write(data: string): void {
739
if (this.disposed) return;
740
this.core.write(data);
741
+ this.synchronizeMouseTrackingState();
742
// Restart the blink cycle from the visible phase so the cursor never sits
743
// invisible through a stream of output or a burst of typing echo.
744
this.cursorOn = true;
@@ -719,7 +748,9 @@ export class GhosttyTerminalSurface {
748
749
resetAndWrite(data: string): void {
750
if (this.disposed) return;
751
+ this.lastMouseMotionData = "";
752
this.core.resetAndWrite(data);
753
+ this.synchronizeMouseTrackingState();
754
// A replayed session starts from the visible phase like any other write:
755
// reattaching mid-blink must not open on an invisible cursor.
756
this.cursorOn = true;
@@ -1272,9 +1303,10 @@ export class GhosttyTerminalSurface {
1303
if (this.linkActivationPointerId === event.pointerId) return;
1304
// Hover motion is only reportable in any-event tracking (DEC 1003); normal and
1305
// button-event tracking never report motion without a captured pressed button.
1306
+ const anyEventTracking = this.synchronizeMouseTrackingState();
1307
if (
1308
this.mouseReportingPointerId === event.pointerId ||
1277
- shouldReportTerminalMouse(this.core.isMouseAnyEventTracking(), event)
1309
+ shouldReportTerminalMouse(anyEventTracking, event)
1310
) {
1311
event.preventDefault();
1312
this.hoverPointer = { x: event.clientX, y: event.clientY };
@@ -1286,6 +1318,7 @@ export class GhosttyTerminalSurface {
1318
this.sendMouse("motion", this.buttonFromButtons(event.buttons), event);
1319
return;
1320
}
1321
+ this.lastMouseMotionData = "";
1322
if (!this.selectionAnchorScreen || !this.canvas.hasPointerCapture(event.pointerId)) {
1323
this.updateHoverCursor(event);
1324
return;
@@ -1364,6 +1397,7 @@ export class GhosttyTerminalSurface {
1397
}
1398
1399
private readonly onPointerLeave = () => {
1400
+ this.lastMouseMotionData = "";
1401
this.clearHoveredLink();
1402
};
1403
@@ -1822,11 +1856,7 @@ export class GhosttyTerminalSurface {
1856
return terminalLinkAtPositionWithRange(this.snapshot.rowData, cell.y, cell.x);
1857
}
1858
1825
- private sendMouse(
1826
- action: "press" | "release" | "motion",
1827
- button: number | null,
1828
- event: MouseEvent,
1829
- ): void {
1859
+ private sendMouse(action: TerminalMouseAction, button: number | null, event: MouseEvent): void {
1860
const bounds = this.canvas.getBoundingClientRect();
1861
const data = this.core.encodeMouse({
1862
action,
@@ -1848,7 +1878,23 @@ export class GhosttyTerminalSurface {
1878
paddingBottom: Math.max(0, bounds.height - this.originY - this.rows * this.metrics.height),
1879
anyButtonPressed: event.buttons !== 0,
1880
});
1851
- if (data.length > 0) this.options.onData(data);
1881
+ const resolution = resolveTerminalMouseData(action, data, this.lastMouseMotionData);
1882
+ this.lastMouseMotionData = resolution.nextMotionData;
1883
+ if (resolution.send) this.options.onData(data);
1884
+ }
1885
+
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
1900
private buttonFromButtons(buttons: number): number | null {