UNPKG

7.87 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 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// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.IconThemeService = exports.NoneIconTheme = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const event_1 = require("../common/event");
22const disposable_1 = require("../common/disposable");
23const frontend_application_config_provider_1 = require("./frontend-application-config-provider");
24const preferences_1 = require("./preferences");
25const debounce = require("lodash.debounce");
26const ICON_THEME_PREFERENCE_KEY = 'workbench.iconTheme';
27let NoneIconTheme = class NoneIconTheme {
28 constructor() {
29 this.id = 'none';
30 this.label = 'None';
31 this.description = 'Disable file icons';
32 this.hasFileIcons = true;
33 this.hasFolderIcons = true;
34 this.onDidChangeEmitter = new event_1.Emitter();
35 this.onDidChange = this.onDidChangeEmitter.event;
36 this.toDeactivate = new disposable_1.DisposableCollection();
37 }
38 activate() {
39 if (this.toDeactivate.disposed) {
40 this.toDeactivate.push(disposable_1.Disposable.create(() => this.fireDidChange()));
41 this.fireDidChange();
42 }
43 return this.toDeactivate;
44 }
45 fireDidChange() {
46 this.onDidChangeEmitter.fire({ affects: () => true });
47 }
48 canHandle() {
49 if (this.toDeactivate.disposed) {
50 return 0;
51 }
52 return Number.MAX_SAFE_INTEGER - 1024;
53 }
54 getIcon() {
55 return '';
56 }
57};
58NoneIconTheme = (0, tslib_1.__decorate)([
59 (0, inversify_1.injectable)()
60], NoneIconTheme);
61exports.NoneIconTheme = NoneIconTheme;
62let IconThemeService = class IconThemeService {
63 constructor() {
64 this.onDidChangeEmitter = new event_1.Emitter();
65 this.onDidChange = this.onDidChangeEmitter.event;
66 this._iconThemes = new Map();
67 this.onDidChangeCurrentEmitter = new event_1.Emitter();
68 this.onDidChangeCurrent = this.onDidChangeCurrentEmitter.event;
69 this.toDeactivate = new disposable_1.DisposableCollection();
70 this.updateIconThemePreference = debounce(() => this.doUpdateIconThemePreference(), 500);
71 }
72 get ids() {
73 return this._iconThemes.keys();
74 }
75 get definitions() {
76 return this._iconThemes.values();
77 }
78 getDefinition(id) {
79 return this._iconThemes.get(id);
80 }
81 init() {
82 this.register(this.fallback);
83 this.setCurrent(this.fallback, false);
84 this.preferences.ready.then(() => {
85 this.validateActiveTheme();
86 this.updateIconThemePreference();
87 this.preferences.onPreferencesChanged(changes => {
88 if (ICON_THEME_PREFERENCE_KEY in changes) {
89 this.validateActiveTheme();
90 }
91 });
92 });
93 }
94 register(iconTheme) {
95 if (this._iconThemes.has(iconTheme.id)) {
96 console.warn(new Error(`Icon theme '${iconTheme.id}' has already been registered, skipping.`));
97 return disposable_1.Disposable.NULL;
98 }
99 this._iconThemes.set(iconTheme.id, iconTheme);
100 this.onDidChangeEmitter.fire(undefined);
101 this.validateActiveTheme();
102 this.updateIconThemePreference();
103 return disposable_1.Disposable.create(() => {
104 this.unregister(iconTheme.id);
105 this.updateIconThemePreference();
106 });
107 }
108 unregister(id) {
109 const iconTheme = this._iconThemes.get(id);
110 if (!iconTheme) {
111 return undefined;
112 }
113 this._iconThemes.delete(id);
114 this.onDidChangeEmitter.fire(undefined);
115 if (id === this.getCurrent().id) {
116 this.setCurrent(this.default, false);
117 }
118 return iconTheme;
119 }
120 get current() {
121 return this.getCurrent().id;
122 }
123 set current(id) {
124 const newCurrent = this._iconThemes.get(id);
125 if (newCurrent && this.getCurrent().id !== newCurrent.id) {
126 this.setCurrent(newCurrent);
127 }
128 }
129 getCurrent() {
130 return this.activeTheme;
131 }
132 /**
133 * @param persistSetting If `true`, the theme's id will be set as the value of the `workbench.iconTheme` preference. (default: `true`)
134 */
135 setCurrent(newCurrent, persistSetting = true) {
136 if (newCurrent !== this.getCurrent()) {
137 this.activeTheme = newCurrent;
138 this.toDeactivate.dispose();
139 this.toDeactivate.push(newCurrent.activate());
140 this.onDidChangeCurrentEmitter.fire(newCurrent.id);
141 }
142 if (persistSetting) {
143 this.preferences.updateValue(ICON_THEME_PREFERENCE_KEY, newCurrent.id);
144 }
145 }
146 getConfiguredTheme() {
147 const configuredId = this.preferences.get(ICON_THEME_PREFERENCE_KEY);
148 return configuredId ? this._iconThemes.get(configuredId) : undefined;
149 }
150 validateActiveTheme() {
151 if (this.preferences.isReady) {
152 const configured = this.getConfiguredTheme();
153 if (configured && configured !== this.getCurrent()) {
154 this.setCurrent(configured, false);
155 }
156 }
157 }
158 doUpdateIconThemePreference() {
159 const preference = this.schemaProvider.getSchemaProperty(ICON_THEME_PREFERENCE_KEY);
160 if (preference) {
161 const sortedThemes = Array.from(this.definitions).sort((a, b) => a.label.localeCompare(b.label));
162 this.schemaProvider.updateSchemaProperty(ICON_THEME_PREFERENCE_KEY, {
163 ...preference,
164 enum: sortedThemes.map(e => e.id),
165 enumItemLabels: sortedThemes.map(e => e.label)
166 });
167 }
168 }
169 get default() {
170 return this._iconThemes.get(frontend_application_config_provider_1.FrontendApplicationConfigProvider.get().defaultIconTheme) || this.fallback;
171 }
172 get fallback() {
173 return this.noneIconTheme;
174 }
175};
176IconThemeService.STORAGE_KEY = 'iconTheme';
177(0, tslib_1.__decorate)([
178 (0, inversify_1.inject)(NoneIconTheme),
179 (0, tslib_1.__metadata)("design:type", NoneIconTheme)
180], IconThemeService.prototype, "noneIconTheme", void 0);
181(0, tslib_1.__decorate)([
182 (0, inversify_1.inject)(preferences_1.PreferenceService),
183 (0, tslib_1.__metadata)("design:type", Object)
184], IconThemeService.prototype, "preferences", void 0);
185(0, tslib_1.__decorate)([
186 (0, inversify_1.inject)(preferences_1.PreferenceSchemaProvider),
187 (0, tslib_1.__metadata)("design:type", preferences_1.PreferenceSchemaProvider)
188], IconThemeService.prototype, "schemaProvider", void 0);
189(0, tslib_1.__decorate)([
190 (0, inversify_1.postConstruct)(),
191 (0, tslib_1.__metadata)("design:type", Function),
192 (0, tslib_1.__metadata)("design:paramtypes", []),
193 (0, tslib_1.__metadata)("design:returntype", void 0)
194], IconThemeService.prototype, "init", null);
195IconThemeService = (0, tslib_1.__decorate)([
196 (0, inversify_1.injectable)()
197], IconThemeService);
198exports.IconThemeService = IconThemeService;
199//# sourceMappingURL=icon-theme-service.js.map
\No newline at end of file