UNPKG

9.14 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 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 preference.enum = sortedThemes.map(e => e.id);
93 preference.enumItemLabels = sortedThemes.map(e => e.label);
94 this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, preference);
95 }
96 }
97 getThemes() {
98 const result = [];
99 for (const o in this.themes) {
100 if (this.themes.hasOwnProperty(o)) {
101 result.push(this.themes[o]);
102 }
103 }
104 return result;
105 }
106 getTheme(themeId) {
107 return this.themes[themeId] || this.defaultTheme;
108 }
109 tryGetTheme(themeId) {
110 return this.themes[themeId];
111 }
112 /** Should only be called at startup. */
113 loadUserTheme() {
114 var _a;
115 const storedThemeId = (_a = window.localStorage.getItem(ThemeService_1.STORAGE_KEY)) !== null && _a !== void 0 ? _a : this.defaultTheme.id;
116 const theme = this.getTheme(storedThemeId);
117 this.setCurrentTheme(theme.id, false);
118 this.deferredInitializer.resolve();
119 }
120 /**
121 * @param persist If `true`, the value of the `workbench.colorTheme` preference will be set to the provided ID.
122 */
123 setCurrentTheme(themeId, persist = true) {
124 var _a, _b;
125 const newTheme = this.tryGetTheme(themeId);
126 const oldTheme = this.activeTheme;
127 if (newTheme && newTheme !== oldTheme) {
128 (_a = oldTheme === null || oldTheme === void 0 ? void 0 : oldTheme.deactivate) === null || _a === void 0 ? void 0 : _a.call(oldTheme);
129 (_b = newTheme.activate) === null || _b === void 0 ? void 0 : _b.call(newTheme);
130 this.activeTheme = newTheme;
131 this.themeChange.fire({ newTheme, oldTheme });
132 }
133 if (persist) {
134 this.preferences.updateValue(COLOR_THEME_PREFERENCE_KEY, themeId);
135 }
136 }
137 getCurrentTheme() {
138 return this.activeTheme;
139 }
140 getConfiguredTheme() {
141 const configuredId = this.preferences.get(COLOR_THEME_PREFERENCE_KEY);
142 return configuredId ? this.themes[configuredId.toString()] : undefined;
143 }
144 /**
145 * The default theme. If that is not applicable, returns with the fallback theme.
146 */
147 get defaultTheme() {
148 var _a;
149 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));
150 }
151 /**
152 * Resets the state to the user's default, or to the fallback theme. Also discards any persisted state in the local storage.
153 */
154 reset() {
155 this.setCurrentTheme(this.defaultTheme.id);
156 }
157};
158ThemeService.STORAGE_KEY = 'theme';
159__decorate([
160 (0, inversify_1.inject)(preferences_1.PreferenceService),
161 __metadata("design:type", Object)
162], ThemeService.prototype, "preferences", void 0);
163__decorate([
164 (0, inversify_1.inject)(preferences_1.PreferenceSchemaProvider),
165 __metadata("design:type", preferences_1.PreferenceSchemaProvider)
166], ThemeService.prototype, "schemaProvider", void 0);
167__decorate([
168 (0, inversify_1.postConstruct)(),
169 __metadata("design:type", Function),
170 __metadata("design:paramtypes", []),
171 __metadata("design:returntype", void 0)
172], ThemeService.prototype, "init", null);
173ThemeService = ThemeService_1 = __decorate([
174 (0, inversify_1.injectable)()
175], ThemeService);
176exports.ThemeService = ThemeService;
177class BuiltinThemeProvider {
178}
179exports.BuiltinThemeProvider = BuiltinThemeProvider;
180BuiltinThemeProvider.darkTheme = {
181 id: 'dark',
182 type: 'dark',
183 label: 'Dark (Theia)',
184 editorTheme: 'dark-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
185};
186BuiltinThemeProvider.lightTheme = {
187 id: 'light',
188 type: 'light',
189 label: 'Light (Theia)',
190 editorTheme: 'light-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
191};
192BuiltinThemeProvider.hcTheme = {
193 id: 'hc-theia',
194 type: 'hc',
195 label: 'High Contrast (Theia)',
196 editorTheme: 'hc-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
197};
198BuiltinThemeProvider.hcLightTheme = {
199 id: 'hc-theia-light',
200 type: 'hcLight',
201 label: 'High Contrast Light (Theia)',
202 editorTheme: 'hc-theia-light' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
203};
204BuiltinThemeProvider.themes = [
205 BuiltinThemeProvider.darkTheme,
206 BuiltinThemeProvider.lightTheme,
207 BuiltinThemeProvider.hcTheme,
208 BuiltinThemeProvider.hcLightTheme
209];
210//# sourceMappingURL=theming.js.map
\No newline at end of file