@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
197 lines • 9.5 kB
JavaScript
"use strict";
// *****************************************************************************
// Copyright (C) 2022 STMicroelectronics, Ericsson, ARM, EclipseSource 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
// *****************************************************************************
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecondaryWindowHandler = void 0;
const tslib_1 = require("tslib");
const debounce = require("lodash.debounce");
const inversify_1 = require("inversify");
const widgets_1 = require("./widgets");
const message_service_1 = require("../common/message-service");
const event_1 = require("../common/event");
const secondary_window_service_1 = require("./window/secondary-window-service");
const keybinding_1 = require("./keybinding");
/** Widget to be contained directly in a secondary window. */
class SecondaryWindowRootWidget extends widgets_1.Widget {
constructor() {
super();
this.layout = new widgets_1.BoxLayout();
}
addWidget(widget) {
this.layout.addWidget(widget);
widgets_1.BoxPanel.setStretch(widget, 1);
}
}
/**
* Offers functionality to move a widget out of the main window to a newly created window.
* Widgets must explicitly implement the `ExtractableWidget` interface to support this.
*
* This handler manages the opened secondary windows and sets up messaging between them and the Theia main window.
* In addition, it provides access to the extracted widgets and provides notifications when widgets are added to or removed from this handler.
*
*/
let SecondaryWindowHandler = class SecondaryWindowHandler {
constructor() {
/** List of widgets in secondary windows. */
this._widgets = [];
this.onWillAddWidgetEmitter = new event_1.Emitter();
/** Subscribe to get notified when a widget is added to this handler, i.e. the widget was moved to an secondary window . */
this.onWillAddWidget = this.onWillAddWidgetEmitter.event;
this.onDidAddWidgetEmitter = new event_1.Emitter();
/** Subscribe to get notified when a widget is added to this handler, i.e. the widget was moved to an secondary window . */
this.onDidAddWidget = this.onDidAddWidgetEmitter.event;
this.onWillRemoveWidgetEmitter = new event_1.Emitter();
/** Subscribe to get notified when a widget is removed from this handler, i.e. the widget's window was closed or the widget was disposed. */
this.onWillRemoveWidget = this.onWillRemoveWidgetEmitter.event;
this.onDidRemoveWidgetEmitter = new event_1.Emitter();
/** Subscribe to get notified when a widget is removed from this handler, i.e. the widget's window was closed or the widget was disposed. */
this.onDidRemoveWidget = this.onDidRemoveWidgetEmitter.event;
}
/** @returns List of widgets in secondary windows. */
get widgets() {
// Create new array in case the original changes while this is used.
return [...this._widgets];
}
/**
* Sets up message forwarding from the main window to secondary windows.
* Does nothing if this service has already been initialized.
*
* @param shell The `ApplicationShell` that widgets will be moved out from.
*/
init(shell) {
if (this.applicationShell) {
// Already initialized
return;
}
this.applicationShell = shell;
}
/**
* Moves the given widget to a new window.
*
* @param widget the widget to extract
*/
moveWidgetToSecondaryWindow(widget) {
if (!this.applicationShell) {
console.error('Widget cannot be extracted because the WidgetExtractionHandler has not been initialized.');
return;
}
if (!widget.isExtractable) {
console.error('Widget is not extractable.', widget.id);
return;
}
const newWindow = this.secondaryWindowService.createSecondaryWindow(widget, this.applicationShell);
if (!newWindow) {
this.messageService.error('The widget could not be moved to a secondary window because the window creation failed. Please make sure to allow popups.');
return;
}
const mainWindowTitle = document.title;
newWindow.addEventListener('load', () => {
this.keybindings.registerEventListeners(newWindow);
// Use the widget's title as the window title
// Even if the widget's label were malicious, this should be safe against XSS because the HTML standard defines this is inserted via a text node.
// See https://html.spec.whatwg.org/multipage/dom.html#document.title
newWindow.document.title = `${widget.title.label} — ${mainWindowTitle}`;
const element = newWindow.document.getElementById('widget-host');
if (!element) {
console.error('Could not find dom element to attach to in secondary window');
return;
}
this.onWillAddWidgetEmitter.fire([widget, newWindow]);
widget.secondaryWindow = newWindow;
const rootWidget = new SecondaryWindowRootWidget();
rootWidget.addClass('secondary-widget-root');
rootWidget.addClass('monaco-workbench'); // needed for compatility with VSCode styles
widgets_1.Widget.attach(rootWidget, element);
rootWidget.addWidget(widget);
widget.show();
widget.update();
this.addWidget(widget, newWindow);
// Close the window if the widget is disposed, e.g. by a command closing all widgets.
widget.disposed.connect(() => {
this.onWillRemoveWidgetEmitter.fire([widget, newWindow]);
this.removeWidget(widget, newWindow);
if (!newWindow.closed) {
newWindow.close();
}
});
// debounce to avoid rapid updates while resizing the secondary window
const updateWidget = debounce(() => {
rootWidget.update();
}, 100);
newWindow.addEventListener('resize', () => {
updateWidget();
});
widget.activate();
});
}
/**
* If the given widget is tracked by this handler, activate it and focus its secondary window.
*
* @param widgetId The widget to activate specified by its id
* @returns The activated `ExtractableWidget` or `undefined` if the given widget id is unknown to this handler.
*/
activateWidget(widgetId) {
const trackedWidget = this.revealWidget(widgetId);
trackedWidget === null || trackedWidget === void 0 ? void 0 : trackedWidget.activate();
return trackedWidget;
}
/**
* If the given widget is tracked by this handler, reveal it by focussing its secondary window.
*
* @param widgetId The widget to reveal specified by its id
* @returns The revealed `ExtractableWidget` or `undefined` if the given widget id is unknown to this handler.
*/
revealWidget(widgetId) {
const trackedWidget = this._widgets.find(w => w.id === widgetId);
if (trackedWidget && this.getFocusedWindow()) {
this.secondaryWindowService.focus(trackedWidget.secondaryWindow);
}
return trackedWidget;
}
getFocusedWindow() {
return window.document.hasFocus() ? window : this.secondaryWindowService.getWindows().find(candidate => candidate.document.hasFocus());
}
addWidget(widget, win) {
if (!this._widgets.includes(widget)) {
this._widgets.push(widget);
this.onDidAddWidgetEmitter.fire([widget, win]);
}
}
removeWidget(widget, win) {
const index = this._widgets.indexOf(widget);
if (index > -1) {
this._widgets.splice(index, 1);
this.onDidRemoveWidgetEmitter.fire([widget, win]);
}
}
};
exports.SecondaryWindowHandler = SecondaryWindowHandler;
tslib_1.__decorate([
(0, inversify_1.inject)(keybinding_1.KeybindingRegistry),
tslib_1.__metadata("design:type", keybinding_1.KeybindingRegistry)
], SecondaryWindowHandler.prototype, "keybindings", void 0);
tslib_1.__decorate([
(0, inversify_1.inject)(message_service_1.MessageService),
tslib_1.__metadata("design:type", message_service_1.MessageService)
], SecondaryWindowHandler.prototype, "messageService", void 0);
tslib_1.__decorate([
(0, inversify_1.inject)(secondary_window_service_1.SecondaryWindowService),
tslib_1.__metadata("design:type", Object)
], SecondaryWindowHandler.prototype, "secondaryWindowService", void 0);
exports.SecondaryWindowHandler = SecondaryWindowHandler = tslib_1.__decorate([
(0, inversify_1.injectable)()
], SecondaryWindowHandler);
//# sourceMappingURL=secondary-window-handler.js.map