forked from tamkeen-tms/electron-window-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1130 lines (952 loc) · 31.3 KB
/
index.js
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Hi there. Good luck with your Electron app.
* Please check out the full documentation on npm / github
*
* https://npmjs.com/package/electron-window-manager
* https://github.com/TamkeenLMS/electron-window-manager
*
* ~ zain
* */
'use strict';
const Electron = require('electron');
const Application = Electron.app;
const BrowserWindow = Electron.BrowserWindow;
const EventEmitter = new (require('events').EventEmitter);
const FileSystem = require('fs');
const WatchJS = require('melanke-watchjs');
const Shortcuts = require('electron-localshortcut');
const isObject = obj => {
const type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};
const isString = str => {
return Object.prototype.toString.call(str) === '[object String]';
};
/**
* Creates a new Window instance
*
* @param name [optional] The code name for the window, each window must have a unique name
* @param title [optional] The window title
* @param url [optional] The targeted page/url of the window
* @param setupTemplate [optional] The name of the setup template you want to use with this new window
* @param setup [optional] The setup object that will be passed to the BrowserWindow module
* @param showDevTools [optional] Whether to show the dev tools or not, false by default
* */
const Window = function(name, title, url, setupTemplate, setup, showDevTools){
// Check if the window already exists
if(windowManager.windows[name]){
console.log('Window ' + name + ' already exists!');
// Move the focus on it
windowManager.focusOn(name);
return;
}
// The window unique name, if omitted a serialized name will be used instead; window_1 ~> window_2 ~> ...
this.name = name || ( 'window_' + ( Object.keys(windowManager.windows).length + 1 ) );
// The BrowserWindow module instance
this.object = null;
this.setup = {
'show': false,
'setupTemplate': setupTemplate
};
if(title) this.setup.title = title;
if(url) this.setup.url = url;
if(showDevTools) this.setup.showDevTools = showDevTools;
// If the setup is just the window dimensions, like '500x350'
if(isString(setup) && setup.indexOf('x') >= 0){
const dimensions = setup.split('x');
setup = {
'width': parseInt(dimensions[0], 10),
'height': parseInt(dimensions[1], 10)
};
}
// Overwrite the default setup
if(isObject(setup)){
this.setup = Object.assign(this.setup, setup);
}
// Register the window on the window manager
windowManager.windows[this.name] = this;
};
/**
* Updates the window setup
* */
Window.prototype.set = function(prop, value){
if(value){
this.setup[prop] = value;
}else if(isObject(prop)){
this.setup = Object.assign(this.setup, prop);
}
};
/**
* Sets the window preferred layout
* @param name The name of the layout, registered using layouts.add()
* */
Window.prototype.useLayout = function(name){
// Set the window's layout
this.setup.layout = name;
};
/**
* Sets the setup template to use
* */
Window.prototype.applySetupTemplate = function(name){
this.setup.setupTemplate = name;
};
/**
* Sets the target URL for the window
* */
Window.prototype.setURL = function(url){
this.setup.url = url;
};
/**
* Created window instance
* @param url [optional] The window target URL in case you didn't provide it in the constructor
* */
Window.prototype.create = function(url) {
let instance = this;
if(url){
this.setup.url = url;
}
// Get a copy of the window manager config
const config = windowManager.config;
// If a setup setupTemplate is provided
let template = this.setup.setupTemplate || config.defaultSetupTemplate;
if(template && this.setup.setupTemplate !== false){
// Get the setupTemplate
template = templates.get(template);
// Merge with this window setup
if(template){
this.setup = Object.assign(template, this.setup);
}else{
console.log('The setup template "' + template + '" wasn\'t found!');
}
}
// The title
{
if(!this.setup.title && config.defaultWindowTitle){
this.setup.title = config.defaultWindowTitle;
}
if(this.setup.title && config.windowsTitlePrefix && !config.defaultWindowTitle){
this.setup.title = config.windowsTitlePrefix + this.setup.title;
}
}
// Handle the "position" feature/property
if(this.setup.position){
// If an array was passed
if(Array.isArray(this.setup.position)){
this.setup.x = this.setup.position[0];
this.setup.y = this.setup.position[1];
}else{
// Resolve the position into x & y coordinates
const xy = utils.resolvePosition(this.setup);
if(xy){
this.setup.y = xy[1];
this.setup.x = xy[0];
}
}
}
// The defaults
if(!this.setup.resizable) this.setup.resizable = false;
if(!this.setup.useContentSize) this.setup.useContentSize = true;
if(!this.setup.x && !this.setup.y) this.setup.center = true;
if(!this.setup.destroyOnClose) this.setup.destroyOnClose = false;
// Create the new browser window instance, with the passed setup
this.object = new BrowserWindow(this.setup);
// Log the action
console.log('Window "' + this.name + '" was created');
// On load failure
this.object.webContents.on('did-fail-load', function(){
instance.down();
});
// If the width/height not provided!
const bounds = this.object.getBounds();
if(!this.setup.width) this.setup.width = bounds.width;
if(!this.setup.height) this.setup.height = bounds.height;
// Open the window target content/url
if(this.setup.url){
this.loadURL(this.setup.url);
}
// Set the window menu (null is valid to not have a menu at all)
if(this.setup.menu !== undefined){
this.object.setMenu(this.setup.menu);
}
// Show the dev tools ?
if(this.setup.showDevTools === true){
// Show the dev tools
this.object.toggleDevTools();
}
// On close
this.object.on('closed', function(){
console.log('Window "' + instance.name + '" was closed');
// Delete the reference on the windowManager object
if(instance.setup.destroyOnClose) {
delete windowManager.windows[instance.name];
}
// Delete the window object
instance.object = null;
instance = null;
});
return this;
};
/**
* Open the created window instance
* @param url [optional] The window target URL in case you didn't provide it in the constructor
* @param hide [optional] Whether to show or hide the newely-created window, false by default
* */
Window.prototype.open = function(url, hide){
// If the window is already created
if(isObject(this.object)){
this.focus();
return false;
}
// Create the window
this.create(url);
// Show the window
if(!hide){
this.object.show();
}
};
/**
* Makes the focus on this window
* */
Window.prototype.focus = function(){
this.object.focus();
return this;
};
/**
* Load a URL into the window
* */
Window.prototype.loadURL = function(url, options){
// Ready the url
url = utils.readyURL(url || this.setup.url);
const instance = this;
const layout = (this.setup.layout !== false) ?(this.setup.layout || windowManager.config.defaultLayout) :false;
// If a layout is specified
let layoutFile = layouts.get(layout);
if(layout && !layoutFile){
console.log('The layout "' + layout +'" wasn\'t found!');
}
if(layout && layoutFile && url.substring(0, 4) !== 'http'){
url = url.replace('file://', '');
layoutFile = layoutFile.replace('file://', '');
// Load the the layout first
FileSystem.readFile(layoutFile, 'utf-8', function(error, layoutCode){
if(error){
console.log('Couldn\'t load the layout file: ' + layoutFile);
// Take the page down!
instance.down();
return false;
}
// Load the targeted file body
FileSystem.readFile(url, 'utf-8', function(error, content){
if(error){
console.log('Couldn\'t load the target file:' + url);
// Take the page down!
instance.down();
return false;
}
// Get the final body
content = layoutCode
.replace(/\{\{appBase\}\}/g, utils.getAppLocalPath())
.replace('{{content}}', content);
// Load the final output
instance.html(content, options);
});
});
}else{
// Load the passed url
instance.content().loadURL(url, options);
}
};
/**
* Sets the content of the window to whatever HTML code your provide
* @param code The HTML code
* @param options
* */
Window.prototype.html = function(code, options){
this.content().loadURL('data:text/html;charset=utf-8,' + code, options);
};
/**
* Triggers the load-failure callback. This method is called whenever the targeted content isn't available or
* accessible. It will display a custom message by default, unless you define a custom callback for the window
* */
Window.prototype.down = function(){
// Force ignore the layout!
this.setup.layout = false;
// Either a custom failure call back, or call the global one
const callback = this.setup.onLoadFailure || windowManager.config.onLoadFailure;
// Trigger the call back
callback.call(null, this);
};
/**
* Returns the "webContents" object of the window
* */
Window.prototype.content = function(){
return this.object.webContents;
};
/**
* Reload the window content
* @param ignoreCache By default the page cache will be used, pass TRUE to ignore this cache when reloading
* */
Window.prototype.reload = function(ignoreCache){
if(ignoreCache === true){
// Reload ignoring the cache!
this.content().reloadIgnoringCache();
}else{
// Reload the content, with the cache available
this.content().reload();
}
};
/**
* Returns the url of the current page inside the window
* */
Window.prototype.currentURL = function(){
return this.content().getURL();
};
/**
* A callback to fire when the page is ready
* @param withTheDomReady Pass true to execute the callback when the DOM is ready, and not just the page have loaded
* @param callback The callback to trigger when the page is ready. This callback is passed two to parameters;
* the first is the window instance object, and the second is the window content object
* */
Window.prototype.onReady = function(withTheDomReady, callback){
const instance = this;
const event = (withTheDomReady === true) ?'dom-ready' :'did-finish-load';
// Fire the callback and pass the window .webContents to it
this.content().on(event, function(){
callback.call(null, instance, instance.content());
});
};
/**
* Executes JS code on the created window
* @param code The JS code
* */
Window.prototype.execute = function(code){
this.content().executeJavaScript(code);
};
/**
* Go back to the previous page/url to the current
* */
Window.prototype.goBack = function(){
if(this.content().canGoBack()){
this.content().goBack();
}
};
/**
* Closes the window
* */
Window.prototype.close = function(){
this.object.close();
};
/**
* Destroys a the window instance
* */
Window.prototype.destroy = function(){
this.object.destroy();
delete this;
console.log('Window "' + this.name + '" was destroyed');
};
/**
* Minimizes the window
* */
Window.prototype.minimize = function(){
this.object.minimize();
return this;
};
/**
* Maximizes/Unmaximizes the window
* */
Window.prototype.maximize = function(){
if(this.object.isMaximized()) this.object.restore();
else this.object.maximize();
return this;
};
/**
* Restore the window into focus
* */
Window.prototype.restore = function(){
this.object.restore();
return this;
};
/**
* Makes the window full screen
* */
Window.prototype.toFullScreen = function(){
this.object.setFullScreen(true);
return this;
};
/**
* Toggles developer tools
* @param detached [optional] Whether to open the dev tools in a separate window or not
* */
Window.prototype.toggleDevTools = function(detached){
this.object.toggleDevTools({detached: detached || false});
return this;
};
/**
* Attaching shortcut to the window
* */
Window.prototype.registerShortcut = function(accelerator, callback){
const instance = this;
Shortcuts.register(this.object, accelerator, function(){
callback.call(null, instance);
});
return this;
};
/**
* Moves the window to a specific x y position, or you can simple use a pre-defined position, like "right", "left"
* "topLeft", "bottomRight", ...
* */
Window.prototype.move = function(x, y){
// Get the window bounds first
const bounds = this.object.getBounds();
// If a position name was provided
if(isString(x)){
this.setup.position = x;
const xy = utils.resolvePosition(this.setup);
if(xy){
x = xy[0];
y = xy[1]
}
}
// Set the bounds
this.object.setBounds({
'x': x || bounds.x,
'y': y || bounds.y,
'width': this.setup.width,
'height': this.setup.height
});
return this;
};
/**
* Resize the window, by entering either the width or the height, or both
* */
Window.prototype.resize = function(width, heigt){
// Get the current bounds
const bounds = this.object.getBounds();
this.object.setBounds({
'width': width || bounds.width,
'height': heigt || bounds.height,
'x': bounds.x,
'y': bounds.y
});
return this;
};
/**
* The setup templates. Where you can create a ready-to-use setup templates/groups for the BrowserWindow instance
* */
const templates = {
'templates': {},
/**
* Set a new template
* */
'set': function(name, setup){
if(!isObject(setup) || this.templates[name]) return false;
this.templates[name] = setup;
},
/**
* Fetches the setup by name
* */
'get': function(name){
return Object.assign({}, this.templates[name]);
},
/**
* Change/modify the template properties
* @param name The name of the template
* @param setup The new changes, as an object
* */
'modify': function(name, setup){
if(!isObject(setup) || !this.templates[name]) return false;
this.templates[name] = Object.assign(this.get(name), setup);
},
/**
* Return a setup property value of a setup templates
* @param name The name of the template
* @param prop The property needed back
* */
'getProperty': function(name, prop){
return this.get(name)[prop];
}
};
/**
* A bunch of tools/utilities for the module
* */
const utils = {
/**
* Returns the full path to the application directory
* */
'getAppLocalPath': function(){
return Application.getAppPath() + '/';
},
/**
* Readies the passed URL for opening. If it starts with "/" it will be prefixed with the app directory
* path. Also if it contain "{appBase}", this value will be replaces with the app path too.
* */
'readyURL': function(url){
if(url[0] === '/'){
return windowManager.config.appBase + url.substr(1);
}else{
return url.replace('{appBase}', windowManager.config.appBase);
}
},
/**
* Resolves a position name into x & y coordinates.
* @param setup The window setup object
* */
'resolvePosition': function(setup){
const screen = Electron.screen;
const screenSize = screen.getPrimaryDisplay().workAreaSize;
const position = setup.position;
let x = 0;
let y = 0;
const positionMargin = 0;
let windowWidth = setup.width;
let windowHeight = setup.height;
// If the window dimensions are not set
if(!windowWidth || !windowHeight){
console.log('Cannot position a window with the width/height not defined!');
// Put in in the center
setup.center = true;
return false;
}
// If the position name is incorrect
if(['center', 'top', 'right', 'bottom', 'left', 'topLeft', 'leftTop', 'topRight',
'rightTop', 'bottomRight', 'rightBottom', 'bottomLeft', 'leftBottom'].indexOf(position) < 0){
console.log('The specified position "' + position + '" is\'not correct! Check the docs.');
return false;
}
// It's center by default, no need to carry on
if(position === 'center'){
return false;
}
// Compensate for the frames
if (setup.frame === true) {
switch (position) {
case 'left':
break;
case 'right':
windowWidth += 8;
break;
case 'top':
windowWidth += 13;
break;
case 'bottom':
windowHeight += 50;
windowWidth += 13;
break;
case 'leftTop':
case 'topLeft':
windowWidth += 0;
windowHeight += 50;
break;
case 'rightTop':
case 'topRight':
windowWidth += 8;
windowHeight += 50;
break;
case 'leftBottom':
case 'bottomLeft':
windowWidth -= 0;
windowHeight += 50;
break;
case 'rightBottom':
case 'bottomRight':
windowWidth += 8;
windowHeight += 50;
break;
}
}
switch (position) {
case 'left':
y = Math.floor((screenSize.height - windowHeight) / 2);
x = positionMargin - 8;
break;
case 'right':
y = Math.floor((screenSize.height - windowHeight) / 2);
x = (screenSize.width - windowWidth) - positionMargin;
break;
case 'top':
y = positionMargin;
x = Math.floor((screenSize.width - windowWidth) / 2);
break;
case 'bottom':
y = (screenSize.height - windowHeight) - positionMargin;
x = Math.floor((screenSize.width - windowWidth) / 2);
break;
case 'leftTop':
case 'topLeft':
y = positionMargin;
x = positionMargin - 8;
break;
case 'rightTop':
case 'topRight':
y = positionMargin;
x = (screenSize.width - windowWidth) - positionMargin;
break;
case 'leftBottom':
case 'bottomLeft':
y = (screenSize.height - windowHeight) - positionMargin;
x = positionMargin - 8;
break;
case 'rightBottom':
case 'bottomRight':
y = (screenSize.height - windowHeight) - positionMargin;
x = (screenSize.width - windowWidth) - positionMargin;
break;
}
return [x, y];
}
};
/**
* Manges the layouts information
* */
const layouts = {
'layouts': {},
/**
* Registers a new layout
* @param name The name of the layout
* @param path The path to the layout. It will be automatically prefixed with the app full path
* */
'add': function(name, path){
this.layouts[name] = utils.readyURL(path);
},
/**
* Retrieves the layout path, by name
* @param name The name of the layout registered earlier
* */
'get': function(name){
return this.layouts[name];
}
};
/**
* The module interface
* */
const windowManager = {
/**
* The templates management API
* */
'templates': templates,
/**
* The layouts management API
* */
'layouts': layouts,
/**
* The utilities
* */
'utils': utils,
/**
* The event emitter
* */
'eventEmitter': EventEmitter,
/**
* The shortcuts module
* */
'shortcuts': Shortcuts,
/**
* The global configuration
* */
'config': {
'appBase': null, // The full path to the application directory
'devMode': true, // Turns the development mode on/off
'layouts': false, // A list of the layouts, a direct shortcut, instead of using layouts.add for each layout
'defaultLayout': false, // The default layout name
'defaultSetupTemplate': false, // The default setup template name
'defaultWindowTitle': null, // The default window title
'windowsTitlePrefix': null, // A prefix for the windows title
/**
* The window url global load-failure callback
* */
"onLoadFailure": function(window){
window.content().loadURL('file://' + __dirname + '/loadFailure.html');
}
},
/**
* The Window instances, stored by names
* */
'windows': {},
/**
* Initiate the module
* @param config The configuration for the module
* */
'init': function(config){
// If the config object is provided
if(isObject(config)){
this.config = Object.assign(this.config, config);
}else if(isString(config)){
if(config.length && config[config.length-1] !== '/'){
config = config + '/';
}
this.config.appBase = config;
}
// If the app base isn't provided
if(!this.config.appBase){
this.config.appBase = utils.getAppLocalPath();
}
// If the layouts list was passed in the config
if(this.config.layouts && isObject(this.config.layouts)){
Object.keys(this.config.layouts).forEach(key => {
layouts.add(key, this.config.layouts[key]);
});
}
// If the dev mode is on
if(this.config.devMode === true){
// Attach some shortcuts
Application.on('ready', function(){
// CTRL+F12 to toggle the dev tools
Shortcuts.register('CmdOrCtrl+F12', function(){
const window = windowManager.getCurrent();
if(window) window.toggleDevTools();
});
// CTRL+R to reload the page
Shortcuts.register('CmdOrCtrl+R', function(){
const window = windowManager.getCurrent();
if(window) window.reload();
});
});
}
// If a default setup is provided
if(this.config.defaultSetup){
this.setDefaultSetup(this.config.defaultSetup);
delete this.config.defaultSetup;
}
},
/**
* Sets the default setup for all the BrowserWindow instances, unless a different template is selected
* or false is passed instead. It creates a new template with the name "default" for this setup.
* @param setup The setup object
* */
'setDefaultSetup': function(setup){
if(!isObject(setup)) return false;
// Add the setup template
templates.set('default', setup);
// Make it the default setup
this.config.defaultSetupTemplate = 'default';
},
/**
* Using this method you can create more than one window with the setup information retrieved from a JSON file.
* */
'importList': function(file){
const list = require(utils.getAppLocalPath() + file);
if(!isObject(list)) return false;
Object.keys(list).forEach(key => {
let window = list[key];
windowManager.createNew(key, window.title, window.url, window.setupTemplate, window.setup);
});
},
/**
* Create a new window instance. Check the Window object for documentation.
* */
'createNew': function(name, title, url, setupTemplate, setup, showDevTools){
// Create the window instance
const window = new Window(name, title, url, setupTemplate, setup, showDevTools);
// If the window was created
return (window == null || Object.keys(window).length === 0) ?false :window;
},
/**
* Opens a new window
* */
'open': function(name, title, content, setupTemplate, setup, showDevTools){
const window = this.createNew(name, title, content, setupTemplate, setup, showDevTools);
if(window) window.open();
return window;
},
/**
* Create a clone of the passed window
* */
'clone': function(name){
const window = this.get(name);
if(!window) return;
return this.createNew(false, false, false, false, this.setup);
},
/**
* Get a window instance, by name
* */
'get': function(name){
if(!this.windows[name]){
console.log('Window ' + name + ' doesn\'t exist!');
return false;
}
return this.windows[name];
},
/**
* Get a window instance, by BrowserWindow instance id
*/
'getById': function(id) {
let instance;
Object.keys(this.windows).forEach(key => {
let window = this.windows[key];
if(window.object.id === id){
instance = window;
}
});
return instance;
},
/**
* Fetches the currently-under-focus window
* */
'getCurrent': function(){
const thisWindow = BrowserWindow.getFocusedWindow();
if(!thisWindow) return false;
return this.getById(thisWindow.id);
},
/**
* Closes a window, by name
* */
'close': function(name){
this.get(name).object.close();
},
/**
* Closes this/current window
* */
'closeCurrent': function(){
const current = this.getCurrent();
if(current) current.close();
},
/**
* Destroy a window instance, by name
* */
'destroy': function(name){
this.get(name).destroy();
},
/**
* Close all windows created by this module
* */
'closeAll': function(){
Object.keys(this.windows).forEach(key => {
let window = this.windows[key];
window.close();
});
},
/**
* Close all window except for one
* */
'closeAllExcept': function(name){
// Get all the windows
const windows = BrowserWindow.getAllWindows();
// Get the window through the name
const windowID = this.get(name).object.id;
if(!windows.length || !windowID) return false;
// Loop through the windows, close all of them and focus on the targeted one
Object.keys(windows).forEach(key => {
let window = windows[key];
if(window.id !== windowID){
window.close();
}
});
this.get(name).focus();
},
/**
* Focuses on a specific, by name
* */