@interopio/browser-platform 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/browser.platform.d.ts +455 -0
  2. package/changelog.md +69 -0
  3. package/dist/browser.platform.es.js +46074 -0
  4. package/dist/browser.platform.es.js.map +1 -0
  5. package/dist/browser.platform.umd.js +46082 -0
  6. package/dist/browser.platform.umd.js.map +1 -0
  7. package/license.md +21 -0
  8. package/package.json +78 -0
  9. package/readme.md +9 -0
  10. package/rollup.config.js +45 -0
  11. package/src/common/constants.ts +46 -0
  12. package/src/common/defaultConfig.ts +56 -0
  13. package/src/common/gateway.d.ts +32 -0
  14. package/src/common/types.ts +222 -0
  15. package/src/connection/gateway.ts +132 -0
  16. package/src/connection/no-gateway.ts +1 -0
  17. package/src/connection/portsBridge.ts +441 -0
  18. package/src/connection/preferred.ts +247 -0
  19. package/src/controllers/domains.ts +156 -0
  20. package/src/controllers/glue.ts +604 -0
  21. package/src/controllers/interception.ts +53 -0
  22. package/src/controllers/license.ts +50 -0
  23. package/src/controllers/main.ts +256 -0
  24. package/src/controllers/plugins.ts +121 -0
  25. package/src/controllers/serviceWorker.ts +193 -0
  26. package/src/controllers/state.ts +84 -0
  27. package/src/controllers/system.ts +99 -0
  28. package/src/controllers/transactions.ts +80 -0
  29. package/src/factory.ts +40 -0
  30. package/src/fallbacks/browser.ts +47 -0
  31. package/src/fallbacks/dekstop.ts +21 -0
  32. package/src/index.ts +18 -0
  33. package/src/libs/applications/appStore/directory.ts +235 -0
  34. package/src/libs/applications/appStore/remoteWatcher.ts +112 -0
  35. package/src/libs/applications/controller.ts +469 -0
  36. package/src/libs/applications/decoders.ts +98 -0
  37. package/src/libs/applications/defaults.ts +6 -0
  38. package/src/libs/applications/types.ts +98 -0
  39. package/src/libs/channels/controller.ts +109 -0
  40. package/src/libs/channels/decoders.ts +19 -0
  41. package/src/libs/extension/controller.ts +120 -0
  42. package/src/libs/extension/decoders.ts +18 -0
  43. package/src/libs/extension/types.ts +11 -0
  44. package/src/libs/intents/constants.ts +9 -0
  45. package/src/libs/intents/controller.ts +500 -0
  46. package/src/libs/intents/decoders.ts +96 -0
  47. package/src/libs/intents/resolverHelper.ts +264 -0
  48. package/src/libs/intents/types.ts +119 -0
  49. package/src/libs/layouts/constants.ts +3 -0
  50. package/src/libs/layouts/controller.ts +476 -0
  51. package/src/libs/layouts/decoders.ts +132 -0
  52. package/src/libs/layouts/global/builder.ts +400 -0
  53. package/src/libs/layouts/global/resetter.ts +116 -0
  54. package/src/libs/layouts/global/restorer.ts +217 -0
  55. package/src/libs/layouts/global/validator.ts +169 -0
  56. package/src/libs/layouts/idbStore.ts +63 -0
  57. package/src/libs/layouts/types.ts +133 -0
  58. package/src/libs/manager/controller.ts +175 -0
  59. package/src/libs/manager/decoders.ts +6 -0
  60. package/src/libs/manager/defaults.ts +5 -0
  61. package/src/libs/manager/identity.ts +92 -0
  62. package/src/libs/manager/session.ts +195 -0
  63. package/src/libs/manager/types.ts +51 -0
  64. package/src/libs/notifications/controller.ts +447 -0
  65. package/src/libs/notifications/decoders.ts +166 -0
  66. package/src/libs/notifications/types.ts +49 -0
  67. package/src/libs/notifications/utils.ts +7 -0
  68. package/src/libs/search/controller.ts +232 -0
  69. package/src/libs/search/decoders.ts +6 -0
  70. package/src/libs/search/repositories/applications.ts +62 -0
  71. package/src/libs/search/repositories/layouts.ts +48 -0
  72. package/src/libs/search/repositories/workspaces.ts +47 -0
  73. package/src/libs/search/types.ts +9 -0
  74. package/src/libs/themes/constants.ts +9 -0
  75. package/src/libs/themes/controller.ts +153 -0
  76. package/src/libs/themes/decoders.ts +28 -0
  77. package/src/libs/themes/types.ts +28 -0
  78. package/src/libs/windows/controller.ts +498 -0
  79. package/src/libs/windows/decoders.ts +64 -0
  80. package/src/libs/windows/types.ts +59 -0
  81. package/src/libs/workspaces/controller.ts +922 -0
  82. package/src/libs/workspaces/decoders.ts +868 -0
  83. package/src/libs/workspaces/defaultConfig.ts +11 -0
  84. package/src/libs/workspaces/frames.ts +239 -0
  85. package/src/libs/workspaces/hibernationWatcher.ts +231 -0
  86. package/src/libs/workspaces/types.ts +506 -0
  87. package/src/platform.ts +156 -0
  88. package/src/shared/decoders.ts +479 -0
  89. package/src/shared/fetchTimeout.ts +25 -0
  90. package/src/shared/ioc.ts +580 -0
  91. package/src/shared/logger.ts +19 -0
  92. package/src/shared/promisePlus.ts +62 -0
  93. package/src/shared/sequelizer.ts +48 -0
  94. package/src/shared/utils.ts +86 -0
  95. package/src/storage/local.ts +111 -0
  96. package/src/storage/session.ts +436 -0
  97. package/tsconfig.json +16 -0
@@ -0,0 +1,498 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable @typescript-eslint/no-unused-vars */
3
+ import { IOConnectBrowser } from "@interopio/browser";
4
+ import { generate } from "shortid";
5
+ import { BridgeOperation, FocusEventData, InternalPlatformConfig, LibController, OperationCheckConfig, OperationCheckResult, SessionWindowData } from "../../common/types";
6
+ import { GlueController } from "../../controllers/glue";
7
+ import { SessionStorageController } from "../../storage/session";
8
+ import { PromiseWrap } from "../../shared/promisePlus";
9
+ import { frameWindowBoundsResultDecoder, openWindowConfigDecoder, simpleWindowDecoder, windowBoundsResultDecoder, windowMoveResizeConfigDecoder, windowOperationDecoder, windowTitleConfigDecoder, windowUrlResultDecoder } from "./decoders";
10
+ import { WindowsStateController } from "../../controllers/state";
11
+ import { HelloSuccess, OpenWindowConfig, OpenWindowSuccess, SelfAssignedWindowData, SimpleWindowCommand, WindowBoundsResult, WindowMoveResizeConfig, WindowOperationsTypes, WindowTitleConfig, WindowUrlResult } from "./types";
12
+ import { getRelativeBounds } from "../../shared/utils";
13
+ import logger from "../../shared/logger";
14
+ import { WorkspaceWindowData } from "../workspaces/types";
15
+ import { workspaceWindowDataDecoder } from "../workspaces/decoders";
16
+ import { IoC } from "../../shared/ioc";
17
+ import { focusEventDataDecoder, operationCheckConfigDecoder, operationCheckResultDecoder } from "../../shared/decoders";
18
+
19
+ export class WindowsController implements LibController {
20
+ private started = false;
21
+ private clientResponseTimeoutMs!: number;
22
+ private defaultBounds!: IOConnectBrowser.Windows.Bounds;
23
+
24
+ private operations: { [key in WindowOperationsTypes]: BridgeOperation } = {
25
+ openWindow: { name: "openWindow", execute: this.openWindow.bind(this), dataDecoder: openWindowConfigDecoder },
26
+ windowHello: { name: "windowHello", execute: this.handleWindowHello.bind(this) },
27
+ getBounds: { name: "getBounds", dataDecoder: simpleWindowDecoder, resultDecoder: windowBoundsResultDecoder, execute: this.handleGetBounds.bind(this) },
28
+ getFrameBounds: { name: "getFrameBounds", dataDecoder: simpleWindowDecoder, resultDecoder: frameWindowBoundsResultDecoder, execute: this.handleGetBounds.bind(this) },
29
+ getUrl: { name: "getUrl", dataDecoder: simpleWindowDecoder, resultDecoder: windowUrlResultDecoder, execute: this.handleGetUrl.bind(this) },
30
+ moveResize: { name: "moveResize", dataDecoder: windowMoveResizeConfigDecoder, execute: this.handleMoveResize.bind(this) },
31
+ focus: { name: "focus", dataDecoder: simpleWindowDecoder, execute: this.handleFocus.bind(this) },
32
+ close: { name: "close", dataDecoder: simpleWindowDecoder, execute: this.handleClose.bind(this) },
33
+ getTitle: { name: "getTitle", dataDecoder: simpleWindowDecoder, resultDecoder: windowTitleConfigDecoder, execute: this.handleGetTitle.bind(this) },
34
+ setTitle: { name: "setTitle", dataDecoder: windowTitleConfigDecoder, execute: this.handleSetTitle.bind(this) },
35
+ registerWorkspaceWindow: { name: "registerWorkspaceWindow", dataDecoder: workspaceWindowDataDecoder, execute: this.registerWorkspaceWindow.bind(this) },
36
+ unregisterWorkspaceWindow: { name: "unregisterWorkspaceWindow", dataDecoder: simpleWindowDecoder, execute: this.handleWorkspaceClientRemoval.bind(this) },
37
+ operationCheck: { name: "operationCheck", dataDecoder: operationCheckConfigDecoder, resultDecoder: operationCheckResultDecoder, execute: this.handleOperationCheck.bind(this) },
38
+ focusChange: { name: "focusChange", dataDecoder: focusEventDataDecoder, execute: this.handleFocusEvent.bind(this) }
39
+ };
40
+
41
+ constructor(
42
+ private readonly glueController: GlueController,
43
+ private readonly sessionController: SessionStorageController,
44
+ private readonly stateController: WindowsStateController,
45
+ private readonly ioc: IoC
46
+ ) { }
47
+
48
+ private get logger(): IOConnectBrowser.Logger.API | undefined {
49
+ return logger.get("windows.controller");
50
+ }
51
+
52
+ public get moveResizeOperation(): BridgeOperation {
53
+ return this.operations.moveResize;
54
+ }
55
+
56
+ public get getFrameBoundsOperation(): BridgeOperation {
57
+ return this.operations.getFrameBounds;
58
+ }
59
+
60
+ public get setTitleOperation(): BridgeOperation {
61
+ return this.operations.setTitle;
62
+ }
63
+
64
+ public get getBoundsOperation() {
65
+ return this.operations.getBounds;
66
+ }
67
+
68
+ public handlePlatformShutdown(): void {
69
+ this.started = false;
70
+ }
71
+
72
+ public async start(config: InternalPlatformConfig): Promise<void> {
73
+ this.clientResponseTimeoutMs = config.windows.windowResponseTimeoutMs;
74
+ this.defaultBounds = config.windows.defaultWindowOpenBounds;
75
+
76
+ this.started = true;
77
+
78
+ this.stateController.onWindowDisappeared(this.cleanUpWindow.bind(this));
79
+ }
80
+
81
+ public async handleControl(args: any): Promise<any> {
82
+ if (!this.started) {
83
+ new Error("Cannot handle this windows control message, because the controller has not been started");
84
+ }
85
+
86
+ const windowsData = args.data;
87
+
88
+ const commandId = args.commandId;
89
+
90
+ const operationValidation = windowOperationDecoder.run(args.operation);
91
+
92
+ if (!operationValidation.ok) {
93
+ throw new Error(`This window request cannot be completed, because the operation name did not pass validation: ${JSON.stringify(operationValidation.error)}`);
94
+ }
95
+
96
+ const operationName: WindowOperationsTypes = operationValidation.result;
97
+
98
+ const incomingValidation = this.operations[operationName].dataDecoder?.run(windowsData);
99
+
100
+ if (incomingValidation && !incomingValidation.ok) {
101
+ throw new Error(`Windows request for ${operationName} rejected, because the provided arguments did not pass the validation: ${JSON.stringify(incomingValidation.error)}`);
102
+ }
103
+
104
+ this.logger?.debug(`[${commandId}] ${operationName} command is valid with data: ${JSON.stringify(windowsData)}`);
105
+
106
+ const result = await this.operations[operationName].execute(windowsData, commandId);
107
+
108
+ const resultValidation = this.operations[operationName].resultDecoder?.run(result);
109
+
110
+ if (resultValidation && !resultValidation.ok) {
111
+ throw new Error(`Windows request for ${operationName} could not be completed, because the operation result did not pass the validation: ${JSON.stringify(resultValidation.error)}`);
112
+ }
113
+
114
+ this.logger?.trace(`[${commandId}] ${operationName} command was executed successfully`);
115
+
116
+ return result;
117
+ }
118
+
119
+ public async getWindowTitle(windowId: string, commandId: string): Promise<string> {
120
+ const boundsResult = await this.handleGetTitle({ windowId }, commandId);
121
+ return boundsResult.title;
122
+ }
123
+
124
+ public async getWindowBounds(windowId: string, commandId: string): Promise<IOConnectBrowser.Windows.Bounds> {
125
+ const boundsResult = await this.handleGetBounds({ windowId }, commandId);
126
+ return boundsResult.bounds;
127
+ }
128
+
129
+ public async processNewWindow(windowData: SessionWindowData, context?: any, childWindow?: Window): Promise<void> {
130
+ this.logger?.trace(`processing a new window with id: ${windowData.windowId} and name: ${windowData.name}`);
131
+ this.sessionController.saveWindowData(windowData);
132
+
133
+ if (childWindow) {
134
+ this.stateController.add(childWindow, windowData.windowId);
135
+ }
136
+
137
+ if (context) {
138
+ this.logger?.trace(`setting the context for window ${windowData.windowId}`);
139
+ await this.glueController.setStartContext(windowData.windowId, context, "window");
140
+ }
141
+
142
+ this.emitStreamData("windowAdded", windowData);
143
+ }
144
+
145
+ public async handleWorkspaceClientRemoval(client: SimpleWindowCommand): Promise<void> {
146
+ this.cleanUpWindow(client.windowId);
147
+ this.ioc.portsBridge.removeGwClient(client.windowId);
148
+ }
149
+
150
+ public handleClientUnloaded(windowId: string, win: Window): void {
151
+ this.logger?.trace(`handling unloading of ${windowId}`);
152
+ if (!windowId) {
153
+ return;
154
+ }
155
+
156
+ if (!win || win.closed) {
157
+ this.logger?.trace(`${windowId} detected as closed, processing window cleanup`);
158
+ return this.cleanUpWindow(windowId);
159
+ }
160
+
161
+ this.logger?.trace(`${windowId} detected as not closed, adding to state controller`);
162
+ this.stateController.add(win, windowId);
163
+ }
164
+
165
+ public cleanUpWindow(windowId: string): void {
166
+
167
+ this.stateController.remove(windowId);
168
+
169
+ const somethingRemoved = this.sessionController.fullWindowClean(windowId);
170
+
171
+
172
+ if (somethingRemoved) {
173
+ this.glueController.clearContext(windowId, "window").catch(() => { });
174
+ this.emitStreamData("windowRemoved", { windowId });
175
+ }
176
+ }
177
+
178
+ public async registerSelfAssignedWindow(data: SelfAssignedWindowData, commandId: string): Promise<void> {
179
+ this.logger?.trace(`[${commandId}] handling workspace window registration with id: ${data.windowId} and name: ${data.name}`);
180
+
181
+ this.sessionController.saveWindowData({ windowId: data.windowId, name: data.name, selfAssigned: true });
182
+
183
+ this.sessionController.saveNonGlue({ windowId: data.windowId });
184
+
185
+ this.emitStreamData("windowAdded", { windowId: data.windowId, name: data.name });
186
+
187
+ this.logger?.trace(`[${commandId}] workspace window registered successfully with id ${data.windowId} and name ${data.name}`);
188
+ }
189
+
190
+ public async registerWorkspaceWindow(data: WorkspaceWindowData, commandId: string): Promise<void> {
191
+ this.logger?.trace(`[${commandId}] handling workspace window registration with id: ${data.windowId} and name: ${data.name}`);
192
+
193
+ this.sessionController.saveWindowData({ windowId: data.windowId, name: data.name });
194
+
195
+ this.sessionController.saveWorkspaceClient({ windowId: data.windowId, frameId: data.frameId, initialTitle: data.title, workspaceId: data.workspaceId });
196
+
197
+ this.sessionController.saveNonGlue({ windowId: data.windowId });
198
+
199
+ const hibernatedContext = await this.glueController.pullHibernatedContext(data.windowId);
200
+
201
+ const startContext = data.context || hibernatedContext;
202
+
203
+ if (startContext) {
204
+ await this.glueController.setStartContext(data.windowId, startContext, "window");
205
+ }
206
+
207
+ this.emitStreamData("windowAdded", { windowId: data.windowId, name: data.name });
208
+
209
+ this.logger?.trace(`[${commandId}] workspace window registered successfully with id ${data.windowId} and name ${data.name}`);
210
+ }
211
+
212
+ private async handleFocusEvent(data: FocusEventData, commandId: string): Promise<void> {
213
+ this.logger?.trace(`[${commandId}] handling focus event from window id: ${data.windowId} and hasFocus: ${data.hasFocus}`);
214
+
215
+ this.emitStreamData("focusChange", data);
216
+
217
+ this.logger?.trace(`[${commandId}] focus event from window id: ${data.windowId} and hasFocus: ${data.hasFocus} handled`);
218
+ }
219
+
220
+ private async handleOperationCheck(config: OperationCheckConfig): Promise<OperationCheckResult> {
221
+ const operations = Object.keys(this.operations);
222
+
223
+ const isSupported = operations.some((operation) => operation.toLowerCase() === config.operation.toLowerCase());
224
+
225
+ return { isSupported };
226
+ }
227
+
228
+ private emitStreamData(operation: string, data: any): void {
229
+ this.logger?.trace(`sending notification of event: ${operation} with data: ${JSON.stringify(data)}`);
230
+ this.glueController.pushSystemMessage("windows", operation, data);
231
+ }
232
+
233
+ private async openWindow(config: OpenWindowConfig, commandId: string): Promise<OpenWindowSuccess> {
234
+
235
+ const nameExists = this.sessionController.getWindowDataByName(config.name);
236
+
237
+ if (nameExists) {
238
+ throw new Error(`Cannot open a window with name: ${config.name}, because a window with that name already exists.`);
239
+ }
240
+
241
+ this.logger?.trace(`[${commandId}] handling open command with a valid name: ${config.name}, url: ${config.url} and options: ${JSON.stringify(config.options)}`);
242
+
243
+ const openBounds = await this.getStartingBounds(config, commandId);
244
+
245
+ const windowId = (config as any).options?.windowId ?? `g42-${generate()}`;
246
+
247
+ const windowData: SessionWindowData = {
248
+ name: config.name,
249
+ windowId,
250
+ initialBounds: openBounds,
251
+ initialUrl: config.url,
252
+ initialContext: config.options?.context,
253
+ layoutComponentId: (config as any).options?.layoutComponentId
254
+ };
255
+
256
+ const options = `left=${openBounds.left},top=${openBounds.top},width=${openBounds.width},height=${openBounds.height}`;
257
+
258
+ this.logger?.trace(`[${commandId}] calling native window open with bounds: ${options}`);
259
+
260
+ const childWindow = window.open(config.url, windowData.windowId, options);
261
+
262
+ if (!childWindow) {
263
+ throw new Error(`Cannot open window with url: ${config.url} and name: ${config.name}. The most likely reason is that the user has not approved popups or has a blocker.`);
264
+ }
265
+
266
+ await this.processNewWindow(windowData, config.options?.context, childWindow);
267
+
268
+ this.logger?.trace(`[${commandId}] the new window is opened, saved in session, state and announced, responding to the caller`);
269
+
270
+ return windowData;
271
+ }
272
+
273
+ private async handleWindowHello(client: { windowId?: string }, commandId: string): Promise<HelloSuccess> {
274
+ this.logger?.trace(`[${commandId}] handling a hello message from a real windowId: ${client.windowId}`);
275
+
276
+ if (client.windowId) {
277
+ this.stateController.remove(client.windowId);
278
+
279
+ // this is an explicit remove with in most cases will not do anything, because the stateController removes the nonGlue, but not in the case of a workspace-registered window
280
+ this.sessionController.removeNonGlue({ windowId: client.windowId });
281
+
282
+ const workspaceClient = this.sessionController.getWorkspaceClientById(client.windowId);
283
+
284
+ if (workspaceClient && workspaceClient.initialTitle) {
285
+ const windowId = client.windowId;
286
+ const title = workspaceClient.initialTitle;
287
+
288
+ PromiseWrap<void>(() => this.glueController.callWindow<WindowTitleConfig, void>("windows", this.operations.setTitle, { windowId, title }, { windowId }), this.clientResponseTimeoutMs)
289
+ .catch((err) => this.logger?.trace(`[${commandId}] error while setting the workspace window title: ${err.message}`));
290
+ }
291
+ }
292
+
293
+ const isWorkspaceFrame = !!(client.windowId && this.sessionController.getFrameData(client.windowId));
294
+
295
+ const allWindows = this.sessionController.getAllWindowsData().map<OpenWindowSuccess>((w) => ({ windowId: w.windowId, name: w.name }));
296
+
297
+ this.logger?.trace(`[${commandId}] a full list of all current windows has been compiled, sending it to the caller`);
298
+
299
+ return { windows: allWindows, isWorkspaceFrame };
300
+ }
301
+
302
+ private handleGetUrl(data: SimpleWindowCommand, commandId: string): Promise<WindowUrlResult> {
303
+
304
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
305
+
306
+ if (!windowData) {
307
+ throw new Error(`Cannot get the url of window: ${data.windowId}, because it is does not exist for the platform`);
308
+ }
309
+
310
+ this.logger?.trace(`[${commandId}] handling a get url request for window ${data.windowId}`);
311
+
312
+ const timeoutMessage = `Cannot get the url of window: ${data.windowId}, because it is either a non-glue window or it hasn't initiated it's glue yet`;
313
+
314
+ return PromiseWrap<WindowUrlResult>(() => this.glueController.callWindow<SimpleWindowCommand, WindowUrlResult>("windows", this.operations.getUrl, data, { windowId: data.windowId }), this.clientResponseTimeoutMs, timeoutMessage);
315
+ }
316
+
317
+ private handleGetTitle(data: SimpleWindowCommand, commandId: string): Promise<WindowTitleConfig> {
318
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
319
+
320
+ if (!windowData) {
321
+ throw new Error(`Cannot get the title of window: ${data.windowId}, because it is does not exist for the platform`);
322
+ }
323
+
324
+ this.logger?.trace(`[${commandId}] handling a get title request for window ${data.windowId}`);
325
+
326
+ const timeoutMessage = `Cannot get the title of window: ${data.windowId}, because it is either a non-glue window or it hasn't initiated it's glue yet`;
327
+
328
+ return PromiseWrap<WindowTitleConfig>(() => this.glueController.callWindow<SimpleWindowCommand, WindowTitleConfig>("windows", this.operations.getTitle, data, { windowId: data.windowId }), this.clientResponseTimeoutMs, timeoutMessage);
329
+ }
330
+
331
+ private async handleSetTitle(data: WindowTitleConfig, commandId: string): Promise<void> {
332
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
333
+
334
+ if (!windowData) {
335
+ throw new Error(`Cannot set the title of window: ${data.windowId}, because it is does not exist for the platform`);
336
+ }
337
+
338
+ const workspaceClient = this.sessionController.getWorkspaceClientById(data.windowId);
339
+
340
+ if (workspaceClient) {
341
+ await this.ioc.workspacesController.setItemTitle({ itemId: data.windowId, title: data.title }, commandId);
342
+ }
343
+
344
+ this.logger?.trace(`[${commandId}] handling a set title request for window ${data.windowId} and title: ${data.title}`);
345
+
346
+ const timeoutMessage = `Cannot set the title of window: ${data.windowId}, because it is either a non-glue window or it hasn't initiated it's glue yet`;
347
+
348
+ await PromiseWrap<void>(() => this.glueController.callWindow<WindowTitleConfig, void>("windows", this.operations.setTitle, data, { windowId: data.windowId }), this.clientResponseTimeoutMs, timeoutMessage);
349
+ }
350
+
351
+ private async handleMoveResize(data: WindowMoveResizeConfig, commandId: string): Promise<void> {
352
+ const workspaceClient = this.sessionController.getWorkspaceClientById(data.windowId);
353
+
354
+ if (workspaceClient) {
355
+ throw new Error(`Cannot move resize window id ${data.windowId}, because it is in a workspace. Consider using the workspaces API to get more control`);
356
+ }
357
+
358
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
359
+
360
+ if (!windowData) {
361
+ throw new Error(`Cannot move resize window: ${data.windowId}, because it is does not exist for the platform`);
362
+ }
363
+
364
+ if (windowData.name === "Platform") {
365
+ throw new Error("Move-resizing the main application is not allowed");
366
+ }
367
+
368
+ this.logger?.trace(`[${commandId}] handling a move resize request for window ${data.windowId} and data: ${JSON.stringify(data)}`);
369
+
370
+ const timeoutMessage = `Cannot move resize window: ${data.windowId}, because it is either a non-glue window or it hasn't initiated it's glue yet`;
371
+
372
+ await PromiseWrap<void>(() => this.glueController.callWindow<WindowMoveResizeConfig, void>("windows", this.operations.moveResize, data, { windowId: data.windowId }), this.clientResponseTimeoutMs, timeoutMessage);
373
+
374
+ // this is needed because the browser's resize API is sync, but the window bounds in the browser are not updated sync
375
+ await this.pause(500);
376
+ }
377
+
378
+ private handleGetBounds(data: SimpleWindowCommand, commandId: string): Promise<WindowBoundsResult> {
379
+ const workspaceClient = this.sessionController.getWorkspaceClientById(data.windowId);
380
+
381
+ if (workspaceClient) {
382
+ throw new Error(`Cannot get bounds of window id ${data.windowId}, because it is in a workspace. Consider using the workspaces API to get more info`);
383
+ }
384
+
385
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
386
+
387
+ if (!windowData) {
388
+ throw new Error(`Cannot get the bounds of window: ${data.windowId}, because it is does not exist for the platform`);
389
+ }
390
+
391
+ this.logger?.trace(`[${commandId}] handling a get bounds request for window ${data.windowId}`);
392
+
393
+ const timeoutMessage = `Cannot get the bounds of window: ${data.windowId}, because it is either a non-glue window or it hasn't initiated it's glue yet`;
394
+
395
+ return PromiseWrap<WindowBoundsResult>(() => this.glueController.callWindow<SimpleWindowCommand, WindowBoundsResult>("windows", this.operations.getBounds, data, { windowId: data.windowId }), this.clientResponseTimeoutMs, timeoutMessage);
396
+ }
397
+
398
+ private async handleFocus(data: SimpleWindowCommand, commandId: string): Promise<void> {
399
+ const workspaceClient = this.sessionController.getWorkspaceClientById(data.windowId);
400
+
401
+ if (workspaceClient) {
402
+ throw new Error(`Cannot focus window id ${data.windowId}, because it is in a workspace. Consider using the workspaces API to get more control`);
403
+ }
404
+
405
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
406
+
407
+ if (!windowData) {
408
+ throw new Error(`Cannot focus window: ${data.windowId}, because it is not known by the platform`);
409
+ }
410
+
411
+ this.logger?.trace(`[${commandId}] handling a focus request for window ${data.windowId}`);
412
+
413
+ window.open(undefined, windowData.windowId);
414
+ }
415
+
416
+ private async handleClose(data: SimpleWindowCommand, commandId: string): Promise<void> {
417
+ const workspaceClient = this.sessionController.getWorkspaceClientById(data.windowId);
418
+
419
+ if (workspaceClient) {
420
+ this.logger?.trace(`[${commandId}] this window is detected as a workspace window, closing via the workspaces controller`);
421
+
422
+ await this.ioc.workspacesController.closeItem({ itemId: data.windowId }, commandId);
423
+
424
+ return;
425
+ }
426
+
427
+ const applicationInstance = this.sessionController.getInstanceData(data.windowId);
428
+
429
+ if (applicationInstance) {
430
+ this.logger?.trace(`[${commandId}] this window is detected as an application instance, closing via the appManager controller`);
431
+
432
+ await this.ioc.applicationsController.handleInstanceStop({ id: data.windowId }, commandId);
433
+
434
+ return;
435
+ }
436
+
437
+ const windowData = this.sessionController.getWindowDataById(data.windowId);
438
+
439
+ if (!windowData) {
440
+ throw new Error(`Cannot close window: ${data.windowId}, because it is not known by the platform`);
441
+ }
442
+
443
+ if (windowData.name === "Platform") {
444
+ throw new Error("Closing the main application is not allowed");
445
+ }
446
+
447
+ if (windowData.selfAssigned) {
448
+ throw new Error("Closing self-assigned windows (windows not opened by the Glue API) is not allowed");
449
+ }
450
+
451
+ this.logger?.trace(`[${commandId}] handling a close request for window ${data.windowId}`);
452
+
453
+ window.open(undefined, windowData.windowId)?.close();
454
+
455
+ this.cleanUpWindow(windowData.windowId);
456
+
457
+ this.logger?.trace(`[${commandId}] window ${data.windowId} has been closed, removed from session, state and announced`);
458
+ }
459
+
460
+ private async getStartingBounds(config: OpenWindowConfig, commandId: string): Promise<IOConnectBrowser.Windows.Bounds> {
461
+ const openBounds = {
462
+ top: config.options?.top ?? this.defaultBounds.top,
463
+ left: config.options?.left ?? this.defaultBounds.left,
464
+ height: config.options?.height ?? this.defaultBounds.height,
465
+ width: config.options?.width ?? this.defaultBounds.width
466
+ };
467
+
468
+ if (!config.options?.relativeTo) {
469
+ return openBounds;
470
+ }
471
+
472
+ const relativeWindowId = config.options.relativeTo;
473
+
474
+ const windowData = this.sessionController.getWindowDataById(relativeWindowId);
475
+
476
+ if (!windowData) {
477
+ return openBounds;
478
+ }
479
+
480
+ try {
481
+ const boundsResult = await this.handleGetBounds({ windowId: windowData.windowId }, commandId);
482
+
483
+ const relativeWindowBounds = boundsResult.bounds;
484
+
485
+ const relativeDir = config.options.relativeDirection ?? "right";
486
+
487
+ const newBounds = getRelativeBounds(openBounds, relativeWindowBounds, relativeDir);
488
+
489
+ return newBounds;
490
+ } catch (error) {
491
+ return openBounds;
492
+ }
493
+ }
494
+
495
+ private pause(ms: number): Promise<void> {
496
+ return new Promise((resolve) => setTimeout(resolve, ms));
497
+ }
498
+ }
@@ -0,0 +1,64 @@
1
+ import { boolean, constant, Decoder, number, object, oneOf, optional, string } from "decoder-validate";
2
+ import { nonEmptyStringDecoder, nonNegativeNumberDecoder, windowBoundsDecoder, windowOpenSettingsDecoder } from "../../shared/decoders";
3
+ import { FrameWindowBoundsResult, OpenWindowConfig, OpenWindowSuccess, SimpleWindowCommand, WindowBoundsResult, WindowMoveResizeConfig, WindowOperationsTypes, WindowTitleConfig, WindowUrlResult } from "./types";
4
+
5
+ export const windowOperationDecoder: Decoder<WindowOperationsTypes> = oneOf<"openWindow" | "windowHello" | "getUrl" | "getTitle" | "setTitle" | "moveResize" | "focus" | "close" | "getBounds" | "getFrameBounds" | "registerWorkspaceWindow" | "unregisterWorkspaceWindow" | "operationCheck" | "focusChange">(
6
+ constant("openWindow"),
7
+ constant("windowHello"),
8
+ constant("getUrl"),
9
+ constant("getTitle"),
10
+ constant("setTitle"),
11
+ constant("moveResize"),
12
+ constant("focus"),
13
+ constant("close"),
14
+ constant("getBounds"),
15
+ constant("getFrameBounds"),
16
+ constant("registerWorkspaceWindow"),
17
+ constant("unregisterWorkspaceWindow"),
18
+ constant("operationCheck"),
19
+ constant("focusChange")
20
+ );
21
+
22
+ export const openWindowConfigDecoder: Decoder<OpenWindowConfig> = object({
23
+ name: nonEmptyStringDecoder,
24
+ url: nonEmptyStringDecoder,
25
+ options: optional(windowOpenSettingsDecoder)
26
+ });
27
+
28
+ export const openWindowSuccessDecoder: Decoder<OpenWindowSuccess> = object({
29
+ windowId: nonEmptyStringDecoder,
30
+ name: nonEmptyStringDecoder
31
+ });
32
+
33
+ export const simpleWindowDecoder: Decoder<SimpleWindowCommand> = object({
34
+ windowId: nonEmptyStringDecoder
35
+ });
36
+
37
+ export const windowBoundsResultDecoder: Decoder<WindowBoundsResult> = object({
38
+ windowId: nonEmptyStringDecoder,
39
+ bounds: windowBoundsDecoder
40
+ });
41
+
42
+ export const frameWindowBoundsResultDecoder: Decoder<FrameWindowBoundsResult> = object({
43
+ bounds: windowBoundsDecoder
44
+ });
45
+
46
+ export const windowUrlResultDecoder: Decoder<WindowUrlResult> = object({
47
+ windowId: nonEmptyStringDecoder,
48
+ url: nonEmptyStringDecoder
49
+ });
50
+
51
+ export const windowMoveResizeConfigDecoder: Decoder<WindowMoveResizeConfig> = object({
52
+ windowId: nonEmptyStringDecoder,
53
+ top: optional(number()),
54
+ left: optional(number()),
55
+ width: optional(nonNegativeNumberDecoder),
56
+ height: optional(nonNegativeNumberDecoder),
57
+ relative: optional(boolean())
58
+ });
59
+
60
+ export const windowTitleConfigDecoder: Decoder<WindowTitleConfig> = object({
61
+ windowId: nonEmptyStringDecoder,
62
+ title: string()
63
+ });
64
+
@@ -0,0 +1,59 @@
1
+ import { IOConnectBrowser } from "@interopio/browser";
2
+
3
+ export type WindowOperationsTypes = "openWindow" |
4
+ "windowHello" | "getUrl" | "getTitle" | "setTitle" |
5
+ "moveResize" | "focus" | "close" | "getBounds" | "getFrameBounds" |
6
+ "registerWorkspaceWindow" | "unregisterWorkspaceWindow" | "operationCheck" | "focusChange";
7
+
8
+ export interface OpenWindowConfig {
9
+ name: string;
10
+ url: string;
11
+ options?: IOConnectBrowser.Windows.Settings;
12
+ }
13
+
14
+ export interface OpenWindowSuccess {
15
+ windowId: string;
16
+ name: string;
17
+ }
18
+
19
+ export interface HelloSuccess {
20
+ windows: OpenWindowSuccess[];
21
+ isWorkspaceFrame: boolean;
22
+ }
23
+
24
+ export interface SimpleWindowCommand {
25
+ windowId: string;
26
+ }
27
+
28
+ export interface WindowTitleConfig {
29
+ windowId: string;
30
+ title: string;
31
+ }
32
+
33
+ export interface WindowMoveResizeConfig {
34
+ windowId: string;
35
+ top?: number;
36
+ left?: number;
37
+ width?: number;
38
+ height?: number;
39
+ relative?: boolean;
40
+ }
41
+
42
+ export interface WindowBoundsResult {
43
+ windowId: string;
44
+ bounds: IOConnectBrowser.Windows.Bounds;
45
+ }
46
+
47
+ export interface FrameWindowBoundsResult {
48
+ bounds: IOConnectBrowser.Windows.Bounds;
49
+ }
50
+
51
+ export interface WindowUrlResult {
52
+ windowId: string;
53
+ url: string;
54
+ }
55
+
56
+ export interface SelfAssignedWindowData {
57
+ windowId: string;
58
+ name: string;
59
+ }