UNPKG

@sussudio/platform

Version:

Internal APIs for VS Code's service injection the base services.

154 lines (153 loc) 4.75 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { Codicon, CSSIcon } from '@sussudio/base/common/codicons.mjs'; import { Emitter } from '@sussudio/base/common/event.mjs'; import { Disposable, toDisposable } from '@sussudio/base/common/lifecycle.mjs'; import { createDecorator } from '../../instantiation/common/instantiation.mjs'; import * as platform from '../../registry/common/platform.mjs'; import { ColorScheme } from './theme.mjs'; export const IThemeService = createDecorator('themeService'); export var ThemeColor; (function (ThemeColor) { function isThemeColor(obj) { return obj && typeof obj === 'object' && typeof obj.id === 'string'; } ThemeColor.isThemeColor = isThemeColor; })(ThemeColor || (ThemeColor = {})); export function themeColorFromId(id) { return { id }; } export var ThemeIcon; (function (ThemeIcon) { function isThemeIcon(obj) { return ( obj && typeof obj === 'object' && typeof obj.id === 'string' && (typeof obj.color === 'undefined' || ThemeColor.isThemeColor(obj.color)) ); } ThemeIcon.isThemeIcon = isThemeIcon; const _regexFromString = new RegExp( `^\\$\\((${CSSIcon.iconNameExpression}(?:${CSSIcon.iconModifierExpression})?)\\)$`, ); function fromString(str) { const match = _regexFromString.exec(str); if (!match) { return undefined; } const [, name] = match; return { id: name }; } ThemeIcon.fromString = fromString; function fromId(id) { return { id }; } ThemeIcon.fromId = fromId; function modify(icon, modifier) { let id = icon.id; const tildeIndex = id.lastIndexOf('~'); if (tildeIndex !== -1) { id = id.substring(0, tildeIndex); } if (modifier) { id = `${id}~${modifier}`; } return { id }; } ThemeIcon.modify = modify; function getModifier(icon) { const tildeIndex = icon.id.lastIndexOf('~'); if (tildeIndex !== -1) { return icon.id.substring(tildeIndex + 1); } return undefined; } ThemeIcon.getModifier = getModifier; function isEqual(ti1, ti2) { return ti1.id === ti2.id && ti1.color?.id === ti2.color?.id; } ThemeIcon.isEqual = isEqual; function asThemeIcon(codicon, color) { return { id: codicon.id, color: color ? themeColorFromId(color) : undefined }; } ThemeIcon.asThemeIcon = asThemeIcon; ThemeIcon.asClassNameArray = CSSIcon.asClassNameArray; ThemeIcon.asClassName = CSSIcon.asClassName; ThemeIcon.asCSSSelector = CSSIcon.asCSSSelector; })(ThemeIcon || (ThemeIcon = {})); export const FileThemeIcon = Codicon.file; export const FolderThemeIcon = Codicon.folder; export function getThemeTypeSelector(type) { switch (type) { case ColorScheme.DARK: return 'vs-dark'; case ColorScheme.HIGH_CONTRAST_DARK: return 'hc-black'; case ColorScheme.HIGH_CONTRAST_LIGHT: return 'hc-light'; default: return 'vs'; } } // static theming participant export const Extensions = { ThemingContribution: 'base.contributions.theming', }; class ThemingRegistry { themingParticipants = []; onThemingParticipantAddedEmitter; constructor() { this.themingParticipants = []; this.onThemingParticipantAddedEmitter = 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); }); } get onThemingParticipantAdded() { return this.onThemingParticipantAddedEmitter.event; } getThemingParticipants() { return this.themingParticipants; } } const themingRegistry = new ThemingRegistry(); platform.Registry.add(Extensions.ThemingContribution, themingRegistry); export function registerThemingParticipant(participant) { return themingRegistry.onColorThemeChange(participant); } /** * Utility base class for all themable components. */ export class Themable extends Disposable { themeService; theme; 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 } getColor(id, modify) { let color = this.theme.getColor(id); if (color && modify) { color = modify(color, this.theme); } return color ? color.toString() : null; } }