|
| 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 { addDisposableDomListener } from 'browser/Lifecycle'; |
| 7 | +import { IRenderService } from 'browser/services/Services'; |
| 8 | +import { Disposable } from 'common/Lifecycle'; |
| 9 | +import { IBufferService, IDecorationService, IInternalDecoration, IOptionsService } from 'common/services/Services'; |
| 10 | + |
| 11 | +// This is used to reduce memory usage |
| 12 | +// when refreshStyle is called |
| 13 | +// by storing and updating |
| 14 | +// the sizes of the decorations to be drawn |
| 15 | +const renderSizes = new Uint16Array(3); |
| 16 | +const enum SizeIndex { |
| 17 | + OUTER_SIZE = 0, |
| 18 | + INNER_SIZE = 0 |
| 19 | +} |
| 20 | + |
| 21 | +export class OverviewRulerRenderer extends Disposable { |
| 22 | + private readonly _canvas: HTMLCanvasElement; |
| 23 | + private readonly _ctx: CanvasRenderingContext2D; |
| 24 | + private readonly _decorationElements: Map<IInternalDecoration, HTMLElement> = new Map(); |
| 25 | + private get _width(): number { |
| 26 | + return this._optionsService.options.overviewRulerWidth || 0; |
| 27 | + } |
| 28 | + private _animationFrame: number | undefined; |
| 29 | + |
| 30 | + constructor( |
| 31 | + private readonly _viewportElement: HTMLElement, |
| 32 | + private readonly _screenElement: HTMLElement, |
| 33 | + @IBufferService private readonly _bufferService: IBufferService, |
| 34 | + @IDecorationService private readonly _decorationService: IDecorationService, |
| 35 | + @IRenderService private readonly _renderService: IRenderService, |
| 36 | + @IOptionsService private readonly _optionsService: IOptionsService |
| 37 | + ) { |
| 38 | + super(); |
| 39 | + this._canvas = document.createElement('canvas'); |
| 40 | + this._canvas.classList.add('xterm-decoration-overview-ruler'); |
| 41 | + this._viewportElement.parentElement?.insertBefore(this._canvas, this._viewportElement); |
| 42 | + const ctx = this._canvas.getContext('2d'); |
| 43 | + if (!ctx) { |
| 44 | + throw new Error('Ctx cannot be null'); |
| 45 | + } else { |
| 46 | + this._ctx = ctx; |
| 47 | + } |
| 48 | + this._queueRefresh(true); |
| 49 | + this.register(this._bufferService.buffers.onBufferActivate(() => { |
| 50 | + this._canvas!.style.display = this._bufferService.buffer === this._bufferService.buffers.alt ? 'none' : 'block'; |
| 51 | + })); |
| 52 | + this.register(this._renderService.onRenderedBufferChange(() => this._queueRefresh())); |
| 53 | + this.register(this._renderService.onDimensionsChange(() => this._queueRefresh(true, true))); |
| 54 | + this.register(addDisposableDomListener(window, 'resize', () => this._queueRefresh(true))); |
| 55 | + this.register(this._decorationService.onDecorationRegistered(() => this._queueRefresh(undefined, true))); |
| 56 | + this.register(this._decorationService.onDecorationRemoved(decoration => this._removeDecoration(decoration))); |
| 57 | + this.register(this._optionsService.onOptionChange(o => { |
| 58 | + if (o === 'overviewRulerWidth') { |
| 59 | + renderSizes[SizeIndex.OUTER_SIZE] = Math.floor(this._width / 3); |
| 60 | + renderSizes[SizeIndex.INNER_SIZE] = Math.ceil(this._width / 3); |
| 61 | + this._queueRefresh(); |
| 62 | + } |
| 63 | + })); |
| 64 | + console.log(this._width/3); |
| 65 | + renderSizes[SizeIndex.OUTER_SIZE] = Math.floor(this._width / 3); |
| 66 | + renderSizes[SizeIndex.INNER_SIZE] = Math.ceil(this._width / 3); |
| 67 | + } |
| 68 | + |
| 69 | + public override dispose(): void { |
| 70 | + for (const decoration of this._decorationElements) { |
| 71 | + this._ctx?.clearRect( |
| 72 | + 0, |
| 73 | + Math.round(this._canvas.height * (decoration[0].marker.line / this._bufferService.buffers.active.lines.length)), |
| 74 | + this._canvas.width, |
| 75 | + window.devicePixelRatio |
| 76 | + ); |
| 77 | + } |
| 78 | + this._decorationElements.clear(); |
| 79 | + this._canvas?.remove(); |
| 80 | + super.dispose(); |
| 81 | + } |
| 82 | + |
| 83 | + private _refreshStyle(decoration: IInternalDecoration, updateAnchor?: boolean): void { |
| 84 | + if (updateAnchor) { |
| 85 | + if (decoration.options.anchor === 'right') { |
| 86 | + this._canvas.style.right = decoration.options.x ? `${decoration.options.x * this._renderService.dimensions.actualCellWidth}px` : ''; |
| 87 | + } else { |
| 88 | + this._canvas.style.left = decoration.options.x ? `${decoration.options.x * this._renderService.dimensions.actualCellWidth}px` : ''; |
| 89 | + } |
| 90 | + } |
| 91 | + if (!decoration.options.overviewRulerOptions) { |
| 92 | + this._decorationElements.delete(decoration); |
| 93 | + return; |
| 94 | + } |
| 95 | + this._ctx.lineWidth = !decoration.options.overviewRulerOptions.position ? 2 : 6; |
| 96 | + this._ctx.strokeStyle = decoration.options.overviewRulerOptions.color; |
| 97 | + this._ctx.strokeRect( |
| 98 | + !decoration.options.overviewRulerOptions.position || decoration.options.overviewRulerOptions.position === 'left' ? 0 : decoration.options.overviewRulerOptions.position === 'right' ? renderSizes[SizeIndex.OUTER_SIZE] + renderSizes[SizeIndex.INNER_SIZE]: renderSizes[SizeIndex.OUTER_SIZE], |
| 99 | + Math.round(this._canvas.height * (decoration.options.marker.line / this._bufferService.buffers.active.lines.length)), |
| 100 | + !decoration.options.overviewRulerOptions.position ? this._width : decoration.options.overviewRulerOptions.position === 'center' ? renderSizes[SizeIndex.INNER_SIZE]: renderSizes[SizeIndex.OUTER_SIZE], |
| 101 | + window.devicePixelRatio |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private _refreshDecorations(updateCanvasDimensions?: boolean, updateAnchor?: boolean): void { |
| 106 | + if (updateCanvasDimensions) { |
| 107 | + this._canvas.style.width = `${this._width}px`; |
| 108 | + this._canvas.style.height = `${this._screenElement.clientHeight}px`; |
| 109 | + this._canvas.width = Math.floor((this._width)* window.devicePixelRatio); |
| 110 | + this._canvas.height = Math.floor(this._screenElement.clientHeight * window.devicePixelRatio); |
| 111 | + } |
| 112 | + this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height); |
| 113 | + for (const decoration of this._decorationService.decorations) { |
| 114 | + this._renderDecoration(decoration, updateAnchor); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private _renderDecoration(decoration: IInternalDecoration, updateAnchor?: boolean): void { |
| 119 | + const element = this._decorationElements.get(decoration); |
| 120 | + if (!element) { |
| 121 | + this._decorationElements.set(decoration, this._canvas); |
| 122 | + } |
| 123 | + this._refreshStyle(decoration, updateAnchor); |
| 124 | + decoration.onRenderEmitter.fire(this._canvas); |
| 125 | + } |
| 126 | + |
| 127 | + private _queueRefresh(updateCanvasDimensions?: boolean, updateAnchor?: boolean): void { |
| 128 | + if (this._animationFrame !== undefined) { |
| 129 | + return; |
| 130 | + } |
| 131 | + this._animationFrame = window.requestAnimationFrame(() => { |
| 132 | + this._refreshDecorations(updateCanvasDimensions, updateAnchor); |
| 133 | + this._animationFrame = undefined; |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + private _removeDecoration(decoration: IInternalDecoration): void { |
| 138 | + this._decorationElements.get(decoration)?.remove(); |
| 139 | + this._decorationElements.delete(decoration); |
| 140 | + } |
| 141 | +} |
0 commit comments