|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 The xterm.js authors. All rights reserved. |
| 3 | + * @license MIT |
| 4 | + */ |
| 5 | + |
| 6 | +import { IDecorationService, IRenderService } from 'browser/services/Services'; |
| 7 | +import { EventEmitter, IEvent } from 'common/EventEmitter'; |
| 8 | +import { Disposable } from 'common/Lifecycle'; |
| 9 | +import { IBufferService, IInstantiationService } from 'common/services/Services'; |
| 10 | +import { IDecorationOptions, IDecoration, IMarker } from 'xterm'; |
| 11 | + |
| 12 | +export class DecorationService extends Disposable implements IDecorationService { |
| 13 | + |
| 14 | + private readonly _decorations: Decoration[] = []; |
| 15 | + private _container: HTMLElement | undefined; |
| 16 | + private _screenElement: HTMLElement | undefined; |
| 17 | + private _renderService: IRenderService | undefined; |
| 18 | + |
| 19 | + constructor( |
| 20 | + @IBufferService private readonly _bufferService: IBufferService, |
| 21 | + @IInstantiationService private readonly _instantiationService: IInstantiationService) { |
| 22 | + super(); |
| 23 | + } |
| 24 | + |
| 25 | + public attachToDom(screenElement: HTMLElement, renderService: IRenderService): void { |
| 26 | + this._renderService = renderService; |
| 27 | + this._screenElement = screenElement; |
| 28 | + this._container = document.createElement('div'); |
| 29 | + this._container.classList.add('xterm-decoration-container'); |
| 30 | + screenElement.appendChild(this._container); |
| 31 | + this.refresh(); |
| 32 | + this.register(this._renderService.onRenderedBufferChange(() => this.refresh())); |
| 33 | + this.register(this._renderService.onDimensionsChange(() => this.refresh(true))); |
| 34 | + } |
| 35 | + |
| 36 | + public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { |
| 37 | + if (decorationOptions.marker.isDisposed || !this._container) { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + const decoration = this._instantiationService.createInstance(Decoration, decorationOptions, this._container); |
| 41 | + this._decorations.push(decoration); |
| 42 | + decoration.onDispose(() => this._decorations.splice(this._decorations.indexOf(decoration), 1)); |
| 43 | + return decoration; |
| 44 | + } |
| 45 | + |
| 46 | + public refresh(recreate?: boolean): void { |
| 47 | + if (!this._bufferService || !this._renderService) { |
| 48 | + return; |
| 49 | + } |
| 50 | + for (const decoration of this._decorations) { |
| 51 | + decoration.render(this._renderService, recreate); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public dispose(): void { |
| 56 | + for (const decoration of this._decorations) { |
| 57 | + decoration.dispose(); |
| 58 | + } |
| 59 | + if (this._container) { |
| 60 | + this._screenElement?.removeChild(this._container); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +export class Decoration extends Disposable implements IDecoration { |
| 65 | + private static _nextId = 1; |
| 66 | + private readonly _marker: IMarker; |
| 67 | + private _element: HTMLElement | undefined; |
| 68 | + private readonly _id: number = Decoration._nextId++; |
| 69 | + public isDisposed: boolean = false; |
| 70 | + |
| 71 | + public get element(): HTMLElement | undefined { return this._element; } |
| 72 | + public get marker(): IMarker { return this._marker; } |
| 73 | + |
| 74 | + private _onDispose = new EventEmitter<void>(); |
| 75 | + public get onDispose(): IEvent<void> { return this._onDispose.event; } |
| 76 | + |
| 77 | + private _onRender = new EventEmitter<HTMLElement>(); |
| 78 | + public get onRender(): IEvent<HTMLElement> { return this._onRender.event; } |
| 79 | + |
| 80 | + public x: number; |
| 81 | + public anchor: 'left' | 'right'; |
| 82 | + public width: number; |
| 83 | + public height: number; |
| 84 | + |
| 85 | + constructor( |
| 86 | + options: IDecorationOptions, |
| 87 | + private readonly _container: HTMLElement, |
| 88 | + @IBufferService private readonly _bufferService: IBufferService |
| 89 | + ) { |
| 90 | + super(); |
| 91 | + this.x = options.x ?? 0; |
| 92 | + this._marker = options.marker; |
| 93 | + this.anchor = options.anchor || 'left'; |
| 94 | + this.width = options.width || 1; |
| 95 | + this.height = options.height || 1; |
| 96 | + } |
| 97 | + |
| 98 | + public render(renderService: IRenderService, recreate?: boolean): void { |
| 99 | + if (!this._element || recreate) { |
| 100 | + this._createElement(renderService, recreate); |
| 101 | + } |
| 102 | + if (this._container && this._element && !this._container.contains(this._element)) { |
| 103 | + this._container.append(this._element); |
| 104 | + } |
| 105 | + this._refreshStyle(renderService); |
| 106 | + this._onRender.fire(this._element!); |
| 107 | + } |
| 108 | + |
| 109 | + private _createElement(renderService: IRenderService, recreate?: boolean): void { |
| 110 | + if (recreate && this._element) { |
| 111 | + this._container.removeChild(this._element); |
| 112 | + } |
| 113 | + this._element = document.createElement('div'); |
| 114 | + this._element.classList.add('xterm-decoration'); |
| 115 | + this._element.style.width = `${this.width * renderService.dimensions.scaledCellWidth}px`; |
| 116 | + this._element.style.height = `${this.height * renderService.dimensions.scaledCellHeight}px`; |
| 117 | + this._element.style.top = `${(this.marker.line - this._bufferService.buffers.active.ydisp) * renderService.dimensions.scaledCellHeight}px`; |
| 118 | + |
| 119 | + if (this.x && this.x > this._bufferService.cols) { |
| 120 | + this._element!.style.display = 'none'; |
| 121 | + } |
| 122 | + if (this.anchor === 'right') { |
| 123 | + this._element.style.right = this.x ? `${this.x * renderService.dimensions.scaledCellWidth}px` : ''; |
| 124 | + } else { |
| 125 | + this._element.style.left = this.x ? `${this.x * renderService.dimensions.scaledCellWidth}px` : ''; |
| 126 | + } |
| 127 | + this.register({ |
| 128 | + dispose: () => { |
| 129 | + if (this.isDisposed) { |
| 130 | + return; |
| 131 | + } |
| 132 | + this._container.removeChild(this._element!); |
| 133 | + this.isDisposed = true; |
| 134 | + this._marker.dispose(); |
| 135 | + // Emit before super.dispose such that dispose listeners get a change to react |
| 136 | + this._onDispose.fire(); |
| 137 | + super.dispose(); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + private _refreshStyle(renderService: IRenderService): void { |
| 143 | + const line = this.marker.line - this._bufferService.buffers.active.ydisp; |
| 144 | + if (line < 0 || line > this._bufferService.rows) { |
| 145 | + // outside of viewport |
| 146 | + this._element!.style.display = 'none'; |
| 147 | + } else { |
| 148 | + this._element!.style.top = `${line * renderService.dimensions.scaledCellHeight}px`; |
| 149 | + this._element!.style.display = 'block'; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments