Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/browser/viewParts/rulersGpu/rulersGpu.ts
3296 views
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 { ViewPart } from '../../view/viewPart.js';
7
import { RenderingContext, RestrictedRenderingContext } from '../../view/renderingContext.js';
8
import { ViewContext } from '../../../common/viewModel/viewContext.js';
9
import * as viewEvents from '../../../common/viewEvents.js';
10
import { EditorOption } from '../../../common/config/editorOptions.js';
11
import type { ViewGpuContext } from '../../gpu/viewGpuContext.js';
12
import type { IObjectCollectionBufferEntry } from '../../gpu/objectCollectionBuffer.js';
13
import type { RectangleRenderer, RectangleRendererEntrySpec } from '../../gpu/rectangleRenderer.js';
14
import { Color } from '../../../../base/common/color.js';
15
import { editorRuler } from '../../../common/core/editorColorRegistry.js';
16
import { autorun, type IReader } from '../../../../base/common/observable.js';
17
18
/**
19
* Rulers are vertical lines that appear at certain columns in the editor. There can be >= 0 rulers
20
* at a time.
21
*/
22
export class RulersGpu extends ViewPart {
23
24
private readonly _gpuShapes: IObjectCollectionBufferEntry<RectangleRendererEntrySpec>[] = [];
25
26
constructor(
27
context: ViewContext,
28
private readonly _viewGpuContext: ViewGpuContext
29
) {
30
super(context);
31
this._register(autorun(reader => this._updateEntries(reader)));
32
}
33
34
// --- begin event handlers
35
36
public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
37
this._updateEntries(undefined);
38
return true;
39
}
40
41
// --- end event handlers
42
43
public prepareRender(ctx: RenderingContext): void {
44
// Nothing to read
45
}
46
47
public render(ctx: RestrictedRenderingContext): void {
48
// Rendering is handled by RectangleRenderer
49
}
50
51
private _updateEntries(reader: IReader | undefined) {
52
const options = this._context.configuration.options;
53
const rulers = options.get(EditorOption.rulers);
54
const typicalHalfwidthCharacterWidth = options.get(EditorOption.fontInfo).typicalHalfwidthCharacterWidth;
55
const devicePixelRatio = this._viewGpuContext.devicePixelRatio.read(reader);
56
for (let i = 0, len = rulers.length; i < len; i++) {
57
const ruler = rulers[i];
58
const shape = this._gpuShapes[i];
59
const color = ruler.color ? Color.fromHex(ruler.color) : this._context.theme.getColor(editorRuler) ?? Color.white;
60
const rulerData: Parameters<RectangleRenderer['register']> = [
61
ruler.column * typicalHalfwidthCharacterWidth * devicePixelRatio,
62
0,
63
Math.max(1, Math.ceil(devicePixelRatio)),
64
Number.MAX_SAFE_INTEGER,
65
color.rgba.r / 255,
66
color.rgba.g / 255,
67
color.rgba.b / 255,
68
color.rgba.a,
69
];
70
if (!shape) {
71
this._gpuShapes[i] = this._viewGpuContext.rectangleRenderer.register(...rulerData);
72
} else {
73
shape.setRaw(rulerData);
74
}
75
}
76
while (this._gpuShapes.length > rulers.length) {
77
this._gpuShapes.splice(-1, 1)[0].dispose();
78
}
79
}
80
}
81
82