UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

119 lines • 4.92 kB
// ***************************************************************************** // Copyright (C) 2022 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 { inject, injectable, named, postConstruct } from 'inversify'; import { escapeRegExpCharacters } from '../../common/strings.js'; import { Emitter } from '../../common/event.js'; import { CorePreferences } from '../../common/core-preferences.js'; import { FrontendApplicationConfigProvider } from '../frontend-application-config-provider.js'; import { ContributionProvider } from '../../common/index.js'; export const WindowTitleContribution = Symbol('WindowTitleAddOnContribution'); export const InitialWindowTitleParts = { activeEditorShort: undefined, activeEditorMedium: undefined, activeEditorLong: undefined, activeFolderShort: undefined, activeFolderMedium: undefined, activeFolderLong: undefined, folderName: undefined, folderPath: undefined, rootName: undefined, rootPath: undefined, // A getter, so that the configuration is read when the title is created rather than when this module is loaded. get appName() { return FrontendApplicationConfigProvider.get().applicationName; }, remoteName: undefined, dirty: undefined, developmentHost: undefined }; let WindowTitleService = class WindowTitleService { constructor() { this._title = ''; this.onDidChangeTitleEmitter = new Emitter(); this.titleParts = new Map(Object.entries(InitialWindowTitleParts)); this.separator = ' - '; } init() { this.titleTemplate = this.preferences['window.title']; this.separator = this.preferences['window.titleSeparator']; this.updateTitle(); this.preferences.onPreferenceChanged(e => { if (e.preferenceName === 'window.title') { this.titleTemplate = this.preferences['window.title']; this.updateTitle(); } else if (e.preferenceName === 'window.titleSeparator') { this.separator = this.preferences['window.titleSeparator']; this.updateTitle(); } }); } get onDidChangeTitle() { return this.onDidChangeTitleEmitter.event; } get title() { return this._title; } update(parts) { for (const [key, value] of Object.entries(parts)) { this.titleParts.set(key, value); } this.updateTitle(); } updateTitle() { if (!this.titleTemplate) { this._title = ''; } else { let title = this.titleTemplate; for (const [key, value] of this.titleParts.entries()) { if (key !== 'developmentHost') { const label = `$\{${key}\}`; const regex = new RegExp(escapeRegExpCharacters(label), 'g'); title = title.replace(regex, value ?? ''); } } const separatedTitle = title.split('${separator}').filter(e => e.trim().length > 0); this._title = separatedTitle.join(this.separator); const contributions = this.titleContributions.getContributions(); for (const contribution of contributions) { this._title = contribution.enhanceTitle(this.title, this.titleParts); } } const developmentHost = this.titleParts.get('developmentHost'); if (developmentHost) { this._title = developmentHost + this.separator + this._title; } document.title = this._title || FrontendApplicationConfigProvider.get().applicationName; this.onDidChangeTitleEmitter.fire(this._title); } }; __decorate([ inject(CorePreferences) ], WindowTitleService.prototype, "preferences", void 0); __decorate([ inject(ContributionProvider), named(WindowTitleContribution) ], WindowTitleService.prototype, "titleContributions", void 0); __decorate([ postConstruct() ], WindowTitleService.prototype, "init", null); WindowTitleService = __decorate([ injectable() ], WindowTitleService); export { WindowTitleService }; //# sourceMappingURL=window-title-service.js.map