-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (48 loc) · 1.51 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
const { app, Tray, Menu, MenuItem, BrowserWindow } = require('electron')
const path = require('path')
const getAudioDevices = require('./lib/get-audio-devices')
const setAudioDevice = require('./lib/set-audio-device')
const iconPath = path.join(__dirname, 'media', 'icon.png')
let appIcon = null
let win = null
let autoLaunchControl = require('./lib/control-auto-launch')()
app.on('ready', () => {
win = new BrowserWindow({ show: false })
appIcon = new Tray(iconPath)
const contextMenu = Menu.buildFromTemplate([
{ type: 'separator' },
{ type: 'checkbox',
label: 'Start on boot',
checked: false,
click: autoLaunchControl.toggle
},
{ label: 'Quit', role: 'quit'}
])
autoLaunchControl.isEnabled()
.then((isEnabled) => {
contextMenu.items[contextMenu.length - 2].checked = true
})
getAudioDevices().then((devices) => {
devices.forEach((device) => { addMenuItem(contextMenu, device) })
})
appIcon.setToolTip('Racket 🎶')
appIcon.setContextMenu(contextMenu)
})
function addMenuItem (contextMenu, device) {
const menuItem = new MenuItem({
label: device.full,
type: 'checkbox',
checked: false,
click: callSetAudioDevice.bind(null, contextMenu, device)
})
contextMenu.insert(0, menuItem)
}
function callSetAudioDevice (contextMenu, device, menuItem) {
setAudioDevice(device)
.then(() => {
contextMenu.items.slice(0, -3).forEach((item, index) => {
contextMenu.items[index].checked = false
})
menuItem.checked = true
})
}