Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When completing color keys in settings, fill in current value #213451

Merged
merged 17 commits into from
Jun 5, 2024
24 changes: 21 additions & 3 deletions src/vs/platform/theme/common/colorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assertNever } from 'vs/base/common/assert';
import { RunOnceScheduler } from 'vs/base/common/async';
import { Color } from 'vs/base/common/color';
import { Emitter, Event } from 'vs/base/common/event';
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
import { IJSONSchema, IJSONSchemaSnippet } from 'vs/base/common/jsonSchema';
import { IJSONContributionRegistry, Extensions as JSONExtensions } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
import * as platform from 'vs/platform/registry/common/platform';
import { IColorTheme } from 'vs/platform/theme/common/themeService';
Expand Down Expand Up @@ -117,25 +117,43 @@ export interface IColorRegistry {
*/
getColorReferenceSchema(): IJSONSchema;

/**
* Notify when the color theme or settings change.
*/
notifyThemeUpdate(theme: IColorTheme): void;

}

type IJSONSchemaForColors = IJSONSchema & { properties: { [name: string]: IJSONSchemaWithSnippets } };
type IJSONSchemaWithSnippets = IJSONSchema & { defaultSnippets: IJSONSchemaSnippet[] };

class ColorRegistry implements IColorRegistry {

private readonly _onDidChangeSchema = new Emitter<void>();
readonly onDidChangeSchema: Event<void> = this._onDidChangeSchema.event;

private colorsById: { [key: string]: ColorContribution };
private colorSchema: IJSONSchema & { properties: IJSONSchemaMap } = { type: 'object', properties: {} };
private colorSchema: IJSONSchemaForColors = { type: 'object', properties: {} };
private colorReferenceSchema: IJSONSchema & { enum: string[]; enumDescriptions: string[] } = { type: 'string', enum: [], enumDescriptions: [] };

constructor() {
this.colorsById = {};
}

public notifyThemeUpdate(colorThemeData: IColorTheme) {
for (const key of Object.keys(this.colorsById)) {
const color = colorThemeData.getColor(key);
if (color) {
this.colorSchema.properties[key].defaultSnippets[0].body = `\${1:${color.toString()}}`;
}
}
this._onDidChangeSchema.fire();
}

public registerColor(id: string, defaults: ColorDefaults | null, description: string, needsTransparency = false, deprecationMessage?: string): ColorIdentifier {
const colorContribution: ColorContribution = { id, description, defaults, needsTransparency, deprecationMessage };
this.colorsById[id] = colorContribution;
const propertySchema: IJSONSchema = { type: 'string', description, format: 'color-hex', defaultSnippets: [{ body: '${1:#ff0000}' }] };
const propertySchema: IJSONSchemaWithSnippets = { type: 'string', description, format: 'color-hex', defaultSnippets: [{ body: '${1:#ff0000}' }] };
if (deprecationMessage) {
propertySchema.deprecationMessage = deprecationMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export class WorkbenchThemeService extends Disposable implements IWorkbenchTheme
this.currentProductIconTheme = ProductIconThemeData.createUnloadedTheme('');
this.productIconThemeSequencer = new Sequencer();

this._register(this.onDidColorThemeChange(theme => getColorRegistry().notifyThemeUpdate(theme)));

// In order to avoid paint flashing for tokens, because
// themes are loaded asynchronously, we need to initialize
// a color theme document with good defaults until the theme is loaded
Expand Down
Loading