Debian Codegraph @

node/@xterm/xterm/src/browser/decorations/OverviewRulerRenderer.ts

raw
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 { ColorZoneStore, IColorZone, IColorZoneStore } from 'browser/decorations/ColorZoneStore';
7 import { ICoreBrowserService, IRenderService, IThemeService } from 'browser/services/Services';
8 import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
9 import { IBufferService, IDecorationService, IOptionsService } from 'common/services/Services';
10
11 const enum Constants {
12 OVERVIEW_RULER_BORDER_WIDTH = 1
13 }
14
15 // Helper objects to avoid excessive calculation and garbage collection during rendering. These are
16 // static values for each render and can be accessed using the decoration position as the key.
17 const drawHeight = {
18 full: 0,
19 left: 0,
20 center: 0,
21 right: 0
22 };
23 const drawWidth = {
24 full: 0,
25 left: 0,
26 center: 0,
27 right: 0
28 };
29 const drawX = {
30 full: 0,
31 left: 0,
32 center: 0,
33 right: 0
34 };
35
36 export class OverviewRulerRenderer extends Disposable {
37 private readonly _canvas: HTMLCanvasElement;
38 private readonly _ctx: CanvasRenderingContext2D;
39 private readonly _colorZoneStore: IColorZoneStore = new ColorZoneStore();
40 private get _width(): number {
41 return this._optionsService.options.overviewRuler?.width || 0;
42 }
43 private _animationFrame: number | undefined;
44
45 private _shouldUpdateDimensions: boolean | undefined = true;
46 private _shouldUpdateAnchor: boolean | undefined = true;
47 private _lastKnownBufferLength: number = 0;
48
49 private _containerHeight: number | undefined;
50
51 constructor(
52 private readonly _viewportElement: HTMLElement,
53 private readonly _screenElement: HTMLElement,
54 @IBufferService private readonly _bufferService: IBufferService,
55 @IDecorationService private readonly _decorationService: IDecorationService,
56 @IRenderService private readonly _renderService: IRenderService,
57 @IOptionsService private readonly _optionsService: IOptionsService,
58 @IThemeService private readonly _themeService: IThemeService,
59 @ICoreBrowserService private readonly _coreBrowserService: ICoreBrowserService
60 ) {
61 super();
62 this._canvas = this._coreBrowserService.mainDocument.createElement('canvas');
63 this._canvas.classList.add('xterm-decoration-overview-ruler');
64 this._refreshCanvasDimensions();
65 this._viewportElement.parentElement?.insertBefore(this._canvas, this._viewportElement);
66 this._register(toDisposable(() => this._canvas?.remove()));
67
68 const ctx = this._canvas.getContext('2d');
69 if (!ctx) {
70 throw new Error('Ctx cannot be null');
71 } else {
72 this._ctx = ctx;
73 }
74
75 this._register(this._decorationService.onDecorationRegistered(() => this._queueRefresh(undefined, true)));
76 this._register(this._decorationService.onDecorationRemoved(() => this._queueRefresh(undefined, true)));
77
78 this._register(this._renderService.onRenderedViewportChange(() => this._queueRefresh()));
79 this._register(this._bufferService.buffers.onBufferActivate(() => {
80 this._canvas!.style.display = this._bufferService.buffer === this._bufferService.buffers.alt ? 'none' : 'block';
81 }));
82 this._register(this._bufferService.onScroll(() => {
83 if (this._lastKnownBufferLength !== this._bufferService.buffers.normal.lines.length) {
84 this._refreshDrawHeightConstants();
85 this._refreshColorZonePadding();
86 }
87 }));
88
89 // Container height changed
90 this._register(this._renderService.onRender((): void => {
91 if (!this._containerHeight || this._containerHeight !== this._screenElement.clientHeight) {
92 this._queueRefresh(true);
93 this._containerHeight = this._screenElement.clientHeight;
94 }
95 }));
96
97 this._register(this._coreBrowserService.onDprChange(() => this._queueRefresh(true)));
98 this._register(this._optionsService.onSpecificOptionChange('overviewRuler', () => this._queueRefresh(true)));
99 this._register(this._themeService.onChangeColors(() => this._queueRefresh()));
100 this._queueRefresh(true);
101 }
102
103 private _refreshDrawConstants(): void {
104 // width
105 const outerWidth = Math.floor((this._canvas.width - Constants.OVERVIEW_RULER_BORDER_WIDTH) / 3);
106 const innerWidth = Math.ceil((this._canvas.width - Constants.OVERVIEW_RULER_BORDER_WIDTH) / 3);
107 drawWidth.full = this._canvas.width;
108 drawWidth.left = outerWidth;
109 drawWidth.center = innerWidth;
110 drawWidth.right = outerWidth;
111 // height
112 this._refreshDrawHeightConstants();
113 // x
114 drawX.full = Constants.OVERVIEW_RULER_BORDER_WIDTH;
115 drawX.left = Constants.OVERVIEW_RULER_BORDER_WIDTH;
116 drawX.center = Constants.OVERVIEW_RULER_BORDER_WIDTH + drawWidth.left;
117 drawX.right = Constants.OVERVIEW_RULER_BORDER_WIDTH + drawWidth.left + drawWidth.center;
118 }
119
120 private _refreshDrawHeightConstants(): void {
121 drawHeight.full = Math.round(2 * this._coreBrowserService.dpr);
122 // Calculate actual pixels per line
123 const pixelsPerLine = this._canvas.height / this._bufferService.buffer.lines.length;
124 // Clamp actual pixels within a range
125 const nonFullHeight = Math.round(Math.max(Math.min(pixelsPerLine, 12), 6) * this._coreBrowserService.dpr);
126 drawHeight.left = nonFullHeight;
127 drawHeight.center = nonFullHeight;
128 drawHeight.right = nonFullHeight;
129 }
130
131 private _refreshColorZonePadding(): void {
132 this._colorZoneStore.setPadding({
133 full: Math.floor(this._bufferService.buffers.active.lines.length / (this._canvas.height - 1) * drawHeight.full),
134 left: Math.floor(this._bufferService.buffers.active.lines.length / (this._canvas.height - 1) * drawHeight.left),
135 center: Math.floor(this._bufferService.buffers.active.lines.length / (this._canvas.height - 1) * drawHeight.center),
136 right: Math.floor(this._bufferService.buffers.active.lines.length / (this._canvas.height - 1) * drawHeight.right)
137 });
138 this._lastKnownBufferLength = this._bufferService.buffers.normal.lines.length;
139 }
140
141 private _refreshCanvasDimensions(): void {
142 this._canvas.style.width = `${this._width}px`;
143 this._canvas.width = Math.round(this._width * this._coreBrowserService.dpr);
144 this._canvas.style.height = `${this._screenElement.clientHeight}px`;
145 this._canvas.height = Math.round(this._screenElement.clientHeight * this._coreBrowserService.dpr);
146 this._refreshDrawConstants();
147 this._refreshColorZonePadding();
148 }
149
150 private _refreshDecorations(): void {
151 if (this._shouldUpdateDimensions) {
152 this._refreshCanvasDimensions();
153 }
154 this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
155 this._colorZoneStore.clear();
156 for (const decoration of this._decorationService.decorations) {
157 this._colorZoneStore.addDecoration(decoration);
158 }
159 this._ctx.lineWidth = 1;
160 this._renderRulerOutline();
161 const zones = this._colorZoneStore.zones;
162 for (const zone of zones) {
163 if (zone.position !== 'full') {
164 this._renderColorZone(zone);
165 }
166 }
167 for (const zone of zones) {
168 if (zone.position === 'full') {
169 this._renderColorZone(zone);
170 }
171 }
172 this._shouldUpdateDimensions = false;
173 this._shouldUpdateAnchor = false;
174 }
175
176 private _renderRulerOutline(): void {
177 this._ctx.fillStyle = this._themeService.colors.overviewRulerBorder.css;
178 this._ctx.fillRect(0, 0, Constants.OVERVIEW_RULER_BORDER_WIDTH, this._canvas.height);
179 if (this._optionsService.rawOptions.overviewRuler.showTopBorder) {
180 this._ctx.fillRect(Constants.OVERVIEW_RULER_BORDER_WIDTH, 0, this._canvas.width - Constants.OVERVIEW_RULER_BORDER_WIDTH, Constants.OVERVIEW_RULER_BORDER_WIDTH);
181 }
182 if (this._optionsService.rawOptions.overviewRuler.showBottomBorder) {
183 this._ctx.fillRect(Constants.OVERVIEW_RULER_BORDER_WIDTH, this._canvas.height - Constants.OVERVIEW_RULER_BORDER_WIDTH, this._canvas.width - Constants.OVERVIEW_RULER_BORDER_WIDTH, this._canvas.height);
184 }
185 }
186
187 private _renderColorZone(zone: IColorZone): void {
188 this._ctx.fillStyle = zone.color;
189 this._ctx.fillRect(
190 /* x */ drawX[zone.position || 'full'],
191 /* y */ Math.round(
192 (this._canvas.height - 1) * // -1 to ensure at least 2px are allowed for decoration on last line
193 (zone.startBufferLine / this._bufferService.buffers.active.lines.length) - drawHeight[zone.position || 'full'] / 2
194 ),
195 /* w */ drawWidth[zone.position || 'full'],
196 /* h */ Math.round(
197 (this._canvas.height - 1) * // -1 to ensure at least 2px are allowed for decoration on last line
198 ((zone.endBufferLine - zone.startBufferLine) / this._bufferService.buffers.active.lines.length) + drawHeight[zone.position || 'full']
199 )
200 );
201 }
202
203 private _queueRefresh(updateCanvasDimensions?: boolean, updateAnchor?: boolean): void {
204 this._shouldUpdateDimensions = updateCanvasDimensions || this._shouldUpdateDimensions;
205 this._shouldUpdateAnchor = updateAnchor || this._shouldUpdateAnchor;
206 if (this._animationFrame !== undefined) {
207 return;
208 }
209 this._animationFrame = this._coreBrowserService.window.requestAnimationFrame(() => {
210 this._refreshDecorations();
211 this._animationFrame = undefined;
212 });
213 }
214 }