1 | "use strict";
|
2 | var DefaultSecondaryWindowService_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.DefaultSecondaryWindowService = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const inversify_1 = require("inversify");
|
22 | const window_service_1 = require("./window-service");
|
23 | const saveable_1 = require("../saveable");
|
24 | const preferences_1 = require("../preferences");
|
25 | const common_1 = require("../../common");
|
26 | let DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = class DefaultSecondaryWindowService {
|
27 | constructor() {
|
28 | |
29 |
|
30 |
|
31 |
|
32 | this.prefix = crypto.getRandomValues(new Uint32Array(1))[0];
|
33 |
|
34 | this.nextId = 0;
|
35 | this.secondaryWindows = [];
|
36 | }
|
37 | init() {
|
38 |
|
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 |
|
50 |
|
51 |
|
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 |
|
63 | this.windowService.onUnload(() => {
|
64 |
|
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;
|
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 |
|
169 | DefaultSecondaryWindowService.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);
|
184 | DefaultSecondaryWindowService = DefaultSecondaryWindowService_1 = (0, tslib_1.__decorate)([
|
185 | (0, inversify_1.injectable)()
|
186 | ], DefaultSecondaryWindowService);
|
187 | exports.DefaultSecondaryWindowService = DefaultSecondaryWindowService;
|
188 |
|
\ | No newline at end of file |