Skip to content

Commit 313ca27

Browse files
t3dotggclaude
andauthored
fix(web): terminal drag selection scrolls faster the farther past the edge (#22872)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a27b246 commit 313ca27

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
resolveTerminalMouseData,
1212
terminalLinkAtPositionWithRange,
1313
terminalContentOriginY,
14+
terminalSelectionAutoscrollRows,
1415
terminalWheelDeltaRows,
1516
GhosttyTerminalSurface,
1617
type GhosttyTerminalSurfaceOptions,
@@ -747,6 +748,21 @@ describe("terminalWheelDeltaRows", () => {
747748
});
748749
});
749750

751+
// regression: #19492 drag selection scrolled one row per tick however far past the edge
752+
describe("terminalSelectionAutoscrollRows", () => {
753+
it("scrolls more rows per tick the farther the pointer is past an edge", () => {
754+
const top = 100;
755+
const bottom = 500;
756+
expect(terminalSelectionAutoscrollRows(300, top, bottom)).toBe(0);
757+
expect(terminalSelectionAutoscrollRows(99, top, bottom)).toBe(-1);
758+
expect(terminalSelectionAutoscrollRows(75, top, bottom)).toBe(-8);
759+
expect(terminalSelectionAutoscrollRows(501, top, bottom)).toBe(1);
760+
expect(terminalSelectionAutoscrollRows(525, top, bottom)).toBe(8);
761+
expect(terminalSelectionAutoscrollRows(550, top, bottom)).toBe(15);
762+
expect(terminalSelectionAutoscrollRows(2_000, top, bottom)).toBe(15);
763+
});
764+
});
765+
750766
describe("advanceTerminalSelectionClickSequence", () => {
751767
it("recognizes stationary double and triple pointer presses without PointerEvent.detail", () => {
752768
const first = advanceTerminalSelectionClickSequence(null, {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,21 @@ export function terminalWheelDeltaRows(
423423
return { rows, remainder: total - rows };
424424
}
425425

426+
// Ramp from 1 to 15 rows per autoscroll tick over the first 50px past the edge, as xterm.js does.
427+
const SELECTION_AUTOSCROLL_RAMP_PX = 50;
428+
const SELECTION_AUTOSCROLL_MAX_ROWS = 15;
429+
430+
export function terminalSelectionAutoscrollRows(
431+
clientY: number,
432+
top: number,
433+
bottom: number,
434+
): number {
435+
const distance = clientY < top ? clientY - top : clientY > bottom ? clientY - bottom : 0;
436+
if (distance === 0) return 0;
437+
const ramp = Math.max(-1, Math.min(1, distance / SELECTION_AUTOSCROLL_RAMP_PX));
438+
return Math.sign(ramp) + Math.round(ramp * (SELECTION_AUTOSCROLL_MAX_ROWS - 1));
439+
}
440+
426441
function terminalWheelArrowData(rows: number, applicationCursorKeys: boolean): string {
427442
if (rows === 0) return "";
428443
const sequence =
@@ -1434,7 +1449,7 @@ export class GhosttyTerminalSurface {
14341449
const bounds = this.canvas.getBoundingClientRect();
14351450
const top = Math.max(bounds.top, 0) + CONTENT_PADDING;
14361451
const bottom = Math.min(bounds.bottom, window.innerHeight) - CONTENT_PADDING;
1437-
this.setSelectionAutoscroll(event.clientY < top ? -1 : event.clientY > bottom ? 1 : 0);
1452+
this.setSelectionAutoscroll(terminalSelectionAutoscrollRows(event.clientY, top, bottom));
14381453
const cell = this.cellAt(event.clientX, event.clientY);
14391454
if (cell.x === this.selectionEnd?.x && cell.y === this.selectionEnd.y) return;
14401455
this.extendSelectionTo(event.clientX, event.clientY);

0 commit comments

Comments
 (0)