UNPKG

8.57 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17var ThemeService_1;
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.BuiltinThemeProvider = exports.ThemeService = void 0;
20const tslib_1 = require("tslib");
21const event_1 = require("../common/event");
22const disposable_1 = require("../common/disposable");
23const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
24const application_props_1 = require("@theia/application-package/lib/application-props");
25const inversify_1 = require("inversify");
26const promise_util_1 = require("../common/promise-util");
27const preferences_1 = require("./preferences");
28const debounce = require("lodash.debounce");
29const COLOR_THEME_PREFERENCE_KEY = 'workbench.colorTheme';
30const NO_THEME = { id: 'no-theme', label: 'Not a real theme.', type: 'dark' };
31let 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 /** Should only be called at startup. */
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 * @param persist If `true`, the value of the `workbench.colorTheme` preference will be set to the provided ID.
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 * The default theme. If that is not applicable, returns with the fallback theme.
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 * Resets the state to the user's default, or to the fallback theme. Also discards any persisted state in the local storage.
147 */
148 reset() {
149 this.setCurrentTheme(this.defaultTheme.id);
150 }
151};
152ThemeService.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);
167ThemeService = ThemeService_1 = (0, tslib_1.__decorate)([
168 (0, inversify_1.injectable)()
169], ThemeService);
170exports.ThemeService = ThemeService;
171class BuiltinThemeProvider {
172}
173exports.BuiltinThemeProvider = BuiltinThemeProvider;
174BuiltinThemeProvider.darkTheme = {
175 id: 'dark',
176 type: 'dark',
177 label: 'Dark (Theia)',
178 editorTheme: 'dark-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
179};
180BuiltinThemeProvider.lightTheme = {
181 id: 'light',
182 type: 'light',
183 label: 'Light (Theia)',
184 editorTheme: 'light-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
185};
186BuiltinThemeProvider.hcTheme = {
187 id: 'hc-theia',
188 type: 'hc',
189 label: 'High Contrast (Theia)',
190 editorTheme: 'hc-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
191};
192BuiltinThemeProvider.hcLightTheme = {
193 id: 'hc-theia-light',
194 type: 'hcLight',
195 label: 'High Contrast Light (Theia)',
196 editorTheme: 'hc-theia-light' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
197};
198BuiltinThemeProvider.themes = [
199 BuiltinThemeProvider.darkTheme,
200 BuiltinThemeProvider.lightTheme,
201 BuiltinThemeProvider.hcTheme,
202 BuiltinThemeProvider.hcLightTheme
203];
204//# sourceMappingURL=theming.js.map
\No newline at end of file