/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { IColorTheme, IThemeService, } from '../../../../vs/platform/theme/common/themeService'; import { ColorIdentifier, contrastBorder, listFocusBackground, listFocusForeground, listActiveSelectionBackground, listActiveSelectionForeground, listInactiveSelectionForeground, listInactiveSelectionBackground, listInactiveFocusBackground, listHoverBackground, listHoverForeground, listDropBackground, widgetShadow, activeContrastBorder, badgeBackground, badgeForeground, menuForeground, menuBackground, menuSelectionForeground, menuSelectionBackground, menuSelectionBorder, menuBorder, menuSeparatorBackground, listFilterWidgetOutline, listFilterWidgetNoMatchesOutline, listFilterWidgetBackground, treeIndentGuidesStroke, ColorValue, resolveColorValue, listFocusOutline, listInactiveFocusOutline, tableColumnsBorder, } from '../../../../vs/platform/theme/common/colorRegistry'; import { IDisposable } from '../../../../vs/base/common/lifecycle'; import { Color } from '../../../../vs/base/common/color'; import { IThemable, styleFn } from '../../../../vs/base/common/styler'; export interface IStyleOverrides { [color: string]: ColorIdentifier | undefined; } export interface IColorMapping { [optionsKey: string]: ColorValue | undefined; } export interface IComputedStyles { [color: string]: Color | undefined; } export function computeStyles( theme: IColorTheme, styleMap: IColorMapping ): IComputedStyles { const styles = Object.create(null) as IComputedStyles; for (let key in styleMap) { const value = styleMap[key]; if (value) { styles[key] = resolveColorValue(value, theme); } } return styles; } export function attachStyler( themeService: IThemeService, styleMap: T, widgetOrCallback: IThemable | styleFn ): IDisposable { function applyStyles(): void { const styles = computeStyles(themeService.getColorTheme(), styleMap); if (typeof widgetOrCallback === 'function') { widgetOrCallback(styles); } else { widgetOrCallback.style(styles); } } applyStyles(); return themeService.onDidColorThemeChange(applyStyles); } export interface IBadgeStyleOverrides extends IStyleOverrides { badgeBackground?: ColorIdentifier; badgeForeground?: ColorIdentifier; } export function attachBadgeStyler( widget: IThemable, themeService: IThemeService, style?: IBadgeStyleOverrides ): IDisposable { return attachStyler( themeService, { badgeBackground: style?.badgeBackground || badgeBackground, badgeForeground: style?.badgeForeground || badgeForeground, badgeBorder: contrastBorder, } as IBadgeStyleOverrides, widget ); } export function attachListStyler( widget: IThemable, themeService: IThemeService, overrides?: IColorMapping ): IDisposable { return attachStyler( themeService, { ...defaultListStyles, ...(overrides || {}) }, widget ); } export const defaultListStyles: IColorMapping = { listFocusBackground, listFocusForeground, listFocusOutline, listActiveSelectionBackground, listActiveSelectionForeground, listFocusAndSelectionBackground: listActiveSelectionBackground, listFocusAndSelectionForeground: listActiveSelectionForeground, listInactiveSelectionBackground, listInactiveSelectionForeground, listInactiveFocusBackground, listInactiveFocusOutline, listHoverBackground, listHoverForeground, listDropBackground, listSelectionOutline: activeContrastBorder, listHoverOutline: activeContrastBorder, listFilterWidgetBackground, listFilterWidgetOutline, listFilterWidgetNoMatchesOutline, listMatchesShadow: widgetShadow, treeIndentGuidesStroke, tableColumnsBorder, }; export interface IMenuStyleOverrides extends IColorMapping { shadowColor?: ColorIdentifier; borderColor?: ColorIdentifier; foregroundColor?: ColorIdentifier; backgroundColor?: ColorIdentifier; selectionForegroundColor?: ColorIdentifier; selectionBackgroundColor?: ColorIdentifier; selectionBorderColor?: ColorIdentifier; separatorColor?: ColorIdentifier; } export const defaultMenuStyles = { shadowColor: widgetShadow, borderColor: menuBorder, foregroundColor: menuForeground, backgroundColor: menuBackground, selectionForegroundColor: menuSelectionForeground, selectionBackgroundColor: menuSelectionBackground, selectionBorderColor: menuSelectionBorder, separatorColor: menuSeparatorBackground, }; export function attachMenuStyler( widget: IThemable, themeService: IThemeService, style?: IMenuStyleOverrides ): IDisposable { return attachStyler(themeService, { ...defaultMenuStyles, ...style }, widget); }