Debian Codegraph @

node/@xterm/xterm/src/common/services/DecorationService.ts

raw
1 /**
2 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { css } from 'common/Color';
7 import { Disposable, DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
8 import { IDecorationService, IInternalDecoration } from 'common/services/Services';
9 import { SortedList } from 'common/SortedList';
10 import { IColor } from 'common/Types';
11 import { IDecoration, IDecorationOptions, IMarker } from '@xterm/xterm';
12 import { Emitter } from 'vs/base/common/event';
13
14 // Work variables to avoid garbage collection
15 let $xmin = 0;
16 let $xmax = 0;
17
18 export class DecorationService extends Disposable implements IDecorationService {
19 public serviceBrand: any;
20
21 /**
22 * A list of all decorations, sorted by the marker's line value. This relies on the fact that
23 * while marker line values do change, they should all change by the same amount so this should
24 * never become out of order.
25 */
26 private readonly _decorations: SortedList<IInternalDecoration> = new SortedList(e => e?.marker.line);
27
28 private readonly _onDecorationRegistered = this._register(new Emitter<IInternalDecoration>());
29 public readonly onDecorationRegistered = this._onDecorationRegistered.event;
30 private readonly _onDecorationRemoved = this._register(new Emitter<IInternalDecoration>());
31 public readonly onDecorationRemoved = this._onDecorationRemoved.event;
32
33 public get decorations(): IterableIterator<IInternalDecoration> { return this._decorations.values(); }
34
35 constructor() {
36 super();
37
38 this._register(toDisposable(() => this.reset()));
39 }
40
41 public registerDecoration(options: IDecorationOptions): IDecoration | undefined {
42 if (options.marker.isDisposed) {
43 return undefined;
44 }
45 const decoration = new Decoration(options);
46 if (decoration) {
47 const markerDispose = decoration.marker.onDispose(() => decoration.dispose());
48 const listener = decoration.onDispose(() => {
49 listener.dispose();
50 if (decoration) {
51 if (this._decorations.delete(decoration)) {
52 this._onDecorationRemoved.fire(decoration);
53 }
54 markerDispose.dispose();
55 }
56 });
57 this._decorations.insert(decoration);
58 this._onDecorationRegistered.fire(decoration);
59 }
60 return decoration;
61 }
62
63 public reset(): void {
64 for (const d of this._decorations.values()) {
65 d.dispose();
66 }
67 this._decorations.clear();
68 }
69
70 public *getDecorationsAtCell(x: number, line: number, layer?: 'bottom' | 'top'): IterableIterator<IInternalDecoration> {
71 let xmin = 0;
72 let xmax = 0;
73 for (const d of this._decorations.getKeyIterator(line)) {
74 xmin = d.options.x ?? 0;
75 xmax = xmin + (d.options.width ?? 1);
76 if (x >= xmin && x < xmax && (!layer || (d.options.layer ?? 'bottom') === layer)) {
77 yield d;
78 }
79 }
80 }
81
82 public forEachDecorationAtCell(x: number, line: number, layer: 'bottom' | 'top' | undefined, callback: (decoration: IInternalDecoration) => void): void {
83 this._decorations.forEachByKey(line, d => {
84 $xmin = d.options.x ?? 0;
85 $xmax = $xmin + (d.options.width ?? 1);
86 if (x >= $xmin && x < $xmax && (!layer || (d.options.layer ?? 'bottom') === layer)) {
87 callback(d);
88 }
89 });
90 }
91 }
92
93 class Decoration extends DisposableStore implements IInternalDecoration {
94 public readonly marker: IMarker;
95 public element: HTMLElement | undefined;
96
97 public readonly onRenderEmitter = this.add(new Emitter<HTMLElement>());
98 public readonly onRender = this.onRenderEmitter.event;
99 private readonly _onDispose = this.add(new Emitter<void>());
100 public readonly onDispose = this._onDispose.event;
101
102 private _cachedBg: IColor | undefined | null = null;
103 public get backgroundColorRGB(): IColor | undefined {
104 if (this._cachedBg === null) {
105 if (this.options.backgroundColor) {
106 this._cachedBg = css.toColor(this.options.backgroundColor);
107 } else {
108 this._cachedBg = undefined;
109 }
110 }
111 return this._cachedBg;
112 }
113
114 private _cachedFg: IColor | undefined | null = null;
115 public get foregroundColorRGB(): IColor | undefined {
116 if (this._cachedFg === null) {
117 if (this.options.foregroundColor) {
118 this._cachedFg = css.toColor(this.options.foregroundColor);
119 } else {
120 this._cachedFg = undefined;
121 }
122 }
123 return this._cachedFg;
124 }
125
126 constructor(
127 public readonly options: IDecorationOptions
128 ) {
129 super();
130 this.marker = options.marker;
131 if (this.options.overviewRulerOptions && !this.options.overviewRulerOptions.position) {
132 this.options.overviewRulerOptions.position = 'full';
133 }
134 }
135
136 public override dispose(): void {
137 this._onDispose.fire();
138 super.dispose();
139 }
140 }