1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | var ThemeService_1;
|
27 | Object.defineProperty(exports, "__esModule", { value: true });
|
28 | exports.BuiltinThemeProvider = exports.ThemeService = void 0;
|
29 | const event_1 = require("../common/event");
|
30 | const disposable_1 = require("../common/disposable");
|
31 | const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
|
32 | const application_props_1 = require("@theia/application-package/lib/application-props");
|
33 | const inversify_1 = require("inversify");
|
34 | const promise_util_1 = require("../common/promise-util");
|
35 | const preferences_1 = require("./preferences");
|
36 | const debounce = require("lodash.debounce");
|
37 | const COLOR_THEME_PREFERENCE_KEY = 'workbench.colorTheme';
|
38 | const NO_THEME = { id: 'no-theme', label: 'Not a real theme.', type: 'dark' };
|
39 | let 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 |
|
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 |
|
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 |
|
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 |
|
153 |
|
154 | reset() {
|
155 | this.setCurrentTheme(this.defaultTheme.id);
|
156 | }
|
157 | };
|
158 | ThemeService.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);
|
173 | ThemeService = ThemeService_1 = __decorate([
|
174 | (0, inversify_1.injectable)()
|
175 | ], ThemeService);
|
176 | exports.ThemeService = ThemeService;
|
177 | class BuiltinThemeProvider {
|
178 | }
|
179 | exports.BuiltinThemeProvider = BuiltinThemeProvider;
|
180 | BuiltinThemeProvider.darkTheme = {
|
181 | id: 'dark',
|
182 | type: 'dark',
|
183 | label: 'Dark (Theia)',
|
184 | editorTheme: 'dark-theia'
|
185 | };
|
186 | BuiltinThemeProvider.lightTheme = {
|
187 | id: 'light',
|
188 | type: 'light',
|
189 | label: 'Light (Theia)',
|
190 | editorTheme: 'light-theia'
|
191 | };
|
192 | BuiltinThemeProvider.hcTheme = {
|
193 | id: 'hc-theia',
|
194 | type: 'hc',
|
195 | label: 'High Contrast (Theia)',
|
196 | editorTheme: 'hc-theia'
|
197 | };
|
198 | BuiltinThemeProvider.hcLightTheme = {
|
199 | id: 'hc-theia-light',
|
200 | type: 'hcLight',
|
201 | label: 'High Contrast Light (Theia)',
|
202 | editorTheme: 'hc-theia-light'
|
203 | };
|
204 | BuiltinThemeProvider.themes = [
|
205 | BuiltinThemeProvider.darkTheme,
|
206 | BuiltinThemeProvider.lightTheme,
|
207 | BuiltinThemeProvider.hcTheme,
|
208 | BuiltinThemeProvider.hcLightTheme
|
209 | ];
|
210 |
|
\ | No newline at end of file |