| 1 |
/*--------------------------------------------------------------------------------------------- |
| 2 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 |
*--------------------------------------------------------------------------------------------*/ |
| 5 |
|
| 6 |
import { ICoreBrowserService, IRenderService } from 'browser/services/Services'; |
| 7 |
import { Disposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 8 |
import { IBufferService, IDecorationService, IInternalDecoration } from 'common/services/Services'; |
| 9 |
|
| 10 |
export class BufferDecorationRenderer extends Disposable { |
| 11 |
private readonly _container: HTMLElement; |
| 12 |
private readonly _decorationElements: Map<IInternalDecoration, HTMLElement> = new Map(); |
| 13 |
|
| 14 |
private _animationFrame: number | undefined; |
| 15 |
private _altBufferIsActive: boolean = false; |
| 16 |
private _dimensionsChanged: boolean = false; |
| 17 |
|
| 18 |
constructor( |
| 19 |
private readonly _screenElement: HTMLElement, |
| 20 |
@IBufferService private readonly _bufferService: IBufferService, |
| 21 |
@ICoreBrowserService private readonly _coreBrowserService: ICoreBrowserService, |
| 22 |
@IDecorationService private readonly _decorationService: IDecorationService, |
| 23 |
@IRenderService private readonly _renderService: IRenderService |
| 24 |
) { |
| 25 |
super(); |
| 26 |
|
| 27 |
this._container = document.createElement('div'); |
| 28 |
this._container.classList.add('xterm-decoration-container'); |
| 29 |
this._screenElement.appendChild(this._container); |
| 30 |
|
| 31 |
this._register(this._renderService.onRenderedViewportChange(() => this._doRefreshDecorations())); |
| 32 |
this._register(this._renderService.onDimensionsChange(() => { |
| 33 |
this._dimensionsChanged = true; |
| 34 |
this._queueRefresh(); |
| 35 |
})); |
| 36 |
this._register(this._coreBrowserService.onDprChange(() => this._queueRefresh())); |
| 37 |
this._register(this._bufferService.buffers.onBufferActivate(() => { |
| 38 |
this._altBufferIsActive = this._bufferService.buffer === this._bufferService.buffers.alt; |
| 39 |
})); |
| 40 |
this._register(this._decorationService.onDecorationRegistered(() => this._queueRefresh())); |
| 41 |
this._register(this._decorationService.onDecorationRemoved(decoration => this._removeDecoration(decoration))); |
| 42 |
this._register(toDisposable(() => { |
| 43 |
this._container.remove(); |
| 44 |
this._decorationElements.clear(); |
| 45 |
})); |
| 46 |
} |
| 47 |
|
| 48 |
private _queueRefresh(): void { |
| 49 |
if (this._animationFrame !== undefined) { |
| 50 |
return; |
| 51 |
} |
| 52 |
this._animationFrame = this._renderService.addRefreshCallback(() => { |
| 53 |
this._doRefreshDecorations(); |
| 54 |
this._animationFrame = undefined; |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
private _doRefreshDecorations(): void { |
| 59 |
for (const decoration of this._decorationService.decorations) { |
| 60 |
this._renderDecoration(decoration); |
| 61 |
} |
| 62 |
this._dimensionsChanged = false; |
| 63 |
} |
| 64 |
|
| 65 |
private _renderDecoration(decoration: IInternalDecoration): void { |
| 66 |
this._refreshStyle(decoration); |
| 67 |
if (this._dimensionsChanged) { |
| 68 |
this._refreshXPosition(decoration); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
private _createElement(decoration: IInternalDecoration): HTMLElement { |
| 73 |
const element = this._coreBrowserService.mainDocument.createElement('div'); |
| 74 |
element.classList.add('xterm-decoration'); |
| 75 |
element.classList.toggle('xterm-decoration-top-layer', decoration?.options?.layer === 'top'); |
| 76 |
element.style.width = `${Math.round((decoration.options.width || 1) * this._renderService.dimensions.css.cell.width)}px`; |
| 77 |
element.style.height = `${(decoration.options.height || 1) * this._renderService.dimensions.css.cell.height}px`; |
| 78 |
element.style.top = `${(decoration.marker.line - this._bufferService.buffers.active.ydisp) * this._renderService.dimensions.css.cell.height}px`; |
| 79 |
element.style.lineHeight = `${this._renderService.dimensions.css.cell.height}px`; |
| 80 |
|
| 81 |
const x = decoration.options.x ?? 0; |
| 82 |
if (x && x > this._bufferService.cols) { |
| 83 |
// exceeded the container width, so hide |
| 84 |
element.style.display = 'none'; |
| 85 |
} |
| 86 |
this._refreshXPosition(decoration, element); |
| 87 |
|
| 88 |
return element; |
| 89 |
} |
| 90 |
|
| 91 |
private _refreshStyle(decoration: IInternalDecoration): void { |
| 92 |
const line = decoration.marker.line - this._bufferService.buffers.active.ydisp; |
| 93 |
if (line < 0 || line >= this._bufferService.rows) { |
| 94 |
// outside of viewport |
| 95 |
if (decoration.element) { |
| 96 |
decoration.element.style.display = 'none'; |
| 97 |
decoration.onRenderEmitter.fire(decoration.element); |
| 98 |
} |
| 99 |
} else { |
| 100 |
let element = this._decorationElements.get(decoration); |
| 101 |
if (!element) { |
| 102 |
element = this._createElement(decoration); |
| 103 |
decoration.element = element; |
| 104 |
this._decorationElements.set(decoration, element); |
| 105 |
this._container.appendChild(element); |
| 106 |
decoration.onDispose(() => { |
| 107 |
this._decorationElements.delete(decoration); |
| 108 |
element!.remove(); |
| 109 |
}); |
| 110 |
} |
| 111 |
element.style.display = this._altBufferIsActive ? 'none' : 'block'; |
| 112 |
if (!this._altBufferIsActive) { |
| 113 |
element.style.width = `${Math.round((decoration.options.width || 1) * this._renderService.dimensions.css.cell.width)}px`; |
| 114 |
element.style.height = `${(decoration.options.height || 1) * this._renderService.dimensions.css.cell.height}px`; |
| 115 |
element.style.top = `${line * this._renderService.dimensions.css.cell.height}px`; |
| 116 |
element.style.lineHeight = `${this._renderService.dimensions.css.cell.height}px`; |
| 117 |
} |
| 118 |
decoration.onRenderEmitter.fire(element); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
private _refreshXPosition(decoration: IInternalDecoration, element: HTMLElement | undefined = decoration.element): void { |
| 123 |
if (!element) { |
| 124 |
return; |
| 125 |
} |
| 126 |
const x = decoration.options.x ?? 0; |
| 127 |
if ((decoration.options.anchor || 'left') === 'right') { |
| 128 |
element.style.right = x ? `${x * this._renderService.dimensions.css.cell.width}px` : ''; |
| 129 |
} else { |
| 130 |
element.style.left = x ? `${x * this._renderService.dimensions.css.cell.width}px` : ''; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private _removeDecoration(decoration: IInternalDecoration): void { |
| 135 |
this._decorationElements.get(decoration)?.remove(); |
| 136 |
this._decorationElements.delete(decoration); |
| 137 |
decoration.dispose(); |
| 138 |
} |
| 139 |
} |