-
Notifications
You must be signed in to change notification settings - Fork 32
/
biselectron.js
586 lines (455 loc) · 16.9 KB
/
biselectron.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
/* LICENSE
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._
BioImage Suite Web is licensed under the Apache License, Version 2.0 (the "License");
- you may not use this software except in compliance with the License.
- You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
__Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.__
ENDLICENSE */
/* jshint node:true */
"use strict";
let getTime = function() {
// http://stackoverflow.com/questions/7357734/how-do-i-get-the-time-of-day-in-javascript-node-js
let date = new Date();
let hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
let min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
let sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
return "[" + hour + ":" + min + ":" + sec +"]";
};
// -------------------------------------------------------------------------------
const electron = require('electron');
require('@electron/remote/main').initialize();
require('electron-debug')({showDevTools: false,
enabled : true});
const path=require('path');
const fs=require('fs');
const app=electron.app; // Module to control application life.
const globalShortcut=electron.globalShortcut;
app.commandLine.appendSwitch('auto-detect', 'false');
app.commandLine.appendSwitch('no-proxy-server');
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
const ipcMain = electron.ipcMain;
const shell=electron.shell;
const toolfile=require('./images/tools.json');
const state = {
winlist : [null],
screensize : {
width : 100,
height:100
},
console : null,
consolehandler : null,
indev : process.defaultApp || /[\\/]electron-prebuilt[\\/]/.test(process.execPath) || /[\\/]electron[\\/]/.test(process.execPath),
dimensions : {
width : 1024,
height : 1024,
},
toolname : "index",
};
let v=process.versions.node;
let s=v.split(".");
let major=parseInt(s[0]);
/*if (major>20) {
console.log(`----\n---- You are using a version of node older than 12.0 (actual version=${v}).\n`);
process.exit(1);
}*/
if (state.indev) {
state.commandargs= process.argv.slice(3) || [];
state.mainfilename=process.argv[2];
console.log(getTime()+' Electron version='+process.versions.electron+' node='+process.versions.node);
} else {
state.commandargs= process.argv.slice(2) || [];
state.mainfilename=process.argv[1];
}
state.mainfilename=state.mainfilename || "";
state.queryargs='';
// Check if filename ends in .html if not add it
if (state.mainfilename!=='') {
if (state.mainfilename.indexOf('?')>0) {
let st=state.mainfilename.split('?');
state.mainfilename=st[0];
state.queryargs=st[1];
}
let ext=state.mainfilename.split('.').pop();
if (ext!=='html') {
state.toolname=state.mainfilename;
state.mainfilename+='.html';
}
}
const biswebTerminate=function(code=0) {
app.quit(code);
};
const macExitQuestion=function() {
return new Promise( (resolve,reject) => setTimeout( async () => {
const dialog = electron.dialog;
const response=await dialog.showMessageBox({
title : "Are you sure?",
type : "question",
buttons : [ "Cancel", "Quit" ],
defaultId : 0,
message : "This will terminate BioImage Suite Web. Are you sure?",
});
if (response.response===1)
resolve('exiting');
else
reject('quit event cancelled');
}));
};
// ----------------------------------------------------------------------------------------
const getHeightWidth= function(name) {
let tools=toolfile.tools;
const obj = {
height : 1120,
width : 1024
};
const maxw=electron.screen.getPrimaryDisplay().workAreaSize.width;
const maxh=electron.screen.getPrimaryDisplay().workAreaSize.height;
const scale= electron.screen.getPrimaryDisplay().scaleFactor || 1.0;
let found=false,i=0;
let keys=Object.keys(tools);
while (i<keys.length && found===false) {
let url=tools[keys[i]].url;
if (name.indexOf(url)===0) {
obj.height= tools[keys[i]].height || 950;
obj.width= tools[keys[i]].width || 700;
obj.height=Math.round(obj.height*scale);
obj.width=Math.round(obj.width*scale);
found=true;
} else {
i=i+1;
}
}
if (!found && name.indexOf("test")>0) {
obj.width=maxw;
obj.height=maxh;
}
if (obj['width']>maxw)
obj['width']=maxw;
else if (obj['width']<600)
obj['width']=600;
if (obj.height<600)
obj.height=600;
if (obj.height>maxh)
obj.height=maxh;
return obj;
};
const createWindow=function(index,fullURL) {
fullURL = fullURL || state.toolname;
let i0=fullURL.lastIndexOf("\\");
if (i0<0)
i0=fullURL.lastIndexOf("/");
if (i0>=0)
i0+=1;
let i1=fullURL.lastIndexOf(".html");
let name=state.toolname;
if (i1>i0 && i1>0 && i0>0) {
name=fullURL.substr(i0,i1-i0);
} else if (i0>0 && i1<0) {
name=fullURL.substr(i0,fullURL.length);
} else if (i0<0 && i1>0) {
name=fullURL.substr(0,i1);
}
state.dimensions=getHeightWidth(name);
let i_width= state.dimensions.width;
let i_height= state.dimensions.height;
let xval=100;
let yval=100;
if (index==-2) {
index=state.winlist.length;
} else {
if (index===-1) {
index=state.winlist.length;
}
}
if (index===0) {
fullURL='file://' + path.resolve(__dirname , 'index.html');
xval=undefined;
yval=undefined;
}
let opts= { width: i_width,
height: i_height,
maxwidth: state.screensize.width,
maxheight: state.screensize.height
};
if (opts.height>state.screensize.height-10)
opts.height=state.screensize.height-10;
if (opts.width>state.screensize.width-10)
opts.width=state.screensize.width-10;
if (index!==0) {
xval+=Math.round(Math.random()*10);
yval+=Math.round(Math.random()*10);
if (xval+opts.width>state.screensize.width)
xval=undefined;
if (yval+opts.height>state.screensize.height)
yval=undefined;
}
if (state.queryargs!=='') {
fullURL=fullURL+'?'+state.queryargs;
state.queryargs='';
}
let preload= path.resolve(__dirname, 'bispreload.js');
console.log(getTime()+' Creating new window '+fullURL + ', index='+index+' dims='+JSON.stringify(opts));
state.winlist[index]=new BrowserWindow({width: opts.width,
height: opts.height,
maxWidth: opts.maxwidth,
maxHeight : opts.maxheight,
show: true,
x : xval,
y : yval,
webPreferences: {
nodeIntegration: false,
preload: preload,
contextIsolation: false,
enableRemoteModule : true,
},
autoHideMenuBar : true,
icon: __dirname+'/images/favicon.ico'});
//state.winlist[index].setAutoHideMenuBar(true);
state.winlist[index].setMenuBarVisibility(false);
if (process.platform === 'darwin')
state.winlist[index].setMenu(null);
state.winlist[index].once('ready-to-show', () => {
state.winlist[index].show();
});
state.winlist[index].on('closed', function() {
state.winlist[index] = null;
});
console.log('Loading URL',fullURL);
state.winlist[index].loadURL(fullURL).then( () => {
console.log('.... Loaded URL=',fullURL);
}).catch( (e) => {
console.log('.... Failed to load ',fullURL,', error=',e);
setTimeout( () => {
console.log('... Trying to load again',fullURL);
state.winlist[index].loadURL(fullURL);
},1000);
});
return index;
};
var createConsole=function() {
let opts= {width: 800, height: 500};
let fullURL='file://' + path.resolve(__dirname , 'console.html');
let preload= path.resolve(__dirname, 'bispreload.js');
state.console=new BrowserWindow({width: opts.width,
height: opts.height,
webPreferences: {
nodeIntegration: false,
preload: preload,
},
icon: __dirname+'/images/favicon.ico'});
state.console.setAutoHideMenuBar(true);
state.console.setMenuBarVisibility(false);
if (process.platform === 'darwin')
state.console.setMenu(null);
state.console.loadURL(fullURL);
state.console.minimize();
state.console.hide();
state.console.on('close', function (e) {
e.preventDefault();
e.returnValue=false;
state.console.hide();
return false;
});
};
var trapOpenNewWindowEvent=function(index) {
console.log('Attaching',index);
state.winlist[index].webContents.on('new-window',function(event,url/*,frameName,disposition,options*/) {
console.log('webcontents url=',url);
event.preventDefault();
let lm=url.split("/"), fname=lm[lm.length-1], ismainwindow=false;
if (fname==="index.html") {
ismainwindow=true;
if (state.winlist[0]!==null) {
state.winlist[0].show();
console.log('.... link was to index.html showing main window');
return;
}
}
if (url.indexOf('http://')===0 ||
url.indexOf('https://')===0
) {
console.log(getTime()+' Electron opening ' + url + ' in browser.');
shell.openExternal(url);
return;
}
let index=state.winlist.length;
if (ismainwindow)
index=0;
console.log('....\n.... creating new window from webpage ... loading url=',index,url);
createWindow(index,url);
trapOpenNewWindowEvent(index);
});
};
var createNewWindow = function(url) {
console.log('....\n..... create new window',url);
const index=createWindow(-2,url);
trapOpenNewWindowEvent(index);
};
var createOrShowMainWindow = function(hide=false) {
if (state.winlist[0]!==null) {
state.winlist[0].show();
return;
}
console.log('....\n..... create or show main window');
createWindow(0);
trapOpenNewWindowEvent(0);
if (hide) {
state.winlist[0].minimize();
}
};
// -----------------------------------------------------------------------
// ----------------------------------- App Level Stuff -------------------
// -----------------------------------------------------------------------
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
biswebTerminate();
return;
}
if (process.platform === 'darwin') {
macExitQuestion().then( (m) => {
console.log('.... exiting',m);
biswebTerminate(0);
}).catch( (e) => {
console.log('.... not exiting',e);
createOrShowMainWindow(true);
});
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', async function() {
let setup=toolfile;
let keys=Object.keys(setup.tools);
let fullURL='';
if (state.mainfilename.length>1) {
console.log(getTime()+' Testing starting file=',state.mainfilename);
fullURL=path.resolve(__dirname , state.mainfilename);
if (!fs.existsSync(fullURL))
fullURL=path.resolve(__dirname , '../'+state.mainfilename);
if (!fs.existsSync(fullURL)) {
console.log(getTime()+' The starting file=',state.mainfilename,' does not exist.');
state.mainfilename='';
}
}
console.log(getTime()+' Electron ready ' + state.mainfilename + ' indev='+state.indev);
if (state.queryargs.length>0) {
console.log(getTime()+' \t Query args= "'+state.queryargs+'"');
}
if (state.commandargs.length>0) {
console.log(getTime()+' Command args='+state.commandargs);
}
state.screensize.width=electron.screen.getPrimaryDisplay().workAreaSize.width;
if (state.screensize.width<900)
state.screensize.width=900;
state.screensize.height=electron.screen.getPrimaryDisplay().workAreaSize.height;
if (state.screensize.height<700)
state.screensize.height=700;
let tools=setup.tools;
const Menu=electron.Menu;
let mitems=[];
let i=0;
if (process.platform === 'darwin') {
for (i=0;i<keys.length;i++) {
let key=keys[i];
let a='file://'+path.resolve(__dirname , tools[key].url+'.html'); // jshint ignore:line
/* jshint ignore:start */
(function outer(a){
mitems.push({ label: tools[key].title, click: () =>{
createNewWindow(a);
}});
}(a));
/* jshint ignore:end */
}
let menu=Menu.buildFromTemplate([
{ label: "Application Selector", click: () => { createOrShowMainWindow(); }},
{ label: 'Tools', submenu : mitems }
]);
let menu2=Menu.buildFromTemplate([
{ label: 'Main',
submenu : [
{ label: "Application Selector", click: () => { createOrShowMainWindow(); }},
{ type: 'separator'},
{
label : 'Exit ⌘Q', click: () => {
macExitQuestion().then( () => {
biswebTerminate();
}).catch( (e) => {
console.log(e);
});
}
}
]
},
{ label: 'Tools',
submenu : mitems
}
]);
app.dock.setMenu(menu);
Menu.setApplicationMenu(menu2);
}
if (process.platform === 'darwin') {
globalShortcut.register('CommandOrControl+Q', () => {
macExitQuestion().then( () => {
biswebTerminate(0);
}).catch( (e) => {
console.log('Anyalive'+e);
createOrShowMainWindow();
});
});
}
setTimeout( () => {
if (state.mainfilename === '') {
createOrShowMainWindow();
} else {
createNewWindow("file://"+fullURL);
}
},100);
});
app.on('activate', () => {
setTimeout( () => {
createOrShowMainWindow();
},500);
});
ipcMain.on('ping', function (event, arg) {
console.log(getTime()+' (PING) '+arg);
});
ipcMain.on('showdevtools', function () {
let win = BrowserWindow.getFocusedWindow();
if (win) {
win.webContents.openDevTools();
}
});
ipcMain.on('showconsole',function() {
if (state.console===null)
createConsole();
state.console.show();
});
ipcMain.on('bisconsoleinit',function(event) {
state.consolehandler=event.sender;
});
ipcMain.on('bisconsole', function (event,arg) {
if (state.consolehandler===null)
console.log(arg);
else
state.consolehandler.send('add-text',arg);
});
ipcMain.on('clearconsole', function (event,arg) {
if (state.consolehandler)
state.consolehandler.send('clear-text',arg);
});
ipcMain.on('arguments', function (event,arg) {
if (state.winlist.length>2 || state.commandargs.length<1) {
return;
}
console.log(getTime()+ ' Sending arguments to ' +arg);
event.sender.send('arguments-reply',state.commandargs);
});