Merge pull request #3940 from Tyriar/1140
Implement duration-based smooth scrolling
This commit is contained in:
+59
-1
@@ -13,6 +13,12 @@ import { IRenderDimensions } from 'browser/renderer/Types';
|
|||||||
|
|
||||||
const FALLBACK_SCROLL_BAR_WIDTH = 15;
|
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.
|
* 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.
|
* Logic for the virtual scroll bar is included in this object.
|
||||||
@@ -36,6 +42,11 @@ 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 _smoothScrollState: ISmoothScrollState = {
|
||||||
|
startTime: 0,
|
||||||
|
origin: -1,
|
||||||
|
target: -1
|
||||||
|
};
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly _scrollLines: (amount: number) => void,
|
private readonly _scrollLines: (amount: number) => void,
|
||||||
@@ -168,6 +179,37 @@ export class Viewport extends Disposable implements IViewport {
|
|||||||
this._scrollLines(diff);
|
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
|
* 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 +238,23 @@ 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.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);
|
return this._bubbleScroll(ev, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ export const DEFAULT_OPTIONS: Readonly<ITerminalOptions> = {
|
|||||||
scrollback: 1000,
|
scrollback: 1000,
|
||||||
scrollSensitivity: 1,
|
scrollSensitivity: 1,
|
||||||
screenReaderMode: false,
|
screenReaderMode: false,
|
||||||
|
smoothScrollDuration: 0,
|
||||||
macOptionIsMeta: false,
|
macOptionIsMeta: false,
|
||||||
macOptionClickForcesSelection: false,
|
macOptionClickForcesSelection: false,
|
||||||
minimumContrastRatio: 1,
|
minimumContrastRatio: 1,
|
||||||
|
|||||||
@@ -232,6 +232,7 @@ export interface ITerminalOptions {
|
|||||||
screenReaderMode: boolean;
|
screenReaderMode: boolean;
|
||||||
scrollback: number;
|
scrollback: number;
|
||||||
scrollSensitivity: number;
|
scrollSensitivity: number;
|
||||||
|
smoothScrollDuration: number;
|
||||||
tabStopWidth: number;
|
tabStopWidth: number;
|
||||||
theme: ITheme;
|
theme: ITheme;
|
||||||
windowsMode: boolean;
|
windowsMode: boolean;
|
||||||
|
|||||||
Vendored
+6
@@ -169,6 +169,12 @@ declare module 'xterm-headless' {
|
|||||||
*/
|
*/
|
||||||
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.
|
||||||
|
*/
|
||||||
|
smoothScrollDuration?: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size of tab stops in the terminal.
|
* The size of tab stops in the terminal.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Vendored
+6
@@ -201,6 +201,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.
|
||||||
|
*/
|
||||||
|
smoothScrollDuration?: number;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size of tab stops in the terminal.
|
* The size of tab stops in the terminal.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user