-
Notifications
You must be signed in to change notification settings - Fork 312
/
main.js
339 lines (299 loc) · 8.63 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
/* eslint-disable prefer-destructuring */
const {
app,
shell,
Menu,
BrowserWindow,
globalShortcut,
session,
ipcMain,
safeStorage,
dialog,
} = require('electron')
const path = require('path')
const url = require('url')
const { autoUpdater } = require('electron-updater')
const log = require('electron-log')
const port = process.env.PORT || 3000
let mainWindow = null
let initialDeepLinkUri = null
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient('neon', process.execPath, [
path.resolve(process.argv[1]),
])
app.setAsDefaultProtocolClient('neon2', process.execPath, [
path.resolve(process.argv[1]),
])
}
} else {
app.setAsDefaultProtocolClient('neon')
app.setAsDefaultProtocolClient('neon2')
}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
} else {
app.on('second-instance', (_event, commandLine) => {
const url = commandLine.pop()
if (!mainWindow) {
initialDeepLinkUri = url
return
}
mainWindow.webContents.send('link', url)
})
}
app.on('open-url', (_event, url) => {
if (!mainWindow) {
initialDeepLinkUri = url
return
}
mainWindow.webContents.send('link', url)
})
// adapted from https://github.com/chentsulin/electron-react-boilerplate
const installExtensions = () => {
const installer = require('electron-devtools-installer') // eslint-disable-line import/no-extraneous-dependencies
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS']
return Promise.all(
extensions.map(name => installer.default(installer[name])),
).catch(console.error)
}
app.on('ready', () => {
// https://github.com/electron/electron/blob/master/docs/tutorial/security.md#csp-http-header
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({ responseHeaders: "default-src 'none'" }) // eslint-disable-line
})
const onAppReady = () => {
mainWindow = new BrowserWindow({
height: 750,
width: 1200,
minHeight: 750,
minWidth: 1200,
titleBarStyle: 'hidden',
frame: false,
show: false,
icon: path.join(__dirname, 'icons/png/64x64.png'),
contextIsolation: true,
webPreferences: {
enableRemoteModule: true,
contextIsolation: false,
allowRunningInsecureContent: false,
webSecurity: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js'),
},
})
autoUpdater.checkForUpdatesAndNotify()
mainWindow.on('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
// https://discuss.atom.io/t/prevent-window-navigation-when-dropping-a-link/24365
mainWindow.webContents.on('will-navigate', ev => {
ev.preventDefault()
})
mainWindow.on('close', e => {
if (mainWindow) {
e.preventDefault()
mainWindow.webContents.send('quit')
}
})
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.once('dom-ready', () => {
mainWindow.webContents.openDevTools()
})
}
if (process.platform === 'win32' && process.argv.length > 1) {
initialDeepLinkUri = process.argv[1]
}
if (process.platform !== 'darwin') {
// Windows/Linux Menu
mainWindow.setMenu(null)
} else {
// Menu is required for MacOS
const template = [
{
label: app.getName(),
submenu: [{ role: 'about' }, { type: 'separator' }, { role: 'quit' }],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
],
},
{
label: 'View',
submenu: [{ role: 'toggledevtools' }],
},
{
role: 'help',
submenu: [
{
label: 'City of Zion',
click() {
shell.openExternal('https://cityofzion.io/')
},
},
{
label: 'GitHub',
click() {
shell.openExternal('https://github.com/CityOfZion')
},
},
{
label: 'NEO Reddit',
click() {
shell.openExternal('https://www.reddit.com/r/NEO/')
},
},
],
},
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
const inputMenu = Menu.buildFromTemplate([
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
click() {
mainWindow.webContents.paste()
},
},
])
mainWindow.webContents.on('context-menu', () => {
inputMenu.popup(mainWindow)
})
if (process.env.START_HOT) {
mainWindow.loadURL(`http://localhost:${port}/dist`)
} else {
mainWindow.loadURL(
url.format({
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, '/app/dist/index.html'),
}),
)
}
}
// register any shortcuts here
globalShortcut.register('CommandOrControl+M', () => {
mainWindow.minimize()
})
if (process.env.NODE_ENV === 'development') {
installExtensions().then(() => onAppReady())
} else {
onAppReady()
}
})
app.on('web-contents-created', (event, wc) => {
wc.on('before-input-event', (event, input) => {
// Windows/Linux hotkeys
if (process.platform !== 'darwin') {
if (input.key === 'F12') {
mainWindow.webContents.openDevTools()
event.preventDefault()
}
}
})
})
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})
ipcMain.on('closed', () => {
mainWindow = null
app.quit()
})
ipcMain.handle(
'getStoragePath',
async () => `${app.getPath('userData')}/storage`,
)
ipcMain.handle('getPath', async (event, folder) => app.getPath(folder))
ipcMain.handle('safeStorageEncrypt', async (event, value) => {
const buffer = safeStorage.encryptString(value)
const encrypted = buffer.toString('base64')
return encrypted
})
ipcMain.handle('safeStorageDecrypt', async (event, value) => {
const buffer = Buffer.from(value, 'base64')
const plainText = safeStorage.decryptString(buffer)
return plainText
})
ipcMain.handle('dialog', async (event, method, params) => {
const result = await dialog[method](params)
return result
})
ipcMain.handle('getInitialDeepLinkUri', async () => {
let uri = initialDeepLinkUri
if (uri && uri.startsWith('neon://uri=/wc?uri=')) {
// the new format comes with this prefix and it's not encoded. So, we are removing the prefix and encoding, to keep the old logic working for the old format
uri = uri.replace('/wc?uri=', '')
uri = `neon://uri=${btoa(
decodeURIComponent(uri.replace('neon://uri=', '')),
)}`
}
initialDeepLinkUri = null
return uri
})
ipcMain.handle('minimize', () => {
const win = BrowserWindow.getFocusedWindow()
win.minimize()
})
ipcMain.handle('maximize', () => {
const win = BrowserWindow.getFocusedWindow()
win.setFullScreen(!win.isFullScreen())
})
ipcMain.handle('close', () => {
const win = BrowserWindow.getFocusedWindow()
win.close()
})
ipcMain.handle('restore', () => {
if (!mainWindow) return
if (mainWindow.isMinimized()) {
mainWindow.restore()
} else {
mainWindow.show()
}
mainWindow.focus()
})
autoUpdater.logger = log
autoUpdater.logger.transports.file.level = 'info'
function sendStatusToWindow(text) {
log.info(text)
mainWindow.webContents.send('message', text)
}
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...')
})
autoUpdater.on('update-available', info => {
// eslint-disable-next-line prefer-template
sendStatusToWindow('Update available. ' + info)
})
autoUpdater.on('update-not-available', () => {
sendStatusToWindow('Update not available.')
})
autoUpdater.on('error', err => {
// eslint-disable-next-line prefer-template
sendStatusToWindow('Error in auto-updater. ' + err)
})
autoUpdater.on('download-progress', progressObj => {
// eslint-disable-next-line prefer-template
let logMessage = 'Download speed: ' + progressObj.bytesPerSecond
// eslint-disable-next-line prefer-template
logMessage = logMessage + ' - Downloaded ' + progressObj.percent + '%'
logMessage =
// eslint-disable-next-line prefer-template
logMessage + ' (' + progressObj.transferred + '/' + progressObj.total + ')'
sendStatusToWindow(logMessage)
})
autoUpdater.on('update-downloaded', info => {
// eslint-disable-next-line prefer-template
sendStatusToWindow('Update downloaded ' + info)
})