Skip to content
TypeScript

fix(web): terminal drag selection scrolls faster the farther past the edge (#22872)

Theo Browne313ca2715fbe
diff --git a/apps/web/src/terminal/ghostty/surface.test.ts b/apps/web/src/terminal/ghostty/surface.test.ts
index 753508c918e..8816105599b 100644
--- a/apps/web/src/terminal/ghostty/surface.test.ts
+++ b/apps/web/src/terminal/ghostty/surface.test.ts
@@ -11,6 +11,7 @@ import {
resolveTerminalMouseData,
terminalLinkAtPositionWithRange,
terminalContentOriginY,
+ terminalSelectionAutoscrollRows,
terminalWheelDeltaRows,
GhosttyTerminalSurface,
type GhosttyTerminalSurfaceOptions,
@@ -747,6 +748,21 @@ describe("terminalWheelDeltaRows", () => {
});
});
+// regression: #19492 drag selection scrolled one row per tick however far past the edge
+describe("terminalSelectionAutoscrollRows", () => {
+ it("scrolls more rows per tick the farther the pointer is past an edge", () => {
+ const top = 100;
+ const bottom = 500;
+ expect(terminalSelectionAutoscrollRows(300, top, bottom)).toBe(0);
+ expect(terminalSelectionAutoscrollRows(99, top, bottom)).toBe(-1);
+ expect(terminalSelectionAutoscrollRows(75, top, bottom)).toBe(-8);
+ expect(terminalSelectionAutoscrollRows(501, top, bottom)).toBe(1);
+ expect(terminalSelectionAutoscrollRows(525, top, bottom)).toBe(8);
+ expect(terminalSelectionAutoscrollRows(550, top, bottom)).toBe(15);
+ expect(terminalSelectionAutoscrollRows(2_000, top, bottom)).toBe(15);
+ });
+});
+
describe("advanceTerminalSelectionClickSequence", () => {
it("recognizes stationary double and triple pointer presses without PointerEvent.detail", () => {
const first = advanceTerminalSelectionClickSequence(null, {
diff --git a/apps/web/src/terminal/ghostty/surface.ts b/apps/web/src/terminal/ghostty/surface.ts
index cf24224fded..964a947f67a 100644
--- a/apps/web/src/terminal/ghostty/surface.ts
+++ b/apps/web/src/terminal/ghostty/surface.ts
@@ -423,6 +423,21 @@ export function terminalWheelDeltaRows(
return { rows, remainder: total - rows };
}
+// Ramp from 1 to 15 rows per autoscroll tick over the first 50px past the edge, as xterm.js does.
+const SELECTION_AUTOSCROLL_RAMP_PX = 50;
+const SELECTION_AUTOSCROLL_MAX_ROWS = 15;
+
+export function terminalSelectionAutoscrollRows(
+ clientY: number,
+ top: number,
+ bottom: number,
+): number {
+ const distance = clientY < top ? clientY - top : clientY > bottom ? clientY - bottom : 0;
+ if (distance === 0) return 0;
+ const ramp = Math.max(-1, Math.min(1, distance / SELECTION_AUTOSCROLL_RAMP_PX));
+ return Math.sign(ramp) + Math.round(ramp * (SELECTION_AUTOSCROLL_MAX_ROWS - 1));
+}
+
function terminalWheelArrowData(rows: number, applicationCursorKeys: boolean): string {
if (rows === 0) return "";
const sequence =
@@ -1434,7 +1449,7 @@ export class GhosttyTerminalSurface {
const bounds = this.canvas.getBoundingClientRect();
const top = Math.max(bounds.top, 0) + CONTENT_PADDING;
const bottom = Math.min(bounds.bottom, window.innerHeight) - CONTENT_PADDING;
- this.setSelectionAutoscroll(event.clientY < top ? -1 : event.clientY > bottom ? 1 : 0);
+ this.setSelectionAutoscroll(terminalSelectionAutoscrollRows(event.clientY, top, bottom));
const cell = this.cellAt(event.clientX, event.clientY);
if (cell.x === this.selectionEnd?.x && cell.y === this.selectionEnd.y) return;
this.extendSelectionTo(event.clientX, event.clientY);