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 | 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 |
|
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 |
|
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 |
|
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 |
|
155 |
|
156 | reset() {
|
157 | this.setCurrentTheme(this.defaultTheme.id);
|
158 | }
|
159 | };
|
160 | ThemeService.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);
|
175 | ThemeService = ThemeService_1 = __decorate([
|
176 | (0, inversify_1.injectable)()
|
177 | ], ThemeService);
|
178 | exports.ThemeService = ThemeService;
|
179 | class BuiltinThemeProvider {
|
180 | }
|
181 | exports.BuiltinThemeProvider = BuiltinThemeProvider;
|
182 | BuiltinThemeProvider.darkTheme = {
|
183 | id: 'dark',
|
184 | type: 'dark',
|
185 | label: 'Dark (Theia)',
|
186 | editorTheme: 'dark-theia'
|
187 | };
|
188 | BuiltinThemeProvider.lightTheme = {
|
189 | id: 'light',
|
190 | type: 'light',
|
191 | label: 'Light (Theia)',
|
192 | editorTheme: 'light-theia'
|
193 | };
|
194 | BuiltinThemeProvider.hcTheme = {
|
195 | id: 'hc-theia',
|
196 | type: 'hc',
|
197 | label: 'High Contrast (Theia)',
|
198 | editorTheme: 'hc-theia'
|
199 | };
|
200 | BuiltinThemeProvider.hcLightTheme = {
|
201 | id: 'hc-theia-light',
|
202 | type: 'hcLight',
|
203 | label: 'High Contrast Light (Theia)',
|
204 | editorTheme: 'hc-theia-light'
|
205 | };
|
206 | BuiltinThemeProvider.themes = [
|
207 | BuiltinThemeProvider.darkTheme,
|
208 | BuiltinThemeProvider.lightTheme,
|
209 | BuiltinThemeProvider.hcTheme,
|
210 | BuiltinThemeProvider.hcLightTheme
|
211 | ];
|
212 |
|
\ | No newline at end of file |