Path: blob/main/src/vs/editor/browser/viewParts/rulersGpu/rulersGpu.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { ViewPart } from '../../view/viewPart.js';6import { RenderingContext, RestrictedRenderingContext } from '../../view/renderingContext.js';7import { ViewContext } from '../../../common/viewModel/viewContext.js';8import * as viewEvents from '../../../common/viewEvents.js';9import { EditorOption } from '../../../common/config/editorOptions.js';10import type { ViewGpuContext } from '../../gpu/viewGpuContext.js';11import type { IObjectCollectionBufferEntry } from '../../gpu/objectCollectionBuffer.js';12import type { RectangleRenderer, RectangleRendererEntrySpec } from '../../gpu/rectangleRenderer.js';13import { Color } from '../../../../base/common/color.js';14import { editorRuler } from '../../../common/core/editorColorRegistry.js';15import { autorun, type IReader } from '../../../../base/common/observable.js';1617/**18* Rulers are vertical lines that appear at certain columns in the editor. There can be >= 0 rulers19* at a time.20*/21export class RulersGpu extends ViewPart {2223private readonly _gpuShapes: IObjectCollectionBufferEntry<RectangleRendererEntrySpec>[] = [];2425constructor(26context: ViewContext,27private readonly _viewGpuContext: ViewGpuContext28) {29super(context);30this._register(autorun(reader => this._updateEntries(reader)));31}3233// --- begin event handlers3435public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {36this._updateEntries(undefined);37return true;38}3940// --- end event handlers4142public prepareRender(ctx: RenderingContext): void {43// Nothing to read44}4546public render(ctx: RestrictedRenderingContext): void {47// Rendering is handled by RectangleRenderer48}4950private _updateEntries(reader: IReader | undefined) {51const options = this._context.configuration.options;52const rulers = options.get(EditorOption.rulers);53const typicalHalfwidthCharacterWidth = options.get(EditorOption.fontInfo).typicalHalfwidthCharacterWidth;54const devicePixelRatio = this._viewGpuContext.devicePixelRatio.read(reader);55for (let i = 0, len = rulers.length; i < len; i++) {56const ruler = rulers[i];57const shape = this._gpuShapes[i];58const color = ruler.color ? Color.fromHex(ruler.color) : this._context.theme.getColor(editorRuler) ?? Color.white;59const rulerData: Parameters<RectangleRenderer['register']> = [60ruler.column * typicalHalfwidthCharacterWidth * devicePixelRatio,610,62Math.max(1, Math.ceil(devicePixelRatio)),63Number.MAX_SAFE_INTEGER,64color.rgba.r / 255,65color.rgba.g / 255,66color.rgba.b / 255,67color.rgba.a,68];69if (!shape) {70this._gpuShapes[i] = this._viewGpuContext.rectangleRenderer.register(...rulerData);71} else {72shape.setRaw(rulerData);73}74}75while (this._gpuShapes.length > rulers.length) {76this._gpuShapes.splice(-1, 1)[0].dispose();77}78}79}808182