-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (60 loc) · 1.64 KB
/
app.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
let { app, nativeTheme, Menu, Tray, shell } = require('electron')
let path = require('path')
let LINK_INTERPOLATION = /(\${label(.*)})/
function makeMenu(menu) {
return menu
.map(({ label, submenu, link, type }) => {
if (submenu) {
return {
label,
submenu: makeMenu(submenu),
}
} else if (link) {
return {
label,
click: () => {
let realLink = link
if (LINK_INTERPOLATION.test(realLink)) {
let [, marker, space] = realLink.match(LINK_INTERPOLATION)
let linkLabel = label.toLowerCase()
if (space) {
linkLabel = linkLabel.replace(/\s/g, space)
}
realLink = realLink.replace(marker, linkLabel)
}
shell.openExternal(realLink)
},
}
} else if (type) {
return { type }
} else if (label) {
return { label }
} else {
return false
}
})
.filter(Boolean)
}
// hiding the dock doesn't seem to work when the app is opened through the
// Applications icon for some strange reason
// app.dock.hide()
app.on('ready', async () => {
let tray = new Tray(
path.join(
__dirname,
nativeTheme.shouldUseDarkColors ? 'icon-white.png' : 'icon.png'
)
)
let menu = makeMenu(require('./menu.json'))
let contextMenu = Menu.buildFromTemplate([
...menu,
{ type: 'separator' },
{ role: 'quit', label: 'Quit' },
])
tray.setToolTip('A menu app')
tray.setContextMenu(contextMenu)
})
// Quit the app when the window is closed
app.on('window-all-closed', () => {
app.quit()
})