UNPKG

4.78 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 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.WindowTitleService = exports.InitialWindowTitleParts = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const strings_1 = require("../../common/strings");
22const event_1 = require("../../common/event");
23const core_preferences_1 = require("../core-preferences");
24const frontend_application_config_provider_1 = require("../frontend-application-config-provider");
25exports.InitialWindowTitleParts = {
26 activeEditorShort: undefined,
27 activeEditorMedium: undefined,
28 activeEditorLong: undefined,
29 activeFolderShort: undefined,
30 activeFolderMedium: undefined,
31 activeFolderLong: undefined,
32 folderName: undefined,
33 folderPath: undefined,
34 rootName: undefined,
35 rootPath: undefined,
36 appName: frontend_application_config_provider_1.FrontendApplicationConfigProvider.get().applicationName,
37 remoteName: undefined,
38 dirty: undefined,
39 developmentHost: undefined
40};
41let WindowTitleService = class WindowTitleService {
42 constructor() {
43 this._title = '';
44 this.onDidChangeTitleEmitter = new event_1.Emitter();
45 this.titleParts = new Map(Object.entries(exports.InitialWindowTitleParts));
46 this.separator = ' - ';
47 }
48 init() {
49 this.titleTemplate = this.preferences['window.title'];
50 this.separator = this.preferences['window.titleSeparator'];
51 this.updateTitle();
52 this.preferences.onPreferenceChanged(e => {
53 if (e.preferenceName === 'window.title') {
54 this.titleTemplate = e.newValue;
55 this.updateTitle();
56 }
57 else if (e.preferenceName === 'window.titleSeparator') {
58 this.separator = e.newValue;
59 this.updateTitle();
60 }
61 });
62 }
63 get onDidChangeTitle() {
64 return this.onDidChangeTitleEmitter.event;
65 }
66 get title() {
67 return this._title;
68 }
69 update(parts) {
70 for (const [key, value] of Object.entries(parts)) {
71 this.titleParts.set(key, value);
72 }
73 this.updateTitle();
74 }
75 updateTitle() {
76 if (!this.titleTemplate) {
77 this._title = '';
78 }
79 else {
80 let title = this.titleTemplate;
81 for (const [key, value] of this.titleParts.entries()) {
82 if (key !== 'developmentHost') {
83 const label = `$\{${key}\}`;
84 const regex = new RegExp((0, strings_1.escapeRegExpCharacters)(label), 'g');
85 title = title.replace(regex, value !== null && value !== void 0 ? value : '');
86 }
87 }
88 const separatedTitle = title.split('${separator}').filter(e => e.trim().length > 0);
89 this._title = separatedTitle.join(this.separator);
90 }
91 const developmentHost = this.titleParts.get('developmentHost');
92 if (developmentHost) {
93 this._title = developmentHost + this.separator + this._title;
94 }
95 document.title = this._title || frontend_application_config_provider_1.FrontendApplicationConfigProvider.get().applicationName;
96 this.onDidChangeTitleEmitter.fire(this._title);
97 }
98};
99(0, tslib_1.__decorate)([
100 (0, inversify_1.inject)(core_preferences_1.CorePreferences),
101 (0, tslib_1.__metadata)("design:type", Object)
102], WindowTitleService.prototype, "preferences", void 0);
103(0, tslib_1.__decorate)([
104 (0, inversify_1.postConstruct)(),
105 (0, tslib_1.__metadata)("design:type", Function),
106 (0, tslib_1.__metadata)("design:paramtypes", []),
107 (0, tslib_1.__metadata)("design:returntype", void 0)
108], WindowTitleService.prototype, "init", null);
109WindowTitleService = (0, tslib_1.__decorate)([
110 (0, inversify_1.injectable)()
111], WindowTitleService);
112exports.WindowTitleService = WindowTitleService;
113//# sourceMappingURL=window-title-service.js.map
\No newline at end of file