UNPKG

monaco-editor

Version:
68 lines (65 loc) 2.42 kB
import { Emitter } from '../../../base/common/event.js'; import { Disposable, toDisposable } from '../../../base/common/lifecycle.js'; import { createDecorator } from '../../instantiation/common/instantiation.js'; import { Registry } from '../../registry/common/platform.js'; import { ThemeTypeSelector, ColorScheme } from './theme.js'; const IThemeService = createDecorator('themeService'); function themeColorFromId(id) { return { id }; } function getThemeTypeSelector(type) { switch (type) { case ColorScheme.DARK: return ThemeTypeSelector.VS_DARK; case ColorScheme.HIGH_CONTRAST_DARK: return ThemeTypeSelector.HC_BLACK; case ColorScheme.HIGH_CONTRAST_LIGHT: return ThemeTypeSelector.HC_LIGHT; default: return ThemeTypeSelector.VS; } } // static theming participant const Extensions = { ThemingContribution: 'base.contributions.theming' }; class ThemingRegistry extends Disposable { constructor() { super(); this.themingParticipants = []; this.themingParticipants = []; this.onThemingParticipantAddedEmitter = this._register(new Emitter()); } onColorThemeChange(participant) { this.themingParticipants.push(participant); this.onThemingParticipantAddedEmitter.fire(participant); return toDisposable(() => { const idx = this.themingParticipants.indexOf(participant); this.themingParticipants.splice(idx, 1); }); } getThemingParticipants() { return this.themingParticipants; } } const themingRegistry = new ThemingRegistry(); Registry.add(Extensions.ThemingContribution, themingRegistry); function registerThemingParticipant(participant) { return themingRegistry.onColorThemeChange(participant); } /** * Utility base class for all themable components. */ class Themable extends Disposable { constructor(themeService) { super(); this.themeService = themeService; this.theme = themeService.getColorTheme(); // Hook up to theme changes this._register(this.themeService.onDidColorThemeChange(theme => this.onThemeChange(theme))); } onThemeChange(theme) { this.theme = theme; this.updateStyles(); } updateStyles() { // Subclasses to override } } export { Extensions, IThemeService, Themable, getThemeTypeSelector, registerThemingParticipant, themeColorFromId };