styler.ts 14.5 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import { ITheme, IThemeService } from 'vs/platform/theme/common/themeService';
J
Joao Moreno 已提交
9
import { inputBackground, inputForeground, ColorIdentifier, selectForeground, selectBackground, selectBorder, inputBorder, foreground, editorBackground, contrastBorder, inputActiveOptionBorder, listFocusBackground, listFocusForeground, listActiveSelectionBackground, listActiveSelectionForeground, listInactiveSelectionForeground, listInactiveSelectionBackground, listInactiveFocusForeground, listInactiveFocusBackground, listHoverBackground, listHoverForeground, listDropBackground, pickerGroupBorder, pickerGroupForeground, widgetShadow, inputValidationInfoBorder, inputValidationInfoBackground, inputValidationWarningBorder, inputValidationWarningBackground, inputValidationErrorBorder, inputValidationErrorBackground, activeContrastBorder, buttonForeground, buttonBackground, buttonHoverBackground, ColorFunction, lighten, badgeBackground, badgeForeground, progressBarBackground } from 'vs/platform/theme/common/colorRegistry';
10
import { IDisposable } from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
11

12 13
export type styleFn = (colors: { [name: string]: ColorIdentifier }) => void;

B
Benjamin Pasero 已提交
14
export interface IThemable {
15
	style: styleFn;
B
Benjamin Pasero 已提交
16 17
}

18 19 20 21 22
export interface IColorMapping {
	[optionsKey: string]: ColorIdentifier | ColorFunction | undefined;
}

export function attachStyler<T extends IColorMapping>(themeService: IThemeService, optionsMapping: T, widgetOrCallback: IThemable | styleFn): IDisposable {
B
Benjamin Pasero 已提交
23 24 25
	function applyStyles(theme: ITheme): void {
		const styles = Object.create(null);
		for (let key in optionsMapping) {
26
			const value = optionsMapping[key as string];
27 28 29 30 31
			if (typeof value === 'string') {
				styles[key] = theme.getColor(value);
			} else if (typeof value === 'function') {
				styles[key] = value(theme);
			}
B
Benjamin Pasero 已提交
32 33
		}

34 35 36 37 38
		if (typeof widgetOrCallback === 'function') {
			widgetOrCallback(styles);
		} else {
			widgetOrCallback.style(styles);
		}
B
Benjamin Pasero 已提交
39 40 41 42 43 44 45
	}

	applyStyles(themeService.getTheme());

	return themeService.onThemeChange(applyStyles);
}

B
Benjamin Pasero 已提交
46
export function attachCheckboxStyler(widget: IThemable, themeService: IThemeService, style?: { inputActiveOptionBorderColor?: ColorIdentifier }): IDisposable {
B
Benjamin Pasero 已提交
47
	return attachStyler(themeService, {
48
		inputActiveOptionBorder: (style && style.inputActiveOptionBorderColor) || inputActiveOptionBorder
49
	}, widget);
B
Benjamin Pasero 已提交
50 51
}

52 53 54
export function attachBadgeStyler(widget: IThemable, themeService: IThemeService, style?:
	{
		badgeBackground?: ColorIdentifier,
55
		badgeForeground?: ColorIdentifier
56
	}): IDisposable {
B
Benjamin Pasero 已提交
57
	return attachStyler(themeService, {
58 59
		badgeBackground: (style && style.badgeBackground) || badgeBackground,
		badgeForeground: (style && style.badgeForeground) || badgeForeground,
60
		badgeBorder: contrastBorder
61 62 63
	}, widget);
}

64 65 66 67 68
export function attachInputBoxStyler(widget: IThemable, themeService: IThemeService, style?:
	{
		inputBackground?: ColorIdentifier,
		inputForeground?: ColorIdentifier,
		inputBorder?: ColorIdentifier,
B
Benjamin Pasero 已提交
69 70 71 72 73 74
		inputValidationInfoBorder?: ColorIdentifier,
		inputValidationInfoBackground?: ColorIdentifier,
		inputValidationWarningBorder?: ColorIdentifier,
		inputValidationWarningBackground?: ColorIdentifier,
		inputValidationErrorBorder?: ColorIdentifier,
		inputValidationErrorBackground?: ColorIdentifier
75
	}): IDisposable {
B
Benjamin Pasero 已提交
76
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
77
		inputBackground: (style && style.inputBackground) || inputBackground,
B
Benjamin Pasero 已提交
78
		inputForeground: (style && style.inputForeground) || inputForeground,
79
		inputBorder: (style && style.inputBorder) || inputBorder,
B
Benjamin Pasero 已提交
80 81 82 83 84 85
		inputValidationInfoBorder: (style && style.inputValidationInfoBorder) || inputValidationInfoBorder,
		inputValidationInfoBackground: (style && style.inputValidationInfoBackground) || inputValidationInfoBackground,
		inputValidationWarningBorder: (style && style.inputValidationWarningBorder) || inputValidationWarningBorder,
		inputValidationWarningBackground: (style && style.inputValidationWarningBackground) || inputValidationWarningBackground,
		inputValidationErrorBorder: (style && style.inputValidationErrorBorder) || inputValidationErrorBorder,
		inputValidationErrorBackground: (style && style.inputValidationErrorBackground) || inputValidationErrorBackground
86
	}, widget);
87 88
}

B
Benjamin Pasero 已提交
89
export function attachSelectBoxStyler(widget: IThemable, themeService: IThemeService, style?: { selectBackground?: ColorIdentifier, selectForeground?: ColorIdentifier, selectBorder?: ColorIdentifier }): IDisposable {
B
Benjamin Pasero 已提交
90
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
91 92 93
		selectBackground: (style && style.selectBackground) || selectBackground,
		selectForeground: (style && style.selectForeground) || selectForeground,
		selectBorder: (style && style.selectBorder) || selectBorder
94
	}, widget);
95 96
}

97 98 99 100 101 102
export function attachFindInputBoxStyler(widget: IThemable, themeService: IThemeService, style?:
	{
		inputBackground?: ColorIdentifier,
		inputForeground?: ColorIdentifier,
		inputBorder?: ColorIdentifier,
		inputActiveOptionBorder?: ColorIdentifier,
B
Benjamin Pasero 已提交
103 104 105 106 107 108
		inputValidationInfoBorder?: ColorIdentifier,
		inputValidationInfoBackground?: ColorIdentifier,
		inputValidationWarningBorder?: ColorIdentifier,
		inputValidationWarningBackground?: ColorIdentifier,
		inputValidationErrorBorder?: ColorIdentifier,
		inputValidationErrorBackground?: ColorIdentifier
109
	}): IDisposable {
B
Benjamin Pasero 已提交
110
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
111 112 113
		inputBackground: (style && style.inputBackground) || inputBackground,
		inputForeground: (style && style.inputForeground) || inputForeground,
		inputBorder: (style && style.inputBorder) || inputBorder,
114
		inputActiveOptionBorder: (style && style.inputActiveOptionBorder) || inputActiveOptionBorder,
B
Benjamin Pasero 已提交
115 116 117 118 119 120
		inputValidationInfoBorder: (style && style.inputValidationInfoBorder) || inputValidationInfoBorder,
		inputValidationInfoBackground: (style && style.inputValidationInfoBackground) || inputValidationInfoBackground,
		inputValidationWarningBorder: (style && style.inputValidationWarningBorder) || inputValidationWarningBorder,
		inputValidationWarningBackground: (style && style.inputValidationWarningBackground) || inputValidationWarningBackground,
		inputValidationErrorBorder: (style && style.inputValidationErrorBorder) || inputValidationErrorBorder,
		inputValidationErrorBackground: (style && style.inputValidationErrorBackground) || inputValidationErrorBackground
121
	}, widget);
B
Benjamin Pasero 已提交
122 123
}

B
Benjamin Pasero 已提交
124 125 126 127
export function attachQuickOpenStyler(widget: IThemable, themeService: IThemeService, style?: {
	foreground?: ColorIdentifier,
	background?: ColorIdentifier,
	borderColor?: ColorIdentifier,
128
	widgetShadow?: ColorIdentifier,
B
Benjamin Pasero 已提交
129
	progressBarBackground?: ColorIdentifier,
B
Benjamin Pasero 已提交
130 131 132
	inputBackground?: ColorIdentifier,
	inputForeground?: ColorIdentifier,
	inputBorder?: ColorIdentifier,
B
Benjamin Pasero 已提交
133 134 135 136 137 138
	inputValidationInfoBorder?: ColorIdentifier,
	inputValidationInfoBackground?: ColorIdentifier,
	inputValidationWarningBorder?: ColorIdentifier,
	inputValidationWarningBackground?: ColorIdentifier,
	inputValidationErrorBorder?: ColorIdentifier,
	inputValidationErrorBackground?: ColorIdentifier
139 140
	pickerGroupForeground?: ColorIdentifier,
	pickerGroupBorder?: ColorIdentifier,
B
Benjamin Pasero 已提交
141
	listFocusBackground?: ColorIdentifier,
142
	listFocusForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
143 144 145 146 147
	listActiveSelectionBackground?: ColorIdentifier,
	listActiveSelectionForeground?: ColorIdentifier,
	listFocusAndSelectionBackground?: ColorIdentifier,
	listFocusAndSelectionForeground?: ColorIdentifier,
	listInactiveSelectionBackground?: ColorIdentifier,
148
	listInactiveSelectionForeground?: ColorIdentifier,
J
Joao Moreno 已提交
149 150
	listInactiveFocusBackground?: ColorIdentifier,
	listInactiveFocusForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
151
	listHoverBackground?: ColorIdentifier,
152
	listHoverForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
153
	listDropBackground?: ColorIdentifier,
154 155 156
	listFocusOutline?: ColorIdentifier,
	listSelectionOutline?: ColorIdentifier,
	listHoverOutline?: ColorIdentifier
B
Benjamin Pasero 已提交
157
}): IDisposable {
B
Benjamin Pasero 已提交
158
	return attachStyler(themeService, {
159 160
		foreground: (style && style.foreground) || foreground,
		background: (style && style.background) || editorBackground,
161
		borderColor: style && style.borderColor || contrastBorder,
162
		widgetShadow: style && style.widgetShadow || widgetShadow,
B
Benjamin Pasero 已提交
163
		progressBarBackground: style && style.progressBarBackground || progressBarBackground,
164 165
		pickerGroupForeground: style && style.pickerGroupForeground || pickerGroupForeground,
		pickerGroupBorder: style && style.pickerGroupBorder || pickerGroupBorder,
166 167
		inputBackground: (style && style.inputBackground) || inputBackground,
		inputForeground: (style && style.inputForeground) || inputForeground,
B
Benjamin Pasero 已提交
168
		inputBorder: (style && style.inputBorder) || inputBorder,
B
Benjamin Pasero 已提交
169 170 171 172 173 174
		inputValidationInfoBorder: (style && style.inputValidationInfoBorder) || inputValidationInfoBorder,
		inputValidationInfoBackground: (style && style.inputValidationInfoBackground) || inputValidationInfoBackground,
		inputValidationWarningBorder: (style && style.inputValidationWarningBorder) || inputValidationWarningBorder,
		inputValidationWarningBackground: (style && style.inputValidationWarningBackground) || inputValidationWarningBackground,
		inputValidationErrorBorder: (style && style.inputValidationErrorBorder) || inputValidationErrorBorder,
		inputValidationErrorBackground: (style && style.inputValidationErrorBackground) || inputValidationErrorBackground,
B
Benjamin Pasero 已提交
175
		listFocusBackground: (style && style.listFocusBackground) || listFocusBackground,
176
		listFocusForeground: (style && style.listFocusForeground) || listFocusForeground,
177
		listActiveSelectionBackground: (style && style.listActiveSelectionBackground) || lighten(listActiveSelectionBackground, 0.1),
B
Benjamin Pasero 已提交
178
		listActiveSelectionForeground: (style && style.listActiveSelectionForeground) || listActiveSelectionForeground,
179
		listFocusAndSelectionBackground: style && style.listFocusAndSelectionBackground || listActiveSelectionBackground,
180
		listFocusAndSelectionForeground: (style && style.listFocusAndSelectionForeground) || listActiveSelectionForeground,
B
Benjamin Pasero 已提交
181
		listInactiveSelectionBackground: (style && style.listInactiveSelectionBackground) || listInactiveSelectionBackground,
182
		listInactiveSelectionForeground: (style && style.listInactiveSelectionForeground) || listInactiveSelectionForeground,
J
Joao Moreno 已提交
183 184
		listInactiveFocusBackground: (style && style.listInactiveFocusBackground) || listInactiveFocusBackground,
		listInactiveFocusForeground: (style && style.listInactiveFocusForeground) || listInactiveFocusForeground,
B
Benjamin Pasero 已提交
185
		listHoverBackground: (style && style.listHoverBackground) || listHoverBackground,
186
		listHoverForeground: (style && style.listHoverForeground) || listHoverForeground,
B
Benjamin Pasero 已提交
187
		listDropBackground: (style && style.listDropBackground) || listDropBackground,
188 189 190
		listFocusOutline: (style && style.listFocusOutline) || activeContrastBorder,
		listSelectionOutline: (style && style.listSelectionOutline) || activeContrastBorder,
		listHoverOutline: (style && style.listHoverOutline) || activeContrastBorder
191
	}, widget);
B
Benjamin Pasero 已提交
192 193 194 195
}

export function attachListStyler(widget: IThemable, themeService: IThemeService, style?: {
	listFocusBackground?: ColorIdentifier,
196
	listFocusForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
197 198 199 200 201
	listActiveSelectionBackground?: ColorIdentifier,
	listActiveSelectionForeground?: ColorIdentifier,
	listFocusAndSelectionBackground?: ColorIdentifier,
	listFocusAndSelectionForeground?: ColorIdentifier,
	listInactiveSelectionBackground?: ColorIdentifier,
202
	listInactiveSelectionForeground?: ColorIdentifier,
J
Joao Moreno 已提交
203 204
	listInactiveFocusBackground?: ColorIdentifier,
	listInactiveFocusForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
205
	listHoverBackground?: ColorIdentifier,
206
	listHoverForeground?: ColorIdentifier,
B
Benjamin Pasero 已提交
207
	listDropBackground?: ColorIdentifier,
208 209 210 211
	listFocusOutline?: ColorIdentifier,
	listInactiveFocusOutline?: ColorIdentifier,
	listSelectionOutline?: ColorIdentifier,
	listHoverOutline?: ColorIdentifier,
B
Benjamin Pasero 已提交
212
}): IDisposable {
B
Benjamin Pasero 已提交
213
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
214
		listFocusBackground: (style && style.listFocusBackground) || listFocusBackground,
215
		listFocusForeground: (style && style.listFocusForeground) || listFocusForeground,
216
		listActiveSelectionBackground: (style && style.listActiveSelectionBackground) || lighten(listActiveSelectionBackground, 0.1),
B
Benjamin Pasero 已提交
217
		listActiveSelectionForeground: (style && style.listActiveSelectionForeground) || listActiveSelectionForeground,
218
		listFocusAndSelectionBackground: style && style.listFocusAndSelectionBackground || listActiveSelectionBackground,
219
		listFocusAndSelectionForeground: (style && style.listFocusAndSelectionForeground) || listActiveSelectionForeground,
B
Benjamin Pasero 已提交
220
		listInactiveSelectionBackground: (style && style.listInactiveSelectionBackground) || listInactiveSelectionBackground,
221
		listInactiveSelectionForeground: (style && style.listInactiveSelectionForeground) || listInactiveSelectionForeground,
J
Joao Moreno 已提交
222 223
		listInactiveFocusBackground: (style && style.listInactiveFocusBackground) || listInactiveFocusBackground,
		listInactiveFocusForeground: (style && style.listInactiveFocusForeground) || listInactiveFocusForeground,
B
Benjamin Pasero 已提交
224
		listHoverBackground: (style && style.listHoverBackground) || listHoverBackground,
225
		listHoverForeground: (style && style.listHoverForeground) || listHoverForeground,
B
Benjamin Pasero 已提交
226
		listDropBackground: (style && style.listDropBackground) || listDropBackground,
227 228 229
		listFocusOutline: (style && style.listFocusOutline) || activeContrastBorder,
		listSelectionOutline: (style && style.listSelectionOutline) || activeContrastBorder,
		listHoverOutline: (style && style.listHoverOutline) || activeContrastBorder,
B
Benjamin Pasero 已提交
230
		listInactiveFocusOutline: style && style.listInactiveFocusOutline // not defined by default, only opt-in
231
	}, widget);
232 233
}

B
Benjamin Pasero 已提交
234
export function attachButtonStyler(widget: IThemable, themeService: IThemeService, style?: { buttonForeground?: ColorIdentifier, buttonBackground?: ColorIdentifier, buttonHoverBackground?: ColorIdentifier }): IDisposable {
B
Benjamin Pasero 已提交
235
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
236 237
		buttonForeground: (style && style.buttonForeground) || buttonForeground,
		buttonBackground: (style && style.buttonBackground) || buttonBackground,
B
Benjamin Pasero 已提交
238 239
		buttonHoverBackground: (style && style.buttonHoverBackground) || buttonHoverBackground,
		buttonBorder: contrastBorder
240 241 242
	}, widget);
}

B
Benjamin Pasero 已提交
243
export function attachProgressBarStyler(widget: IThemable, themeService: IThemeService, style?: { progressBarBackground?: ColorIdentifier }): IDisposable {
B
Benjamin Pasero 已提交
244
	return attachStyler(themeService, {
B
Benjamin Pasero 已提交
245 246 247 248
		progressBarBackground: (style && style.progressBarBackground) || progressBarBackground
	}, widget);
}

249
export function attachStylerCallback(themeService: IThemeService, colors: { [name: string]: ColorIdentifier }, callback: styleFn): IDisposable {
B
Benjamin Pasero 已提交
250
	return attachStyler(themeService, colors, callback);
B
Benjamin Pasero 已提交
251
}