-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
435 lines (387 loc) · 11.6 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
'use strict'
// "C:\Users\cullens\AppData\Local\Google\Chrome\User Data\Default\Extensions\fmkadmapgofadopljbjfkapdkoienihi"
// /home/common/.config/chromium/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/3.6.0_0
// Import parts of electron to use
const { app, BrowserWindow, Menu, ipcMain } = require('electron')
const path = require('path')
const url = require('url')
// For communication between windows or between window and electron process
// const { ipcMain } = require('electron')
const log = require('electron-log')
const { autoUpdater } = require('electron-updater')
const os = require('os')
const fs = require('fs')
// 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 updaterWindow = null
// Keep a reference for dev mode
let dev = false
if (
process.defaultApp ||
/[\\/]electron-prebuilt[\\/]/.test(process.execPath) ||
/[\\/]electron[\\/]/.test(process.execPath)
) {
dev = true
}
// Temporary fix broken high-dpi scale factor on Windows (125% scaling)
// info: https://github.com/electron/electron/issues/9691
if (process.platform === 'win32') {
app.commandLine.appendSwitch('high-dpi-support', 'true')
app.commandLine.appendSwitch('force-device-scale-factor', '1')
}
let resources
if (dev) {
resources = path.join(__dirname, 'assets', 'icons')
console.log(__dirname)
} else {
resources = path.join(process.resourcesPath, '..', 'assets', 'icons')
console.log(resources)
}
ipcMain.on('updaterMessage', (event, arg) => {
console.log(arg) // prints "ping"
if (arg.type === 'updateControl') {
if (arg.payload === 'download') {
autoUpdater.downloadUpdate()
} else if (arg.payload === 'installAndRestart') {
autoUpdater.quitAndInstall()
}
}
// event.reply('asynchronous-reply', 'pong')
})
function createUpdaterWindow() {
if (updaterWindow === null) {
if (dev) {
updaterWindow = new BrowserWindow({
icon: path.join(resources, '96x96.png'),
width: 800,
height: 600,
resizable: true,
show: false,
title: 'Updater',
})
} else {
updaterWindow = new BrowserWindow({
icon: path.join(resources, '96x96.png'),
width: 400,
height: 200,
resizable: false,
show: false,
title: 'Updater',
})
}
// updaterWindow.toggleDevTools()
}
// win.setMenuBarVisibility(false)
// win.webContents.openDevTools();
updaterWindow.on('closed', () => {
updaterWindow = null
})
// .AppUpdater ⇐ EventEmitter
// .checkForUpdates() ⇒ Promise<UpdateCheckResult>
// .checkForUpdatesAndNotify() ⇒ Promise< | UpdateCheckResult>
// .downloadUpdate(cancellationToken) ⇒ Promise<any>
// .getFeedURL() ⇒ undefined | null | String
// .setFeedURL(options)
// .quitAndInstall(isSilent, isForceRunAfter)
updaterWindow.webContents.on('did-finish-load', function() {
// setTimeout(() => updaterWindow.show(), 650)
if (dev) {
console.log('wats happening')
updaterWindow.webContents.openDevTools()
}
updaterWindow.show()
autoUpdater.autoDownload = false
autoUpdater.checkForUpdates()
const currentVersion = app.getVersion()
sendVersionToWindow(currentVersion)
})
updaterWindow.on('page-title-updated', evt => {
evt.preventDefault()
})
}
function createWindow() {
// and load the index.html of the app.
let indexPath
let updaterPath
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
hash: '/',
host: 'localhost:8080',
pathname: '/',
slashes: true,
})
updaterPath = url.format({
protocol: 'http:',
hash: '/updater',
host: 'localhost:8080',
pathname: 'index.html',
slashes: true,
})
} else {
indexPath = url.format({
protocol: 'file:',
hash: '/',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true,
})
updaterPath = url.format({
protocol: 'file:',
hash: '/updater',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true,
})
}
var menu = Menu.buildFromTemplate([
{
label: 'File',
submenu: [
{
label: 'Settings',
click: (menuItem, currentWindow) => {
currentWindow.webContents.send('menu-item', {
menuItem,
currentWindow,
})
},
},
{
label: 'Check For Updates',
click: () => {
createUpdaterWindow()
updaterWindow.loadURL(updaterPath)
},
},
{
label: 'Exit',
click: () => app.quit(),
},
],
},
{
label: 'Dev',
submenu: [
{
label: 'Clear Local Storage',
click: (menuItem, currentWindow) => {
console.log(menuItem)
currentWindow.webContents.send('menu-item', {
menuItem,
currentWindow,
})
},
},
{
label: 'Clear Cache',
click: (menuItem, currentWindow) => {
currentWindow.webContents.session.clearCache(() => {
console.log('cache cleared')
})
},
},
{
label: 'Reload',
click: (menuItem, currentWindow) => {
console.log(menuItem)
currentWindow.webContents.reload()
},
},
{
label: 'Hard Reload',
click: (menuItem, currentWindow) => {
console.log(menuItem)
currentWindow.webContents.reloadIgnoringCache()
},
},
{
label: 'Toggle Dev Tools',
click: (menuItem, currentWindow) => {
console.log(menuItem)
currentWindow.toggleDevTools()
},
},
{
label: 'Reset Redux to Defaults',
submenu: [
{
label: 'Session Settings',
click: (menuItem, currentWindow) => {
currentWindow.webContents.send('menu-item', {
menuItem,
currentWindow,
})
},
id: 'session-settings',
},
],
},
],
},
])
Menu.setApplicationMenu(null)
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1250,
height: 768,
minWidth: 1250,
minHeight: 768,
show: false,
icon: path.join(resources, '96x96.png'),
title: 'Tile Viewer',
})
mainWindow.setMenu(menu)
if (dev) {
console.log(os.homedir())
let reactDevToolsPath
let reduxDevToolsPath
if (process.platform === 'win32') {
reactDevToolsPath = path.join(
os.homedir(),
'AppData/Local/Google/Chrome/User Data/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/4.7.0_0',
)
reduxDevToolsPath = path.join(
os.homedir(),
'AppData/Local/Google/Chrome/User Data/Default/Extensions/lmhkpmbekcpmknklioeibfkpmmfibljd/2.17.0_0',
)
} else if (process.platform === 'linux') {
// Ubuntu dev machine
reactDevToolsPath = path.join(
os.homedir(),
'.config/chromium/Default/Extensions/fmkadmapgofadopljbjfkapdkoienihi/4.7.0_0',
)
reduxDevToolsPath = path.join(
os.homedir(),
'.config/chromium/Default/Extensions/lmhkpmbekcpmknklioeibfkpmmfibljd/2.17.0_0',
)
}
console.log(reactDevToolsPath)
console.log(reduxDevToolsPath)
if (fs.existsSync(reactDevToolsPath)) {
BrowserWindow.addDevToolsExtension(reactDevToolsPath)
}
if (fs.existsSync(reduxDevToolsPath)) {
BrowserWindow.addDevToolsExtension(reduxDevToolsPath)
}
}
console.log(updaterPath)
mainWindow.loadURL(indexPath)
// Emitted when the window is closed.
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.
mainWindow = null
})
// Don't show until we are ready and loaded
mainWindow.once('ready-to-show', () => {
// Open the DevTools automatically if developing
if (dev) {
console.log('wats happening')
mainWindow.webContents.openDevTools()
}
mainWindow.show()
createUpdaterWindow()
updaterWindow.loadURL(updaterPath)
})
}
// 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)
// 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 (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// 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()
}
})
function sendStatusToWindow(text) {
log.info(text)
updaterWindow.webContents.send('updaterMessage', {
type: 'statusMessage',
payload: text,
})
}
function sendVersionToWindow(version) {
log.info(version)
updaterWindow.webContents.send('updaterMessage', {
type: 'versionMessage',
payload: version,
})
}
function askUserToUpdate() {
log.info('Update available, asking user to update')
win.webContents.send('message', 'showUpdatePrompt###')
}
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...')
})
autoUpdater.on('update-available', info => {
sendStatusToWindow('Update available.')
console.log(info)
const payloadString = JSON.stringify({
version: info.version,
os: process.platform,
})
updaterWindow.webContents.send('updaterMessage', {
type: 'availableVersionMessage',
payload: payloadString,
})
})
autoUpdater.on('update-not-available', info => {
sendStatusToWindow('Update not available.')
const payloadString = JSON.stringify({
version: info.version,
os: process.platform,
})
updaterWindow.webContents.send('updaterMessage', {
type: 'availableVersionMessage',
payload: payloadString,
})
})
autoUpdater.on('error', err => {
sendStatusToWindow('Error in auto-updater. ' + err)
})
autoUpdater.on('download-progress', progressObj => {
let log_message = 'Download speed: ' + progressObj.bytesPerSecond
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%'
log_message = log_message + ' (' + progressObj.transferred + '/' + progressObj.total + ')'
sendStatusToWindow('Downloading...')
updaterWindow.webContents.send('updaterMessage', {
type: 'downloadProgress',
payload: JSON.stringify(progressObj),
})
})
autoUpdater.on('update-downloaded', info => {
sendStatusToWindow('Update downloaded.')
})
// app.on('ready', function() {
// // Create the Menu
// const menu = Menu.buildFromTemplate(template);
// Menu.setApplicationMenu(menu);
// createDefaultWindow();
// });
// app.on('window-all-closed', () => {
// app.quit();
// });
//
// CHOOSE one of the following options for Auto updates
//
// -------------------------------------------------------------------
// Auto updates - Option 1 - Simplest version
//
// This will immediately download an update, then install when the
// app quits.
// -------------------------------------------------------------------
// app.on('ready', function() {
// autoUpdater.checkForUpdatesAndNotify();
// });