-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
164 lines (133 loc) · 4.09 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
const { app, shell, BrowserWindow, Menu } = require('electron')
const Store = require('electron-store')
const { autoUpdater } = require('electron-updater')
let currentAppUrl = new URL('https://esc-configurator.com/')
const store = new Store()
let clearStorage = () => {}
function createWindow () {
const appUrl = currentAppUrl
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 900,
minHeight: 600
})
clearStorage = () => mainWindow.webContents.session.clearStorageData()
mainWindow.loadURL(appUrl.href)
// Prevent navigation in main window
mainWindow.webContents.on('will-navigate', e => {
e.preventDefault()
})
// Open links in user browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (/https?:\/\//i.test(url)) shell.openExternal(url)
return { action: 'deny' }
})
const grantedDevices = store.get('granted-devices', [])
const containsDevice = device =>
grantedDevices.some(granted =>
Object.keys(granted).every(key => granted[key] === device[key]))
// Allow serial permission
mainWindow.webContents.session.setPermissionCheckHandler((_, permission) => permission === 'serial')
// Grant permissions for previously allowed serial devices
mainWindow.webContents.session.setDevicePermissionHandler(details => {
if (new URL(details.origin).hostname === appUrl.hostname && details.deviceType === 'serial') {
return containsDevice(details.device)
}
return false
})
// Handle serial port request
mainWindow.webContents.session.on('select-serial-port', (event, ports, _, callback) => {
event.preventDefault()
if (ports.length === 0) return
const portWindow = new BrowserWindow({
parent: mainWindow,
modal: true,
show: false,
resizable: false,
width: 600,
height: 300
})
const portNames = ports.map(p => p.displayName || p.portName)
portWindow.loadFile('port-modal/modal.html', { query: { data: JSON.stringify(portNames) } })
portWindow.once('ready-to-show', () => portWindow.show())
// TODO: https://github.com/electron/electron/issues/28215
// child.once('close', () => {
portWindow.webContents.once('close', () => {
const url = new URL(portWindow.webContents.getURL())
const index = parseInt(url.hash.slice(1), 10)
const port = ports[index]
if (port) {
const device = {
vendor_id: parseInt(port.vendorId, 10),
product_id: parseInt(port.productId, 10),
serial_number: port.serialNumber
}
if (!containsDevice(device)) {
grantedDevices.push(device)
store.set('granted-devices', grantedDevices)
}
}
callback(port ? port.portId : '')
})
})
mainWindow.once('close', () => {
mainWindow.webContents.session.removeAllListeners()
mainWindow.webContents.removeAllListeners()
})
}
app.enableSandbox()
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
autoUpdater.checkForUpdatesAndNotify()
autoUpdater.logger = require('electron-log')
autoUpdater.logger.transports.file.level = 'info'
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
const menu = Menu.buildFromTemplate([
{ role: 'appMenu' },
{
role: 'fileMenu',
submenu: [
{
label: 'Clear App Data',
click: () => {
store.clear()
clearStorage()
}
},
{
label: 'Switch To Development Version',
id: 'dev-version',
click: () => {
BrowserWindow.getAllWindows().forEach(w => w.close())
currentAppUrl = new URL('https://develop.esc-configurator.com/')
createWindow()
Menu.getApplicationMenu().getMenuItemById('dev-version').enabled = false
}
},
{ type: 'separator' },
{ role: process.platform === 'darwin' ? 'close' : 'quit' }
]
},
{ role: 'editMenu' },
{ role: 'viewMenu' },
{ role: 'windowMenu' },
{
role: 'help',
submenu: [{
label: 'Learn More',
click: () => shell.openExternal('https://github.com/stylesuxx/esc-configurator')
},
{
label: 'App Repository',
click: () => shell.openExternal('https://github.com/mathiasvr/esc-configurator-app')
}]
}
])
Menu.setApplicationMenu(menu)