1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var ThemeService_1;
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.BuiltinThemeProvider = exports.ThemeService = void 0;
|
20 | const tslib_1 = require("tslib");
|
21 | const event_1 = require("../common/event");
|
22 | const disposable_1 = require("../common/disposable");
|
23 | const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
|
24 | const application_props_1 = require("@theia/application-package/lib/application-props");
|
25 | const inversify_1 = require("inversify");
|
26 | const promise_util_1 = require("../common/promise-util");
|
27 | const preferences_1 = require("./preferences");
|
28 | const debounce = require("lodash.debounce");
|
29 | const COLOR_THEME_PREFERENCE_KEY = 'workbench.colorTheme';
|
30 | const NO_THEME = { id: 'no-theme', label: 'Not a real theme.', type: 'dark' };
|
31 | let ThemeService = ThemeService_1 = class ThemeService {
|
32 | constructor() {
|
33 | this.themes = {};
|
34 | this.activeTheme = NO_THEME;
|
35 | this.themeChange = new event_1.Emitter();
|
36 | this.deferredInitializer = new promise_util_1.Deferred();
|
37 | this.onDidColorThemeChange = this.themeChange.event;
|
38 | this.updateColorThemePreference = debounce(() => this.doUpdateColorThemePreference(), 500);
|
39 | }
|
40 | get initialized() {
|
41 | return this.deferredInitializer.promise;
|
42 | }
|
43 | init() {
|
44 | this.register(...BuiltinThemeProvider.themes);
|
45 | this.loadUserTheme();
|
46 | this.preferences.ready.then(() => {
|
47 | this.validateActiveTheme();
|
48 | this.updateColorThemePreference();
|
49 | this.preferences.onPreferencesChanged(changes => {
|
50 | if (COLOR_THEME_PREFERENCE_KEY in changes) {
|
51 | this.validateActiveTheme();
|
52 | }
|
53 | });
|
54 | });
|
55 | }
|
56 | register(...themes) {
|
57 | for (const theme of themes) {
|
58 | this.themes[theme.id] = theme;
|
59 | }
|
60 | this.validateActiveTheme();
|
61 | this.updateColorThemePreference();
|
62 | return disposable_1.Disposable.create(() => {
|
63 | for (const theme of themes) {
|
64 | delete this.themes[theme.id];
|
65 | if (this.activeTheme === theme) {
|
66 | this.setCurrentTheme(this.defaultTheme.id, false);
|
67 | }
|
68 | }
|
69 | this.updateColorThemePreference();
|
70 | });
|
71 | }
|
72 | validateActiveTheme() {
|
73 | if (this.preferences.isReady) {
|
74 | const configuredTheme = this.getConfiguredTheme();
|
75 | if (configuredTheme && configuredTheme !== this.activeTheme) {
|
76 | this.setCurrentTheme(configuredTheme.id, false);
|
77 | }
|
78 | }
|
79 | }
|
80 | doUpdateColorThemePreference() {
|
81 | const preference = this.schemaProvider.getSchemaProperty(COLOR_THEME_PREFERENCE_KEY);
|
82 | if (preference) {
|
83 | const sortedThemes = this.getThemes().sort((a, b) => a.label.localeCompare(b.label));
|
84 | this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, {
|
85 | ...preference,
|
86 | enum: sortedThemes.map(e => e.id),
|
87 | enumItemLabels: sortedThemes.map(e => e.label)
|
88 | });
|
89 | }
|
90 | }
|
91 | getThemes() {
|
92 | const result = [];
|
93 | for (const o in this.themes) {
|
94 | if (this.themes.hasOwnProperty(o)) {
|
95 | result.push(this.themes[o]);
|
96 | }
|
97 | }
|
98 | return result;
|
99 | }
|
100 | getTheme(themeId) {
|
101 | return this.themes[themeId] || this.defaultTheme;
|
102 | }
|
103 | tryGetTheme(themeId) {
|
104 | return this.themes[themeId];
|
105 | }
|
106 |
|
107 | loadUserTheme() {
|
108 | var _a;
|
109 | const storedThemeId = (_a = window.localStorage.getItem(ThemeService_1.STORAGE_KEY)) !== null && _a !== void 0 ? _a : this.defaultTheme.id;
|
110 | const theme = this.getTheme(storedThemeId);
|
111 | this.setCurrentTheme(theme.id, false);
|
112 | this.deferredInitializer.resolve();
|
113 | }
|
114 | |
115 |
|
116 |
|
117 | setCurrentTheme(themeId, persist = true) {
|
118 | var _a, _b;
|
119 | const newTheme = this.tryGetTheme(themeId);
|
120 | const oldTheme = this.activeTheme;
|
121 | if (newTheme && newTheme !== oldTheme) {
|
122 | (_a = oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.deactivate) === null || _a === void 0 ? void 0 : _a.call(oldTheme);
|
123 | (_b = newTheme.activate) === null || _b === void 0 ? void 0 : _b.call(newTheme);
|
124 | this.activeTheme = newTheme;
|
125 | this.themeChange.fire({ newTheme, oldTheme });
|
126 | }
|
127 | if (persist) {
|
128 | this.preferences.updateValue(COLOR_THEME_PREFERENCE_KEY, themeId);
|
129 | }
|
130 | }
|
131 | getCurrentTheme() {
|
132 | return this.activeTheme;
|
133 | }
|
134 | getConfiguredTheme() {
|
135 | const configuredId = this.preferences.get(COLOR_THEME_PREFERENCE_KEY);
|
136 | return configuredId ? this.themes[configuredId.toString()] : undefined;
|
137 | }
|
138 | |
139 |
|
140 |
|
141 | get defaultTheme() {
|
142 | var _a;
|
143 | return (_a = this.tryGetTheme(application_props_1.DefaultTheme.defaultForOSTheme(frontend_application_config_provider_1.FrontendApplicationConfigProvider.get().defaultTheme))) !== null && _a !== void 0 ? _a : this.getTheme(application_props_1.DefaultTheme.defaultForOSTheme(application_props_1.ApplicationProps.DEFAULT.frontend.config.defaultTheme));
|
144 | }
|
145 | |
146 |
|
147 |
|
148 | reset() {
|
149 | this.setCurrentTheme(this.defaultTheme.id);
|
150 | }
|
151 | };
|
152 | ThemeService.STORAGE_KEY = 'theme';
|
153 | (0, tslib_1.__decorate)([
|
154 | (0, inversify_1.inject)(preferences_1.PreferenceService),
|
155 | (0, tslib_1.__metadata)("design:type", Object)
|
156 | ], ThemeService.prototype, "preferences", void 0);
|
157 | (0, tslib_1.__decorate)([
|
158 | (0, inversify_1.inject)(preferences_1.PreferenceSchemaProvider),
|
159 | (0, tslib_1.__metadata)("design:type", preferences_1.PreferenceSchemaProvider)
|
160 | ], ThemeService.prototype, "schemaProvider", void 0);
|
161 | (0, tslib_1.__decorate)([
|
162 | (0, inversify_1.postConstruct)(),
|
163 | (0, tslib_1.__metadata)("design:type", Function),
|
164 | (0, tslib_1.__metadata)("design:paramtypes", []),
|
165 | (0, tslib_1.__metadata)("design:returntype", void 0)
|
166 | ], ThemeService.prototype, "init", null);
|
167 | ThemeService = ThemeService_1 = (0, tslib_1.__decorate)([
|
168 | (0, inversify_1.injectable)()
|
169 | ], ThemeService);
|
170 | exports.ThemeService = ThemeService;
|
171 | class BuiltinThemeProvider {
|
172 | }
|
173 | exports.BuiltinThemeProvider = BuiltinThemeProvider;
|
174 | BuiltinThemeProvider.darkTheme = {
|
175 | id: 'dark',
|
176 | type: 'dark',
|
177 | label: 'Dark (Theia)',
|
178 | editorTheme: 'dark-theia'
|
179 | };
|
180 | BuiltinThemeProvider.lightTheme = {
|
181 | id: 'light',
|
182 | type: 'light',
|
183 | label: 'Light (Theia)',
|
184 | editorTheme: 'light-theia'
|
185 | };
|
186 | BuiltinThemeProvider.hcTheme = {
|
187 | id: 'hc-theia',
|
188 | type: 'hc',
|
189 | label: 'High Contrast (Theia)',
|
190 | editorTheme: 'hc-theia'
|
191 | };
|
192 | BuiltinThemeProvider.hcLightTheme = {
|
193 | id: 'hc-theia-light',
|
194 | type: 'hcLight',
|
195 | label: 'High Contrast Light (Theia)',
|
196 | editorTheme: 'hc-theia-light'
|
197 | };
|
198 | BuiltinThemeProvider.themes = [
|
199 | BuiltinThemeProvider.darkTheme,
|
200 | BuiltinThemeProvider.lightTheme,
|
201 | BuiltinThemeProvider.hcTheme,
|
202 | BuiltinThemeProvider.hcLightTheme
|
203 | ];
|
204 |
|
\ | No newline at end of file |