UNPKG

8.86 kBJavaScriptView Raw
1"use strict";
2var DefaultSecondaryWindowService_1;
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.DefaultSecondaryWindowService = void 0;
5const tslib_1 = require("tslib");
6// *****************************************************************************
7// Copyright (C) 2022 STMicroelectronics, Ericsson, ARM, EclipseSource and others.
8//
9// This program and the accompanying materials are made available under the
10// terms of the Eclipse Public License v. 2.0 which is available at
11// http://www.eclipse.org/legal/epl-2.0.
12//
13// This Source Code may also be made available under the following Secondary
14// Licenses when the conditions for such availability set forth in the Eclipse
15// Public License v. 2.0 are satisfied: GNU General Public License, version 2
16// with the GNU Classpath Exception which is available at
17// https://www.gnu.org/software/classpath/license.html.
18//
19// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
20// *****************************************************************************
21const inversify_1 = require("inversify");
22const window_service_1 = require("./window-service");
23const saveable_1 = require("../saveable");
24const preferences_1 = require("../preferences");
25const common_1 = require("../../common");
26let DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = class DefaultSecondaryWindowService {
27 constructor() {
28 /**
29 * Randomized prefix to be included in opened windows' ids.
30 * This avoids conflicts when creating sub-windows from multiple theia instances (e.g. by opening Theia multiple times in the same browser)
31 */
32 this.prefix = crypto.getRandomValues(new Uint32Array(1))[0];
33 /** Unique id. Increase after every access. */
34 this.nextId = 0;
35 this.secondaryWindows = [];
36 }
37 init() {
38 // Set up messaging with secondary windows
39 window.addEventListener('message', (event) => {
40 console.trace('Message on main window', event);
41 if (event.data.fromSecondary) {
42 console.trace('Message comes from secondary window');
43 return;
44 }
45 if (event.data.fromMain) {
46 console.trace('Message has mainWindow marker, therefore ignore it');
47 return;
48 }
49 // Filter setImmediate messages. Do not forward because these come in with very high frequency.
50 // They are not needed in secondary windows because these messages are just a work around
51 // to make setImmediate work in the main window: https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate
52 if (typeof event.data === 'string' && event.data.startsWith('setImmediate')) {
53 return;
54 }
55 console.trace('Delegate main window message to secondary windows', event);
56 this.secondaryWindows.forEach(secondaryWindow => {
57 if (!secondaryWindow.window.closed) {
58 secondaryWindow.window.postMessage({ ...event.data, fromMain: true }, '*');
59 }
60 });
61 });
62 // Close all open windows when the main window is closed.
63 this.windowService.onUnload(() => {
64 // Iterate backwards because calling window.close might remove the window from the array
65 for (let i = this.secondaryWindows.length - 1; i >= 0; i--) {
66 this.secondaryWindows[i].close();
67 }
68 });
69 }
70 createSecondaryWindow(widget, shell) {
71 var _a;
72 const [height, width, left, top] = this.findSecondaryWindowCoordinates(widget);
73 let options = `popup=1,width=${width},height=${height},left=${left},top=${top}`;
74 if (this.preferenceService.get('window.secondaryWindowAlwaysOnTop')) {
75 options += ',alwaysOnTop=true';
76 }
77 const newWindow = (_a = window.open(DefaultSecondaryWindowService_1.SECONDARY_WINDOW_URL, this.nextWindowId(), options)) !== null && _a !== void 0 ? _a : undefined;
78 if (newWindow) {
79 this.secondaryWindows.push(newWindow);
80 newWindow.addEventListener('DOMContentLoaded', () => {
81 newWindow.addEventListener('beforeunload', evt => {
82 const saveable = saveable_1.Saveable.get(widget);
83 const wouldLoseState = !!saveable && saveable.dirty && saveable.autoSave === 'off';
84 if (wouldLoseState) {
85 evt.returnValue = '';
86 evt.preventDefault();
87 return 'non-empty';
88 }
89 }, { capture: true });
90 newWindow.addEventListener('unload', () => {
91 const saveable = saveable_1.Saveable.get(widget);
92 shell.closeWidget(widget.id, {
93 save: !!saveable && saveable.dirty && saveable.autoSave !== 'off'
94 });
95 const extIndex = this.secondaryWindows.indexOf(newWindow);
96 if (extIndex > -1) {
97 this.secondaryWindows.splice(extIndex, 1);
98 }
99 ;
100 });
101 this.windowCreated(newWindow, widget, shell);
102 });
103 }
104 return newWindow;
105 }
106 windowCreated(newWindow, widget, shell) {
107 newWindow.addEventListener('unload', () => {
108 shell.closeWidget(widget.id);
109 });
110 }
111 findWindow(windowName) {
112 for (const w of this.secondaryWindows) {
113 if (w.name === windowName) {
114 return w;
115 }
116 }
117 return undefined;
118 }
119 findSecondaryWindowCoordinates(widget) {
120 const clientBounds = widget.node.getBoundingClientRect();
121 const preference = this.preferenceService.get('window.secondaryWindowPlacement');
122 let height;
123 let width;
124 let left;
125 let top;
126 const offsetY = 20; // Offset to avoid the window title bar
127 switch (preference) {
128 case 'originalSize': {
129 height = widget.node.clientHeight;
130 width = widget.node.clientWidth;
131 left = window.screenLeft + clientBounds.x;
132 top = window.screenTop + (window.outerHeight - window.innerHeight) + offsetY;
133 if (common_1.environment.electron.is()) {
134 top = window.screenTop + clientBounds.y;
135 }
136 break;
137 }
138 case 'halfWidth': {
139 height = window.innerHeight - (window.outerHeight - window.innerHeight);
140 width = window.innerWidth / 2;
141 left = window.screenLeft;
142 top = window.screenTop;
143 if (!common_1.environment.electron.is()) {
144 height = window.innerHeight + clientBounds.y - offsetY;
145 }
146 break;
147 }
148 case 'fullSize': {
149 height = window.innerHeight - (window.outerHeight - window.innerHeight);
150 width = window.innerWidth;
151 left = window.screenLeft;
152 top = window.screenTop;
153 if (!common_1.environment.electron.is()) {
154 height = window.innerHeight + clientBounds.y - offsetY;
155 }
156 break;
157 }
158 }
159 return [height, width, left, top];
160 }
161 focus(win) {
162 win.focus();
163 }
164 nextWindowId() {
165 return `${this.prefix}-secondaryWindow-${this.nextId++}`;
166 }
167};
168// secondary-window.html is part of Theia's generated code. It is generated by dev-packages/application-manager/src/generator/frontend-generator.ts
169DefaultSecondaryWindowService.SECONDARY_WINDOW_URL = 'secondary-window.html';
170(0, tslib_1.__decorate)([
171 (0, inversify_1.inject)(window_service_1.WindowService),
172 (0, tslib_1.__metadata)("design:type", Object)
173], DefaultSecondaryWindowService.prototype, "windowService", void 0);
174(0, tslib_1.__decorate)([
175 (0, inversify_1.inject)(preferences_1.PreferenceService),
176 (0, tslib_1.__metadata)("design:type", Object)
177], DefaultSecondaryWindowService.prototype, "preferenceService", void 0);
178(0, tslib_1.__decorate)([
179 (0, inversify_1.postConstruct)(),
180 (0, tslib_1.__metadata)("design:type", Function),
181 (0, tslib_1.__metadata)("design:paramtypes", []),
182 (0, tslib_1.__metadata)("design:returntype", void 0)
183], DefaultSecondaryWindowService.prototype, "init", null);
184DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = (0, tslib_1.__decorate)([
185 (0, inversify_1.injectable)()
186], DefaultSecondaryWindowService);
187exports.DefaultSecondaryWindowService = DefaultSecondaryWindowService;
188//# sourceMappingURL=default-secondary-window-service.js.map
\No newline at end of file