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

Implemented smooth scrolling. #3616

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 2 additions & 0 deletions demo/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ function initOptions(term: TerminalType): void {
} else if (o === 'scrollSensitivity') {
term.options.scrollSensitivity = parseFloat(input.value);
updateTerminalSize();
} else if (o === 'smoothScrollingSpeed') {
term.options.smoothScrollingSpeed = parseFloat(input.value);
} else if(o === 'scrollback') {
term.options.scrollback = parseInt(input.value);
setTimeout(() => updateTerminalSize(), 5);
Expand Down
43 changes: 42 additions & 1 deletion src/browser/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export class Viewport extends Disposable implements IViewport {
private _lastHadScrollBar: boolean = false;
private _activeBuffer: IBuffer;
private _renderDimensions: IRenderDimensions;
private _remainingLinesToScroll: number = 0;
private _lastSmoothScrollStepTime: number = Date.now();
private _smoothScrollTimeout?: NodeJS.Timeout;

// Stores a partial line amount when scrolling, this is used to keep track of how much of a line
// is scrolled so we can "scroll" over partial lines and feel natural on touchpads. This is a
Expand Down Expand Up @@ -186,6 +189,30 @@ export class Viewport extends Disposable implements IViewport {
this._scrollLines(diff);
}

/**
* Performs single step of the smooth scrolling.
*/
private _performSmoothScrollStep(): void {
// Calculate and update time
const now = Date.now();
const deltaSeconds = (now - this._lastSmoothScrollStepTime) / 1000;
this._lastSmoothScrollStepTime = now;

const maxStepAbs = Math.abs(this._remainingLinesToScroll);

if (maxStepAbs > 0) {
const stepAbs = Math.max(1, Math.min(maxStepAbs, Math.floor(maxStepAbs * this._optionsService.rawOptions.smoothScrollingSpeed * deltaSeconds)));
const step = (this._remainingLinesToScroll > 0 ? 1 : -1) * stepAbs;
this._viewportElement.scrollTop += step;
this._remainingLinesToScroll -= step;
}

if (this._remainingLinesToScroll === 0 && this._smoothScrollTimeout) {
clearInterval(this._smoothScrollTimeout);
this._smoothScrollTimeout = undefined;
}
}

/**
* Handles bubbling of scroll event in case the viewport has reached top or bottom
* @param ev The scroll event.
Expand Down Expand Up @@ -214,7 +241,21 @@ export class Viewport extends Disposable implements IViewport {
if (amount === 0) {
return false;
}
this._viewportElement.scrollTop += amount;

if (!this._optionsService.rawOptions.smoothScrolling) {
this._viewportElement.scrollTop += amount;
} else {
// Check whether user wants to scroll the other way (different signs)
if (amount * this._remainingLinesToScroll < 0) {
this._remainingLinesToScroll = amount;
} else {
this._remainingLinesToScroll += amount;
}
if (this._remainingLinesToScroll !== 0 && !this._smoothScrollTimeout) {
this._lastSmoothScrollStepTime = Date.now();
this._smoothScrollTimeout = setInterval(() => this._performSmoothScrollStep(), this._optionsService.rawOptions.smoothScrollingStepInterval);
}
}
return this._bubbleScroll(ev, amount);
}

Expand Down
9 changes: 9 additions & 0 deletions src/common/services/OptionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const DEFAULT_OPTIONS: Readonly<ITerminalOptions> = {
logLevel: 'info',
scrollback: 1000,
scrollSensitivity: 1,
smoothScrolling: false,
smoothScrollingSpeed: 10,
smoothScrollingStepInterval: 20,
screenReaderMode: false,
macOptionIsMeta: false,
macOptionClickForcesSelection: false,
Expand Down Expand Up @@ -156,11 +159,17 @@ export class OptionsService implements IOptionsService {
throw new Error(`${key} cannot be less than 0, value: ${value}`);
}
break;
case 'smoothScrollingSpeed':
case 'fastScrollSensitivity':
case 'scrollSensitivity':
if (value <= 0) {
throw new Error(`${key} cannot be less than or equal to 0, value: ${value}`);
}
case 'smoothScrollingStepInterval':
value = Math.floor(value);
if (value < 1 || value > 250) {
throw new Error(`${key} has to be between 1 and 250, value: ${value}`);
}
case 'rows':
case 'cols':
if (!value && value !== 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/common/services/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ export interface ITerminalOptions {
screenReaderMode: boolean;
scrollback: number;
scrollSensitivity: number;
smoothScrolling: boolean;
smoothScrollingSpeed: number;
smoothScrollingStepInterval: number;
tabStopWidth: number;
theme: ITheme;
windowsMode: boolean;
Expand Down
20 changes: 20 additions & 0 deletions typings/xterm-headless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,26 @@ declare module 'xterm-headless' {
*/
scrollSensitivity?: number;

/**
* Whether to use smooth scrolling.
*/
smoothScrolling?: boolean;

/**
* The speed of the in-terminal position adjustment.
* Speed is then multiplied by the amount of lines user
* wants to scroll. Fast scrolling speeds up the position adjustment.
* Takes no effect when `smoothScrolling` is set to `false`.
*/
smoothScrollingSpeed?: number;

/**
* The interval in milliseconds that will determine how often
* the smooth scrolling should update the in-terminal position.
* Takes no effect when `smoothScrolling` is set `false`.
*/
smoothScrollingStepInterval?: number;

/**
* The size of tab stops in the terminal.
*/
Expand Down
20 changes: 20 additions & 0 deletions typings/xterm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,26 @@ declare module 'xterm' {
*/
scrollSensitivity?: number;

/**
* Whether to use smooth scrolling.
*/
smoothScrolling?: boolean;

/**
* The speed of the in-terminal position adjustment.
* Speed is then multiplied by the amount of lines user
* wants to scroll. Fast scrolling speeds up the position adjustment.
* Takes no effect when `smoothScrolling` is set to `false`.
*/
smoothScrollingSpeed?: number;

/**
* The interval in milliseconds that will determine how often
* the smooth scrolling should update the in-terminal position.
* Takes no effect when `smoothScrolling` is set `false`.
*/
smoothScrollingStepInterval?: number;

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