-
Notifications
You must be signed in to change notification settings - Fork 0
/
tray.js
85 lines (69 loc) · 2.37 KB
/
tray.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
module.exports = {
init,
hasTray
}
const cp = require('child_process')
const path = require('path')
const electron = require('electron')
const app = electron.app
const Menu = electron.Menu
const Tray = electron.Tray
const app = require('./app')
const trayIcon
function init() {
// OS X has no tray icon
if (process.platform === 'darwin') return
// // On Linux, asynchronously check for libappindicator1
// if (process.platform === 'linux') {
// checkLinuxTraySupport(function (supportsTray) {
// if (supportsTray) createTrayIcon()
// })
// }
// // Windows always supports minimize-to-tray
// if (process.platform === 'win32') createTrayIcon()
createTrayIcon()
}
function hasTray() {
return !!trayIcon
}
function createTrayIcon() {
trayIcon = new Tray(path.join(__dirname, '..', 'icons', '16x16.png'))
// On Windows, left click to open the app, right click for context menu
// On Linux, any click (right or left) opens the context menu
trayIcon.on('click', showApp)
// Show the tray context menu, and keep the available commands up to date
updateTrayMenu()
windows.main.on('show', updateTrayMenu)
windows.main.on('hide', updateTrayMenu)
}
// function checkLinuxTraySupport(cb) {
// // Check that we're on Ubuntu (or another debian system) and that we have
// // libappindicator1. If WebTorrent was installed from the deb file, we should
// // always have it. If it was installed from the zip file, we might not.
// cp.exec('dpkg --get-selections libappindicator1', function (err, stdout) {
// if (err) return cb(false)
// // Unfortunately there's no cleaner way, as far as I can tell, to check
// // whether a debian package is installed:
// cb(stdout.endsWith('\tinstall\n'))
// })
// }
function updateTrayMenu() {
const showHideMenuItem
if (windows.main.isVisible()) {
showHideMenuItem = { label: 'Hide to tray', click: hideApp }
} else {
showHideMenuItem = { label: 'Show', click: showApp }
}
const contextMenu = Menu.buildFromTemplate([
showHideMenuItem,
{ label: 'Quit', click: () => electron.app.quit() }
])
trayIcon.setContextMenu(contextMenu)
}
function showApp() {
windows.main.show()
}
function hideApp() {
windows.main.hide()
windows.main.send('dispatch', 'backToList')
}