forked from SabakiHQ/Sabaki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
276 lines (220 loc) · 7.35 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
const {app, shell, dialog, ipcMain} = require('electron')
const {BrowserWindow, Menu} = require('electron')
const setting = require('./modules/setting')
const updater = require('./modules/updater')
let windows = []
let openfile = null
let isReady = false
function newWindow(path) {
let window = new BrowserWindow({
icon: process.platform == 'linux' ? `${__dirname}/logo.png` : null,
title: app.getName(),
useContentSize: true,
width: setting.get('window.width'),
height: setting.get('window.height'),
minWidth: setting.get('window.minwidth'),
minHeight: setting.get('window.minheight'),
backgroundColor: '#111111',
show: false
})
windows.push(window)
buildMenu()
window.webContents.setAudioMuted(!setting.get('sound.enable'))
window.webContents.on('did-finish-load', () => {
if (path) window.webContents.send('load-file', path)
}).on('new-window', evt => {
evt.preventDefault()
})
window.on('focus', () => {
window.webContents.send('window-focus')
}).on('closed', () => {
window = null
})
window.loadURL(`file://${__dirname}/browser/index.html`)
if (setting.get('debug.dev_tools')) {
window.toggleDevTools()
}
return window
}
function buildMenu(disableAll = false) {
let template = JSON.parse(JSON.stringify(require('./menu.json')))
// Create app menu for OS X
if (process.platform == 'darwin') {
let appMenu = [{role: 'about'}]
// Remove original 'Check for Updates' menu item
let helpMenu = template.find(x => x.label.replace('&', '') == 'Help')
let items = helpMenu.submenu.splice(0, 3)
appMenu.push(...items.slice(0, 2))
// Remove original 'Preferences' menu item
let fileMenu = template.find(x => x.label.replace('&', '') == 'File')
let preferenceItem = fileMenu.submenu.splice(fileMenu.submenu.length - 2, 2)[1]
appMenu.push(
{type: 'separator'},
preferenceItem,
{type: 'separator'},
{submenu: [], role: 'services'},
{
label: 'Text',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'selectall'}
],
},
{type: 'separator'},
{role: 'hide'},
{role: 'hideothers'},
{type: 'separator'},
{role: 'quit'}
)
template.unshift({
label: '{name}',
submenu: appMenu
})
// Add 'Window' menu
template.splice(template.length - 1, 0, {
submenu: [
{
label: 'New Window',
click: () => newWindow(),
enabled: true
},
{role: 'minimize'},
{type: 'separator'},
{role: 'front'}
],
role: 'window'
})
}
// Load engines
let engineMenu = template.find(x => x.label && x.label.replace('&', '') == 'Engine')
if (engineMenu) {
let attachMenu = engineMenu.submenu[0].submenu
attachMenu.length = 0
setting.load()
setting.getEngines().forEach(engine => {
attachMenu.push({
label: engine.name,
click: () => {
let window = BrowserWindow.getFocusedWindow()
if (!window) return
window.webContents.send('attach-engine', engine.path, engine.args)
}
})
})
if (setting.getEngines().length != 0) {
attachMenu.push({type: 'separator'})
}
attachMenu.push({
label: '&Manage Engines…',
action: 'manageengines'
})
}
// Process menu items
let processMenu = items => {
items.forEach(item => {
if ('label' in item) {
item.label = item.label
.replace('{name}', app.getName())
.replace('{version}', app.getVersion())
}
if ('action' in item) {
let action = item.action
item.click = () => {
let window = BrowserWindow.getFocusedWindow()
if (!window) return
window.webContents.send('menu-click', action)
}
delete item.action
}
if ('checked' in item) {
item.type = 'checkbox'
item.checked = !!setting.get(item.checked)
}
if (disableAll && !item.enabled && !('submenu' in item || 'role' in item)) {
item.enabled = false
}
if ('submenu' in item) {
processMenu(item.submenu)
}
})
}
processMenu(template)
// Build
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
// Create dock menu
if (process.platform == 'darwin') {
app.dock.setMenu(Menu.buildFromTemplate([{
label: 'New Window',
click: () => newWindow()
}]))
}
}
function checkForUpdates(showNoUpdatesDialog) {
let repo = `yishn/${app.getName()}`
updater.check(repo, (err, hasUpdates, url) => {
if (err) return
if (hasUpdates) {
dialog.showMessageBox({
type: 'info',
buttons: ['Download Update', 'Not Now'],
title: app.getName(),
message: 'There is a new version of ' + app.getName() + ' available.',
noLink: true,
cancelId: 1
}, response => response == 0 ? shell.openExternal(url) : null)
} else if (showNoUpdatesDialog) {
dialog.showMessageBox({
type: 'info',
buttons: ['OK'],
title: app.getName(),
message: 'There are no updates available.'
}, () => {})
}
})
}
ipcMain.on('new-window', (evt, ...args) => newWindow(...args))
ipcMain.on('build-menu', (evt, ...args) => buildMenu(...args))
ipcMain.on('check-for-updates', (evt, ...args) => checkForUpdates(...args))
app.on('window-all-closed', () => {
if (process.platform != 'darwin') {
app.quit()
} else {
buildMenu(true)
}
})
app.on('ready', () => {
isReady = true
if (!openfile && process.argv.length >= 2)
openfile = process.argv[1]
newWindow(openfile)
if (setting.get('app.startup_check_updates')) {
setTimeout(checkForUpdates, setting.get('app.startup_check_updates_delay'))
}
})
app.on('activate', (evt, hasVisibleWindows) => {
if (!hasVisibleWindows) newWindow()
})
app.on('open-file', (evt, path) => {
evt.preventDefault()
if (!isReady) {
openfile = path
} else {
newWindow(path)
}
})
process.on('uncaughtException', err => {
dialog.showErrorBox(`${app.getName()} v${app.getVersion()}`, [
'Something weird happened. ',
app.getName(),
' will shut itself down. ',
'If possible, please report this on ',
`${app.getName()}’s repository on GitHub.\n\n`,
err.stack
].join(''))
app.quit()
})