Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement duration-based smooth scrolling #3940

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion src/browser/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import { IRenderDimensions } from 'browser/renderer/Types';

const FALLBACK_SCROLL_BAR_WIDTH = 15;

interface ISmoothScrollState {
startTime: number;
origin: number;
target: number;
}

/**
* Represents the viewport of a terminal, the visible area within the larger buffer of output.
* Logic for the virtual scroll bar is included in this object.
Expand All @@ -36,6 +42,11 @@ export class Viewport extends Disposable implements IViewport {

private _refreshAnimationFrame: number | null = null;
private _ignoreNextScrollEvent: boolean = false;
private _smoothScrollState: ISmoothScrollState = {
startTime: 0,
origin: -1,
target: -1
};

constructor(
private readonly _scrollLines: (amount: number) => void,
Expand Down Expand Up @@ -168,6 +179,37 @@ export class Viewport extends Disposable implements IViewport {
this._scrollLines(diff);
}

private _smoothScroll(): void {
// Check valid state
if (this._isDisposed || this._smoothScrollState.origin === -1 || this._smoothScrollState.target === -1) {
return;
}

// Calculate position complete
const percent = this._smoothScrollPercent();
this._viewportElement.scrollTop = this._smoothScrollState.origin + Math.round(percent * (this._smoothScrollState.target - this._smoothScrollState.origin));

// Continue or finish smooth scroll
if (percent < 1) {
window.requestAnimationFrame(() => this._smoothScroll());
} else {
this._clearSmoothScrollState();
}
}

private _smoothScrollPercent(): number {
if (!this._optionsService.rawOptions.smoothScrollDuration || !this._smoothScrollState.startTime) {
return 1;
}
return Math.max(Math.min((Date.now() - this._smoothScrollState.startTime) / this._optionsService.rawOptions.smoothScrollDuration, 1), 0);
}

private _clearSmoothScrollState(): void {
this._smoothScrollState.startTime = 0;
this._smoothScrollState.origin = -1;
this._smoothScrollState.target = -1;
}

/**
* Handles bubbling of scroll event in case the viewport has reached top or bottom
* @param ev The scroll event.
Expand Down Expand Up @@ -196,7 +238,23 @@ export class Viewport extends Disposable implements IViewport {
if (amount === 0) {
return false;
}
this._viewportElement.scrollTop += amount;
if (!this._optionsService.rawOptions.smoothScrollDuration) {
this._viewportElement.scrollTop += amount;
} else {
this._smoothScrollState.startTime = Date.now();
if (this._smoothScrollPercent() < 1) {
this._smoothScrollState.origin = this._viewportElement.scrollTop;
if (this._smoothScrollState.target === -1) {
this._smoothScrollState.target = this._viewportElement.scrollTop + amount;
} else {
this._smoothScrollState.target += amount;
}
this._smoothScrollState.target = Math.max(Math.min(this._smoothScrollState.target, this._viewportElement.scrollHeight), 0);
this._smoothScroll();
} else {
this._clearSmoothScrollState();
}
}
return this._bubbleScroll(ev, amount);
}

Expand Down
1 change: 1 addition & 0 deletions src/common/services/OptionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const DEFAULT_OPTIONS: Readonly<ITerminalOptions> = {
scrollback: 1000,
scrollSensitivity: 1,
screenReaderMode: false,
smoothScrollDuration: 0,
macOptionIsMeta: false,
macOptionClickForcesSelection: false,
minimumContrastRatio: 1,
Expand Down
1 change: 1 addition & 0 deletions src/common/services/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export interface ITerminalOptions {
screenReaderMode: boolean;
scrollback: number;
scrollSensitivity: number;
smoothScrollDuration: number;
tabStopWidth: number;
theme: ITheme;
windowsMode: boolean;
Expand Down
6 changes: 6 additions & 0 deletions typings/xterm-headless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ declare module 'xterm-headless' {
*/
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.
*/
smoothScrollDuration?: number;

/**
* The size of tab stops in the terminal.
*/
Expand Down
6 changes: 6 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ declare module 'xterm' {
*/
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.
*/
smoothScrollDuration?: number;

/**
* The size of tab stops in the terminal.
*/
Expand Down