This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
index.js
260 lines (216 loc) · 7.38 KB
/
index.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
const {app, BrowserWindow, ipcMain, session} = require('electron');
app.setName('Standard Notes');
const path = require('path')
const windowStateKeeper = require('electron-window-state')
const shell = require('electron').shell;
const log = require('electron-log');
const Store = require('./javascripts/main/store.js');
import menuManager from './javascripts/main/menuManager.js'
import archiveManager from './javascripts/main/archiveManager.js';
import packageManager from './javascripts/main/packageManager.js';
import searchManager from './javascripts/main/searchManager.js';
import updateManager from './javascripts/main/updateManager.js';
import zoomManager from './javascripts/main/zoomManager.js';
import trayManager from './javascripts/main/trayManager.js';
ipcMain.on('initial-data-loaded', () => {
archiveManager.beginBackups();
});
ipcMain.on('major-data-change', () => {
archiveManager.performBackup();
})
process.on('uncaughtException', function (err) {
console.log(err);
})
log.transports.file.level = 'info';
let darwin = process.platform === 'darwin'
let win, willQuitApp = false;
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (!darwin) {
app.quit()
}
})
function createWindow () {
// Load the previous state with fallback to defaults
let winState = windowStateKeeper({
defaultWidth: 1100,
defaultHeight: 800
})
let iconLocation = path.join(__dirname, '/icon/Icon-512x512.png');
// Defaults to false in store.js
let useSystemMenuBar = Store.instance().get("useSystemMenuBar");
// Create the window using the state information
win = new BrowserWindow({
'x': winState.x,
'y': winState.y,
'width': winState.width,
'height': winState.height,
'minWidth': 300,
'minHeight': 400,
show: false,
icon: iconLocation,
// We want hiddenInset on Mac. On Windows/Linux, doesn't seem to have an effect, but we'll default to it's original value before themed title bar changes were put in place.
titleBarStyle: darwin || useSystemMenuBar ? 'hiddenInset' : null,
// Will apply to Windows and Linux only, since titleBarStyle takes precendence for mac. But we'll explicitely specifiy false for mac to be on the safe side
frame: darwin ? false : useSystemMenuBar,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, "javascripts/renderer/preload.js")
}
})
searchManager.setWindow(win);
archiveManager.setWindow(win);
packageManager.setWindow(win);
updateManager.setWindow(win);
zoomManager.setWindow(win);
trayManager.setWindow(win);
// 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
winState.manage(win)
// win.webContents.openDevTools()
win.on('closed', (event) => {
win = null
})
/*
Both Electron 5 and 6 have an issue where inputs freeze up after an alert is shown.
The only workaround is to blur then focus the window.
https://github.com/electron/electron/issues/20400
*/
const isWindows = process.platform === 'win32';
let needsFocusFix = false;
let triggeringProgrammaticBlur = false;
win.on('blur', (event) => {
win.webContents.send("window-blurred", null);
archiveManager.applicationDidBlur();
if(!triggeringProgrammaticBlur) {
needsFocusFix = true;
}
})
win.on('focus', (event) => {
win.webContents.send("window-focused", null);
if(isWindows && needsFocusFix) {
needsFocusFix = false;
triggeringProgrammaticBlur = true;
setTimeout(function () {
win.blur();
win.focus();
setTimeout(function () {
triggeringProgrammaticBlur = false;
}, 100);
}, 100);
}
})
win.once('ready-to-show', () => {
win.show()
})
win.on('close', (event) => {
if (willQuitApp) {
/* the user tried to quit the app */
win = null;
} else if(darwin || trayManager.shouldMinimizeToTray()) {
/* the user only tried to close the window */
event.preventDefault();
// Fixes Mac full screen issue where pressing close results in a black screen.
if(win.isFullScreen()) {
win.setFullScreen(false);
}
win.hide();
}
})
let windowUrl = 'file://' + __dirname + '/index.html';
if ('APP_RELATIVE_PATH' in process.env) {
windowUrl = 'file://' + __dirname + '/' + process.env.APP_RELATIVE_PATH;
}
win.loadURL(windowUrl);
const shouldOpenUrl = (url) => {
return url.startsWith("http") || url.startsWith("https");
}
// Check file urls for equality by decoding components
// In packaged app, spaces in navigation events urls can contain %20 but not in windowUrl.
const safeFileUrlCompare = (a, b) => {
// Catch exceptions in case of malformed urls.
try {
// Craft URL objects to eliminate production URL values that can contain "#!/" suffixes (on Windows)
let aPath = new URL(decodeURIComponent(a)).pathname;
let bPath = new URL(decodeURIComponent(b)).pathname;
return aPath === bPath;
} catch (error) {
return false;
}
}
// handle link clicks
win.webContents.on('new-window', function(event, url) {
if(shouldOpenUrl(url)) {
shell.openExternal(url);
}
event.preventDefault();
});
// handle link clicks (this event is fired instead of
// 'new-window' when target is not set to _blank)
win.webContents.on('will-navigate', function(event, url) {
// Check for windowUrl equality in the case of window.reload() calls.
if(safeFileUrlCompare(url, windowUrl) === true) {
return;
}
if(shouldOpenUrl(url)) {
shell.openExternal(url);
}
event.preventDefault();
});
}
app.on('before-quit', () => willQuitApp = true);
app.on('activate', function() {
if (!win) {
createWindow();
} else {
win.show();
}
updateManager.checkForUpdate();
});
// feature flag: https://github.com/electron/electron/blob/master/docs/api/breaking-changes.md#appmakesingleinstance
const hasRequestSingleInstanceLock = app.requestSingleInstanceLock ? true : false;
let isSecondInstance = null;
// Someone tried to run a second instance, we should focus our window.
const handleSecondInstance = (argv, cwd) => {
if (win) {
if (!win.isVisible()) win.show();
if (win.isMinimized()) win.restore();
win.focus();
}
}
if (hasRequestSingleInstanceLock) {
isSecondInstance = !app.requestSingleInstanceLock()
app.on('second-instance', (event, argv, cwd) => {
handleSecondInstance(argv, cwd)
})
} else {
isSecondInstance = app.makeSingleInstance((argv, cwd) => {
handleSecondInstance(argv, cwd)
})
}
app.on('ready', function(){
if (isSecondInstance) {
console.warn("Quiting app and focusing existing instance.");
app.quit()
} else {
if(!win) {
createWindow();
} else {
win.focus();
}
menuManager.loadMenu(win, archiveManager, updateManager, trayManager);
updateManager.onNeedMenuReload = () => {
menuManager.reload();
}
if (trayManager.shouldMinimizeToTray() && (process.platform === 'win32' || process.platform === 'linux')) {
trayManager.createTrayIcon();
}
}
})
ipcMain.on("display-app-menu", (event, position) => {
menuManager.popupMenu(position);
});