-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathelectron.ts
790 lines (658 loc) · 28.6 KB
/
electron.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
/**
* @module @morgan-stanley/desktopjs-electron
*/
import {
registerContainer, ContainerWindow, PersistedWindowLayout, Rectangle, Container, WebContainerBase,
ScreenManager, Display, Point, ObjectTransform, PropertyMap, NotificationOptions, ContainerNotification,
TrayIconDetails, MenuItem, Guid, MessageBus, MessageBusSubscription, MessageBusOptions, GlobalShortcutManager,
EventArgs, WindowEventArgs
} from "@morgan-stanley/desktopjs";
registerContainer("Electron", {
condition: () => {
try {
return typeof require !== "undefined" && (require("electron") || require("electron").remote);
} catch (e) {
return false;
}
},
create: (options) => new ElectronContainer(null, null, null, options)
});
class InternalMessageType {
public static readonly initialize: string = "desktopJS.window-initialize";
public static readonly getGroup: string = "desktopJS.window-getGroup";
public static readonly joinGroup: string = "desktopJS.window-joinGroup";
public static readonly leaveGroup: string = "desktopJS.window-leaveGroup";
public static readonly getOptions: string = "desktopJS.window-getOptions";
}
const windowEventMap = {};
/**
* @augments ContainerWindow
*/
export class ElectronContainerWindow extends ContainerWindow {
private readonly container: ElectronContainer;
private readonly window: Window;
public constructor(wrap: any, container: ElectronContainer, win?: Window) {
super(wrap);
this.container = container;
this.window = win;
}
private get isRemote(): boolean {
return ((<any> this.container.internalIpc).send);
}
public get id(): string {
return this.innerWindow.id || this.innerWindow.guestId;
}
public get name(): string {
return this.innerWindow.name;
}
public load(url: string, options?: any) : Promise<void> {
return new Promise<void>(resolve => {
if (options) {
this.innerWindow.loadURL(url, options);
} else {
this.innerWindow.loadURL(url);
}
resolve();
});
}
public focus(): Promise<void> {
this.innerWindow.focus();
return Promise.resolve();
}
public show(): Promise<void> {
this.innerWindow.show();
return Promise.resolve();
}
public hide(): Promise<void> {
this.innerWindow.hide();
return Promise.resolve();
}
public close(): Promise<void> {
this.innerWindow.close();
return Promise.resolve();
}
public maximize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.maximize();
resolve();
});
}
public minimize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.minimize();
resolve();
});
}
public restore(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.restore();
resolve();
});
}
public isShowing(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.innerWindow.isVisible());
});
}
public getSnapshot(): Promise<string> {
return new Promise<string>((resolve, reject) => {
this.innerWindow.capturePage((snapshot) => {
resolve("data:image/png;base64," + snapshot.toPNG().toString("base64"));
});
});
}
public flash(enable: boolean, options?: any): Promise<void> {
this.innerWindow.flashFrame(enable);
return Promise.resolve();
}
public getBounds(): Promise<Rectangle> {
return new Promise<Rectangle>(resolve => {
const { x, y, width, height } = this.innerWindow.getBounds();
resolve(new Rectangle(x, y, width, height));
});
}
public setBounds(bounds: Rectangle): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.setBounds(bounds);
resolve();
});
}
public get allowGrouping() {
return true;
}
public getGroup(): Promise<any[]> {
return new Promise<ContainerWindow[]>(resolve => {
const ids = (this.isRemote)
? (<any>this.container.internalIpc).sendSync(InternalMessageType.getGroup, { source: this.id })
: (<any>this.container).windowManager.getGroup(this.innerWindow);
resolve(ids.map(id => {return (id === this.id) ? this : this.container.wrapWindow(this.container.browserWindow.fromId(id)); }));
});
}
public joinGroup(target: ContainerWindow): Promise<void> {
if (!target || target.id === this.id) {
return Promise.resolve();
}
if (this.isRemote) {
(<any>this.container.internalIpc).send(InternalMessageType.joinGroup, { source: this.id, target: target.id });
} else {
(<any>this.container).windowManager.groupWindows(target.innerWindow, this.innerWindow);
}
return Promise.resolve();
}
public leaveGroup(): Promise<void> {
if (this.isRemote) {
(<any>this.container.internalIpc).send(InternalMessageType.leaveGroup, { source: this.id });
} else {
(<any>this.container).windowManager.ungroupWindows(this.innerWindow);
}
return Promise.resolve();
}
public bringToFront(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.moveTop();
resolve();
});
}
public getOptions(): Promise<any> {
return new Promise<any>((resolve, reject) => {
const options = (this.isRemote)
? (<any>this.container.internalIpc).sendSync(InternalMessageType.getOptions, { source: this.id })
: this.innerWindow[Container.windowOptionsPropertyKey];
resolve(options);
});
}
public getState(): Promise<any> {
if (this.innerWindow && this.innerWindow.webContents) {
return this.innerWindow.webContents.executeJavaScript("window.getState ? window.getState() : undefined");
} else {
return Promise.resolve(undefined);
}
}
public setState(state: any): Promise<void> {
const promise = (this.innerWindow && this.innerWindow.webContents)
? this.innerWindow.webContents.executeJavaScript(`if (window.setState) { window.setState(JSON.parse(\`${JSON.stringify(state)}\`)); }`)
: Promise.resolve();
return promise.then(() => {
this.emit("state-changed", <EventArgs> { name: "state-changed", sender: this, state: state });
ContainerWindow.emit("state-changed", <WindowEventArgs> { name: "state-changed", windowId: this.id, state: state } );
});
}
protected attachListener(eventName: string, listener: (...args: any[]) => void): void {
if (eventName === "beforeunload") {
const win = this.window || window;
if (win && this.id === this.container.getCurrentWindow().id) {
win.addEventListener("beforeunload", listener);
} else {
throw new Error("Event handler for 'beforeunload' can only be added on current window");
}
} else {
this.innerWindow.addListener(windowEventMap[eventName] || eventName, listener);
}
}
protected detachListener(eventName: string, listener: (...args: any[]) => void): void {
this.innerWindow.removeListener(windowEventMap[eventName] || eventName, listener);
}
public get nativeWindow(): Window {
// For Electron we can only return in and for current renderer
return this.window || window;
}
}
/**
* @augments MessageBus
*/
export class ElectronMessageBus implements MessageBus {
public ipc: any;
private browserWindow: any;
public constructor(ipc: any, browserWindow: any) {
this.ipc = ipc;
this.browserWindow = browserWindow;
}
public subscribe<T>(topic: string, listener: (event: any, message: T) => void, options?: MessageBusOptions): Promise<MessageBusSubscription> {
return new Promise<MessageBusSubscription>((resolve, reject) => {
const subscription: MessageBusSubscription = new MessageBusSubscription(topic, (event: any, message: any) => {
listener({ topic: topic }, message);
});
this.ipc.on(topic, subscription.listener);
resolve(subscription);
});
}
public unsubscribe(subscription: MessageBusSubscription): Promise<void> {
const { topic, listener, options } = subscription;
return Promise.resolve(<any>this.ipc.removeListener(topic, listener));
}
public publish<T>(topic: string, message: T, options?: MessageBusOptions): Promise<void> {
// If publisher is targeting a window, do not send to main
if (!options || !options.name) {
if ((<any>this.ipc).send !== undefined) {
// Publish to main from renderer (send is not available on ipcMain)
(<any>this.ipc).send(topic, message);
} else {
// we are in main so invoke listener directly
this.ipc.listeners(topic).forEach(cb => cb({ topic: topic }, message));
}
}
// Broadcast to all windows or to the individual targeted window
if (this.browserWindow && this.browserWindow.getAllWindows) {
for (const window of (this.browserWindow).getAllWindows()) {
if (options && options.name) {
if (options.name === window.name) {
window.webContents.send(topic, message);
}
} else {
window.webContents.send(topic, message);
}
}
}
return Promise.resolve();
}
}
/**
* @extends ContainerBase
*/
export class ElectronContainer extends WebContainerBase {
protected isRemote: Boolean = true;
protected electron: any;
protected app: any;
public browserWindow: any;
protected tray: any;
protected menu: any;
public internalIpc: any;
private windowManager: ElectronWindowManager;
private nodeIntegration: boolean;
/**
* Gets or sets whether to replace the native web Notification API with a wrapper around showNotification.
* @type {boolean}
* @default true
*/
public static replaceNotificationApi: boolean = true;
public static readonly windowOptionsMap: PropertyMap = {
taskbar: { target: "skipTaskbar", convert: (value: any, from: any, to: any) => { return !value; } },
node: {
target: "webPreferences", convert: (value: any, from: any, to: any) => {
return Object.assign(to.webPreferences || {}, { nodeIntegration: value });
}
}
};
public windowOptionsMap: PropertyMap = ElectronContainer.windowOptionsMap;
public constructor(electron?: any, ipc?: any, win?: any, options?: any) {
super(win);
this.hostType = "Electron";
try {
if (electron) {
this.electron = electron;
} else {
// Check if we are in renderer or main by first accessing remote, if undefined switch to main
this.electron = require("electron").remote;
if (typeof this.electron === "undefined") {
this.electron = require("electron");
this.isRemote = false;
}
}
this.app = this.electron.app;
this.browserWindow = this.electron.BrowserWindow;
this.tray = this.electron.Tray;
this.menu = this.electron.Menu;
this.internalIpc = ipc || ((this.isRemote) ? require("electron").ipcRenderer : this.electron.ipcMain);
this.ipc = this.createMessageBus();
if (!this.isRemote || (options && typeof options.isRemote !== "undefined" && !options.isRemote)) {
this.windowManager = new ElectronWindowManager(this.app, this.internalIpc, this.browserWindow);
this.app.on("browser-window-created", (event, window) => {
setImmediate(() => {
Container.emit("window-created", { name: "window-created", windowId: window.webContents.id });
ContainerWindow.emit("window-created", { name: "window-created", windowId: window.webContents.id });
});
});
}
} catch (e) {
console.error(e);
}
let replaceNotificationApi = ElectronContainer.replaceNotificationApi;
if (options && typeof options.replaceNotificationApi !== "undefined") {
replaceNotificationApi = options.replaceNotificationApi;
}
if (replaceNotificationApi) {
this.registerNotificationsApi();
}
this.nodeIntegration = (options && options.node != null) ? options.node : null;
this.screen = new ElectronDisplayManager(this.electron);
this.globalShortcut = new ElectronGlobalShortcutManager(this.electron);
}
protected createMessageBus() : MessageBus {
return new ElectronMessageBus(this.internalIpc, this.browserWindow);
}
protected registerNotificationsApi() {
if (typeof this.globalWindow !== "undefined" && this.globalWindow) {
// Define owningContainer for closure to inner class
const owningContainer: ElectronContainer = this; // tslint:disable-line
this.globalWindow["Notification"] = class ElectronNotification extends ContainerNotification {
constructor(title: string, options?: NotificationOptions) {
super(title, options);
options["notification"] = this;
owningContainer.showNotification(title, options);
}
};
}
}
public getMainWindow(): ContainerWindow {
for (const window of this.browserWindow.getAllWindows()) {
if (window[Container.windowOptionsPropertyKey] && window[Container.windowOptionsPropertyKey].main) {
return this.wrapWindow(window);
}
}
// No windows were marked as main so fallback to the first window created as being main
const win = this.browserWindow.fromId(1);
return win ? this.wrapWindow(win) : undefined;
}
public getCurrentWindow(): ContainerWindow {
return this.wrapWindow(this.electron.getCurrentWindow());
}
protected getWindowOptions(options?: any): any {
// If we have any container level node default, apply it here if no preference is specified in the options
if (this.nodeIntegration != null && !("node" in options)) {
options.node = this.nodeIntegration;
}
return ObjectTransform.transformProperties(options, this.windowOptionsMap);
}
public wrapWindow(containerWindow: any) {
return new ElectronContainerWindow(containerWindow, this);
}
public createWindow(url: string, options?: any): Promise<ContainerWindow> {
const newOptions = this.getWindowOptions(options);
const electronWindow: any = new this.browserWindow(newOptions);
const windowName = newOptions.name || Guid.newGuid();
/*
If we are in the renderer process, we need to ipc to the main process
to set the window name. Otherwise it can not be seen from other renderer
processes.
This requires the main process to have desktopJS container listening
either via desktopJS.resolveContainer() or new desktopJS.Electron.ElectronContainer();
If it is not listening, this ipc call will hang indefinitely. If we are in the
main process we can just directly set the name.
*/
if (this.isRemote) {
electronWindow["name"] = (<any>this.internalIpc).sendSync(InternalMessageType.initialize, { id: electronWindow.id, name: windowName, options: newOptions });
electronWindow[Container.windowOptionsPropertyKey] = options;
} else {
this.windowManager.initializeWindow(electronWindow, windowName, newOptions);
}
electronWindow.loadURL(this.ensureAbsoluteUrl(url));
const newWindow = this.wrapWindow(electronWindow);
this.emit("window-created", { sender: this, name: "window-created", window: newWindow, windowId: electronWindow.id, windowName: windowName });
return Promise.resolve(newWindow);
}
public showNotification(title: string, options?: NotificationOptions) {
const Notification = this.electron.Notification;
const notify = new Notification(Object.assign(options || {}, { title: title }));
if (options["onClick"]) {
notify.addListener("click", options["onClick"]);
}
if (options["notification"]) {
notify.once("show", () => notify.addListener("click", options["notification"]["onclick"]));
}
notify.show();
}
public addTrayIcon(details: TrayIconDetails, listener: () => void, menuItems?: MenuItem[]) {
const tray = new this.tray(details.icon);
if (details.text) {
tray.setToolTip(details.text);
}
if (menuItems) {
tray.setContextMenu(this.menu.buildFromTemplate(menuItems));
}
if (listener) {
tray.on("click", listener);
}
}
protected closeAllWindows(excludeSelf?: Boolean): Promise<void> {
return new Promise<void>((resolve, reject) => {
for (const window of this.browserWindow.getAllWindows()) {
if (!excludeSelf || window !== this.electron.getCurrentWindow()) {
window.close();
}
}
resolve();
});
}
public getAllWindows(): Promise<ContainerWindow[]> {
return Promise.resolve(this.browserWindow.getAllWindows().map(window => this.wrapWindow(window)));
}
public getWindowById(id: string): Promise<ContainerWindow | null> {
return new Promise<ContainerWindow>((resolve, reject) => {
const win = this.browserWindow.fromId(id);
resolve(win ? this.wrapWindow(win) : null);
});
}
public getWindowByName(name: string): Promise<ContainerWindow | null> {
return new Promise<ContainerWindow>((resolve, reject) => {
const win = this.browserWindow.getAllWindows().find(window => window.name === name);
resolve(win ? this.wrapWindow(win) : null);
});
}
public buildLayout(): Promise<PersistedWindowLayout> {
const layout = new PersistedWindowLayout();
const mainWindow = this.getMainWindow().innerWindow;
const promises: Promise<void>[] = [];
return new Promise<PersistedWindowLayout>((resolve, reject) => {
this.getAllWindows().then(windows => {
windows.forEach(window => {
const options = window.innerWindow[Container.windowOptionsPropertyKey];
if (options && "persist" in options && !options.persist) {
return;
}
promises.push(new Promise<void>(innerResolve => {
window.getGroup().then(async group => {
layout.windows.push(
{
id: window.id,
name: window.name,
url: window.innerWindow.webContents.getURL(),
main: (mainWindow === window.innerWindow),
state: await window.getState(),
options: options,
bounds: window.innerWindow.getBounds(),
group: group.map(win => win.id)
});
innerResolve();
});
}));
});
Promise.all(promises).then(() => {
resolve(layout);
}).catch(reject);
});
});
}
}
export class ElectronWindowManager {
private app: any;
private ipc: NodeJS.EventEmitter;
private browserWindow: any;
private lastBounds: Map<number, Rectangle> = new Map(); // BrowserWindow.id -> Rectangle
private ignoredWindows: number[] = []; // Array of BrowserWindow.id
public constructor(app?: any, ipc?: any, browserWindow?: any) {
this.app = app || require("electron").app;
this.ipc = ipc || require("electron").ipcMain;
this.browserWindow = browserWindow || require("electron").BrowserWindow;
this.ipc.on(InternalMessageType.initialize, (event: any, message: any) => {
const { id, name, options } = message;
const win = this.browserWindow.fromId(id);
this.initializeWindow(win, name, options);
event.returnValue = name;
});
this.ipc.on(InternalMessageType.joinGroup, (event: any, message: any) => {
const { "source": sourceId, "target": targetId } = message;
const source = this.browserWindow.fromId(sourceId);
const target = this.browserWindow.fromId(targetId);
this.groupWindows(target, source);
});
this.ipc.on(InternalMessageType.leaveGroup, (event: any, message: any) => {
const { "source": sourceId } = message;
this.ungroupWindows(this.browserWindow.fromId(sourceId));
});
this.ipc.on(InternalMessageType.getGroup, (event: any, message: any) => {
const { "source": sourceId } = message;
event.returnValue = this.getGroup(this.browserWindow.fromId(sourceId));
});
this.ipc.on(InternalMessageType.getOptions, (event: any, message: any) => {
const { "source": sourceId } = message;
event.returnValue = this.browserWindow.fromId(sourceId)[Container.windowOptionsPropertyKey];
});
}
public initializeWindow(win: any, name: string, options: any) {
win.name = name;
win[Container.windowOptionsPropertyKey] = options;
if (options && options.main && (!("quitOnClose" in options) || options.quitOnClose)) {
win.on("closed", () => {
this.app.quit();
});
}
}
private registerWindowEvents(win: any) {
if (win && !win.moveHandler) {
this.lastBounds.set(win.id, win.getBounds());
win.moveHandler = (e) => this.handleMove(win);
win.on("move", win.moveHandler);
}
}
private unregisterWindowEvents(win: any) {
if (win && win.moveHandler) {
this.lastBounds.delete(win.id);
win.removeListener("move", win.moveHandler);
delete win.moveHandler;
}
}
public getGroup(window: any): any[] {
return (window.group)
? this.browserWindow.getAllWindows().filter(win => { return (win.group === window.group); }).map(win => win.id)
: [];
}
public groupWindows(target: any, ...windows: any[]) {
for (const win of windows) {
win.group = target.group || (target.group = Guid.newGuid());
this.registerWindowEvents(win);
ContainerWindow.emit("window-joinGroup", { name: "window-joinGroup", windowId: win.id, targetWindowId: target.id });
}
this.registerWindowEvents(target);
}
public ungroupWindows(...windows: any[]) {
// Unhook and clear group of all provided windows
for (const win of windows) {
this.unregisterWindowEvents(win);
win.group = null;
ContainerWindow.emit("window-leaveGroup", { name: "window-leaveGroup", windowId: win.id });
}
// Group all windows by group and for any group consisting of one window unhook and clear the group
this.groupBy<any>(this.browserWindow.getAllWindows(), "group").filter(group => group.key && group.values && group.values.length === 1).forEach(group => {
for (const win of group.values) {
this.unregisterWindowEvents(win);
win.group = null;
}
});
}
private handleMove(win: any): void {
// Grab the last bounds we had and the current and then store the current
const oldBounds = this.lastBounds.get(win.id);
const newBounds = win.getBounds();
this.lastBounds.set(win.id, newBounds);
// If the height or width change this is a resize and we should just exit out
if (oldBounds.width !== newBounds.width || oldBounds.height !== newBounds.height) {
return;
}
// Prevent cycles
if (this.ignoredWindows.indexOf(win.id) >= 0) {
return;
}
if (win.group) {
// Get all windows other windows in same group
const groupedWindows = this.browserWindow.getAllWindows().filter(window => { return (window.group === win.group && window.id !== win.id); });
if (groupedWindows && groupedWindows.length > 0) {
const diff = { x: oldBounds.x - newBounds.x, y: oldBounds.y - newBounds.y };
groupedWindows.forEach(groupedWindow => {
const targetBounds = groupedWindow.getBounds();
this.ignoredWindows.push(groupedWindow.id);
groupedWindow.setBounds({ x: targetBounds.x - diff.x, y: targetBounds.y - diff.y, width: targetBounds.width, height: targetBounds.height }, true);
this.ignoredWindows.splice(this.ignoredWindows.indexOf(groupedWindow.id), 1);
});
}
}
}
private groupBy<T>(array: T[], groupBy: any): { key: any, values: T[] }[] {
return array.reduce((accumulator, current) => {
const key = groupBy instanceof Function ? groupBy(current) : current[groupBy];
const group = accumulator.find((r) => r && r.key === key);
if (group) {
group.values.push(current);
} else {
accumulator.push({ key: key, values: [current] });
}
return accumulator;
}, []);
}
}
/** @private */
class ElectronDisplayManager implements ScreenManager {
private readonly electron: any;
public constructor(electron: any) {
this.electron = electron;
}
createDisplay(monitorDetails: any) {
const display = new Display();
display.id = monitorDetails.id;
display.scaleFactor = monitorDetails.scaleFactor;
display.bounds = new Rectangle(monitorDetails.bounds.x,
monitorDetails.bounds.y,
monitorDetails.bounds.width,
monitorDetails.bounds.height);
display.workArea = new Rectangle(monitorDetails.workArea.x,
monitorDetails.workArea.y,
monitorDetails.workArea.width,
monitorDetails.workArea.height);
return display;
}
public getPrimaryDisplay(): Promise<Display> {
return new Promise<Display>((resolve, reject) => {
resolve(this.createDisplay(this.electron.screen.getPrimaryDisplay()));
});
}
public getAllDisplays(): Promise<Display[]> {
return new Promise<Display[]>((resolve, reject) => {
resolve(this.electron.screen.getAllDisplays().map(this.createDisplay));
});
}
public getMousePosition(): Promise<Point> {
return new Promise<Point>((resolve, reject) => {
resolve(this.electron.screen.getCursorScreenPoint());
});
}
}
/** @private */
class ElectronGlobalShortcutManager extends GlobalShortcutManager {
private readonly electron: any;
public constructor(electron: any) {
super();
this.electron = electron;
}
public register(shortcut: string, callback: () => void): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.electron.globalShortcut.register(shortcut, callback);
resolve();
});
}
public isRegistered(shortcut: string): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.electron.globalShortcut.isRegistered(shortcut));
});
}
public unregister(shortcut: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.electron.globalShortcut.unregister(shortcut);
resolve();
});
}
public unregisterAll(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.electron.globalShortcut.unregisterAll();
resolve();
});
}
}