forked from ezra-bible-app/ezra-bible-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
352 lines (285 loc) · 10.5 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
/* This file is part of Ezra Bible App.
Copyright (C) 2019 - 2024 Ezra Bible App Development Team <[email protected]>
Ezra Bible App is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Ezra Bible App is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ezra Bible App. See the file LICENSE.
If not, see <http://www.gnu.org/licenses/>. */
const path = require('path');
const url = require('url');
const { app, BrowserWindow, Menu, ipcMain, nativeTheme } = require('electron');
const IPC = require('./app/backend/ipc/ipc.js');
const PlatformHelper = require('./app/lib/platform_helper.js');
if (process.platform === 'win32') {
// This is only needed for making the Windows installer work properly
if (require('electron-squirrel-startup')) app.quit();
}
function initGlobals() {
global.isDev = !app.isPackaged;
global.ipc = new IPC();
global.ipcHandlersRegistered = false;
global.platformHelper = new PlatformHelper();
// 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.
global.mainWindow = null;
global.mainWindowState = null;
// Disable security warnings
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = true;
// Flag used in connection with before-quit handler to ensure the before-quit code is only executed once
global.exitComplete = false;
}
function initDropboxProtocolClient() {
const URL_SCHEME = 'ezrabible';
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(URL_SCHEME, process.execPath, [path.resolve(process.argv[1])]);
}
} else {
app.setAsDefaultProtocolClient(URL_SCHEME);
}
}
function initSentryCrashReports() {
global.sendCrashReports = global.ipc.ipcSettingsHandler.getConfig().get('sendCrashReports', true);
if (!global.isDev && !global.sendCrashReports) {
console.log("Not configuring Sentry based on opt-out.");
}
if (!global.isDev && global.sendCrashReports) {
global.Sentry = require('@sentry/electron/main');
Sentry.init({
debug: false,
dsn: 'https://[email protected]/1488321',
enableNative: true,
environment: process.env.NODE_ENV,
beforeSend: (event) => global.sendCrashReports ? event : null
});
} else {
global.Sentry = {
addBreadcrumb: function() {},
captureMessage: function() {},
Severity: {
Info: ''
}
};
}
}
function initElectronDebug() {
try {
// Loading electron-debug in a try/catch block, because we have observed failures related to this step
// If it fails ... startup is broken. Why it failed? Unclear!
require('electron-debug')({
isEnabled: true,
showDevTools: false,
devToolsMode: 'bottom'
});
} catch (e) {
console.log('Could not load electron-debug');
}
}
function shouldUseDarkMode() {
let useDarkMode = false;
const useSystemTheme = global.ipc.ipcSettingsHandler.getConfig().get('useSystemTheme', false);
if (platformHelper.isMacOsMojaveOrLater() && useSystemTheme) {
if (nativeTheme.shouldUseDarkColors) {
useDarkMode = true;
}
} else {
useDarkMode = global.ipc.ipcSettingsHandler.getConfig().get('useNightMode', false);
}
return useDarkMode;
}
function updateMacOsMenu(labels=undefined) {
var quitAppLabel = 'Quit Ezra Bible App';
if (labels !== undefined) {
quitAppLabel = labels['quit-app'];
}
const menu = Menu.buildFromTemplate([{
label: '&File',
submenu: [{
label: quitAppLabel,
accelerator: 'Ctrl+Q',
click: function () { app.quit(); }
}]
}]);
Menu.setApplicationMenu(menu);
}
function initIpcHandlers() {
if (!global.ipcHandlersRegistered) {
// This ensures that we do not register ipc handlers twice, which is not possible.
// This can happen on macOS, when the window is first closed and then opened another time.
global.ipcHandlersRegistered = true;
// eslint-disable-next-line no-unused-vars
ipcMain.on('manageWindowState', async (event, arg) => {
// Register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
global.mainWindowState.manage(global.mainWindow);
});
ipcMain.on('log', async (event, message) => {
console.log("Log from renderer: " + message);
});
// eslint-disable-next-line no-unused-vars
ipcMain.handle('initIpc', async (event, arg) => {
let returnCode = await global.ipc.init(global.isDev, global.mainWindow);
return returnCode;
});
// eslint-disable-next-line no-unused-vars
ipcMain.handle('startupCompleted', async (event, arg) => {
console.timeEnd('Startup');
});
// eslint-disable-next-line no-unused-vars
ipcMain.handle('localizeMenu', async (event, menuLabels) => {
updateMacOsMenu(menuLabels);
});
}
}
function initMainWindow() {
let preloadScript = '';
if (!global.isDev && global.sendCrashReports) {
preloadScript = path.join(__dirname, 'app/frontend/helpers/sentry.js');
}
let bgColor = '#ffffff';
if (shouldUseDarkMode()) {
bgColor = '#1e1e1e';
}
// Create the browser window.
global.mainWindow = new BrowserWindow({
x: global.mainWindowState.x,
y: global.mainWindowState.y,
width: global.mainWindowState.width,
height: global.mainWindowState.height,
show: true,
frame: true,
title: "Ezra Bible App " + app.getVersion() + (global.isDev ? ` [${app.getLocale()}]` : ''),
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: preloadScript,
enableRemoteModule: true,
defaultEncoding: "UTF-8"
},
icon: path.join(__dirname, `icons/${platformHelper.isWin() ? 'ezra.ico' : 'ezra.png'}`),
backgroundColor: bgColor
});
}
function initWindowEventHandlers() {
// Emitted when the window is closed.
global.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.
global.mainWindow = null;
});
global.mainWindow.on('enter-full-screen', function () {
global.mainWindow.webContents.send('fullscreen-changed');
});
global.mainWindow.on('leave-full-screen', function () {
global.mainWindow.webContents.send('fullscreen-changed');
});
}
async function createWindow(firstStart=true) {
const windowStateKeeper = require('electron-window-state');
global.mainWindowState = windowStateKeeper({
defaultWidth: 1200,
defaultHeight: 800
});
initIpcHandlers();
initMainWindow();
if (firstStart) { // electron remote can only be initialized once, on macOS this function may run multiple times
require('@electron/remote/main').initialize();
}
require("@electron/remote/main").enable(global.mainWindow.webContents);
// The default menu will be created automatically if the app does not set one.
// It contains standard items such as File, Edit, View, Window and Help.
// We disable the menu by default and in a second step we enable it with minimal entries and only on macOS.
Menu.setApplicationMenu(null);
if (platformHelper.isMac()) {
updateMacOsMenu();
}
initWindowEventHandlers();
// and load the index.html of the app.
global.mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
}
function handleDropboxAuthUrl(url) {
if (url.indexOf('ezrabible') != -1) {
global.mainWindow.webContents.send('dropbox-auth-callback', url);
}
}
async function appReady() {
if (process.platform === 'win32' || process.platform === 'linux') {
// see https://www.electronjs.org/docs/latest/tutorial/launch-app-from-url-in-another-app#windows-and-linux-code
const lock = app.requestSingleInstanceLock();
if (!lock) {
app.quit();
return;
} else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
let url = commandLine.pop();
handleDropboxAuthUrl(url);
// Someone tried to run a second instance, we should focus our window.
if (global.mainWindow) {
if (global.mainWindow.isMinimized()) global.mainWindow.restore();
global.mainWindow.focus();
}
});
}
} else if (process.platform === 'darwin') {
app.on('open-url', (event, url) => {
handleDropboxAuthUrl(url);
});
}
await createWindow();
}
function initAppEventHandlers() {
// 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', async () => {
appReady();
});
app.on('before-quit', async (event) => {
if (!global.exitComplete) {
event.preventDefault();
await global.ipc.closeDatabase();
global.exitComplete = true;
app.quit();
}
});
// Quit when all windows are closed.
app.on('window-all-closed', async 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') {
app.quit();
}
});
app.on('activate', async () => {
// On OS X 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 (global.mainWindow === null) {
await createWindow(false);
}
});
}
function init() {
console.time('Startup');
initGlobals();
// Make the app window respond to calls coming from a browser window based on the Dropbox oauth process
initDropboxProtocolClient();
// Enable the Sentry crash report system based on the sendCrashReports option
initSentryCrashReports();
// Enables the Electron dev tools which can be opened using CTRL + SHIFT + i (Linux/Windows) or CMD + SHIFT + i (macOS)
initElectronDebug();
// This will eventually launch the app - based on the app ready event and the appReady handler function.
initAppEventHandlers();
}
init();