This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
443 lines (400 loc) · 14.7 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
/**
* @file Contains the main.js code of logre
* @author yafp
* @namespace main
*/
// -----------------------------------------------------------------------------
// REQUIRE: 3rd PARTY
// -----------------------------------------------------------------------------
const { app, BrowserWindow, electron, ipcMain, Menu } = require('electron')
const path = require('path')
const fs = require('fs')
const shell = require('electron').shell
const openAboutWindow = require('about-window').default // for: about-window
// ----------------------------------------------------------------------------
// REQUIRE: LOGRE MODULES
// ----------------------------------------------------------------------------
const { urlGitHubGeneral, urlGitHubIssues, urlGitHubChangelog, urlGitHubReleases } = require('./app/js/modules/githubUrls.js') // project-urls
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
// ----------------------------------------------------------------------------
// 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
// var path = require('path') // needed for icon-handling
var iconPath = path.join(__dirname, '/app/img/icon/icon.png')
const defaultUserDataPath = app.getPath('userData') // for: storing window position and size
// mainWindow: minimal window size
const mainWindowMinimalWindowHeight = 800
const mainWindowMinimalWindowWidth = 900
// ----------------------------------------------------------------------------
// 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 createWindowMain
* @summary Creates the mainWindow
* @description Creates the mainWindow (restores window position and size of possible)
* @memberof main
*/
function createWindow () {
// 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, 'LogreWindowPosSize.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
width: windowWidth,
minWidth: mainWindowMinimalWindowWidth,
height: windowHeight,
minHeight: mainWindowMinimalWindowHeight,
resizable: true, // false = not resizeable
show: false, // show when ready-to-show
icon: iconPath,
webPreferences:
{
nodeIntegration: true
}
})
// Restore window position if possible
//
// requirements: found values in MediaDupesWindowPosSize.json from the previous session
if ((typeof windowPositionX !== 'undefined') && (typeof windowPositionY !== 'undefined')) {
mainWindow.setPosition(windowPositionX, windowPositionY)
}
// and load the index.html of the app.
mainWindow.loadFile('app/index.html')
// configure menu
mainWindow.setMenu(null) // works for builds, not while developing
mainWindow.on('ready-to-show', function () {
mainWindow.show()
mainWindow.focus()
mainWindow.webContents.send('checkThingsOnceAtStart')
})
// 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, 'LogreWindowPosSize.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 () {
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',
// icon: __dirname + '/app/img/icon/icon.png',
click () {
mainWindow.webContents.send('openSettings')
},
enabled: false,
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 logre updates',
click (item, mainWindow) {
mainWindow.webContents.send('startSearchUpdatesVerbose')
},
enabled: false,
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)
}
// 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. We DONT want to force a singleInstance for this particular app.
createWindow() // 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) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.