@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
195 lines • 7.82 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2017 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
// *****************************************************************************
var ThemeService_1;
import { __decorate } from "tslib";
import { ApplicationProps, DefaultTheme } from '@theia/application-package/lib/application-props';
import { inject, injectable, postConstruct } from 'inversify';
import { Disposable } from '../common/disposable.js';
import { Emitter } from '../common/event.js';
import { PreferenceSchemaService } from '../common/preferences/preference-schema.js';
import { Deferred } from '../common/promise-util.js';
import { FrontendApplicationConfigProvider } from './frontend-application-config-provider.js';
import debounce from '../../shared/lodash.debounce/index.cjs';
import { PreferenceService } from '../common/preferences/index.js';
const COLOR_THEME_PREFERENCE_KEY = 'workbench.colorTheme';
const NO_THEME = { id: 'no-theme', label: 'Not a real theme.', type: 'dark' };
let ThemeService = class ThemeService {
constructor() {
this.themes = {};
this.activeTheme = NO_THEME;
this.themeChange = new Emitter();
this.deferredInitializer = new Deferred();
this.onDidColorThemeChange = this.themeChange.event;
this.updateColorThemePreference = debounce(() => this.doUpdateColorThemePreference(), 500);
}
static { ThemeService_1 = this; }
static { this.STORAGE_KEY = 'theme'; }
get initialized() {
return this.deferredInitializer.promise;
}
init() {
this.register(...BuiltinThemeProvider.themes);
this.loadUserTheme();
this.preferences.ready.then(() => {
this.validateActiveTheme();
this.updateColorThemePreference();
this.preferences.onPreferencesChanged(changes => {
if (changes.some(change => change.preferenceName === COLOR_THEME_PREFERENCE_KEY)) {
this.validateActiveTheme();
}
});
});
}
register(...themes) {
for (const theme of themes) {
this.themes[theme.id] = theme;
}
this.validateActiveTheme();
this.updateColorThemePreference();
return Disposable.create(() => {
for (const theme of themes) {
delete this.themes[theme.id];
if (this.activeTheme === theme) {
this.setCurrentTheme(this.defaultTheme.id, false);
}
}
this.updateColorThemePreference();
});
}
validateActiveTheme() {
if (this.preferences.isReady) {
const configuredTheme = this.getConfiguredTheme();
if (configuredTheme && configuredTheme !== this.activeTheme) {
this.setCurrentTheme(configuredTheme.id, false);
}
}
}
doUpdateColorThemePreference() {
const preference = this.schemaProvider.getSchemaProperty(COLOR_THEME_PREFERENCE_KEY);
if (preference) {
const sortedThemes = this.getThemes().sort((a, b) => a.label.localeCompare(b.label));
this.schemaProvider.updateSchemaProperty(COLOR_THEME_PREFERENCE_KEY, {
...preference,
enum: sortedThemes.map(e => e.id),
enumItemLabels: sortedThemes.map(e => e.label)
});
}
}
getThemes() {
const result = [];
for (const o in this.themes) {
if (this.themes.hasOwnProperty(o)) {
result.push(this.themes[o]);
}
}
return result;
}
getTheme(themeId) {
return this.themes[themeId] || this.defaultTheme;
}
tryGetTheme(themeId) {
return this.themes[themeId];
}
/** Should only be called at startup. */
loadUserTheme() {
const storedThemeId = window.localStorage.getItem(ThemeService_1.STORAGE_KEY) ?? this.defaultTheme.id;
const theme = this.getTheme(storedThemeId);
this.setCurrentTheme(theme.id, false);
this.deferredInitializer.resolve();
}
/**
* @param persist If `true`, the value of the `workbench.colorTheme` preference will be set to the provided ID.
*/
setCurrentTheme(themeId, persist = true) {
const newTheme = this.tryGetTheme(themeId);
const oldTheme = this.activeTheme;
if (newTheme && newTheme !== oldTheme) {
oldTheme?.deactivate?.();
newTheme.activate?.();
this.activeTheme = newTheme;
this.themeChange.fire({ newTheme, oldTheme });
}
if (persist) {
this.preferences.updateValue(COLOR_THEME_PREFERENCE_KEY, themeId);
}
}
getCurrentTheme() {
return this.activeTheme;
}
getConfiguredTheme() {
const configuredId = this.preferences.get(COLOR_THEME_PREFERENCE_KEY);
return configuredId ? this.themes[configuredId.toString()] : undefined;
}
/**
* The default theme. If that is not applicable, returns with the fallback theme.
*/
get defaultTheme() {
return this.tryGetTheme(DefaultTheme.defaultForOSTheme(FrontendApplicationConfigProvider.get().defaultTheme))
?? this.getTheme(DefaultTheme.defaultForOSTheme(ApplicationProps.DEFAULT.frontend.config.defaultTheme));
}
/**
* Resets the state to the user's default, or to the fallback theme. Also discards any persisted state in the local storage.
*/
reset() {
this.setCurrentTheme(this.defaultTheme.id);
}
};
__decorate([
inject(PreferenceService)
], ThemeService.prototype, "preferences", void 0);
__decorate([
inject(PreferenceSchemaService)
], ThemeService.prototype, "schemaProvider", void 0);
__decorate([
postConstruct()
], ThemeService.prototype, "init", null);
ThemeService = ThemeService_1 = __decorate([
injectable()
], ThemeService);
export { ThemeService };
export class BuiltinThemeProvider {
static { this.darkTheme = {
id: 'dark',
type: 'dark',
label: 'Dark (Theia)',
editorTheme: 'dark-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
}; }
static { this.lightTheme = {
id: 'light',
type: 'light',
label: 'Light (Theia)',
editorTheme: 'light-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
}; }
static { this.hcTheme = {
id: 'hc-theia',
type: 'hc',
label: 'High Contrast (Theia)',
editorTheme: 'hc-theia' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
}; }
static { this.hcLightTheme = {
id: 'hc-theia-light',
type: 'hcLight',
label: 'High Contrast Light (Theia)',
editorTheme: 'hc-theia-light' // loaded in /packages/monaco/src/browser/textmate/monaco-theme-registry.ts
}; }
static { this.themes = [
BuiltinThemeProvider.darkTheme,
BuiltinThemeProvider.lightTheme,
BuiltinThemeProvider.hcTheme,
BuiltinThemeProvider.hcLightTheme
]; }
}
//# sourceMappingURL=theming.js.map