| 1 |
/** |
| 2 |
* Copyright (c) 2024 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { ICoreBrowserService, IRenderService, IThemeService } from 'browser/services/Services'; |
| 7 |
import { ViewportConstants } from 'browser/shared/Constants'; |
| 8 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 9 |
import { IBufferService, ICoreMouseService, IOptionsService } from 'common/services/Services'; |
| 10 |
import { CoreMouseEventType } from 'common/Types'; |
| 11 |
import { scheduleAtNextAnimationFrame } from 'vs/base/browser/dom'; |
| 12 |
import { SmoothScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement'; |
| 13 |
import type { ScrollableElementChangeOptions } from 'vs/base/browser/ui/scrollbar/scrollableElementOptions'; |
| 14 |
import { Emitter, Event } from 'vs/base/common/event'; |
| 15 |
import { Scrollable, ScrollbarVisibility, type ScrollEvent } from 'vs/base/common/scrollable'; |
| 16 |
|
| 17 |
export class Viewport extends Disposable { |
| 18 |
|
| 19 |
protected _onRequestScrollLines = this._register(new Emitter<number>()); |
| 20 |
public readonly onRequestScrollLines = this._onRequestScrollLines.event; |
| 21 |
|
| 22 |
private _scrollableElement: SmoothScrollableElement; |
| 23 |
private _styleElement: HTMLStyleElement; |
| 24 |
|
| 25 |
private _queuedAnimationFrame?: number; |
| 26 |
private _latestYDisp?: number; |
| 27 |
private _isSyncing: boolean = false; |
| 28 |
private _isHandlingScroll: boolean = false; |
| 29 |
private _suppressOnScrollHandler: boolean = false; |
| 30 |
|
| 31 |
constructor( |
| 32 |
element: HTMLElement, |
| 33 |
screenElement: HTMLElement, |
| 34 |
@IBufferService private readonly _bufferService: IBufferService, |
| 35 |
@ICoreBrowserService coreBrowserService: ICoreBrowserService, |
| 36 |
@ICoreMouseService coreMouseService: ICoreMouseService, |
| 37 |
@IThemeService themeService: IThemeService, |
| 38 |
@IOptionsService private readonly _optionsService: IOptionsService, |
| 39 |
@IRenderService private readonly _renderService: IRenderService |
| 40 |
) { |
| 41 |
super(); |
| 42 |
|
| 43 |
const scrollable = this._register(new Scrollable({ |
| 44 |
forceIntegerValues: false, |
| 45 |
smoothScrollDuration: this._optionsService.rawOptions.smoothScrollDuration, |
| 46 |
// This is used over `IRenderService.addRefreshCallback` since it can be canceled |
| 47 |
scheduleAtNextAnimationFrame: cb => scheduleAtNextAnimationFrame(coreBrowserService.window, cb) |
| 48 |
})); |
| 49 |
this._register(this._optionsService.onSpecificOptionChange('smoothScrollDuration', () => { |
| 50 |
scrollable.setSmoothScrollDuration(this._optionsService.rawOptions.smoothScrollDuration); |
| 51 |
})); |
| 52 |
|
| 53 |
this._scrollableElement = this._register(new SmoothScrollableElement(screenElement, { |
| 54 |
vertical: ScrollbarVisibility.Auto, |
| 55 |
horizontal: ScrollbarVisibility.Hidden, |
| 56 |
useShadows: false, |
| 57 |
mouseWheelSmoothScroll: true, |
| 58 |
...this._getChangeOptions() |
| 59 |
}, scrollable)); |
| 60 |
this._register(this._optionsService.onMultipleOptionChange([ |
| 61 |
'scrollSensitivity', |
| 62 |
'fastScrollSensitivity', |
| 63 |
'overviewRuler' |
| 64 |
], () => this._scrollableElement.updateOptions(this._getChangeOptions()))); |
| 65 |
// Don't handle mouse wheel if wheel events are supported by the current mouse prototcol |
| 66 |
this._register(coreMouseService.onProtocolChange(type => { |
| 67 |
this._scrollableElement.updateOptions({ |
| 68 |
handleMouseWheel: !(type & CoreMouseEventType.WHEEL) |
| 69 |
}); |
| 70 |
})); |
| 71 |
|
| 72 |
this._scrollableElement.setScrollDimensions({ height: 0, scrollHeight: 0 }); |
| 73 |
this._register(Event.runAndSubscribe(themeService.onChangeColors, () => { |
| 74 |
this._scrollableElement.getDomNode().style.backgroundColor = themeService.colors.background.css; |
| 75 |
})); |
| 76 |
element.appendChild(this._scrollableElement.getDomNode()); |
| 77 |
this._register(toDisposable(() => this._scrollableElement.getDomNode().remove())); |
| 78 |
|
| 79 |
this._styleElement = coreBrowserService.mainDocument.createElement('style'); |
| 80 |
screenElement.appendChild(this._styleElement); |
| 81 |
this._register(toDisposable(() => this._styleElement.remove())); |
| 82 |
this._register(Event.runAndSubscribe(themeService.onChangeColors, () => { |
| 83 |
this._styleElement.textContent = [ |
| 84 |
`.xterm .xterm-scrollable-element > .scrollbar > .slider {`, |
| 85 |
` background: ${themeService.colors.scrollbarSliderBackground.css};`, |
| 86 |
`}`, |
| 87 |
`.xterm .xterm-scrollable-element > .scrollbar > .slider:hover {`, |
| 88 |
` background: ${themeService.colors.scrollbarSliderHoverBackground.css};`, |
| 89 |
`}`, |
| 90 |
`.xterm .xterm-scrollable-element > .scrollbar > .slider.active {`, |
| 91 |
` background: ${themeService.colors.scrollbarSliderActiveBackground.css};`, |
| 92 |
`}` |
| 93 |
].join('\n'); |
| 94 |
})); |
| 95 |
|
| 96 |
this._register(this._bufferService.onResize(() => this.queueSync())); |
| 97 |
this._register(this._bufferService.buffers.onBufferActivate(() => { |
| 98 |
// Reset _latestYDisp when switching buffers to prevent stale scroll position |
| 99 |
// from alt buffer contaminating normal buffer scroll position |
| 100 |
this._latestYDisp = undefined; |
| 101 |
this.queueSync(); |
| 102 |
})); |
| 103 |
this._register(this._bufferService.onScroll(() => this._sync())); |
| 104 |
|
| 105 |
this._register(this._scrollableElement.onScroll(e => this._handleScroll(e))); |
| 106 |
} |
| 107 |
|
| 108 |
public scrollLines(disp: number): void { |
| 109 |
const pos = this._scrollableElement.getScrollPosition(); |
| 110 |
this._scrollableElement.setScrollPosition({ |
| 111 |
reuseAnimation: true, |
| 112 |
scrollTop: pos.scrollTop + disp * this._renderService.dimensions.css.cell.height |
| 113 |
}); |
| 114 |
} |
| 115 |
|
| 116 |
public scrollToLine(line: number, disableSmoothScroll?: boolean): void { |
| 117 |
if (disableSmoothScroll) { |
| 118 |
this._latestYDisp = line; |
| 119 |
} |
| 120 |
this._scrollableElement.setScrollPosition({ |
| 121 |
reuseAnimation: !disableSmoothScroll, |
| 122 |
scrollTop: line * this._renderService.dimensions.css.cell.height |
| 123 |
}); |
| 124 |
} |
| 125 |
|
| 126 |
private _getChangeOptions(): ScrollableElementChangeOptions { |
| 127 |
return { |
| 128 |
mouseWheelScrollSensitivity: this._optionsService.rawOptions.scrollSensitivity, |
| 129 |
fastScrollSensitivity: this._optionsService.rawOptions.fastScrollSensitivity, |
| 130 |
verticalScrollbarSize: this._optionsService.rawOptions.overviewRuler?.width || ViewportConstants.DEFAULT_SCROLL_BAR_WIDTH |
| 131 |
}; |
| 132 |
} |
| 133 |
|
| 134 |
public queueSync(ydisp?: number): void { |
| 135 |
// Update state |
| 136 |
if (ydisp !== undefined) { |
| 137 |
this._latestYDisp = ydisp; |
| 138 |
} |
| 139 |
|
| 140 |
// Don't queue more than one callback |
| 141 |
if (this._queuedAnimationFrame !== undefined) { |
| 142 |
return; |
| 143 |
} |
| 144 |
this._queuedAnimationFrame = this._renderService.addRefreshCallback(() => { |
| 145 |
this._queuedAnimationFrame = undefined; |
| 146 |
this._sync(this._latestYDisp); |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
private _sync(ydisp: number = this._bufferService.buffer.ydisp): void { |
| 151 |
if (!this._renderService || this._isSyncing) { |
| 152 |
return; |
| 153 |
} |
| 154 |
this._isSyncing = true; |
| 155 |
|
| 156 |
// Ignore any onScroll event that happens as a result of dimensions changing as this should |
| 157 |
// never cause a scrollLines call, only setScrollPosition can do that. |
| 158 |
this._suppressOnScrollHandler = true; |
| 159 |
this._scrollableElement.setScrollDimensions({ |
| 160 |
height: this._renderService.dimensions.css.canvas.height, |
| 161 |
scrollHeight: this._renderService.dimensions.css.cell.height * this._bufferService.buffer.lines.length |
| 162 |
}); |
| 163 |
this._suppressOnScrollHandler = false; |
| 164 |
|
| 165 |
// If ydisp has been changed by some other component (input/buffer), then stop animating smooth |
| 166 |
// scroll and scroll there immediately. |
| 167 |
if (ydisp !== this._latestYDisp) { |
| 168 |
this._scrollableElement.setScrollPosition({ |
| 169 |
scrollTop: ydisp * this._renderService.dimensions.css.cell.height |
| 170 |
}); |
| 171 |
} |
| 172 |
|
| 173 |
this._isSyncing = false; |
| 174 |
} |
| 175 |
|
| 176 |
private _handleScroll(e: ScrollEvent): void { |
| 177 |
if (!this._renderService) { |
| 178 |
return; |
| 179 |
} |
| 180 |
if (this._isHandlingScroll || this._suppressOnScrollHandler) { |
| 181 |
return; |
| 182 |
} |
| 183 |
this._isHandlingScroll = true; |
| 184 |
const newRow = Math.round(e.scrollTop / this._renderService.dimensions.css.cell.height); |
| 185 |
const diff = newRow - this._bufferService.buffer.ydisp; |
| 186 |
if (diff !== 0) { |
| 187 |
this._latestYDisp = newRow; |
| 188 |
this._onRequestScrollLines.fire(diff); |
| 189 |
} |
| 190 |
this._isHandlingScroll = false; |
| 191 |
} |
| 192 |
} |