-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
602 lines (540 loc) · 21.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
/**
* @file Contains the main.js code of dirgistered
* @author yafp
* @namespace main
*/
// ----------------------------------------------------------------------------
// REQUIRE: DIRGISTERED MODULES
// ----------------------------------------------------------------------------
const crash = require('./app/js/modules/crashReporter.js') // crashReporter
const sentry = require('./app/js/modules/sentry.js') // sentry
const unhandled = require('./app/js/modules/unhandled.js') // electron-unhandled
const utils = require('./app/js/modules/utils.js')
// -----------------------------------------------------------------------------
// REQUIRE: 3rd PARTY
// -----------------------------------------------------------------------------
const { app, BrowserWindow, electron, ipcMain, Menu } = require('electron')
const shell = require('electron').shell
const path = require('path')
const fs = require('fs')
const openAboutWindow = require('about-window').default // for: about-window
// npm-check:
// use:
// npm-check -s
// to ignore all non-referenced node_modules
// ----------------------------------------------------------------------------
// ERROR-HANDLING:
// ----------------------------------------------------------------------------
crash.initCrashReporter()
unhandled.initUnhandled()
sentry.enableSentry() // sentry is enabled by default
// sentry.disableSentry() // sentry is enabled by default
// ----------------------------------------------------------------------------
// VARIABLES & CONSTANTS
// ----------------------------------------------------------------------------
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
let settingsWindow
const gotTheLock = app.requestSingleInstanceLock() // for: single-instance handling
const defaultUserDataPath = app.getPath('userData') // for: storing window position and size
const { urlGitHubGeneral, urlGitHubIssues, urlGitHubChangelog, urlGitHubReleases } = require('./app/js/modules/githubUrls.js') // project-urls
// Caution: Warning since electron 8
// app.allowRendererProcessReuse = false // see: https://github.com/electron/electron/issues/18397
// mainWindow: minimal window size
const mainWindowMinimalWindowHeight = 650
const mainWindowMinimalWindowWidth = 700
// settingsWundow: minimal window size
const settingsWindowMinimalWindowHeight = 400
const settingsWindowMinimalWindowWidth = 800
var enablePrereleases = false // set default value
var enableErrorReporting = true // set default value
global.sharedObj = {
// settings UI
enablePrereleases: enablePrereleases,
enableErrorReporting: enableErrorReporting
}
// ----------------------------------------------------------------------------
// FUNCTIONS
// ----------------------------------------------------------------------------
/**
* @function doLog
* @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
*/
function doLog (type, message) {
const prefix = '[ Main ] '
const log = require('electron-log')
// electron-log can: error, warn, info, verbose, debug, silly
switch (type) {
case 'info':
log.info(prefix + message)
break
case 'warn':
log.warn(prefix + message)
break
case 'error':
log.error(prefix + message)
break
default:
log.silly(prefix + message)
// code block
}
}
/**
* @function createWindowSettings
* @summary Manages the BrowserWindow for the Settings UI
* @description Manages the BrowserWindow for the Settings UI
* @memberof main
*/
function createWindowSettings () {
doLog('info', 'createWindowSettings ::: Creating the settings window')
// Create the browser window.
settingsWindow = 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', // since 0.3.0
show: true, // hide until: ready-to-show
center: true, // Show window in the center of the screen. (since 0.3.0)
width: settingsWindowMinimalWindowWidth,
minWidth: settingsWindowMinimalWindowWidth,
// resizable: false, // this conflickts with opening dev tools
minimizable: false, // not implemented on linux
maximizable: false, // not implemented on linux
height: settingsWindowMinimalWindowHeight,
minHeight: settingsWindowMinimalWindowHeight,
icon: path.join(__dirname, 'app/img/icon/icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
webSecurity: true // introduced in 0.3.0
}
})
// and load the setting.html of the app.
settingsWindow.loadFile('app/settings.html')
// window needs no menu
settingsWindow.removeMenu()
// Call from renderer: Settings UI - toggle dev tools
ipcMain.on('settingsToggleDevTools', function () {
settingsWindow.webContents.toggleDevTools()
})
// Emitted before the window is closed.
settingsWindow.on('close', function () {
doLog('info', 'createWindowSettings ::: settingsWindow will close (event: close)')
})
// Emitted when the window is closed.
settingsWindow.on('closed', function (event) {
doLog('info', 'createWindowSettings ::: settingsWindow 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.
settingsWindow = null
// unblur main UI
mainWindow.webContents.send('unblurMainUI')
})
}
/**
* @function createWindowMain
* @summary Creates the mainWindow
* @description Creates the mainWindow (restores window position and size of possible)
* @memberof main
*/
function createWindowMain () {
doLog('info', 'createWindowMain ::: Starting to create the application windows')
// Check last window position and size from user data
var windowWidth
var windowHeight
var windowPositionX
var windowPositionY
// Read a local config file
var customUserDataPath = path.join(defaultUserDataPath, 'DirgisteredWindowPosSize.json')
var 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
doLog('info', 'createWindowMain ::: Got last window position and size information from _' + customUserDataPath + '_.')
} catch (e) {
doLog('warn', 'createWindowMain ::: No last window position and size information found in _' + customUserDataPath + '_. Using fallback values')
// set some default values for window size
windowWidth = mainWindowMinimalWindowWidth
windowHeight = mainWindowMinimalWindowHeight
}
// Create the browser window.
mainWindow = new BrowserWindow({
frame: false, // false results in a borderless window. Needed for custom titlebar
titleBarStyle: 'hidden', // needed for custom-electron-titlebar. See: https://electronjs.org/docs/api/frameless-window
backgroundColor: '#ffffff', // since 0.3.0
show: false, // hide until: ready-to-show
center: true, // Show window in the center of the screen. (since 0.3.0)
width: windowWidth,
minWidth: mainWindowMinimalWindowWidth,
height: windowHeight,
minHeight: mainWindowMinimalWindowHeight,
icon: path.join(__dirname, 'app/img/icon/icon.png'),
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true,
webSecurity: true // introduced in 0.3.0
// preload: path.join(__dirname, 'preload.js')
}
})
// Call from renderer: Option: load settings UI
ipcMain.on('settingsUiLoad', function () {
createWindowSettings()
})
// Restore window position if possible
//
// requirements: found values in DirgisteredWindowPosSize.json from the previous session
if ((typeof windowPositionX !== 'undefined') && (typeof windowPositionY !== 'undefined')) {
mainWindow.setPosition(windowPositionX, windowPositionY)
}
// Call from renderer: show mainUI
ipcMain.on('showAndFocusMainUI', function () {
mainWindow.show()
mainWindow.focus()
})
// Call from renderer: Open settings folder
ipcMain.on('settingsFolderOpen', (event) => {
doLog('info', 'createWindowMain ::: Opened the users settings folder (ipcMain)')
const userSettingsPath = path.join(app.getPath('userData'), 'UserSettings') // change path for userSettings
if (shell.openItem(userSettingsPath) === true) {
doLog('info', 'createWindowMain ::: Opened the media-dupes subfolder in users download folder (ipcMain)')
} else {
doLog('error', 'createWindowMain ::: Failed to open the user download folder (ipcMain)')
}
})
// Call from renderer: Update property from globalObj
ipcMain.on('globalObjectSet', function (event, property, value) {
doLog('info', 'globalObjectSet ::: Set _' + property + '_ to: _' + value + '_')
global.sharedObj[property] = value
console.warn(global.sharedObj)
})
// and load the index.html of the app.
mainWindow.loadFile('app/index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the web page becomes unresponsive.
mainWindow.on('unresponsive', function () {
doLog('warn', 'createWindowMain ::: mainWindow is now unresponsive (event: unresponsive)')
})
// Emitted when the unresponsive web page becomes responsive again.
mainWindow.on('responsive', function () {
doLog('info', 'createWindowMain ::: mainWindow is now responsive again (event: responsive)')
})
// Emitted when the web page has been rendered (while not being shown) and window can be displayed without a visual flash.
mainWindow.on('ready-to-show', function () {
doLog('info', 'createWindowMain ::: mainWindow is now ready, so show it and then focus it (event: ready-to-show)')
mainWindow.show()
mainWindow.focus()
mainWindow.webContents.send('startSearchUpdatesSilent') // search silently for dirgistered updates
})
// Emitted before the window is closed.
mainWindow.on('close', function (event) {
doLog('info', 'createWindowMain ::: mainWindow will close (event: close)')
// get window position and size
var data = {
bounds: mainWindow.getBounds()
}
// define target path (in user data)
var customUserDataPath = path.join(defaultUserDataPath, 'DirgisteredWindowPosSize.json')
// try to write
fs.writeFile(customUserDataPath, JSON.stringify(data), function (error) {
if (error) {
doLog('error', 'createWindowMain ::: storing window-position and -size of mainWindow in _' + customUserDataPath + '_ failed with error: _' + error + '_ (event: close)')
throw error
}
doLog('info', 'createWindowMain ::: mainWindow stored window-position and -size in _' + customUserDataPath + '_ (event: close)')
})
})
// Emitted when the window is closed.
mainWindow.on('closed', function (event) {
doLog('info', 'createWindowMain ::: mainWindow 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.
mainWindow = null
})
}
/**
* @function createMenuMain
* @summary Creates the application menu
* @description Creates the application menu
* @memberof main
*/
function createMenuMain () {
// doLog('createMenu', __dirname)
// Create a custom menu
var menu = Menu.buildFromTemplate([
// Menu: File
{
label: 'File',
submenu: [
// Settings
{
label: 'Settings',
enabled: false,
// icon: __dirname + '/app/img/icon/icon.png',
click () {
mainWindow.webContents.send('openSettings')
},
accelerator: 'CmdOrCtrl+,'
},
{
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: [
{
role: 'reload',
label: 'Reload',
click (item, mainWindow) {
mainWindow.reload()
},
accelerator: 'CmdOrCtrl+R'
}
]
},
// 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: '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',
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
{
autoHideMenuBar: true,
titleBarStyle: 'hidden',
minimizable: false, // not implemented on linux
maximizable: false, // not implemented on linux
movable: false, // not implemented on linux
resizable: false,
alwaysOnTop: true,
fullscreenable: false,
skipTaskbar: false
}
})
}
},
// open homepage
{
label: 'Homepage',
click () {
shell.openExternal(urlGitHubGeneral)
},
accelerator: 'F1'
},
// report issue
{
label: 'Report issue',
click () {
shell.openExternal(urlGitHubIssues)
},
accelerator: 'F2'
},
// open changelog
{
label: 'Changelog',
click () {
shell.openExternal(urlGitHubChangelog)
},
accelerator: 'F3'
},
// open Releases
{
label: 'Releases',
click () {
shell.openExternal(urlGitHubReleases)
},
accelerator: 'F4'
},
{
type: 'separator'
},
// Update
{
label: 'Search dirgistered updates',
click (item, mainWindow) {
mainWindow.webContents.send('startSearchUpdatesVerbose')
},
enabled: true,
accelerator: 'F9'
},
{
type: 'separator'
},
// Console
{
id: 'HelpConsole',
label: 'Console',
click (item, mainWindow) {
mainWindow.webContents.toggleDevTools()
},
enabled: true,
accelerator: 'F12'
}
]
}
])
// use the menu
Menu.setApplicationMenu(menu)
}
/**
* @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 () {
if (!gotTheLock) {
doLog('warn', 'forceSingleAppInstance ::: There is already another instance of dirgistered')
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()
}
}
})
}
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
//
// app.on('ready', createWindow)
app.on('ready', function () {
forceSingleAppInstance() // check for single instance
createWindowMain() // create the application UI
createMenuMain() // create the application menu
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindowMain()
})