-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
213 lines (184 loc) · 4.64 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
import { app, BrowserWindow, Menu } from 'electron'
import pkg from '../../package.json'
require('@electron/remote/main').initialize()
// set app name
app.name = pkg.productName
// to hide deprecation message
app.allowRendererProcessReuse = true
// disable electron warning
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = false
const gotTheLock = app.requestSingleInstanceLock()
const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug')
let mainWindow
// only allow single instance of application
if (!isDev) {
if (gotTheLock) {
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow && mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus()
})
} else {
app.quit()
process.exit(0)
}
} else {
// process.env.ELECTRON_ENABLE_LOGGING = true
require('electron-debug')({
showDevTools: false,
})
}
async function installDevTools() {
let installExtension = require('electron-devtools-installer')
installExtension.default(installExtension.VUEJS_DEVTOOLS).catch((err) => {
console.log('Unable to install `vue-devtools`: \n', err)
})
}
function createWindow() {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
backgroundColor: '#fff',
width: 960,
height: 540,
minWidth: 960,
minHeight: 540,
// useContentSize: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: false,
contextIsolation: false,
webSecurity: false,
},
show: false,
})
// eslint-disable-next-line
setMenu()
// load root file/url
if (isDev) {
mainWindow.loadURL('http://localhost:9080')
} else {
mainWindow.loadFile(`${__dirname}/index.html`)
global.__static = require('path')
.join(__dirname, '/static')
.replace(/\\/g, '\\\\')
}
// Show when loaded
mainWindow.on('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
mainWindow.on('closed', () => {
console.log('\nApplication exiting...')
})
}
app.on('ready', () => {
createWindow()
if (isDev) {
installDevTools()
mainWindow.webContents.openDevTools()
}
if (isDebug) {
mainWindow.webContents.openDevTools()
}
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
/**
* Auto Updater
*
* Uncomment the following code below and install `electron-updater` to
* support auto updating. Code Signing with a valid certificate is required.
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/
/*
import { autoUpdater } from 'electron-updater'
autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
})
app.on('ready', () => {
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
*/
const sendMenuEvent = async (data) => {
mainWindow.webContents.send('change-view', data)
}
const template = [
{
label: app.name,
submenu: [
{
label: 'Home',
accelerator: 'CommandOrControl+H',
click() {
sendMenuEvent({ route: '/' })
},
},
{ type: 'separator' },
{ role: 'minimize' },
{ role: 'togglefullscreen' },
{ type: 'separator' },
{ role: 'quit', accelerator: 'Alt+F4' },
],
},
{
role: 'help',
submenu: [
{
label: 'Get Help',
role: 'help',
accelerator: 'F1',
click() {
sendMenuEvent({ route: '/help' })
},
},
{
label: 'About',
role: 'about',
accelerator: 'CommandOrControl+A',
click() {
sendMenuEvent({ route: '/about' })
},
},
],
},
]
function setMenu() {
if (process.platform === 'darwin') {
template.unshift({
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' },
],
})
template.push({
role: 'window',
})
template.push({
role: 'help',
})
template.push({ role: 'services' })
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}