Skip to content

Commit 9355b43

Browse files
committed
Move renderColorDecorator options to core settings.
1 parent 580eb6b commit 9355b43

7 files changed

Lines changed: 155 additions & 11 deletions

File tree

extensions/css/client/src/colorDecorators.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ export class ColorProvider implements DocumentColorProvider {
4949
}
5050

5151
async provideDocumentColors(document: TextDocument): Promise<ColorRange[]> {
52-
if (!this.supportedLanguages[document.languageId] || !this.decoratorEnablement[document.languageId]) {
53-
return [];
54-
}
55-
5652
const ranges = await this.decoratorProvider(document.uri.toString());
5753
const result = [];
5854
for (let range of ranges) {

extensions/json/client/src/colorDecorators.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ export class ColorProvider implements DocumentColorProvider {
4242
}
4343

4444
async provideDocumentColors(document: TextDocument): Promise<ColorRange[]> {
45-
if (!this.supportedLanguages[document.languageId] || !this.decoratorEnablement[document.languageId]) {
46-
return [];
47-
}
48-
4945
const ranges = await this.decoratorProvider(document.uri.toString());
5046
const result = [];
5147
for (let range of ranges) {

src/vs/editor/common/config/commonEditorConfig.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,11 @@ const editorConfiguration: IConfigurationNode = {
593593
'default': EDITOR_DEFAULTS.contribInfo.links,
594594
'description': nls.localize('links', "Controls whether the editor should detect links and make them clickable")
595595
},
596+
'editor.colorDecorator': {
597+
'type': 'boolean',
598+
'default': EDITOR_DEFAULTS.contribInfo.colorDecorator,
599+
'description': nls.localize('colorDecorator', "Controls whether the editor should render the inline color decorators.")
600+
},
596601
'diffEditor.renderSideBySide': {
597602
'type': 'boolean',
598603
'default': true,

src/vs/editor/common/config/editorOptions.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ export interface IEditorOptions {
337337
* Defaults to true.
338338
*/
339339
links?: boolean;
340+
/**
341+
* Enable inline color decorators rendering.
342+
*/
343+
colorDecorator?: boolean;
340344
/**
341345
* Enable custom contextmenu.
342346
* Defaults to true.
@@ -790,6 +794,7 @@ export interface EditorContribOptions {
790794
readonly showFoldingControls: 'always' | 'mouseover';
791795
readonly matchBrackets: boolean;
792796
readonly find: InternalEditorFindOptions;
797+
readonly colorDecorator: boolean;
793798
}
794799

795800
/**
@@ -1134,6 +1139,7 @@ export class InternalEditorOptions {
11341139
&& a.showFoldingControls === b.showFoldingControls
11351140
&& a.matchBrackets === b.matchBrackets
11361141
&& this._equalFindOptions(a.find, b.find)
1142+
&& a.colorDecorator === b.colorDecorator
11371143
);
11381144
}
11391145

@@ -1661,7 +1667,8 @@ export class EditorOptionsValidator {
16611667
folding: _boolean(opts.folding, defaults.folding),
16621668
showFoldingControls: _stringSet<'always' | 'mouseover'>(opts.showFoldingControls, defaults.showFoldingControls, ['always', 'mouseover']),
16631669
matchBrackets: _boolean(opts.matchBrackets, defaults.matchBrackets),
1664-
find: find
1670+
find: find,
1671+
colorDecorator: _boolean(opts.colorDecorator, defaults.colorDecorator),
16651672
};
16661673
}
16671674
}
@@ -1758,7 +1765,8 @@ export class InternalEditorOptionsFactory {
17581765
folding: (accessibilityIsOn ? false : opts.contribInfo.folding), // DISABLED WHEN SCREEN READER IS ATTACHED
17591766
showFoldingControls: opts.contribInfo.showFoldingControls,
17601767
matchBrackets: (accessibilityIsOn ? false : opts.contribInfo.matchBrackets), // DISABLED WHEN SCREEN READER IS ATTACHED
1761-
find: opts.contribInfo.find
1768+
find: opts.contribInfo.find,
1769+
colorDecorator: opts.contribInfo.colorDecorator
17621770
}
17631771
};
17641772
}
@@ -2196,6 +2204,7 @@ export const EDITOR_DEFAULTS: IValidatedEditorOptions = {
21962204
find: {
21972205
seedSearchStringFromSelection: true,
21982206
autoFindInSelection: false
2199-
}
2207+
},
2208+
colorDecorator: true
22002209
},
22012210
};
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
import { Disposable } from 'vs/base/common/lifecycle';
6+
import { ICommonCodeEditor, IEditorContribution } from 'vs/editor/common/editorCommon';
7+
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
8+
import { editorContribution } from 'vs/editor/browser/editorBrowserExtensions';
9+
import { getColors } from '../common/color';
10+
import { ICodeEditorService } from 'vs/editor/common/services/codeEditorService';
11+
import { hash } from 'vs/base/common/hash';
12+
import { ColorProviderRegistry } from 'vs/editor/common/modes';
13+
import { RGBA } from 'vs/base/common/color';
14+
15+
const MAX_DECORATORS = 500;
16+
17+
@editorContribution
18+
export class ColorController extends Disposable implements IEditorContribution {
19+
20+
private static ID: string = 'editor.contrib.colorController';
21+
22+
public static get(editor: ICommonCodeEditor): ColorController {
23+
return editor.getContribution<ColorController>(ColorController.ID);
24+
}
25+
26+
private _isEnabled: boolean;
27+
private _decorations: string[];
28+
private _decorationsTypes: { [key: string]: boolean };
29+
30+
constructor(
31+
private _editor: ICodeEditor,
32+
@ICodeEditorService private _codeEditorService: ICodeEditorService
33+
) {
34+
super();
35+
this._isEnabled = this._editor.getConfiguration().contribInfo.colorDecorator;
36+
this._decorations = [];
37+
this._decorationsTypes = {};
38+
this._register(_editor.onDidChangeModel((e) => this.triggerUpdateDecorations()));
39+
this._register(_editor.onDidChangeModelContent((e) => {
40+
setTimeout(() => this.triggerUpdateDecorations(), 0);
41+
}));
42+
this._register(_editor.onDidChangeModelLanguage((e) => this.triggerUpdateDecorations()));
43+
this._register(_editor.onDidChangeConfiguration((e) => {
44+
let prevIsEnabled = this._isEnabled;
45+
this._isEnabled = this._editor.getConfiguration().contribInfo.colorDecorator;
46+
if (prevIsEnabled !== this._isEnabled) {
47+
this.triggerUpdateDecorations(true);
48+
}
49+
}));
50+
51+
this._register(ColorProviderRegistry.onDidChange((e) => this.triggerUpdateDecorations()));
52+
this.triggerUpdateDecorations();
53+
}
54+
55+
triggerUpdateDecorations(settingsChanges = false) {
56+
if (!this._isEnabled) {
57+
if (settingsChanges) {
58+
// Users turn it off.
59+
this._editor.changeDecorations((changeAccessor) => {
60+
this._decorations = changeAccessor.deltaDecorations(this._decorations, []);
61+
});
62+
for (let subType in this._decorationsTypes) {
63+
this._codeEditorService.removeDecorationType(subType);
64+
}
65+
}
66+
return;
67+
}
68+
69+
getColors(this._editor.getModel()).then((colorInfos) => {
70+
let decorations = [];
71+
let newDecorationsTypes: { [key: string]: boolean } = {};
72+
73+
for (let i = 0; i < colorInfos.length && decorations.length < MAX_DECORATORS; i++) {
74+
const { red, green, blue, alpha } = colorInfos[i].color;
75+
const rgba = new RGBA(Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255), alpha);
76+
let subKey = hash(rgba).toString(16);
77+
let color = `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`;
78+
let key = 'colorBox-' + subKey;
79+
80+
if (!this._decorationsTypes[key] && !newDecorationsTypes[key]) {
81+
this._codeEditorService.registerDecorationType(key, {
82+
before: {
83+
contentText: ' ',
84+
border: 'solid 0.1em #000',
85+
margin: '0.1em 0.2em 0 0.2em',
86+
width: '0.8em',
87+
height: '0.8em',
88+
backgroundColor: color
89+
},
90+
dark: {
91+
before: {
92+
border: 'solid 0.1em #eee'
93+
}
94+
}
95+
});
96+
}
97+
98+
newDecorationsTypes[key] = true;
99+
decorations.push({
100+
range: {
101+
startLineNumber: colorInfos[i].range.startLineNumber,
102+
startColumn: colorInfos[i].range.startColumn,
103+
endLineNumber: colorInfos[i].range.endLineNumber,
104+
endColumn: colorInfos[i].range.endColumn
105+
},
106+
options: this._codeEditorService.resolveDecorationOptions(key, true)
107+
});
108+
}
109+
110+
for (let subType in this._decorationsTypes) {
111+
if (!newDecorationsTypes[subType]) {
112+
this._codeEditorService.removeDecorationType(subType);
113+
}
114+
}
115+
116+
this._editor.changeDecorations((changeAccessor) => {
117+
this._decorations = changeAccessor.deltaDecorations(this._decorations, decorations);
118+
});
119+
});
120+
}
121+
122+
getId(): string {
123+
return ColorController.ID;
124+
}
125+
126+
public dispose(): void {
127+
for (let subType in this._decorationsTypes) {
128+
this._codeEditorService.removeDecorationType(subType);
129+
}
130+
super.dispose();
131+
}
132+
}

src/vs/monaco.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,10 @@ declare module monaco.editor {
28892889
* Defaults to true.
28902890
*/
28912891
links?: boolean;
2892+
/**
2893+
* Enable inline color decorators rendering.
2894+
*/
2895+
colorDecorator?: boolean;
28922896
/**
28932897
* Enable custom contextmenu.
28942898
* Defaults to true.
@@ -3286,6 +3290,7 @@ declare module monaco.editor {
32863290
readonly showFoldingControls: 'always' | 'mouseover';
32873291
readonly matchBrackets: boolean;
32883292
readonly find: InternalEditorFindOptions;
3293+
readonly colorDecorator: boolean;
32893294
}
32903295

32913296
/**

src/vs/platform/telemetry/common/telemetryUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ const configurationValueWhitelist = [
143143
'editor.stablePeek',
144144
'editor.dragAndDrop',
145145
'editor.formatOnSave',
146+
'editor.colorDecorator',
146147

147148
'window.zoomLevel',
148149
'files.autoSave',

0 commit comments

Comments
 (0)