UNPKG

10.2 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2022 STMicroelectronics, Ericsson, ARM, EclipseSource 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// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.SecondaryWindowHandler = void 0;
28const debounce = require("lodash.debounce");
29const inversify_1 = require("inversify");
30const widgets_1 = require("./widgets");
31const message_service_1 = require("../common/message-service");
32const event_1 = require("../common/event");
33const secondary_window_service_1 = require("./window/secondary-window-service");
34const keybinding_1 = require("./keybinding");
35const color_application_contribution_1 = require("./color-application-contribution");
36const styling_service_1 = require("./styling-service");
37/** Widget to be contained directly in a secondary window. */
38class SecondaryWindowRootWidget extends widgets_1.Widget {
39 constructor() {
40 super();
41 this.layout = new widgets_1.BoxLayout();
42 }
43 addWidget(widget) {
44 this.layout.addWidget(widget);
45 widgets_1.BoxPanel.setStretch(widget, 1);
46 }
47}
48/**
49 * Offers functionality to move a widget out of the main window to a newly created window.
50 * Widgets must explicitly implement the `ExtractableWidget` interface to support this.
51 *
52 * This handler manages the opened secondary windows and sets up messaging between them and the Theia main window.
53 * In addition, it provides access to the extracted widgets and provides notifications when widgets are added to or removed from this handler.
54 *
55 * @experimental The functionality provided by this handler is experimental and has known issues in Electron apps.
56 */
57let SecondaryWindowHandler = class SecondaryWindowHandler {
58 constructor() {
59 /** List of widgets in secondary windows. */
60 this._widgets = [];
61 this.onDidAddWidgetEmitter = new event_1.Emitter();
62 /** Subscribe to get notified when a widget is added to this handler, i.e. the widget was moved to an secondary window . */
63 this.onDidAddWidget = this.onDidAddWidgetEmitter.event;
64 this.onDidRemoveWidgetEmitter = new event_1.Emitter();
65 /** 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. */
66 this.onDidRemoveWidget = this.onDidRemoveWidgetEmitter.event;
67 }
68 /** @returns List of widgets in secondary windows. */
69 get widgets() {
70 // Create new array in case the original changes while this is used.
71 return [...this._widgets];
72 }
73 /**
74 * Sets up message forwarding from the main window to secondary windows.
75 * Does nothing if this service has already been initialized.
76 *
77 * @param shell The `ApplicationShell` that widgets will be moved out from.
78 */
79 init(shell) {
80 if (this.applicationShell) {
81 // Already initialized
82 return;
83 }
84 this.applicationShell = shell;
85 }
86 /**
87 * Moves the given widget to a new window.
88 *
89 * @param widget the widget to extract
90 */
91 moveWidgetToSecondaryWindow(widget) {
92 if (!this.applicationShell) {
93 console.error('Widget cannot be extracted because the WidgetExtractionHandler has not been initialized.');
94 return;
95 }
96 if (!widget.isExtractable) {
97 console.error('Widget is not extractable.', widget.id);
98 return;
99 }
100 const newWindow = this.secondaryWindowService.createSecondaryWindow(widget, this.applicationShell);
101 if (!newWindow) {
102 this.messageService.error('The widget could not be moved to a secondary window because the window creation failed. Please make sure to allow popups.');
103 return;
104 }
105 const mainWindowTitle = document.title;
106 newWindow.onload = () => {
107 this.keybindings.registerEventListeners(newWindow);
108 // Use the widget's title as the window title
109 // 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.
110 // See https://html.spec.whatwg.org/multipage/dom.html#document.title
111 newWindow.document.title = `${widget.title.label}${mainWindowTitle}`;
112 const element = newWindow.document.getElementById('widget-host');
113 if (!element) {
114 console.error('Could not find dom element to attach to in secondary window');
115 return;
116 }
117 const unregisterWithColorContribution = this.colorAppContribution.registerWindow(newWindow);
118 const unregisterWithStylingService = this.stylingService.registerWindow(newWindow);
119 widget.secondaryWindow = newWindow;
120 const rootWidget = new SecondaryWindowRootWidget();
121 rootWidget.addClass('secondary-widget-root');
122 widgets_1.Widget.attach(rootWidget, element);
123 rootWidget.addWidget(widget);
124 widget.show();
125 widget.update();
126 this.addWidget(widget);
127 // Close the window if the widget is disposed, e.g. by a command closing all widgets.
128 widget.disposed.connect(() => {
129 unregisterWithColorContribution.dispose();
130 unregisterWithStylingService.dispose();
131 this.removeWidget(widget);
132 if (!newWindow.closed) {
133 newWindow.close();
134 }
135 });
136 // debounce to avoid rapid updates while resizing the secondary window
137 const updateWidget = debounce(() => {
138 rootWidget.update();
139 }, 100);
140 newWindow.addEventListener('resize', () => {
141 updateWidget();
142 });
143 widget.activate();
144 };
145 }
146 /**
147 * If the given widget is tracked by this handler, activate it and focus its secondary window.
148 *
149 * @param widgetId The widget to activate specified by its id
150 * @returns The activated `ExtractableWidget` or `undefined` if the given widget id is unknown to this handler.
151 */
152 activateWidget(widgetId) {
153 const trackedWidget = this.revealWidget(widgetId);
154 trackedWidget === null || trackedWidget === void 0 ? void 0 : trackedWidget.activate();
155 return trackedWidget;
156 }
157 /**
158 * If the given widget is tracked by this handler, reveal it by focussing its secondary window.
159 *
160 * @param widgetId The widget to reveal specified by its id
161 * @returns The revealed `ExtractableWidget` or `undefined` if the given widget id is unknown to this handler.
162 */
163 revealWidget(widgetId) {
164 const trackedWidget = this._widgets.find(w => w.id === widgetId);
165 if (trackedWidget) {
166 this.secondaryWindowService.focus(trackedWidget.secondaryWindow);
167 return trackedWidget;
168 }
169 return undefined;
170 }
171 addWidget(widget) {
172 if (!this._widgets.includes(widget)) {
173 this._widgets.push(widget);
174 this.onDidAddWidgetEmitter.fire(widget);
175 }
176 }
177 removeWidget(widget) {
178 const index = this._widgets.indexOf(widget);
179 if (index > -1) {
180 this._widgets.splice(index, 1);
181 this.onDidRemoveWidgetEmitter.fire(widget);
182 }
183 }
184};
185__decorate([
186 (0, inversify_1.inject)(keybinding_1.KeybindingRegistry),
187 __metadata("design:type", keybinding_1.KeybindingRegistry)
188], SecondaryWindowHandler.prototype, "keybindings", void 0);
189__decorate([
190 (0, inversify_1.inject)(color_application_contribution_1.ColorApplicationContribution),
191 __metadata("design:type", color_application_contribution_1.ColorApplicationContribution)
192], SecondaryWindowHandler.prototype, "colorAppContribution", void 0);
193__decorate([
194 (0, inversify_1.inject)(styling_service_1.StylingService),
195 __metadata("design:type", styling_service_1.StylingService)
196], SecondaryWindowHandler.prototype, "stylingService", void 0);
197__decorate([
198 (0, inversify_1.inject)(message_service_1.MessageService),
199 __metadata("design:type", message_service_1.MessageService)
200], SecondaryWindowHandler.prototype, "messageService", void 0);
201__decorate([
202 (0, inversify_1.inject)(secondary_window_service_1.SecondaryWindowService),
203 __metadata("design:type", Object)
204], SecondaryWindowHandler.prototype, "secondaryWindowService", void 0);
205SecondaryWindowHandler = __decorate([
206 (0, inversify_1.injectable)()
207], SecondaryWindowHandler);
208exports.SecondaryWindowHandler = SecondaryWindowHandler;
209//# sourceMappingURL=secondary-window-handler.js.map
\No newline at end of file