This repository has been archived by the owner on Dec 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.js
1342 lines (1181 loc) · 49.1 KB
/
main.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
/**
* @file Contains all main code
* @author yafp
* @namespace main
*/
'use strict'
// -----------------------------------------------------------------------------
// REQUIRE: 3rd PARTY
// -----------------------------------------------------------------------------
const { app, BrowserWindow, Menu, Tray, ipcMain, globalShortcut } = require('electron')
const shell = require('electron').shell // for: opening external urls in default browser
const isOnline = require('is-online') // for online connectivity checks
const path = require('path')
const fs = require('fs')
const os = require('os') // for: check os.platform()
const openAboutWindow = require('about-window').default // for: about-window
require('v8-compile-cache') // via: https://dev.to/xxczaki/how-to-make-your-electron-app-faster-4ifb
// -----------------------------------------------------------------------------
// Shared object
// -----------------------------------------------------------------------------
//
const consoleOutput = false // can be changed using --verbose
// Settings Tab
const settingDefaultView = ''
const settingTheme = 'default'
const settingAutostart = ''
const settingDisableTray = false
const settingUrgentWindow = false
const settingEnableErrorReporting = true
const settingEnablePrereleases = false
global.sharedObj = {
// verbose mode aka console Output
consoleOutput: consoleOutput,
// settings
settingDefaultView: settingDefaultView,
settingTheme: settingTheme,
settingAutostart: settingAutostart,
settingDisableTray: settingDisableTray,
settingUrgentWindow: settingUrgentWindow,
settingEnableErrorReporting: settingEnableErrorReporting,
settingEnablePrereleases: settingEnablePrereleases
}
// ----------------------------------------------------------------------------
// REQUIRE: TTTH MODULES
// ----------------------------------------------------------------------------
const urls = require('./app/js/ttth/modules/urlsGithub.js') // the github urls
const crash = require('./app/js/ttth/modules/crashReporter.js') // crashReporter
const sentry = require('./app/js/ttth/modules/sentry.js') // sentry
const unhandled = require('./app/js/ttth/modules/unhandled.js') // electron-unhandled
// ----------------------------------------------------------------------------
// COMMAND-LINE-ARGS
// ----------------------------------------------------------------------------
const yargs = require('yargs')
.strict(true) // arguments must be valid
.usage('Usage: $0 <command> [options]')
// define arguments
.option('gpu', {
alias: 'g',
type: 'boolean',
description: 'Starts ttth with gpu-acceleration enabled'
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Starts ttth with verbose output'
})
.option('help', {
alias: 'h',
type: 'boolean',
description: 'Shows the ttth help'
})
.option('version', {
type: 'boolean',
description: 'Shows the ttth version'
})
// displayed in case of invalid parameters
.showHelpOnFail(false, 'Specify --help to display the available options')
// show the project url
.epilog('Project URL: https://github.com/yafp/ttth')
// Show an example
// .example('$0 --help', 'Shows the media-dupes help')
.argv
if (yargs.verbose === true) {
global.sharedObj.consoleOutput = true // update the global object
}
// # 188
if (yargs.gpu === true) {
// nothing to do here as it is enabled by default
// writeLog('info', 'GPU acceleration is now enabled')
} else {
app.disableHardwareAcceleration() // https://www.electronjs.org/docs/api/app#appdisablehardwareacceleration
// writeLog('info', 'GPU acceleration is now disabled')
}
// ----------------------------------------------------------------------------
// ERROR-HANDLING:
// ----------------------------------------------------------------------------
crash.initCrashReporter()
unhandled.initUnhandled()
sentry.enableSentry() // sentry is enabled by default
// ----------------------------------------------------------------------------
// HARDWARE ACCELERATION:
// ----------------------------------------------------------------------------
// #188 - added in 1.9.0
// for testing use: --disable-gpu
//
// Disables hardware acceleration for current app. This method can only be called before app is ready.
// app.disableHardwareAcceleration() // https://www.electronjs.org/docs/api/app#appdisablehardwareacceleration
// var foo = app.getAppMetrics()
// console.error(foo)
// -----------------------------------------------------------------------------
// VARIABLES
// -----------------------------------------------------------------------------
// Keep a global reference of the window objects, if you don't, the window
// will be closed automatically when the JavaScript object is garbage collected.
let mainWindow = null
let configWindow = null
// let windowConfig = null // gonna implement this in 1.10.0
const gotTheLock = app.requestSingleInstanceLock() // for: single-instance handling
const defaultUserDataPath = app.getPath('userData') // for: storing window position and size
const userOSPlatform = os.platform() // for BadgeCount support - see #152
const defaultMainWindowWidth = 800
const defaultMainWindowHeight = 600
// -----------------------------------------------------------------------------
// FUNCTIONS
// -----------------------------------------------------------------------------
/**
* @function writeLog
* @summary Writes console output for the main process
* @description Writes console output for the main process
* @memberof main
* @param {string} type - The log type
* @param {string} message - The log message
* @param {string} optionalObject - An optional object which might contain additional informations
*/
function writeLog (type, message, optionalObject = '') {
const logM = require('electron-log')
const prefix = '[ Main ] '
if (global.sharedObj.consoleOutput === false) {
logM.transports.console.level = false // disable Terminal output. Still logs to DevTools and LogFile
}
// important: https://github.com/megahertz/electron-log/issues/189
// electron-log can: error, warn, info, verbose, debug, silly
switch (type) {
case 'info':
logM.info(prefix + message, optionalObject)
break
case 'warn':
logM.warn(prefix + message, optionalObject)
break
case 'error':
logM.error(prefix + message, optionalObject)
break
default:
logM.silly(prefix + message, optionalObject)
break
}
}
/**
* @function checkNetworkConnectivity
* @summary Checks if internet is accessible
* @description Checks if the internet is accessible, if not triggers an error in the mainWindow
* @memberof main
*/
function checkNetworkConnectivity () {
(async () => {
if (await isOnline() === true) {
writeLog('info', 'checkNetworkConnectivity ::: Got access to the internet.')
} else {
writeLog('error', 'checkNetworkConnectivity ::: Got NO access to the internet.')
mainWindow.webContents.send('showNoConnectivityError') // app should show an error
}
})()
}
/**
* @function showDialog
* @summary Shows a dialog
* @description Displays a dialog - see https://electronjs.org/docs/api/dialog
* @memberof main
* @param {string} dialogType - Can be "none", "info", "error", "question" or "warning"
* @param {string} dialogTitle - The title text
* @param {string} dialogMessage - The message of the dialog
* @param {string} dialogDetail - The detail text
*/
function showDialog (dialogType, dialogTitle, dialogMessage, dialogDetail) {
const { dialog } = require('electron')
const options = {
type: dialogType,
buttons: ['OK'],
defaultId: 2,
title: dialogTitle,
message: dialogMessage,
detail: dialogDetail
}
dialog.showMessageBox(null, options, (response, checkboxChecked) => {
// console.log(response);
})
}
/**
* @function createTray
* @summary Creates the tray of the app
* @description Creates the tray and the related menu.
* @memberof main
*/
function createTray () {
writeLog('info', 'createTray ::: Starting to create a tray item')
let tray = null
tray = new Tray(path.join(__dirname, 'app/img/tray/tray_default.png'))
const contextMenu = Menu.buildFromTemplate([
{
// Window focus
id: 'show',
label: 'Show',
click: function () {
if (mainWindow === null) {
// #134
// do nothing, as no mainWindow exists. Most likely on macOS
} else {
// focus the main window
if (mainWindow.isMinimized()) {
mainWindow.restore()
} else {
// is not minimized. Was maybe: hidden via hide()
mainWindow.show()
}
mainWindow.focus()
}
},
enabled: true
},
{
type: 'separator',
enabled: false
},
{
// Quit
id: 'exit',
label: 'Exit',
enabled: true,
click: function () {
app.quit()
}
}
])
tray.setToolTip('ttth')
tray.setContextMenu(contextMenu)
writeLog('info', 'createTray ::: Finished creating tray')
// Call from renderer: Change Tray Icon to UnreadMessages
ipcMain.on('changeTrayIconToUnreadMessages', function () {
if (tray.isDestroyed() === false) {
tray.setImage(path.join(__dirname, 'app/img/tray/tray_unread.png'))
}
})
// Call from renderer: Change Tray Icon to Default
ipcMain.on('changeTrayIconToDefault', function () {
if (tray.isDestroyed() === false) {
tray.setImage(path.join(__dirname, 'app/img/tray/tray_default.png'))
}
})
// Call from renderer: Option: Urgent window - see #110
ipcMain.on('makeWindowUrgent', function () {
mainWindow.flashFrame(true) // #110 - urgent window
})
// Call from renderer: Option: DisableTray
ipcMain.on('disableTray', function () {
writeLog('info', 'ipcMain.disableTray ::: Disabling tray (ipcMain)')
tray.destroy()
if (tray.isDestroyed() === true) {
writeLog('info', 'ipcMain.disableTray ::: Disabling tray was working')
} else {
writeLog('error', 'ipcMain.disableTray ::: Disabling tray failed')
}
})
}
/**
* @function createWindowConfig
* @summary Creates the config window of the app
* @description Creates the config window
* @memberof main
*/
/*
function createWindowConfig () {
writeLog('info', 'createWindow ::: Starting to create the application windows')
// Create the browser window.
windowConfig = new BrowserWindow({
// parent: mainWindow,
modal: true,
frame: true, // false results in a borderless window. Needed for custom titlebar
titleBarStyle: 'default', // needed for custom-electron-titlebar. See: https://electronjs.org/docs/api/frameless-window
backgroundColor: '#ffffff',
show: true,
center: true, // Show window in the center of the screen
width: 800,
minWidth: 800,
// resizable: false, // this conflickts with opening dev tools
minimizable: false, // not implemented on linux
maximizable: false, // not implemented on linux
height: 700,
minHeight: 700,
icon: path.join(__dirname, 'app/img/icon/icon.png'),
webPreferences: {
nodeIntegration: true,
webSecurity: true // introduced in 0.3.0
}
})
// and load the setting.html of the app.
windowConfig.loadFile('app/configWindow.html')
// window needs no menu
windowConfig.removeMenu()
// Call from renderer: Settings UI - toggle dev tools
ipcMain.on('settingsToggleDevTools', function () {
settingsWindow.webContents.toggleDevTools()
})
// Emitted before the window is closed.
windowConfig.on('close', function () {
writeLog('info', 'createWindowConfig ::: windowConfig will close (event: close)')
})
// Emitted when the window is closed.
windowConfig.on('closed', function (event) {
writeLog('info', 'createWindowConfig ::: windowConfig is closed (event: closed)')
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
windowConfig = null
// unblur main UI
mainWindow.webContents.send('unblurMainUI')
})
}
*/
/**
* @function createWindow
* @summary Creates the main window of the app
* @description Creates the main window, restores window position and size of possible
* @memberof main
*/
function createWindow () {
writeLog('info', 'createWindow ::: Starting to create the application windows')
// Variables for window position and size
let windowWidth
let windowHeight
let windowPositionX
let windowPositionY
// Try to read stored last window position and size
const customUserDataPath = path.join(defaultUserDataPath, 'ttthMainWindowPosSize.json')
let data
try {
data = JSON.parse(fs.readFileSync(customUserDataPath, 'utf8'))
// size
windowWidth = data.bounds.width
windowHeight = data.bounds.height
// position
windowPositionX = data.bounds.x
windowPositionY = data.bounds.y
writeLog('info', 'createWindow ::: Got last window position and size information from _' + customUserDataPath + '_.')
} catch (e) {
writeLog('warn', 'createWindow ::: No last window position and size information found in _' + customUserDataPath + '_. Using fallback values')
// set some default values for window size
windowWidth = defaultMainWindowWidth
windowHeight = defaultMainWindowHeight
}
// Create the browser window.
mainWindow = new BrowserWindow({
// title: '${productName}',
frame: false, // false results in a borderless window
show: false, // hide until: ready-to-show
titleBarStyle: 'hidden', // needed for custom-electron-titlebar
width: windowWidth,
height: windowHeight,
minWidth: defaultMainWindowWidth,
minHeight: defaultMainWindowHeight,
center: true, // Show window in the center of the screen. (since 1.7.0)
backgroundColor: '#ffffff',
icon: path.join(__dirname, 'app/img/icon/icon.png'),
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
webSecurity: true, // introduced in 1.8.0
experimentalFeatures: false, // introduced in 1.8.0
webviewTag: true, // # see #37
devTools: true, // should be possible to open them
partition: 'ttth'
}
})
writeLog('info', 'createWindow ::: Finished creating the mainWindow')
// Restore window position if possible
// requirements: found values in .ttthMainWindowPosSize.json from the previous session
//
if ((typeof windowPositionX !== 'undefined') && (typeof windowPositionY !== 'undefined')) {
writeLog('info', 'createWindow ::: Restoring last stored window-position of mainWindow')
mainWindow.setPosition(windowPositionX, windowPositionY)
}
// Call from renderer: Update property from globalObj
ipcMain.on('globalObjectSet', function (event, property, value) {
writeLog('info', 'ipcMain.globalObjectSet ::: Set property _' + property + '_ to new value: _' + value + '_')
global.sharedObj[property] = value
writeLog('info', 'ipcMain.globalObjectSet ::: sharedObj: ', global.sharedObj)
})
// Load the UI (mainWindow.html) of the app.
mainWindow.loadFile('./app/mainWindow.html')
writeLog('info', 'createWindow ::: Loading mainWindow.html to mainWindow')
// show the formerly hidden main window as it is fully ready now
mainWindow.on('ready-to-show', function () {
mainWindow.show()
mainWindow.focus()
writeLog('info', 'mainWindow.on.ready-to-show ::: mainWindow is now ready, so show it and then focus it')
checkNetworkConnectivity() // check network access
})
// Emitted when the application has finished basic startup.
mainWindow.on('will-finish-launching', function () {
writeLog('info', 'mainWindow.on.will-finish-launching ::: mainWindow will finish launching')
})
// When dom is ready
mainWindow.webContents.once('dom-ready', () => {
writeLog('info', 'mainWindow.on.ready ::: mainwWindow DOM is now ready')
})
// When page title gets changed
mainWindow.webContents.once('page-title-updated', () => {
writeLog('info', 'mainWindow.on.page-title-updated ::: mainWindow got new title')
})
// when the app is shown
mainWindow.on('show', function () {
writeLog('info', 'mainWindow.on.show ::: mainWindow is visible')
})
// when the app loses focus / aka blur
mainWindow.on('blur', function () {
writeLog('info', 'mainWindow.on.blur ::: mainWindow lost focus')
})
// when the app gets focus
mainWindow.on('focus', function () {
writeLog('info', 'mainWindow.on.focus ::: mainWindow got focus')
})
// when the app goes fullscreen
mainWindow.on('enter-full-screen', function () {
writeLog('info', 'mainWindow.on.enter-full-screen ::: mainWindow is now in fullscreen')
})
// when the app goes leaves fullscreen
mainWindow.on('leave-full-screen', function () {
// disabled to reduce clutter
})
// when the app gets resized
mainWindow.on('resize', function () {
// disabled to reduce clutter
})
// when the app gets hidden
mainWindow.on('hide', function () {
writeLog('info', 'mainWindow.on.hide ::: mainWindow is now hidden')
})
// when the app gets maximized
mainWindow.on('maximize', function () {
writeLog('info', 'mainWindow.on.maximize ::: mainWindow is now maximized')
})
// when the app gets unmaximized
mainWindow.on('unmaximize', function () {
writeLog('info', 'mainWindow.on.unmaximize ::: mainWindow is now unmaximized')
})
// when the app gets minimized
mainWindow.on('minimize', function () {
writeLog('info', 'mainWindow.on.minimize ::: mainWindow is now minimized')
})
// when the app gets restored from minimized mode
mainWindow.on('restore', function () {
writeLog('info', 'mainWindow.on.restore ::: mainWindow is now restored')
})
mainWindow.on('app-command', function () {
writeLog('info', 'mainWindow.on.app-command ::: mainWindow got app-command')
})
// Emitted before the window is closed.
mainWindow.on('close', function () {
writeLog('info', 'mainWindow.on.close ::: mainWindow will close')
// get current window position and size
const data = {
bounds: mainWindow.getBounds()
}
// define target path (in user data) to store rthe values
const customUserDataPath = path.join(defaultUserDataPath, 'ttthMainWindowPosSize.json')
// try to write the window position and size to preference file
fs.writeFile(customUserDataPath, JSON.stringify(data), function (error) {
if (error) {
writeLog('error', 'mainWindow.on.close ::: storing window-position and -size of mainWindow in _' + customUserDataPath + '_ failed with error: _' + error + '_.')
writeLog('error', 'mainWindow.on.close ::: Error: ', error)
return console.log(error)
}
writeLog('info', 'mainWindow.on.close ::: Successfully stored window-position and -size in _' + customUserDataPath + '_.')
})
})
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
writeLog('info', 'mainWindow.on.closed ::: mainWindow is now closed')
})
// When the app is unresponsive
mainWindow.on('unresponsive', function () {
writeLog('error', 'mainWindow.on.unresponsive ::: mainWindow is now unresponsive')
showDialog('error', 'Alert', 'ttth seems unresponsive', 'Consider restarting the app')
})
// When the app gets responsive again
mainWindow.on('responsive', function () {
writeLog('info', 'mainWindow.on.responsive ::: mainWindow is now responsive again')
})
// When the app is crashed
mainWindow.webContents.on('crashed', function () {
writeLog('info', 'mainWindow.on.crashed ::: mainWindow crashed')
showDialog('error', 'Alert', 'ttth just crashed', 'Consider reporting this issue')
})
// Call from renderer: Reload mainWindow
ipcMain.on('reloadMainWindow', (event) => {
mainWindow.reload()
writeLog('info', 'ipcMain.reloadMainWindow ::: mainWindow is now reloaded (ipcMain)')
})
// Call from renderer: Open folder with user configured services
ipcMain.on('openUserServicesConfigFolder', (event) => {
const customUserDataPath = path.join(defaultUserDataPath, 'storage')
if (shell.openPath(customUserDataPath) === true) {
writeLog('info', 'ipcMain.openUserServicesConfigFolder ::: ServiceConfigs: Opened the folder _' + customUserDataPath + '_ which contains all user-configured services')
} else {
writeLog('warn', 'ipcMain.openUserServicesConfigFolder ::: ServiceConfigs: Failed to open the folder _' + customUserDataPath + '_ (which contains all user-configured services).')
}
})
// Call from renderer: Open folder with user settings
ipcMain.on('openUserSettingsConfigFolder', (event) => {
const customUserDataPath = path.join(defaultUserDataPath, 'ttthUserSettings')
if (shell.openPath(customUserDataPath) === true) {
writeLog('info', 'ipcMain.openUserSettingsConfigFolder ::: UserSettings: Opened the folder _' + customUserDataPath + '_ which contains all user-configured services')
} else {
writeLog('warn', 'ipcMain.openUserSettingsConfigFolder ::: UserSettings: Failed to open the folder _' + customUserDataPath + '_ (which contains all user-configured services).')
}
})
// Call from renderer ::: deleteAllGlobalServicesShortcut
ipcMain.on('deleteAllGlobalServicesShortcut', function (arg1, numberOfEnabledServices) {
globalShortcut.unregisterAll() // doesnt work - whyever
writeLog('info', 'ipcMain.deleteAllGlobalServicesShortcut ::: Shortcuts: Deleting all global service shortcut at once.')
// delete all global shortcuts manually
/*
var i;
for (i = 1; i <= numberOfEnabledServices; i++)
{
globalShortcut.unregister("CmdOrCtrl+" + i);
writeLog("info", "createWindow ::: Shortcuts: Deleting the global service shortcut: CmdOrCtrl+" + i);
}
*/
writeLog('info', 'ipcMain.deleteAllGlobalServicesShortcut ::: Shortcuts: Finished deleting all global service shortcuts')
})
// Call from renderer ::: createNewGlobalShortcut
ipcMain.on('createNewGlobalShortcut', function (arg1, shortcut, targetTab) {
writeLog('info', 'ipcMain.createNewGlobalShortcut ::: Shortcuts: Creating a new shortcut: _' + shortcut + '_ for the service/tab: _' + targetTab + '_.')
// const ret = globalShortcut.register(shortcut, () => {
globalShortcut.register(shortcut, () => {
writeLog('error', 'Shortcut: _' + shortcut + '_ was pressed.')
mainWindow.webContents.send('switchToTab', targetTab) // activate the related tab
})
})
// Call from renderer: should ttth update the app badge count
// is supported for macOS & Linux (running Unity)
ipcMain.on('updateBadgeCount', (event, arg) => {
let environmentSupported = false
// Possible values are 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.
switch (userOSPlatform) {
case 'darwin':
environmentSupported = true
break
case 'linux':
const checkForUnity = app.isUnityRunning()
if (checkForUnity === true) {
environmentSupported = true
}
break
default:
// do nothing
}
// if the environment supports BadgeCount - update it
if (environmentSupported === true) {
// temporary hack - cause of #182
if (Number.isNaN(arg)) {
writeLog('warn', 'ipcMain.updateBadgeCount ::: Returned value is not a number (NaN). Falling back to value 0') // updating badge count worked
arg = 0 // set a fallback value - see #182
}
const currentBadgeCount = app.getBadgeCount() // get the current badge count
// FIXME: deprecated - Please use 'badgeCount property' instead.
// if badge count has to be updated - try to update it
if (currentBadgeCount !== arg) {
const didUpdateBadgeCount = app.setBadgeCount(arg) // FIXME: deprecated.- Please use 'badgeCount property' instead.
if (didUpdateBadgeCount === true) {
writeLog('info', 'ipcMain.updateBadgeCount ::: Updating application badge count to _' + arg + '_.') // updating badge count worked
} else {
writeLog('warn', 'ipcMain.updateBadgeCount ::: Updating application badge count to _' + arg + '_ failed.') // updating badge count failed
}
}
}
})
// *****************************************************************
// modal window: to allow creating and configuring a single service
// *****************************************************************
//
configWindow = new BrowserWindow({
parent: mainWindow,
modal: true, // Whether this is a modal window. This only works when the window is a child window
// title: '${productName}',
frame: false, // false results in a borderless window
show: false, // hide as default
titleBarStyle: 'hidden',
resizable: false,
width: 600,
height: 650,
minWidth: 600,
minHeight: 650,
backgroundColor: '#ffffff',
icon: path.join(__dirname, 'app/img/icon/icon.png'),
webPreferences: {
enableRemoteModule: true,
nodeIntegration: true,
webviewTag: true // see #37
}
})
writeLog('info', 'createWindow ::: Finished creating configWindow')
// load html form to the window
configWindow.loadFile('app/configWindow.html')
writeLog('info', 'createWindow ::: Loaded configWindow.html to configWindow')
// hide menubar
configWindow.setMenuBarVisibility(false)
writeLog('info', 'createWindow ::: Hiding menubar of configWindow')
// Emitted when the window gets a close event.(close VS closed)
configWindow.on('close', function (event) {
writeLog('info', 'configWindow will close, but we hide it (event: close)')
configWindow.hide() // just hide it - so it can re-opened
})
// Emitted when the window is ready to be shown
configWindow.on('ready-to-show', function (event) {
writeLog('info', 'configWindow is now ready to show (event: ready-to-show)')
// do some checks & routines once at start of the application
mainWindow.webContents.send('startSearchUpdatesSilent') // search silently for ttth updates
})
// Emitted when the window is shown
configWindow.on('show', function (event) {
writeLog('info', 'configWindow is now shown (event: show)')
})
// Call from renderer: show configure-single-service window for a new service
ipcMain.on('showConfigureSingleServiceWindowNew', (event, arg) => {
writeLog('info', 'ipcMain.showConfigureSingleServiceWindowNew ::: configWindow preparing for new service creation')
configWindow.show() // show window
configWindow.webContents.send('serviceToCreate', arg)
})
// Call from renderer: show configure-single-service window
ipcMain.on('showConfigureSingleServiceWindow', (event, arg) => {
writeLog('info', 'ipcMain.showConfigureSingleServiceWindow ::: configWindow preparing for service editing')
configWindow.show() // show window
configWindow.webContents.send('serviceToConfigure', arg)
})
// Call from renderer: hide configure-single-service window
ipcMain.on('closeConfigureSingleServiceWindow', (event) => {
configWindow.hide() // hide window
writeLog('info', 'ipcMain.closeConfigureSingleServiceWindow ::: configWindow is now hidden')
})
// Call from renderer: Tray: RecreateTray
ipcMain.on('recreateTray', function () {
writeLog('info', 'ipcMain.recreateTray ::: Recreating tray')
createTray()
})
writeLog('info', 'createWindow ::: Finished creating mainWindow and configWindow')
}
/**
* @function forceSingleAppInstance
* @summary Takes care that there is only 1 instance of this app running
* @description Takes care that there is only 1 instance of this app running
* @memberof main
*/
function forceSingleAppInstance () {
writeLog('info', 'forceSingleAppInstance ::: Checking if there is only 1 instance of ttth')
if (!gotTheLock) {
writeLog('error', 'forceSingleAppInstance ::: There is already another instance of ttth')
app.quit() // quit the second instance
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our first instance window.
if (mainWindow) {
// #134
/*
if (mainWindow === null) {
// do nothing - there is no mainwindow - most likely we are on macOS
} else {
// mainWindow exists
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus()
}
*/
// simplify:
// mainWindow exists
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus()
}
})
}
}
/**
* @function createMenu
* @summary Creates the application menu
* @description Creates the application menu
* @memberof main
*/
function createMenu () {
// Create a custom menu
const menu = Menu.buildFromTemplate([
// Menu: File
{
label: 'File',
submenu: [
// Settings
{
label: 'Settings',
click (item, mainWindow) {
mainWindow.webContents.send('showSettings')
},
accelerator: 'CmdOrCtrl+,'
},
// Separator
{
type: 'separator'
},
// Exit
{
role: 'quit',
label: 'Exit',
click () {
app.quit()
},
accelerator: 'CmdOrCtrl+Q'
}
]
},
// Menu: Edit
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
selector: 'undo:'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
selector: 'redo:'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:'
}
]
},
// Menu: View
{
label: 'View',
submenu: [
{
label: 'Next Service',
click (item, mainWindow) {
mainWindow.webContents.send('nextTab')
},
accelerator: 'CmdOrCtrl+right'
},
{
label: 'Previous Service',
click (item, mainWindow) {
mainWindow.webContents.send('previousTab')
},
accelerator: 'CmdOrCtrl+left'
},
{
type: 'separator'
},
{
role: 'reload',
label: 'Reload',
click (item, mainWindow) {
mainWindow.reload()
},
accelerator: 'CmdOrCtrl+R'
},
{
label: 'Reload current service',
click (item, mainWindow) {
mainWindow.webContents.send('reloadCurrentService')
},
accelerator: 'CmdOrCtrl+S',
enabled: true
}
]
},
// Menu: Window
{
label: 'Window',
submenu: [
{
role: 'togglefullscreen',
label: 'Toggle Fullscreen',
click (item, mainWindow) {
if (mainWindow.isFullScreen()) {
mainWindow.setFullScreen(false)
} else {
mainWindow.setFullScreen(true)
}
},
accelerator: 'F11' // is most likely predefined on osx - results in: doesnt work on osx
},
{
role: 'hide',
label: 'Hide',
click (item, mainWindow) {
mainWindow.hide()
// mainWindow.reload();
},
accelerator: 'CmdOrCtrl+H',
enabled: true
},
{
role: 'minimize',
label: 'Minimize',
click (item, mainWindow) {
if (mainWindow.isMinimized()) {
// mainWindow.restore();
} else {
mainWindow.minimize()
}
},
accelerator: 'CmdOrCtrl+M'
},
{
label: 'Maximize',
click (item, mainWindow) {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize()
} else {
mainWindow.maximize()
}
},
accelerator: 'CmdOrCtrl+K'
}
]
},
// Menu: Help
{
role: 'help',
label: 'Help',
submenu: [
// About
{
// role: 'about', - see: https://github.com/rhysd/electron-about-window/issues/59
label: 'About',
click () {
openAboutWindow({
icon_path: path.join(__dirname, 'app/img/about/icon_about.png'),
open_devtools: false,
use_version_info: true,
win_options: // https://github.com/electron/electron/blob/master/docs/api/browser-window.md#new-browserwindowoptions
{