1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import { inject, injectable, named } from 'inversify';
|
18 | import { ContributionProvider } from '../common/contribution-provider';
|
19 | import { Theme, ThemeType } from '../common/theme';
|
20 | import { ColorRegistry } from './color-registry';
|
21 | import { DecorationStyle } from './decoration-style';
|
22 | import { FrontendApplicationContribution } from './frontend-application';
|
23 | import { ThemeService } from './theming';
|
24 |
|
25 | export const StylingParticipant = Symbol('StylingParticipant');
|
26 |
|
27 | export interface StylingParticipant {
|
28 | registerThemeStyle(theme: ColorTheme, collector: CssStyleCollector): void
|
29 | }
|
30 |
|
31 | export interface ColorTheme {
|
32 | type: ThemeType
|
33 | label: string
|
34 | getColor(color: string): string | undefined;
|
35 | }
|
36 |
|
37 | export interface CssStyleCollector {
|
38 | addRule(rule: string): void;
|
39 | }
|
40 |
|
41 | @injectable()
|
42 | export 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 | }
|