Debian Codegraph @

node/@xterm/xterm/src/browser/services/ThemeService.ts

raw
1 /**
2 * Copyright (c) 2022 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { ColorContrastCache } from 'browser/ColorContrastCache';
7 import { IThemeService } from 'browser/services/Services';
8 import { DEFAULT_ANSI_COLORS, IColorContrastCache, IColorSet, ReadonlyColorSet } from 'browser/Types';
9 import { color, css, NULL_COLOR } from 'common/Color';
10 import { Disposable } from 'vs/base/common/lifecycle';
11 import { IOptionsService, ITheme } from 'common/services/Services';
12 import { AllColorIndex, IColor, SpecialColorIndex } from 'common/Types';
13 import { Emitter } from 'vs/base/common/event';
14
15 interface IRestoreColorSet {
16 foreground: IColor;
17 background: IColor;
18 cursor: IColor;
19 ansi: IColor[];
20 }
21
22
23 const DEFAULT_FOREGROUND = css.toColor('#ffffff');
24 const DEFAULT_BACKGROUND = css.toColor('#000000');
25 const DEFAULT_CURSOR = css.toColor('#ffffff');
26 const DEFAULT_CURSOR_ACCENT = DEFAULT_BACKGROUND;
27 const DEFAULT_SELECTION = {
28 css: 'rgba(255, 255, 255, 0.3)',
29 rgba: 0xFFFFFF4D
30 };
31 const DEFAULT_OVERVIEW_RULER_BORDER = DEFAULT_FOREGROUND;
32
33 export class ThemeService extends Disposable implements IThemeService {
34 public serviceBrand: undefined;
35
36 private _colors: IColorSet;
37 private _contrastCache: IColorContrastCache = new ColorContrastCache();
38 private _halfContrastCache: IColorContrastCache = new ColorContrastCache();
39 private _restoreColors!: IRestoreColorSet;
40
41 public get colors(): ReadonlyColorSet { return this._colors; }
42
43 private readonly _onChangeColors = this._register(new Emitter<ReadonlyColorSet>());
44 public readonly onChangeColors = this._onChangeColors.event;
45
46 constructor(
47 @IOptionsService private readonly _optionsService: IOptionsService
48 ) {
49 super();
50
51 this._colors = {
52 foreground: DEFAULT_FOREGROUND,
53 background: DEFAULT_BACKGROUND,
54 cursor: DEFAULT_CURSOR,
55 cursorAccent: DEFAULT_CURSOR_ACCENT,
56 selectionForeground: undefined,
57 selectionBackgroundTransparent: DEFAULT_SELECTION,
58 selectionBackgroundOpaque: color.blend(DEFAULT_BACKGROUND, DEFAULT_SELECTION),
59 selectionInactiveBackgroundTransparent: DEFAULT_SELECTION,
60 selectionInactiveBackgroundOpaque: color.blend(DEFAULT_BACKGROUND, DEFAULT_SELECTION),
61 scrollbarSliderBackground: color.opacity(DEFAULT_FOREGROUND, 0.2),
62 scrollbarSliderHoverBackground: color.opacity(DEFAULT_FOREGROUND, 0.4),
63 scrollbarSliderActiveBackground: color.opacity(DEFAULT_FOREGROUND, 0.5),
64 overviewRulerBorder: DEFAULT_FOREGROUND,
65 ansi: DEFAULT_ANSI_COLORS.slice(),
66 contrastCache: this._contrastCache,
67 halfContrastCache: this._halfContrastCache
68 };
69 this._updateRestoreColors();
70 this._setTheme(this._optionsService.rawOptions.theme);
71
72 this._register(this._optionsService.onSpecificOptionChange('minimumContrastRatio', () => this._contrastCache.clear()));
73 this._register(this._optionsService.onSpecificOptionChange('theme', () => this._setTheme(this._optionsService.rawOptions.theme)));
74 }
75
76 /**
77 * Sets the terminal's theme.
78 * @param theme The theme to use. If a partial theme is provided then default
79 * colors will be used where colors are not defined.
80 */
81 private _setTheme(theme: ITheme = {}): void {
82 const colors = this._colors;
83 colors.foreground = parseColor(theme.foreground, DEFAULT_FOREGROUND);
84 colors.background = parseColor(theme.background, DEFAULT_BACKGROUND);
85 colors.cursor = color.blend(colors.background, parseColor(theme.cursor, DEFAULT_CURSOR));
86 colors.cursorAccent = color.blend(colors.background, parseColor(theme.cursorAccent, DEFAULT_CURSOR_ACCENT));
87 colors.selectionBackgroundTransparent = parseColor(theme.selectionBackground, DEFAULT_SELECTION);
88 colors.selectionBackgroundOpaque = color.blend(colors.background, colors.selectionBackgroundTransparent);
89 colors.selectionInactiveBackgroundTransparent = parseColor(theme.selectionInactiveBackground, colors.selectionBackgroundTransparent);
90 colors.selectionInactiveBackgroundOpaque = color.blend(colors.background, colors.selectionInactiveBackgroundTransparent);
91 colors.selectionForeground = theme.selectionForeground ? parseColor(theme.selectionForeground, NULL_COLOR) : undefined;
92 if (colors.selectionForeground === NULL_COLOR) {
93 colors.selectionForeground = undefined;
94 }
95
96 /**
97 * If selection color is opaque, blend it with background with 0.3 opacity
98 * Issue #2737
99 */
100 if (color.isOpaque(colors.selectionBackgroundTransparent)) {
101 const opacity = 0.3;
102 colors.selectionBackgroundTransparent = color.opacity(colors.selectionBackgroundTransparent, opacity);
103 }
104 if (color.isOpaque(colors.selectionInactiveBackgroundTransparent)) {
105 const opacity = 0.3;
106 colors.selectionInactiveBackgroundTransparent = color.opacity(colors.selectionInactiveBackgroundTransparent, opacity);
107 }
108 colors.scrollbarSliderBackground = parseColor(theme.scrollbarSliderBackground, color.opacity(colors.foreground, 0.2));
109 colors.scrollbarSliderHoverBackground = parseColor(theme.scrollbarSliderHoverBackground, color.opacity(colors.foreground, 0.4));
110 colors.scrollbarSliderActiveBackground = parseColor(theme.scrollbarSliderActiveBackground, color.opacity(colors.foreground, 0.5));
111 colors.overviewRulerBorder = parseColor(theme.overviewRulerBorder, DEFAULT_OVERVIEW_RULER_BORDER);
112 colors.ansi = DEFAULT_ANSI_COLORS.slice();
113 colors.ansi[0] = parseColor(theme.black, DEFAULT_ANSI_COLORS[0]);
114 colors.ansi[1] = parseColor(theme.red, DEFAULT_ANSI_COLORS[1]);
115 colors.ansi[2] = parseColor(theme.green, DEFAULT_ANSI_COLORS[2]);
116 colors.ansi[3] = parseColor(theme.yellow, DEFAULT_ANSI_COLORS[3]);
117 colors.ansi[4] = parseColor(theme.blue, DEFAULT_ANSI_COLORS[4]);
118 colors.ansi[5] = parseColor(theme.magenta, DEFAULT_ANSI_COLORS[5]);
119 colors.ansi[6] = parseColor(theme.cyan, DEFAULT_ANSI_COLORS[6]);
120 colors.ansi[7] = parseColor(theme.white, DEFAULT_ANSI_COLORS[7]);
121 colors.ansi[8] = parseColor(theme.brightBlack, DEFAULT_ANSI_COLORS[8]);
122 colors.ansi[9] = parseColor(theme.brightRed, DEFAULT_ANSI_COLORS[9]);
123 colors.ansi[10] = parseColor(theme.brightGreen, DEFAULT_ANSI_COLORS[10]);
124 colors.ansi[11] = parseColor(theme.brightYellow, DEFAULT_ANSI_COLORS[11]);
125 colors.ansi[12] = parseColor(theme.brightBlue, DEFAULT_ANSI_COLORS[12]);
126 colors.ansi[13] = parseColor(theme.brightMagenta, DEFAULT_ANSI_COLORS[13]);
127 colors.ansi[14] = parseColor(theme.brightCyan, DEFAULT_ANSI_COLORS[14]);
128 colors.ansi[15] = parseColor(theme.brightWhite, DEFAULT_ANSI_COLORS[15]);
129 if (theme.extendedAnsi) {
130 const colorCount = Math.min(colors.ansi.length - 16, theme.extendedAnsi.length);
131 for (let i = 0; i < colorCount; i++) {
132 colors.ansi[i + 16] = parseColor(theme.extendedAnsi[i], DEFAULT_ANSI_COLORS[i + 16]);
133 }
134 }
135 // Clear our the cache
136 this._contrastCache.clear();
137 this._halfContrastCache.clear();
138 this._updateRestoreColors();
139 this._onChangeColors.fire(this.colors);
140 }
141
142 public restoreColor(slot?: AllColorIndex): void {
143 this._restoreColor(slot);
144 this._onChangeColors.fire(this.colors);
145 }
146
147 private _restoreColor(slot: AllColorIndex | undefined): void {
148 // unset slot restores all ansi colors
149 if (slot === undefined) {
150 for (let i = 0; i < this._restoreColors.ansi.length; ++i) {
151 this._colors.ansi[i] = this._restoreColors.ansi[i];
152 }
153 return;
154 }
155 switch (slot) {
156 case SpecialColorIndex.FOREGROUND:
157 this._colors.foreground = this._restoreColors.foreground;
158 break;
159 case SpecialColorIndex.BACKGROUND:
160 this._colors.background = this._restoreColors.background;
161 break;
162 case SpecialColorIndex.CURSOR:
163 this._colors.cursor = this._restoreColors.cursor;
164 break;
165 default:
166 this._colors.ansi[slot] = this._restoreColors.ansi[slot];
167 }
168 }
169
170 public modifyColors(callback: (colors: IColorSet) => void): void {
171 callback(this._colors);
172 // Assume the change happened
173 this._onChangeColors.fire(this.colors);
174 }
175
176 private _updateRestoreColors(): void {
177 this._restoreColors = {
178 foreground: this._colors.foreground,
179 background: this._colors.background,
180 cursor: this._colors.cursor,
181 ansi: this._colors.ansi.slice()
182 };
183 }
184 }
185
186 function parseColor(
187 cssString: string | undefined,
188 fallback: IColor
189 ): IColor {
190 if (cssString !== undefined) {
191 try {
192 return css.toColor(cssString);
193 } catch {
194 // no-op
195 }
196 }
197 return fallback;
198 }