Add duration-based smooth scroll

Fixes #1140
This commit is contained in:
Daniel Imms
2022-07-27 05:41:00 -07:00
parent b11217aa36
commit a55728dd28
5 changed files with 65 additions and 2 deletions
+49 -1
View File
@@ -36,6 +36,9 @@ export class Viewport extends Disposable implements IViewport {
private _refreshAnimationFrame: number | null = null; private _refreshAnimationFrame: number | null = null;
private _ignoreNextScrollEvent: boolean = false; private _ignoreNextScrollEvent: boolean = false;
private _lastSmoothScrollOrigin?: number = undefined;
private _lastSmoothScrollTarget?: number = undefined;
private _lastSmoothScrollStartTime?: number = undefined;
constructor( constructor(
private readonly _scrollLines: (amount: number) => void, private readonly _scrollLines: (amount: number) => void,
@@ -168,6 +171,33 @@ export class Viewport extends Disposable implements IViewport {
this._scrollLines(diff); this._scrollLines(diff);
} }
private _smoothScroll(): void {
// Check valid state
if (this._isDisposed || this._lastSmoothScrollOrigin === undefined || this._lastSmoothScrollTarget === undefined) {
return;
}
// Calculate position complete
const percent = this._smoothScrollPercent();
this._viewportElement.scrollTop = this._lastSmoothScrollOrigin + Math.round(percent * (this._lastSmoothScrollTarget - this._lastSmoothScrollOrigin));
// Continue or finish smooth scroll
if (percent < 1) {
window.requestAnimationFrame(() => this._smoothScroll());
} else {
this._lastSmoothScrollStartTime = undefined;
this._lastSmoothScrollOrigin = undefined;
this._lastSmoothScrollTarget = undefined;
}
}
private _smoothScrollPercent(): number {
if (!this._optionsService.rawOptions.smoothScrollingDuration || !this._lastSmoothScrollStartTime) {
return 1;
}
return Math.max(Math.min((Date.now() - this._lastSmoothScrollStartTime) / this._optionsService.rawOptions.smoothScrollingDuration, 1), 0);
}
/** /**
* Handles bubbling of scroll event in case the viewport has reached top or bottom * Handles bubbling of scroll event in case the viewport has reached top or bottom
* @param ev The scroll event. * @param ev The scroll event.
@@ -196,7 +226,25 @@ export class Viewport extends Disposable implements IViewport {
if (amount === 0) { if (amount === 0) {
return false; return false;
} }
this._viewportElement.scrollTop += amount; if (!this._optionsService.rawOptions.smoothScrollingDuration) {
this._viewportElement.scrollTop += amount;
} else {
this._lastSmoothScrollStartTime = Date.now();
if (this._smoothScrollPercent() < 1) {
this._lastSmoothScrollOrigin = this._viewportElement.scrollTop;
if (this._lastSmoothScrollTarget === undefined) {
this._lastSmoothScrollTarget = this._viewportElement.scrollTop + amount;
} else {
this._lastSmoothScrollTarget += amount;
}
this._lastSmoothScrollTarget = Math.max(Math.min(this._lastSmoothScrollTarget, this._viewportElement.scrollHeight), 0);
this._smoothScroll();
} else {
this._lastSmoothScrollStartTime = undefined;
this._lastSmoothScrollOrigin = undefined;
this._lastSmoothScrollTarget = undefined;
}
}
return this._bubbleScroll(ev, amount); return this._bubbleScroll(ev, amount);
} }
+1
View File
@@ -37,6 +37,7 @@ export const DEFAULT_OPTIONS: Readonly<ITerminalOptions> = {
scrollback: 1000, scrollback: 1000,
scrollSensitivity: 1, scrollSensitivity: 1,
screenReaderMode: false, screenReaderMode: false,
smoothScrollingDuration: 125,
macOptionIsMeta: false, macOptionIsMeta: false,
macOptionClickForcesSelection: false, macOptionClickForcesSelection: false,
minimumContrastRatio: 1, minimumContrastRatio: 1,
+1
View File
@@ -241,6 +241,7 @@ export interface ITerminalOptions {
screenReaderMode: boolean; screenReaderMode: boolean;
scrollback: number; scrollback: number;
scrollSensitivity: number; scrollSensitivity: number;
smoothScrollingDuration: number;
tabStopWidth: number; tabStopWidth: number;
theme: ITheme; theme: ITheme;
windowsMode: boolean; windowsMode: boolean;
+8 -1
View File
@@ -182,10 +182,17 @@ declare module 'xterm-headless' {
scrollback?: number; scrollback?: number;
/** /**
* The scrolling speed multiplier used for adjusting normal scrolling speed. * The duration to smoothly scroll between the origin and the target in
* milliseconds. Set to 0 to disable smooth scrolling and scroll instantly.
*/ */
scrollSensitivity?: number; scrollSensitivity?: number;
/**
* The duration to smoothly scroll between the origin and the target. Set
* this to 0 to disable smooth scrolling and scroll instantly.
*/
smoothScrollingDuration?: number;
/** /**
* The size of tab stops in the terminal. * The size of tab stops in the terminal.
*/ */
+6
View File
@@ -233,6 +233,12 @@ declare module 'xterm' {
*/ */
scrollSensitivity?: number; scrollSensitivity?: number;
/**
* The duration to smoothly scroll between the origin and the target in
* milliseconds. Set to 0 to disable smooth scrolling and scroll instantly.
*/
smoothScrollingDuration?: number;
/** /**
* The size of tab stops in the terminal. * The size of tab stops in the terminal.
*/ */