UNPKG

9.17 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26var ThemeService_1;
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.BuiltinThemeProvider = exports.ThemeService = void 0;
29const event_1 = require("../common/event");
30const disposable_1 = require("../common/disposable");
31const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
32const application_props_1 = require("@theia/application-package/lib/application-props");
33const inversify_1 = require("inversify");
34const promise_util_1 = require("../common/promise-util");
35const preferences_1 = require("./preferences");
36const debounce = require("lodash.debounce");
37const COLOR_THEME_PREFERENCE_KEY = 'workbench.colorTheme';
38const NO_THEME = { id: 'no-theme', label: 'Not a real theme.', type: 'dark' };
39let ThemeService = ThemeService_1 = class ThemeService {
40 constructor() {
41 this.themes = {};
42 this.activeTheme = NO_THEME;
43 this.themeChange = new event_1.Emitter();
44 this.deferredInitializer = new promise_util_1.Deferred();
45 this.onDidColorThemeChange = this.themeChange.event;
46 this.updateColorThemePreference = debounce(() => this.doUpdateColorThemePreference(), 500);
47 }
48 get initialized() {
49 return this.deferredInitializer.promise;
50 }
51 init() {
52 this.register(...BuiltinThemeProvider.themes);
53 this.loadUserTheme();
54 this.preferences.ready.then(() => {
55 this.validateActiveTheme();
56 this.updateColorThemePreference();
57 this.preferences.onPreferencesChanged(changes => {
58 if (COLOR_THEME_PREFERENCE_KEY in changes) {
59 this.validateActiveTheme();
60 }
61 });
62 });
63 }
64 register(...themes) {
65 for (const theme of themes) {
66 this.themes[theme.id] = theme;
67 }
68 this.validateActiveTheme();
69 this.updateColorThemePreference();
70 return disposable_1.Disposable.create(() => {
71 for (const theme of themes) {
72 delete this.themes[theme.id];
73 if (this.activeTheme === theme) {
74 this.setCurrentTheme(this.defaultTheme.id, false);
75 }
76 }
77 this.updateColorThemePreference();
78 });
79 }
80 validateActiveTheme() {
81 if (this.preferences.isReady) {
82 const configuredTheme = this.getConfiguredTheme();
83 if (configuredTheme && configuredTheme !== this.activeTheme) {
84 this.setCurrentTheme(configuredTheme.id, false);
85 }
86 }
87 }
88 doUpdateColorThemePreference() {
89 const preference = this.schemaProvider.getSchemaProperty(COLOR_THEME_PREFERENCE_KEY);
90 if (preference) {
91 const sortedThemes = this.getThemes().sort((a, b) => a.label.localeCompare(b.label));
92 this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, {
93 ...preference,
94 enum: sortedThemes.map(e => e.id),
95 enumItemLabels: sortedThemes.map(e => e.label)
96 });
97 }
98 }
99 getThemes() {
100 const result = [];
101 for (const o in this.themes) {
102 if (this.themes.hasOwnProperty(o)) {
103 result.push(this.themes[o]);
104 }
105 }
106 return result;
107 }
108 getTheme(themeId) {
109 return this.themes[themeId] || this.defaultTheme;
110 }
111 tryGetTheme(themeId) {
112 return this.themes[themeId];
113 }
114 /** Should only be called at startup. */
115 loadUserTheme() {
116 var _a;
117 const storedThemeId = (_a = window.localStorage.getItem(ThemeService_1.STORAGE_KEY)) !== null && _a !== void 0 ? _a : this.defaultTheme.id;
118 const theme = this.getTheme(storedThemeId);
119 this.setCurrentTheme(theme.id, false);
120 this.deferredInitializer.resolve();
121 }
122 /**
123 * @param persist If `true`, the value of the `workbench.colorTheme` preference will be set to the provided ID.
124 */
125 setCurrentTheme(themeId, persist = true) {
126 var _a, _b;
127 const newTheme = this.tryGetTheme(themeId);
128 const oldTheme = this.activeTheme;
129 if (newTheme && newTheme !== oldTheme) {
130 (_a = oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.deactivate) === null || _a === void 0 ? void 0 : _a.call(oldTheme);
131 (_b = newTheme.activate) === null || _b === void 0 ? void 0 : _b.call(newTheme);
132 this.activeTheme = newTheme;
133 this.themeChange.fire({ newTheme, oldTheme });
134 }
135 if (persist) {
136 this.preferences.updateValue(COLOR_THEME_PREFERENCE_KEY, themeId);
137 }
138 }
139 getCurrentTheme() {
140 return this.activeTheme;
141 }
142 getConfiguredTheme() {
143 const configuredId = this.preferences.get(COLOR_THEME_PREFERENCE_KEY);
144 return configuredId ? this.themes[configuredId.toString()] : undefined;
145 }
146 /**
147 * The default theme. If that is not applicable, returns with the fallback theme.
148 */
149 get defaultTheme() {
150 var _a;
151 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));
152 }
153 /**
154 * Resets the state to the user's default, or to the fallback theme. Also discards any persisted state in the local storage.
155 */
156 reset() {
157 this.setCurrentTheme(this.defaultTheme.id);
158 }
159};
160ThemeService.STORAGE_KEY = 'theme';
161__decorate([
162 (0, inversify_1.inject)(preferences_1.PreferenceService),
163 __metadata("design:type", Object)
164], ThemeService.prototype, "preferences", void 0);
165__decorate([
166 (0, inversify_1.inject)(preferences_1.PreferenceSchemaProvider),
167 __metadata("design:type", preferences_1.PreferenceSchemaProvider)
168], ThemeService.prototype, "schemaProvider", void 0);
169__decorate([
170 (0, inversify_1.postConstruct)(),
171 __metadata("design:type", Function),
172 __metadata("design:paramtypes", []),
173 __metadata("design:returntype", void 0)
174], ThemeService.prototype, "init", null);
175ThemeService = ThemeService_1 = __decorate([
176 (0, inversify_1.injectable)()
177], ThemeService);
178exports.ThemeService = ThemeService;
179class BuiltinThemeProvider {
180}
181exports.BuiltinThemeProvider = BuiltinThemeProvider;
182BuiltinThemeProvider.darkTheme = {
183 id: 'dark',
184 type: 'dark',
185 label: 'Dark (Theia)',
186 editorTheme: 'dark-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
187};
188BuiltinThemeProvider.lightTheme = {
189 id: 'light',
190 type: 'light',
191 label: 'Light (Theia)',
192 editorTheme: 'light-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
193};
194BuiltinThemeProvider.hcTheme = {
195 id: 'hc-theia',
196 type: 'hc',
197 label: 'High Contrast (Theia)',
198 editorTheme: 'hc-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
199};
200BuiltinThemeProvider.hcLightTheme = {
201 id: 'hc-theia-light',
202 type: 'hcLight',
203 label: 'High Contrast Light (Theia)',
204 editorTheme: 'hc-theia-light' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
205};
206BuiltinThemeProvider.themes = [
207 BuiltinThemeProvider.darkTheme,
208 BuiltinThemeProvider.lightTheme,
209 BuiltinThemeProvider.hcTheme,
210 BuiltinThemeProvider.hcLightTheme
211];
212//# sourceMappingURL=theming.js.map
\No newline at end of file