提交 736e6f4e 编写于 作者: M Martin Aeschlimann

color defaults API

上级 15280c34
......@@ -14,7 +14,7 @@ export let IThemeService = createDecorator<IThemeService>('themeService');
export interface ITheme {
readonly selector: string;
readonly label: string;
getColor(colorId: string): Color;
getColor(color: string): Color;
isLightTheme(): boolean;
isDarkTheme(): boolean;
......
......@@ -6,6 +6,9 @@
import platform = require('vs/platform/platform');
import { IJSONSchema } from 'vs/base/common/jsonSchema';
import { Color } from 'vs/base/common/color';
import { ITheme } from 'vs/platform/theme/common/themeService';
import nls = require('vs/nls');
......@@ -16,20 +19,39 @@ export const Extensions = {
export interface IColorContribution {
readonly id: string;
readonly description: string;
readonly defaults: ColorDefaults;
}
export interface DerivedColor {
(theme: ITheme): Color;
}
export interface ColorDefaults {
light: ColorDescription;
dark: ColorDescription;
hc: ColorDescription;
}
export type ColorDescription = string | IColorContribution | DerivedColor;
export interface IThemingRegistry {
/**
* Register a color to the registry.
*/
registerColor(id: string, description: string): IColorContribution;
registerColor(id: string, description: string, defaults?: ColorDefaults): IColorContribution;
/**
* Get all color contributions
*/
getColors(): IColorContribution[];
/**
* Gets the color of the given id
*/
getColor(id: string): IColorContribution;
/**
* JSON schema of all colors
*/
......@@ -37,6 +59,16 @@ export interface IThemingRegistry {
}
export function darken(colorDesc: ColorDescription, factor: number): DerivedColor {
return (theme) => {
let color = resolveDescription(theme, colorDesc);
if (color) {
return color.darken(factor);
}
return null;
};
}
class ThemingRegistry implements IThemingRegistry {
private colorsById: { [key: string]: IColorContribution };
......@@ -46,8 +78,8 @@ class ThemingRegistry implements IThemingRegistry {
this.colorsById = {};
}
public registerColor(id: string, description: string): IColorContribution {
let colorContribution: IColorContribution = { id, description };
public registerColor(id: string, description: string, defaults: ColorDefaults) {
let colorContribution: IColorContribution = { id, description, defaults };
this.colorsById[id] = colorContribution;
this.colorSchema.properties[id] = { type: 'string', description };
return colorContribution;
......@@ -57,11 +89,40 @@ class ThemingRegistry implements IThemingRegistry {
return Object.keys(this.colorsById).map(id => this.colorsById[id]);
}
public getColor(id: string): IColorContribution {
return this.colorsById[id];
}
public getColorSchema(): IJSONSchema {
return this.colorSchema;
}
}
function resolveDescription(theme: ITheme, colorDesc: ColorDescription): Color {
if (typeof colorDesc === 'string') {
return Color.fromHex(colorDesc);
} else if (typeof colorDesc === 'object' && colorDesc !== null) {
let defaults = colorDesc.defaults;
if (!defaults) {
return null;
}
if (theme.isDarkTheme()) {
return resolveDescription(theme, defaults.dark);
} else if (theme.isLightTheme()) {
return resolveDescription(theme, defaults.light);
} else {
return resolveDescription(theme, defaults.hc);
}
} else if (typeof colorDesc === 'function') {
return colorDesc(theme);
} else {
return null;
}
}
const themingRegistry = new ThemingRegistry();
platform.Registry.add(Extensions.ThemingContribution, themingRegistry);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册