@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
196 lines • 7.4 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2019 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { injectable, inject, postConstruct, named } from 'inversify';
import { Emitter } from '../common/event.js';
import { Disposable, DisposableCollection } from '../common/disposable.js';
import { FrontendApplicationConfigProvider } from './frontend-application-config-provider.js';
import debounce from '../../shared/lodash.debounce/index.cjs';
import { PreferenceSchemaService } from '../common/preferences/preference-schema.js';
import { PreferenceService } from '../common/preferences/index.js';
import { ILogger } from '../common/index.js';
const ICON_THEME_PREFERENCE_KEY = 'workbench.iconTheme';
let NoneIconTheme = class NoneIconTheme {
constructor() {
this.id = 'none';
this.label = 'None';
this.description = 'Disable file icons';
this.hasFileIcons = true;
this.hasFolderIcons = true;
this.onDidChangeEmitter = new Emitter();
this.onDidChange = this.onDidChangeEmitter.event;
this.toDeactivate = new DisposableCollection();
}
activate() {
if (this.toDeactivate.disposed) {
this.toDeactivate.push(Disposable.create(() => this.fireDidChange()));
this.fireDidChange();
}
return this.toDeactivate;
}
fireDidChange() {
this.onDidChangeEmitter.fire({ affects: () => true });
}
canHandle() {
if (this.toDeactivate.disposed) {
return 0;
}
return Number.MAX_SAFE_INTEGER - 1024;
}
getIcon() {
return '';
}
};
NoneIconTheme = __decorate([
injectable()
], NoneIconTheme);
export { NoneIconTheme };
let IconThemeService = class IconThemeService {
constructor() {
this.onDidChangeEmitter = new Emitter();
this.onDidChange = this.onDidChangeEmitter.event;
this._iconThemes = new Map();
this.onDidChangeCurrentEmitter = new Emitter();
this.onDidChangeCurrent = this.onDidChangeCurrentEmitter.event;
this.toDeactivate = new DisposableCollection();
this.updateIconThemePreference = debounce(() => this.doUpdateIconThemePreference(), 500);
}
static { this.STORAGE_KEY = 'iconTheme'; }
get ids() {
return this._iconThemes.keys();
}
get definitions() {
return this._iconThemes.values();
}
getDefinition(id) {
return this._iconThemes.get(id);
}
init() {
this.register(this.fallback);
this.setCurrent(this.fallback, false);
this.preferences.ready.then(() => {
this.validateActiveTheme();
this.updateIconThemePreference();
this.preferences.onPreferencesChanged(changes => {
if (changes.some(change => change.preferenceName === ICON_THEME_PREFERENCE_KEY)) {
this.validateActiveTheme();
}
});
});
}
register(iconTheme) {
if (this._iconThemes.has(iconTheme.id)) {
this.logger.warn(new Error(`Icon theme '${iconTheme.id}' has already been registered, skipping.`));
return Disposable.NULL;
}
this._iconThemes.set(iconTheme.id, iconTheme);
this.onDidChangeEmitter.fire(undefined);
this.validateActiveTheme();
this.updateIconThemePreference();
return Disposable.create(() => {
this.unregister(iconTheme.id);
this.updateIconThemePreference();
});
}
unregister(id) {
const iconTheme = this._iconThemes.get(id);
if (!iconTheme) {
return undefined;
}
this._iconThemes.delete(id);
this.onDidChangeEmitter.fire(undefined);
if (id === this.getCurrent().id) {
this.setCurrent(this.default, false);
}
return iconTheme;
}
get current() {
return this.getCurrent().id;
}
set current(id) {
const newCurrent = this._iconThemes.get(id);
if (newCurrent && this.getCurrent().id !== newCurrent.id) {
this.setCurrent(newCurrent);
}
}
getCurrent() {
return this.activeTheme;
}
/**
* @param persistSetting If `true`, the theme's id will be set as the value of the `workbench.iconTheme` preference. (default: `true`)
*/
setCurrent(newCurrent, persistSetting = true) {
if (newCurrent !== this.getCurrent()) {
this.activeTheme = newCurrent;
this.toDeactivate.dispose();
this.toDeactivate.push(newCurrent.activate());
this.onDidChangeCurrentEmitter.fire(newCurrent.id);
}
if (persistSetting) {
this.preferences.updateValue(ICON_THEME_PREFERENCE_KEY, newCurrent.id);
}
}
getConfiguredTheme() {
const configuredId = this.preferences.get(ICON_THEME_PREFERENCE_KEY);
return configuredId ? this._iconThemes.get(configuredId) : undefined;
}
validateActiveTheme() {
if (this.preferences.isReady) {
const configured = this.getConfiguredTheme();
if (configured && configured !== this.getCurrent()) {
this.setCurrent(configured, false);
}
}
}
doUpdateIconThemePreference() {
const preference = this.schemaProvider.getSchemaProperty(ICON_THEME_PREFERENCE_KEY);
if (preference) {
const sortedThemes = Array.from(this.definitions).sort((a, b) => a.label.localeCompare(b.label));
this.schemaProvider.updateSchemaProperty(ICON_THEME_PREFERENCE_KEY, {
...preference,
enum: sortedThemes.map(e => e.id),
enumItemLabels: sortedThemes.map(e => e.label)
});
}
}
get default() {
return this._iconThemes.get(FrontendApplicationConfigProvider.get().defaultIconTheme) || this.fallback;
}
get fallback() {
return this.noneIconTheme;
}
};
__decorate([
inject(NoneIconTheme)
], IconThemeService.prototype, "noneIconTheme", void 0);
__decorate([
inject(PreferenceService)
], IconThemeService.prototype, "preferences", void 0);
__decorate([
inject(PreferenceSchemaService)
], IconThemeService.prototype, "schemaProvider", void 0);
__decorate([
inject(ILogger),
named('core:IconThemeService')
], IconThemeService.prototype, "logger", void 0);
__decorate([
postConstruct()
], IconThemeService.prototype, "init", null);
IconThemeService = __decorate([
injectable()
], IconThemeService);
export { IconThemeService };
//# sourceMappingURL=icon-theme-service.js.map