| 1 |
/** |
| 2 |
* Copyright (c) 2018 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { DomRendererRowFactory, RowCss } from 'browser/renderer/dom/DomRendererRowFactory'; |
| 7 |
import { WidthCache } from 'browser/renderer/dom/WidthCache'; |
| 8 |
import { INVERTED_DEFAULT_COLOR } from 'browser/renderer/shared/Constants'; |
| 9 |
import { createRenderDimensions } from 'browser/renderer/shared/RendererUtils'; |
| 10 |
import { createSelectionRenderModel } from 'browser/renderer/shared/SelectionRenderModel'; |
| 11 |
import { IRenderDimensions, IRenderer, IRequestRedrawEvent, ISelectionRenderModel } from 'browser/renderer/shared/Types'; |
| 12 |
import { ICharSizeService, ICoreBrowserService, IThemeService } from 'browser/services/Services'; |
| 13 |
import { ILinkifier2, ILinkifierEvent, ITerminal, ReadonlyColorSet } from 'browser/Types'; |
| 14 |
import { color } from 'common/Color'; |
| 15 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 16 |
import { IBufferService, ICoreService, IInstantiationService, IOptionsService } from 'common/services/Services'; |
| 17 |
import { Emitter } from 'vs/base/common/event'; |
| 18 |
|
| 19 |
|
| 20 |
const TERMINAL_CLASS_PREFIX = 'xterm-dom-renderer-owner-'; |
| 21 |
const ROW_CONTAINER_CLASS = 'xterm-rows'; |
| 22 |
const FG_CLASS_PREFIX = 'xterm-fg-'; |
| 23 |
const BG_CLASS_PREFIX = 'xterm-bg-'; |
| 24 |
const FOCUS_CLASS = 'xterm-focus'; |
| 25 |
const SELECTION_CLASS = 'xterm-selection'; |
| 26 |
|
| 27 |
let nextTerminalId = 1; |
| 28 |
|
| 29 |
/** |
| 30 |
* The standard renderer and fallback for when the webgl addon is slow. This is not meant to be |
| 31 |
* particularly fast and will even lack some features such as custom glyphs, hoever this is more |
| 32 |
* reliable as webgl may not work on some machines. |
| 33 |
*/ |
| 34 |
export class DomRenderer extends Disposable implements IRenderer { |
| 35 |
private _rowFactory: DomRendererRowFactory; |
| 36 |
private _terminalClass: number = nextTerminalId++; |
| 37 |
|
| 38 |
private _themeStyleElement!: HTMLStyleElement; |
| 39 |
private _dimensionsStyleElement!: HTMLStyleElement; |
| 40 |
private _rowContainer: HTMLElement; |
| 41 |
private _rowElements: HTMLElement[] = []; |
| 42 |
private _selectionContainer: HTMLElement; |
| 43 |
private _widthCache: WidthCache; |
| 44 |
private _selectionRenderModel: ISelectionRenderModel = createSelectionRenderModel(); |
| 45 |
|
| 46 |
public dimensions: IRenderDimensions; |
| 47 |
|
| 48 |
public readonly onRequestRedraw = this._register(new Emitter<IRequestRedrawEvent>()).event; |
| 49 |
|
| 50 |
constructor( |
| 51 |
private readonly _terminal: ITerminal, |
| 52 |
private readonly _document: Document, |
| 53 |
private readonly _element: HTMLElement, |
| 54 |
private readonly _screenElement: HTMLElement, |
| 55 |
private readonly _viewportElement: HTMLElement, |
| 56 |
private readonly _helperContainer: HTMLElement, |
| 57 |
private readonly _linkifier2: ILinkifier2, |
| 58 |
@IInstantiationService instantiationService: IInstantiationService, |
| 59 |
@ICharSizeService private readonly _charSizeService: ICharSizeService, |
| 60 |
@IOptionsService private readonly _optionsService: IOptionsService, |
| 61 |
@IBufferService private readonly _bufferService: IBufferService, |
| 62 |
@ICoreService private readonly _coreService: ICoreService, |
| 63 |
@ICoreBrowserService private readonly _coreBrowserService: ICoreBrowserService, |
| 64 |
@IThemeService private readonly _themeService: IThemeService |
| 65 |
) { |
| 66 |
super(); |
| 67 |
this._rowContainer = this._document.createElement('div'); |
| 68 |
this._rowContainer.classList.add(ROW_CONTAINER_CLASS); |
| 69 |
this._rowContainer.style.lineHeight = 'normal'; |
| 70 |
this._rowContainer.setAttribute('aria-hidden', 'true'); |
| 71 |
this._refreshRowElements(this._bufferService.cols, this._bufferService.rows); |
| 72 |
this._selectionContainer = this._document.createElement('div'); |
| 73 |
this._selectionContainer.classList.add(SELECTION_CLASS); |
| 74 |
this._selectionContainer.setAttribute('aria-hidden', 'true'); |
| 75 |
|
| 76 |
this.dimensions = createRenderDimensions(); |
| 77 |
this._updateDimensions(); |
| 78 |
this._register(this._optionsService.onOptionChange(() => this._handleOptionsChanged())); |
| 79 |
|
| 80 |
this._register(this._themeService.onChangeColors(e => this._injectCss(e))); |
| 81 |
this._injectCss(this._themeService.colors); |
| 82 |
|
| 83 |
this._rowFactory = instantiationService.createInstance(DomRendererRowFactory, document); |
| 84 |
|
| 85 |
this._element.classList.add(TERMINAL_CLASS_PREFIX + this._terminalClass); |
| 86 |
this._screenElement.appendChild(this._rowContainer); |
| 87 |
this._screenElement.appendChild(this._selectionContainer); |
| 88 |
|
| 89 |
this._register(this._linkifier2.onShowLinkUnderline(e => this._handleLinkHover(e))); |
| 90 |
this._register(this._linkifier2.onHideLinkUnderline(e => this._handleLinkLeave(e))); |
| 91 |
|
| 92 |
this._register(toDisposable(() => { |
| 93 |
this._element.classList.remove(TERMINAL_CLASS_PREFIX + this._terminalClass); |
| 94 |
|
| 95 |
// Outside influences such as React unmounts may manipulate the DOM before our disposal. |
| 96 |
// https://github.com/xtermjs/xterm.js/issues/2960 |
| 97 |
this._rowContainer.remove(); |
| 98 |
this._selectionContainer.remove(); |
| 99 |
this._widthCache.dispose(); |
| 100 |
this._themeStyleElement.remove(); |
| 101 |
this._dimensionsStyleElement.remove(); |
| 102 |
})); |
| 103 |
|
| 104 |
this._widthCache = new WidthCache(this._document, this._helperContainer); |
| 105 |
this._widthCache.setFont( |
| 106 |
this._optionsService.rawOptions.fontFamily, |
| 107 |
this._optionsService.rawOptions.fontSize, |
| 108 |
this._optionsService.rawOptions.fontWeight, |
| 109 |
this._optionsService.rawOptions.fontWeightBold |
| 110 |
); |
| 111 |
this._setDefaultSpacing(); |
| 112 |
} |
| 113 |
|
| 114 |
private _updateDimensions(): void { |
| 115 |
const dpr = this._coreBrowserService.dpr; |
| 116 |
this.dimensions.device.char.width = this._charSizeService.width * dpr; |
| 117 |
this.dimensions.device.char.height = Math.ceil(this._charSizeService.height * dpr); |
| 118 |
this.dimensions.device.cell.width = this.dimensions.device.char.width + Math.round(this._optionsService.rawOptions.letterSpacing); |
| 119 |
this.dimensions.device.cell.height = Math.floor(this.dimensions.device.char.height * this._optionsService.rawOptions.lineHeight); |
| 120 |
this.dimensions.device.char.left = 0; |
| 121 |
this.dimensions.device.char.top = 0; |
| 122 |
this.dimensions.device.canvas.width = this.dimensions.device.cell.width * this._bufferService.cols; |
| 123 |
this.dimensions.device.canvas.height = this.dimensions.device.cell.height * this._bufferService.rows; |
| 124 |
this.dimensions.css.canvas.width = Math.round(this.dimensions.device.canvas.width / dpr); |
| 125 |
this.dimensions.css.canvas.height = Math.round(this.dimensions.device.canvas.height / dpr); |
| 126 |
this.dimensions.css.cell.width = this.dimensions.css.canvas.width / this._bufferService.cols; |
| 127 |
this.dimensions.css.cell.height = this.dimensions.css.canvas.height / this._bufferService.rows; |
| 128 |
|
| 129 |
for (const element of this._rowElements) { |
| 130 |
element.style.width = `${this.dimensions.css.canvas.width}px`; |
| 131 |
element.style.height = `${this.dimensions.css.cell.height}px`; |
| 132 |
element.style.lineHeight = `${this.dimensions.css.cell.height}px`; |
| 133 |
// Make sure rows don't overflow onto following row |
| 134 |
element.style.overflow = 'hidden'; |
| 135 |
} |
| 136 |
|
| 137 |
if (!this._dimensionsStyleElement) { |
| 138 |
this._dimensionsStyleElement = this._document.createElement('style'); |
| 139 |
this._screenElement.appendChild(this._dimensionsStyleElement); |
| 140 |
} |
| 141 |
|
| 142 |
const styles = |
| 143 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} span {` + |
| 144 |
` display: inline-block;` + // TODO: find workaround for inline-block (creates ~20% render penalty) |
| 145 |
` height: 100%;` + |
| 146 |
` vertical-align: top;` + |
| 147 |
`}`; |
| 148 |
|
| 149 |
this._dimensionsStyleElement.textContent = styles; |
| 150 |
|
| 151 |
this._selectionContainer.style.height = this._viewportElement.style.height; |
| 152 |
this._screenElement.style.width = `${this.dimensions.css.canvas.width}px`; |
| 153 |
this._screenElement.style.height = `${this.dimensions.css.canvas.height}px`; |
| 154 |
} |
| 155 |
|
| 156 |
private _injectCss(colors: ReadonlyColorSet): void { |
| 157 |
if (!this._themeStyleElement) { |
| 158 |
this._themeStyleElement = this._document.createElement('style'); |
| 159 |
this._screenElement.appendChild(this._themeStyleElement); |
| 160 |
} |
| 161 |
|
| 162 |
// Base CSS |
| 163 |
let styles = |
| 164 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} {` + |
| 165 |
// Disabling pointer events circumvents a browser behavior that prevents `click` events from |
| 166 |
// being delivered if the target element is replaced during the click. This happened due to |
| 167 |
// refresh() being called during the mousedown handler to start a selection. |
| 168 |
` pointer-events: none;` + |
| 169 |
` color: ${colors.foreground.css};` + |
| 170 |
` font-family: ${this._optionsService.rawOptions.fontFamily};` + |
| 171 |
` font-size: ${this._optionsService.rawOptions.fontSize}px;` + |
| 172 |
` font-kerning: none;` + |
| 173 |
` white-space: pre` + |
| 174 |
`}`; |
| 175 |
styles += |
| 176 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .xterm-dim {` + |
| 177 |
` color: ${color.multiplyOpacity(colors.foreground, 0.5).css};` + |
| 178 |
`}`; |
| 179 |
// Text styles |
| 180 |
styles += |
| 181 |
`${this._terminalSelector} span:not(.${RowCss.BOLD_CLASS}) {` + |
| 182 |
` font-weight: ${this._optionsService.rawOptions.fontWeight};` + |
| 183 |
`}` + |
| 184 |
`${this._terminalSelector} span.${RowCss.BOLD_CLASS} {` + |
| 185 |
` font-weight: ${this._optionsService.rawOptions.fontWeightBold};` + |
| 186 |
`}` + |
| 187 |
`${this._terminalSelector} span.${RowCss.ITALIC_CLASS} {` + |
| 188 |
` font-style: italic;` + |
| 189 |
`}`; |
| 190 |
// Blink animation |
| 191 |
const blinkAnimationUnderlineId = `blink_underline_${this._terminalClass}`; |
| 192 |
const blinkAnimationBarId = `blink_bar_${this._terminalClass}`; |
| 193 |
const blinkAnimationBlockId = `blink_block_${this._terminalClass}`; |
| 194 |
styles += |
| 195 |
`@keyframes ${blinkAnimationUnderlineId} {` + |
| 196 |
` 50% {` + |
| 197 |
` border-bottom-style: hidden;` + |
| 198 |
` }` + |
| 199 |
`}`; |
| 200 |
styles += |
| 201 |
`@keyframes ${blinkAnimationBarId} {` + |
| 202 |
` 50% {` + |
| 203 |
` box-shadow: none;` + |
| 204 |
` }` + |
| 205 |
`}`; |
| 206 |
styles += |
| 207 |
`@keyframes ${blinkAnimationBlockId} {` + |
| 208 |
` 0% {` + |
| 209 |
` background-color: ${colors.cursor.css};` + |
| 210 |
` color: ${colors.cursorAccent.css};` + |
| 211 |
` }` + |
| 212 |
` 50% {` + |
| 213 |
` background-color: inherit;` + |
| 214 |
` color: ${colors.cursor.css};` + |
| 215 |
` }` + |
| 216 |
`}`; |
| 217 |
// Cursor |
| 218 |
styles += |
| 219 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS}.${FOCUS_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_BLINK_CLASS}.${RowCss.CURSOR_STYLE_UNDERLINE_CLASS} {` + |
| 220 |
` animation: ${blinkAnimationUnderlineId} 1s step-end infinite;` + |
| 221 |
`}` + |
| 222 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS}.${FOCUS_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_BLINK_CLASS}.${RowCss.CURSOR_STYLE_BAR_CLASS} {` + |
| 223 |
` animation: ${blinkAnimationBarId} 1s step-end infinite;` + |
| 224 |
`}` + |
| 225 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS}.${FOCUS_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_BLINK_CLASS}.${RowCss.CURSOR_STYLE_BLOCK_CLASS} {` + |
| 226 |
` animation: ${blinkAnimationBlockId} 1s step-end infinite;` + |
| 227 |
`}` + |
| 228 |
// !important helps fix an issue where the cursor will not render on top of the selection, |
| 229 |
// however it's very hard to fix this issue and retain the blink animation without the use of |
| 230 |
// !important. So this edge case fails when cursor blink is on. |
| 231 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_STYLE_BLOCK_CLASS} {` + |
| 232 |
` background-color: ${colors.cursor.css};` + |
| 233 |
` color: ${colors.cursorAccent.css};` + |
| 234 |
`}` + |
| 235 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_STYLE_BLOCK_CLASS}:not(.${RowCss.CURSOR_BLINK_CLASS}) {` + |
| 236 |
` background-color: ${colors.cursor.css} !important;` + |
| 237 |
` color: ${colors.cursorAccent.css} !important;` + |
| 238 |
`}` + |
| 239 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_STYLE_OUTLINE_CLASS} {` + |
| 240 |
` outline: 1px solid ${colors.cursor.css};` + |
| 241 |
` outline-offset: -1px;` + |
| 242 |
`}` + |
| 243 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_STYLE_BAR_CLASS} {` + |
| 244 |
` box-shadow: ${this._optionsService.rawOptions.cursorWidth}px 0 0 ${colors.cursor.css} inset;` + |
| 245 |
`}` + |
| 246 |
`${this._terminalSelector} .${ROW_CONTAINER_CLASS} .${RowCss.CURSOR_CLASS}.${RowCss.CURSOR_STYLE_UNDERLINE_CLASS} {` + |
| 247 |
` border-bottom: 1px ${colors.cursor.css};` + |
| 248 |
` border-bottom-style: solid;` + |
| 249 |
` height: calc(100% - 1px);` + |
| 250 |
`}`; |
| 251 |
// Selection |
| 252 |
styles += |
| 253 |
`${this._terminalSelector} .${SELECTION_CLASS} {` + |
| 254 |
` position: absolute;` + |
| 255 |
` top: 0;` + |
| 256 |
` left: 0;` + |
| 257 |
` z-index: 1;` + |
| 258 |
` pointer-events: none;` + |
| 259 |
`}` + |
| 260 |
`${this._terminalSelector}.focus .${SELECTION_CLASS} div {` + |
| 261 |
` position: absolute;` + |
| 262 |
` background-color: ${colors.selectionBackgroundOpaque.css};` + |
| 263 |
`}` + |
| 264 |
`${this._terminalSelector} .${SELECTION_CLASS} div {` + |
| 265 |
` position: absolute;` + |
| 266 |
` background-color: ${colors.selectionInactiveBackgroundOpaque.css};` + |
| 267 |
`}`; |
| 268 |
// Colors |
| 269 |
for (const [i, c] of colors.ansi.entries()) { |
| 270 |
styles += |
| 271 |
`${this._terminalSelector} .${FG_CLASS_PREFIX}${i} { color: ${c.css}; }` + |
| 272 |
`${this._terminalSelector} .${FG_CLASS_PREFIX}${i}.${RowCss.DIM_CLASS} { color: ${color.multiplyOpacity(c, 0.5).css}; }` + |
| 273 |
`${this._terminalSelector} .${BG_CLASS_PREFIX}${i} { background-color: ${c.css}; }`; |
| 274 |
} |
| 275 |
styles += |
| 276 |
`${this._terminalSelector} .${FG_CLASS_PREFIX}${INVERTED_DEFAULT_COLOR} { color: ${color.opaque(colors.background).css}; }` + |
| 277 |
`${this._terminalSelector} .${FG_CLASS_PREFIX}${INVERTED_DEFAULT_COLOR}.${RowCss.DIM_CLASS} { color: ${color.multiplyOpacity(color.opaque(colors.background), 0.5).css}; }` + |
| 278 |
`${this._terminalSelector} .${BG_CLASS_PREFIX}${INVERTED_DEFAULT_COLOR} { background-color: ${colors.foreground.css}; }`; |
| 279 |
|
| 280 |
this._themeStyleElement.textContent = styles; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* default letter spacing |
| 285 |
* Due to rounding issues in dimensions dpr calc glyph might render |
| 286 |
* slightly too wide or too narrow. The method corrects the stacking offsets |
| 287 |
* by applying a default letter-spacing for all chars. |
| 288 |
* The value gets passed to the row factory to avoid setting this value again |
| 289 |
* (render speedup is roughly 10%). |
| 290 |
*/ |
| 291 |
private _setDefaultSpacing(): void { |
| 292 |
// measure same char as in CharSizeService to get the base deviation |
| 293 |
const spacing = this.dimensions.css.cell.width - this._widthCache.get('W', false, false); |
| 294 |
this._rowContainer.style.letterSpacing = `${spacing}px`; |
| 295 |
this._rowFactory.defaultSpacing = spacing; |
| 296 |
} |
| 297 |
|
| 298 |
public handleDevicePixelRatioChange(): void { |
| 299 |
this._updateDimensions(); |
| 300 |
this._widthCache.clear(); |
| 301 |
this._setDefaultSpacing(); |
| 302 |
} |
| 303 |
|
| 304 |
private _refreshRowElements(cols: number, rows: number): void { |
| 305 |
// Add missing elements |
| 306 |
for (let i = this._rowElements.length; i <= rows; i++) { |
| 307 |
const row = this._document.createElement('div'); |
| 308 |
this._rowContainer.appendChild(row); |
| 309 |
this._rowElements.push(row); |
| 310 |
} |
| 311 |
// Remove excess elements |
| 312 |
while (this._rowElements.length > rows) { |
| 313 |
this._rowContainer.removeChild(this._rowElements.pop()!); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
public handleResize(cols: number, rows: number): void { |
| 318 |
this._refreshRowElements(cols, rows); |
| 319 |
this._updateDimensions(); |
| 320 |
this.handleSelectionChanged(this._selectionRenderModel.selectionStart, this._selectionRenderModel.selectionEnd, this._selectionRenderModel.columnSelectMode); |
| 321 |
} |
| 322 |
|
| 323 |
public handleCharSizeChanged(): void { |
| 324 |
this._updateDimensions(); |
| 325 |
this._widthCache.clear(); |
| 326 |
this._setDefaultSpacing(); |
| 327 |
} |
| 328 |
|
| 329 |
public handleBlur(): void { |
| 330 |
this._rowContainer.classList.remove(FOCUS_CLASS); |
| 331 |
this.renderRows(0, this._bufferService.rows - 1); |
| 332 |
} |
| 333 |
|
| 334 |
public handleFocus(): void { |
| 335 |
this._rowContainer.classList.add(FOCUS_CLASS); |
| 336 |
this.renderRows(this._bufferService.buffer.y, this._bufferService.buffer.y); |
| 337 |
} |
| 338 |
|
| 339 |
public handleSelectionChanged(start: [number, number] | undefined, end: [number, number] | undefined, columnSelectMode: boolean): void { |
| 340 |
// Remove all selections |
| 341 |
this._selectionContainer.replaceChildren(); |
| 342 |
this._rowFactory.handleSelectionChanged(start, end, columnSelectMode); |
| 343 |
this.renderRows(0, this._bufferService.rows - 1); |
| 344 |
|
| 345 |
// Selection does not exist |
| 346 |
if (!start || !end) { |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
this._selectionRenderModel.update(this._terminal, start, end, columnSelectMode); |
| 351 |
if (!this._selectionRenderModel.hasSelection) { |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
// Translate from buffer position to viewport position |
| 356 |
const viewportStartRow = this._selectionRenderModel.viewportStartRow; |
| 357 |
const viewportEndRow = this._selectionRenderModel.viewportEndRow; |
| 358 |
const viewportCappedStartRow = this._selectionRenderModel.viewportCappedStartRow; |
| 359 |
const viewportCappedEndRow = this._selectionRenderModel.viewportCappedEndRow; |
| 360 |
|
| 361 |
// Create the selections |
| 362 |
const documentFragment = this._document.createDocumentFragment(); |
| 363 |
|
| 364 |
if (columnSelectMode) { |
| 365 |
const isXFlipped = start[0] > end[0]; |
| 366 |
documentFragment.appendChild( |
| 367 |
this._createSelectionElement(viewportCappedStartRow, isXFlipped ? end[0] : start[0], isXFlipped ? start[0] : end[0], viewportCappedEndRow - viewportCappedStartRow + 1) |
| 368 |
); |
| 369 |
} else { |
| 370 |
// Draw first row |
| 371 |
const startCol = viewportStartRow === viewportCappedStartRow ? start[0] : 0; |
| 372 |
const endCol = viewportCappedStartRow === viewportEndRow ? end[0] : this._bufferService.cols; |
| 373 |
documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow, startCol, endCol)); |
| 374 |
// Draw middle rows |
| 375 |
const middleRowsCount = viewportCappedEndRow - viewportCappedStartRow - 1; |
| 376 |
documentFragment.appendChild(this._createSelectionElement(viewportCappedStartRow + 1, 0, this._bufferService.cols, middleRowsCount)); |
| 377 |
// Draw final row |
| 378 |
if (viewportCappedStartRow !== viewportCappedEndRow) { |
| 379 |
// Only draw viewportEndRow if it's not the same as viewporttartRow |
| 380 |
const endCol = viewportEndRow === viewportCappedEndRow ? end[0] : this._bufferService.cols; |
| 381 |
documentFragment.appendChild(this._createSelectionElement(viewportCappedEndRow, 0, endCol)); |
| 382 |
} |
| 383 |
} |
| 384 |
this._selectionContainer.appendChild(documentFragment); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Creates a selection element at the specified position. |
| 389 |
* @param row The row of the selection. |
| 390 |
* @param colStart The start column. |
| 391 |
* @param colEnd The end columns. |
| 392 |
*/ |
| 393 |
private _createSelectionElement(row: number, colStart: number, colEnd: number, rowCount: number = 1): HTMLElement { |
| 394 |
const element = this._document.createElement('div'); |
| 395 |
const left = colStart * this.dimensions.css.cell.width; |
| 396 |
let width = this.dimensions.css.cell.width * (colEnd - colStart); |
| 397 |
if (left + width > this.dimensions.css.canvas.width) { |
| 398 |
width = this.dimensions.css.canvas.width - left; |
| 399 |
} |
| 400 |
|
| 401 |
element.style.height = `${rowCount * this.dimensions.css.cell.height}px`; |
| 402 |
element.style.top = `${row * this.dimensions.css.cell.height}px`; |
| 403 |
element.style.left = `${left}px`; |
| 404 |
element.style.width = `${width}px`; |
| 405 |
return element; |
| 406 |
} |
| 407 |
|
| 408 |
public handleCursorMove(): void { |
| 409 |
// No-op, the cursor is drawn when rows are drawn |
| 410 |
} |
| 411 |
|
| 412 |
private _handleOptionsChanged(): void { |
| 413 |
// Force a refresh |
| 414 |
this._updateDimensions(); |
| 415 |
// Refresh CSS |
| 416 |
this._injectCss(this._themeService.colors); |
| 417 |
// update spacing cache |
| 418 |
this._widthCache.setFont( |
| 419 |
this._optionsService.rawOptions.fontFamily, |
| 420 |
this._optionsService.rawOptions.fontSize, |
| 421 |
this._optionsService.rawOptions.fontWeight, |
| 422 |
this._optionsService.rawOptions.fontWeightBold |
| 423 |
); |
| 424 |
this._setDefaultSpacing(); |
| 425 |
} |
| 426 |
|
| 427 |
public clear(): void { |
| 428 |
for (const e of this._rowElements) { |
| 429 |
/** |
| 430 |
* NOTE: This used to be `e.innerText = '';` but that doesn't work when using `jsdom` and |
| 431 |
* `@testing-library/react` |
| 432 |
* |
| 433 |
* references: |
| 434 |
* - https://github.com/testing-library/react-testing-library/issues/1146 |
| 435 |
* - https://github.com/jsdom/jsdom/issues/1245 |
| 436 |
*/ |
| 437 |
e.replaceChildren(); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
public renderRows(start: number, end: number): void { |
| 442 |
const buffer = this._bufferService.buffer; |
| 443 |
const cursorAbsoluteY = buffer.ybase + buffer.y; |
| 444 |
const cursorX = Math.min(buffer.x, this._bufferService.cols - 1); |
| 445 |
const cursorBlink = this._coreService.decPrivateModes.cursorBlink ?? this._optionsService.rawOptions.cursorBlink; |
| 446 |
const cursorStyle = this._coreService.decPrivateModes.cursorStyle ?? this._optionsService.rawOptions.cursorStyle; |
| 447 |
const cursorInactiveStyle = this._optionsService.rawOptions.cursorInactiveStyle; |
| 448 |
|
| 449 |
for (let y = start; y <= end; y++) { |
| 450 |
const row = y + buffer.ydisp; |
| 451 |
const rowElement = this._rowElements[y]; |
| 452 |
const lineData = buffer.lines.get(row); |
| 453 |
if (!rowElement || !lineData) { |
| 454 |
break; |
| 455 |
} |
| 456 |
rowElement.replaceChildren( |
| 457 |
...this._rowFactory.createRow( |
| 458 |
lineData, |
| 459 |
row, |
| 460 |
row === cursorAbsoluteY, |
| 461 |
cursorStyle, |
| 462 |
cursorInactiveStyle, |
| 463 |
cursorX, |
| 464 |
cursorBlink, |
| 465 |
this.dimensions.css.cell.width, |
| 466 |
this._widthCache, |
| 467 |
-1, |
| 468 |
-1 |
| 469 |
) |
| 470 |
); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
private get _terminalSelector(): string { |
| 475 |
return `.${TERMINAL_CLASS_PREFIX}${this._terminalClass}`; |
| 476 |
} |
| 477 |
|
| 478 |
private _handleLinkHover(e: ILinkifierEvent): void { |
| 479 |
this._setCellUnderline(e.x1, e.x2, e.y1, e.y2, e.cols, true); |
| 480 |
} |
| 481 |
|
| 482 |
private _handleLinkLeave(e: ILinkifierEvent): void { |
| 483 |
this._setCellUnderline(e.x1, e.x2, e.y1, e.y2, e.cols, false); |
| 484 |
} |
| 485 |
|
| 486 |
private _setCellUnderline(x: number, x2: number, y: number, y2: number, cols: number, enabled: boolean): void { |
| 487 |
/** |
| 488 |
* NOTE: The linkifier may send out of viewport y-values if: |
| 489 |
* - negative y-value: the link started at a higher line |
| 490 |
* - y-value >= maxY: the link ends at a line below viewport |
| 491 |
* |
| 492 |
* For negative y-values we can simply adjust x = 0, |
| 493 |
* as higher up link start means, that everything from |
| 494 |
* (0,0) is a link under top-down-left-right char progression |
| 495 |
* |
| 496 |
* Additionally there might be a small chance of out-of-sync x|y-values |
| 497 |
* from a race condition of render updates vs. link event handler execution: |
| 498 |
* - (sync) resize: chances terminal buffer in sync, schedules render update async |
| 499 |
* - (async) link handler race condition: new buffer metrics, but still on old render state |
| 500 |
* - (async) render update: brings term metrics and render state back in sync |
| 501 |
*/ |
| 502 |
// clip coords into viewport |
| 503 |
if (y < 0) x = 0; |
| 504 |
if (y2 < 0) x2 = 0; |
| 505 |
const maxY = this._bufferService.rows - 1; |
| 506 |
y = Math.max(Math.min(y, maxY), 0); |
| 507 |
y2 = Math.max(Math.min(y2, maxY), 0); |
| 508 |
|
| 509 |
cols = Math.min(cols, this._bufferService.cols); |
| 510 |
const buffer = this._bufferService.buffer; |
| 511 |
const cursorAbsoluteY = buffer.ybase + buffer.y; |
| 512 |
const cursorX = Math.min(buffer.x, cols - 1); |
| 513 |
const cursorBlink = this._optionsService.rawOptions.cursorBlink; |
| 514 |
const cursorStyle = this._optionsService.rawOptions.cursorStyle; |
| 515 |
const cursorInactiveStyle = this._optionsService.rawOptions.cursorInactiveStyle; |
| 516 |
|
| 517 |
// refresh rows within link range |
| 518 |
for (let i = y; i <= y2; ++i) { |
| 519 |
const row = i + buffer.ydisp; |
| 520 |
const rowElement = this._rowElements[i]; |
| 521 |
const bufferline = buffer.lines.get(row); |
| 522 |
if (!rowElement || !bufferline) { |
| 523 |
break; |
| 524 |
} |
| 525 |
rowElement.replaceChildren( |
| 526 |
...this._rowFactory.createRow( |
| 527 |
bufferline, |
| 528 |
row, |
| 529 |
row === cursorAbsoluteY, |
| 530 |
cursorStyle, |
| 531 |
cursorInactiveStyle, |
| 532 |
cursorX, |
| 533 |
cursorBlink, |
| 534 |
this.dimensions.css.cell.width, |
| 535 |
this._widthCache, |
| 536 |
enabled ? (i === y ? x : 0) : -1, |
| 537 |
enabled ? ((i === y2 ? x2 : cols) - 1) : -1 |
| 538 |
) |
| 539 |
); |
| 540 |
} |
| 541 |
} |
| 542 |
} |