styler.ts 10.9 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';
9
import { inputBackground, inputForeground, ColorIdentifier, selectForeground, selectBackground, selectBorder, inputBorder, foreground, editorBackground, contrastBorder, inputActiveOptionBorder, listFocusBackground, listActiveSelectionBackground, listActiveSelectionForeground, listFocusAndSelectionBackground, listFocusAndSelectionForeground, listInactiveSelectionBackground, listHoverBackground, listDropBackground, pickerGroupBorder, pickerGroupForeground, widgetShadow, infoBorder, infoBackground, warningBorder, warningBackground, errorBorder, errorBackground, activeContrastBorder, buttonForeground, buttonBackground, buttonHoverBackground } from 'vs/platform/theme/common/colorRegistry';
10 11
import { IDisposable } from 'vs/base/common/lifecycle';
import { SIDE_BAR_SECTION_HEADER_BACKGROUND } from 'vs/workbench/common/theme';
B
Benjamin Pasero 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

export interface IThemable {
	style(colors: { [name: string]: ColorIdentifier }): void;
}

export function attachStyler(themeService: IThemeService, widget: IThemable, optionsMapping: { [optionsKey: string]: ColorIdentifier }): IDisposable {
	function applyStyles(theme: ITheme): void {
		const styles = Object.create(null);
		for (let key in optionsMapping) {
			styles[key] = theme.getColor(optionsMapping[key]);
		}

		widget.style(styles);
	}

	applyStyles(themeService.getTheme());

	return themeService.onThemeChange(applyStyles);
}

B
Benjamin Pasero 已提交
32 33
export function attachCheckboxStyler(widget: IThemable, themeService: IThemeService, style?: { inputActiveOptionBorderColor?: ColorIdentifier }): IDisposable {
	return attachStyler(themeService, widget, {
34
		inputActiveOptionBorder: (style && style.inputActiveOptionBorderColor) || inputActiveOptionBorder
B
Benjamin Pasero 已提交
35 36 37
	});
}

38 39 40 41 42 43 44 45 46 47 48 49
export function attachInputBoxStyler(widget: IThemable, themeService: IThemeService, style?:
	{
		inputBackground?: ColorIdentifier,
		inputForeground?: ColorIdentifier,
		inputBorder?: ColorIdentifier,
		infoBorder?: ColorIdentifier,
		infoBackground?: ColorIdentifier,
		warningBorder?: ColorIdentifier,
		warningBackground?: ColorIdentifier,
		errorBorder?: ColorIdentifier,
		errorBackground?: ColorIdentifier
	}): IDisposable {
B
Benjamin Pasero 已提交
50 51
	return attachStyler(themeService, widget, {
		inputBackground: (style && style.inputBackground) || inputBackground,
B
Benjamin Pasero 已提交
52
		inputForeground: (style && style.inputForeground) || inputForeground,
53 54 55 56 57 58 59
		inputBorder: (style && style.inputBorder) || inputBorder,
		infoBorder: (style && style.infoBorder) || infoBorder,
		infoBackground: (style && style.infoBackground) || infoBackground,
		warningBorder: (style && style.warningBorder) || warningBorder,
		warningBackground: (style && style.warningBackground) || warningBackground,
		errorBorder: (style && style.errorBorder) || errorBorder,
		errorBackground: (style && style.errorBackground) || errorBackground
B
Benjamin Pasero 已提交
60
	});
61 62
}

B
Benjamin Pasero 已提交
63 64 65 66 67 68
export function attachSelectBoxStyler(widget: IThemable, themeService: IThemeService, style?: { selectBackground?: ColorIdentifier, selectForeground?: ColorIdentifier, selectBorder?: ColorIdentifier }): IDisposable {
	return attachStyler(themeService, widget, {
		selectBackground: (style && style.selectBackground) || selectBackground,
		selectForeground: (style && style.selectForeground) || selectForeground,
		selectBorder: (style && style.selectBorder) || selectBorder
	});
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83
export function attachFindInputBoxStyler(widget: IThemable, themeService: IThemeService, style?:
	{
		inputBackground?: ColorIdentifier,
		inputForeground?: ColorIdentifier,
		inputBorder?: ColorIdentifier,
		inputActiveOptionBorder?: ColorIdentifier,
		infoBorder?: ColorIdentifier,
		infoBackground?: ColorIdentifier,
		warningBorder?: ColorIdentifier,
		warningBackground?: ColorIdentifier,
		errorBorder?: ColorIdentifier,
		errorBackground?: ColorIdentifier
	}): IDisposable {
B
Benjamin Pasero 已提交
84 85 86 87
	return attachStyler(themeService, widget, {
		inputBackground: (style && style.inputBackground) || inputBackground,
		inputForeground: (style && style.inputForeground) || inputForeground,
		inputBorder: (style && style.inputBorder) || inputBorder,
88 89 90 91 92 93 94
		inputActiveOptionBorder: (style && style.inputActiveOptionBorder) || inputActiveOptionBorder,
		infoBorder: (style && style.infoBorder) || infoBorder,
		infoBackground: (style && style.infoBackground) || infoBackground,
		warningBorder: (style && style.warningBorder) || warningBorder,
		warningBackground: (style && style.warningBackground) || warningBackground,
		errorBorder: (style && style.errorBorder) || errorBorder,
		errorBackground: (style && style.errorBackground) || errorBackground
B
Benjamin Pasero 已提交
95 96 97
	});
}

B
Benjamin Pasero 已提交
98 99 100 101
export function attachQuickOpenStyler(widget: IThemable, themeService: IThemeService, style?: {
	foreground?: ColorIdentifier,
	background?: ColorIdentifier,
	borderColor?: ColorIdentifier,
102
	widgetShadow?: ColorIdentifier,
B
Benjamin Pasero 已提交
103 104 105
	inputBackground?: ColorIdentifier,
	inputForeground?: ColorIdentifier,
	inputBorder?: ColorIdentifier,
106 107 108 109 110 111
	infoBorder?: ColorIdentifier,
	infoBackground?: ColorIdentifier,
	warningBorder?: ColorIdentifier,
	warningBackground?: ColorIdentifier,
	errorBorder?: ColorIdentifier,
	errorBackground?: ColorIdentifier
112 113
	pickerGroupForeground?: ColorIdentifier,
	pickerGroupBorder?: ColorIdentifier,
B
Benjamin Pasero 已提交
114 115 116 117 118 119 120 121
	listFocusBackground?: ColorIdentifier,
	listActiveSelectionBackground?: ColorIdentifier,
	listActiveSelectionForeground?: ColorIdentifier,
	listFocusAndSelectionBackground?: ColorIdentifier,
	listFocusAndSelectionForeground?: ColorIdentifier,
	listInactiveSelectionBackground?: ColorIdentifier,
	listHoverBackground?: ColorIdentifier,
	listDropBackground?: ColorIdentifier,
122 123 124
	listFocusOutline?: ColorIdentifier,
	listSelectionOutline?: ColorIdentifier,
	listHoverOutline?: ColorIdentifier
B
Benjamin Pasero 已提交
125
}): IDisposable {
126 127 128
	return attachStyler(themeService, widget, {
		foreground: (style && style.foreground) || foreground,
		background: (style && style.background) || editorBackground,
129
		borderColor: style && style.borderColor || contrastBorder,
130
		widgetShadow: style && style.widgetShadow || widgetShadow,
131 132
		pickerGroupForeground: style && style.pickerGroupForeground || pickerGroupForeground,
		pickerGroupBorder: style && style.pickerGroupBorder || pickerGroupBorder,
133 134
		inputBackground: (style && style.inputBackground) || inputBackground,
		inputForeground: (style && style.inputForeground) || inputForeground,
B
Benjamin Pasero 已提交
135
		inputBorder: (style && style.inputBorder) || inputBorder,
136 137 138 139 140 141
		infoBorder: (style && style.infoBorder) || infoBorder,
		infoBackground: (style && style.infoBackground) || infoBackground,
		warningBorder: (style && style.warningBorder) || warningBorder,
		warningBackground: (style && style.warningBackground) || warningBackground,
		errorBorder: (style && style.errorBorder) || errorBorder,
		errorBackground: (style && style.errorBackground) || errorBackground,
B
Benjamin Pasero 已提交
142 143 144 145 146 147 148 149
		listFocusBackground: (style && style.listFocusBackground) || listFocusBackground,
		listActiveSelectionBackground: (style && style.listActiveSelectionBackground) || listActiveSelectionBackground,
		listActiveSelectionForeground: (style && style.listActiveSelectionForeground) || listActiveSelectionForeground,
		listFocusAndSelectionBackground: (style && style.listFocusAndSelectionBackground) || listFocusAndSelectionBackground,
		listFocusAndSelectionForeground: (style && style.listFocusAndSelectionForeground) || listFocusAndSelectionForeground,
		listInactiveSelectionBackground: (style && style.listInactiveSelectionBackground) || listInactiveSelectionBackground,
		listHoverBackground: (style && style.listHoverBackground) || listHoverBackground,
		listDropBackground: (style && style.listDropBackground) || listDropBackground,
150 151 152
		listFocusOutline: (style && style.listFocusOutline) || activeContrastBorder,
		listSelectionOutline: (style && style.listSelectionOutline) || activeContrastBorder,
		listHoverOutline: (style && style.listHoverOutline) || activeContrastBorder
B
Benjamin Pasero 已提交
153 154 155 156 157 158 159 160 161
	});
}

export function attachListStyler(widget: IThemable, themeService: IThemeService, style?: {
	listFocusBackground?: ColorIdentifier,
	listActiveSelectionBackground?: ColorIdentifier,
	listActiveSelectionForeground?: ColorIdentifier,
	listFocusAndSelectionBackground?: ColorIdentifier,
	listFocusAndSelectionForeground?: ColorIdentifier,
M
Martin Aeschlimann 已提交
162
	listInactiveFocusBackground?: ColorIdentifier,
B
Benjamin Pasero 已提交
163 164 165
	listInactiveSelectionBackground?: ColorIdentifier,
	listHoverBackground?: ColorIdentifier,
	listDropBackground?: ColorIdentifier,
166 167 168 169
	listFocusOutline?: ColorIdentifier,
	listInactiveFocusOutline?: ColorIdentifier,
	listSelectionOutline?: ColorIdentifier,
	listHoverOutline?: ColorIdentifier,
B
Benjamin Pasero 已提交
170 171 172 173 174 175 176
}): IDisposable {
	return attachStyler(themeService, widget, {
		listFocusBackground: (style && style.listFocusBackground) || listFocusBackground,
		listActiveSelectionBackground: (style && style.listActiveSelectionBackground) || listActiveSelectionBackground,
		listActiveSelectionForeground: (style && style.listActiveSelectionForeground) || listActiveSelectionForeground,
		listFocusAndSelectionBackground: (style && style.listFocusAndSelectionBackground) || listFocusAndSelectionBackground,
		listFocusAndSelectionForeground: (style && style.listFocusAndSelectionForeground) || listFocusAndSelectionForeground,
177
		listInactiveFocusBackground: (style && style.listInactiveFocusBackground),
B
Benjamin Pasero 已提交
178 179 180
		listInactiveSelectionBackground: (style && style.listInactiveSelectionBackground) || listInactiveSelectionBackground,
		listHoverBackground: (style && style.listHoverBackground) || listHoverBackground,
		listDropBackground: (style && style.listDropBackground) || listDropBackground,
181 182 183
		listFocusOutline: (style && style.listFocusOutline) || activeContrastBorder,
		listSelectionOutline: (style && style.listSelectionOutline) || activeContrastBorder,
		listHoverOutline: (style && style.listHoverOutline) || activeContrastBorder,
B
Benjamin Pasero 已提交
184
		listInactiveFocusOutline: style && style.listInactiveFocusOutline // not defined by default, only opt-in
185
	});
186 187
}

188
export function attachHeaderViewStyler(widget: IThemable, themeService: IThemeService, style?: { headerBackground?: ColorIdentifier, contrastBorder?: ColorIdentifier }): IDisposable {
189 190
	return attachStyler(themeService, widget, {
		headerBackground: (style && style.headerBackground) || SIDE_BAR_SECTION_HEADER_BACKGROUND,
191
		headerHighContrastBorder: (style && style.contrastBorder) || contrastBorder
192
	});
B
Benjamin Pasero 已提交
193 194 195 196 197 198 199 200
}

export function attachButtonStyler(widget: IThemable, themeService: IThemeService, style?: { buttonForeground?: ColorIdentifier, buttonBackground?: ColorIdentifier, buttonHoverBackground?: ColorIdentifier }): IDisposable {
	return attachStyler(themeService, widget, {
		buttonForeground: (style && style.buttonForeground) || buttonForeground,
		buttonBackground: (style && style.buttonBackground) || buttonBackground,
		buttonHoverBackground: (style && style.buttonHoverBackground) || buttonHoverBackground
	});
B
Benjamin Pasero 已提交
201
}