UNPKG

2.92 kBPlain TextView Raw
1// *****************************************************************************
2// Copyright (C) 2022 TypeFox and others.
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License v. 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0.
7//
8// This Source Code may also be made available under the following Secondary
9// Licenses when the conditions for such availability set forth in the Eclipse
10// Public License v. 2.0 are satisfied: GNU General Public License, version 2
11// with the GNU Classpath Exception which is available at
12// https://www.gnu.org/software/classpath/license.html.
13//
14// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15// *****************************************************************************
16
17import { inject, injectable, named } from 'inversify';
18import { ContributionProvider } from '../common/contribution-provider';
19import { Theme, ThemeType } from '../common/theme';
20import { ColorRegistry } from './color-registry';
21import { DecorationStyle } from './decoration-style';
22import { FrontendApplicationContribution } from './frontend-application';
23import { ThemeService } from './theming';
24
25export const StylingParticipant = Symbol('StylingParticipant');
26
27export interface StylingParticipant {
28 registerThemeStyle(theme: ColorTheme, collector: CssStyleCollector): void
29}
30
31export interface ColorTheme {
32 type: ThemeType
33 label: string
34 getColor(color: string): string | undefined;
35}
36
37export interface CssStyleCollector {
38 addRule(rule: string): void;
39}
40
41@injectable()
42export class StylingService implements FrontendApplicationContribution {
43
44 protected cssElement = DecorationStyle.createStyleElement('contributedColorTheme');
45
46 @inject(ThemeService)
47 protected readonly themeService: ThemeService;
48
49 @inject(ColorRegistry)
50 protected readonly colorRegistry: ColorRegistry;
51
52 @inject(ContributionProvider) @named(StylingParticipant)
53 protected readonly themingParticipants: ContributionProvider<StylingParticipant>;
54
55 onStart(): void {
56 this.applyStyling(this.themeService.getCurrentTheme());
57 this.themeService.onDidColorThemeChange(e => this.applyStyling(e.newTheme));
58 }
59
60 protected applyStyling(theme: Theme): void {
61 const rules: string[] = [];
62 const colorTheme: ColorTheme = {
63 type: theme.type,
64 label: theme.label,
65 getColor: color => this.colorRegistry.getCurrentColor(color)
66 };
67 const styleCollector: CssStyleCollector = {
68 addRule: rule => rules.push(rule)
69 };
70 for (const themingParticipant of this.themingParticipants.getContributions()) {
71 themingParticipant.registerThemeStyle(colorTheme, styleCollector);
72 }
73 const fullCss = rules.join('\n');
74 this.cssElement.innerText = fullCss;
75 }
76}