Skip to content

Commit 736e6f4

Browse files
committed
color defaults API
1 parent 15280c3 commit 736e6f4

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

src/vs/platform/theme/common/themeService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export let IThemeService = createDecorator<IThemeService>('themeService');
1414
export interface ITheme {
1515
readonly selector: string;
1616
readonly label: string;
17-
getColor(colorId: string): Color;
17+
getColor(color: string): Color;
1818

1919
isLightTheme(): boolean;
2020
isDarkTheme(): boolean;

src/vs/platform/theme/common/themingRegistry.ts

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import platform = require('vs/platform/platform');
88
import { IJSONSchema } from 'vs/base/common/jsonSchema';
9+
import { Color } from 'vs/base/common/color';
10+
import { ITheme } from 'vs/platform/theme/common/themeService';
11+
912

1013
import nls = require('vs/nls');
1114

@@ -16,27 +19,56 @@ export const Extensions = {
1619
export interface IColorContribution {
1720
readonly id: string;
1821
readonly description: string;
22+
readonly defaults: ColorDefaults;
23+
}
24+
25+
export interface DerivedColor {
26+
(theme: ITheme): Color;
27+
}
28+
29+
export interface ColorDefaults {
30+
light: ColorDescription;
31+
dark: ColorDescription;
32+
hc: ColorDescription;
1933
}
2034

35+
export type ColorDescription = string | IColorContribution | DerivedColor;
36+
37+
2138
export interface IThemingRegistry {
2239

2340
/**
2441
* Register a color to the registry.
2542
*/
26-
registerColor(id: string, description: string): IColorContribution;
43+
registerColor(id: string, description: string, defaults?: ColorDefaults): IColorContribution;
2744

2845
/**
2946
* Get all color contributions
3047
*/
3148
getColors(): IColorContribution[];
3249

50+
/**
51+
* Gets the color of the given id
52+
*/
53+
getColor(id: string): IColorContribution;
54+
3355
/**
3456
* JSON schema of all colors
3557
*/
3658
getColorSchema(): IJSONSchema;
3759

3860
}
3961

62+
export function darken(colorDesc: ColorDescription, factor: number): DerivedColor {
63+
return (theme) => {
64+
let color = resolveDescription(theme, colorDesc);
65+
if (color) {
66+
return color.darken(factor);
67+
}
68+
return null;
69+
};
70+
}
71+
4072

4173
class ThemingRegistry implements IThemingRegistry {
4274
private colorsById: { [key: string]: IColorContribution };
@@ -46,8 +78,8 @@ class ThemingRegistry implements IThemingRegistry {
4678
this.colorsById = {};
4779
}
4880

49-
public registerColor(id: string, description: string): IColorContribution {
50-
let colorContribution: IColorContribution = { id, description };
81+
public registerColor(id: string, description: string, defaults: ColorDefaults) {
82+
let colorContribution: IColorContribution = { id, description, defaults };
5183
this.colorsById[id] = colorContribution;
5284
this.colorSchema.properties[id] = { type: 'string', description };
5385
return colorContribution;
@@ -57,11 +89,40 @@ class ThemingRegistry implements IThemingRegistry {
5789
return Object.keys(this.colorsById).map(id => this.colorsById[id]);
5890
}
5991

92+
public getColor(id: string): IColorContribution {
93+
return this.colorsById[id];
94+
}
95+
6096
public getColorSchema(): IJSONSchema {
6197
return this.colorSchema;
6298
}
6399

64100
}
65101

102+
function resolveDescription(theme: ITheme, colorDesc: ColorDescription): Color {
103+
if (typeof colorDesc === 'string') {
104+
return Color.fromHex(colorDesc);
105+
} else if (typeof colorDesc === 'object' && colorDesc !== null) {
106+
let defaults = colorDesc.defaults;
107+
if (!defaults) {
108+
return null;
109+
}
110+
if (theme.isDarkTheme()) {
111+
return resolveDescription(theme, defaults.dark);
112+
} else if (theme.isLightTheme()) {
113+
return resolveDescription(theme, defaults.light);
114+
} else {
115+
return resolveDescription(theme, defaults.hc);
116+
}
117+
} else if (typeof colorDesc === 'function') {
118+
return colorDesc(theme);
119+
} else {
120+
return null;
121+
}
122+
}
123+
124+
125+
126+
66127
const themingRegistry = new ThemingRegistry();
67128
platform.Registry.add(Extensions.ThemingContribution, themingRegistry);

0 commit comments

Comments
 (0)