Toggle show/hide in tray icon
Fix state saving on app exit
This commit is contained in:
30
main/tray.js
30
main/tray.js
@@ -6,17 +6,33 @@ var path = require('path')
|
|||||||
var electron = require('electron')
|
var electron = require('electron')
|
||||||
var windows = require('./windows')
|
var windows = require('./windows')
|
||||||
|
|
||||||
|
var trayIcon
|
||||||
|
|
||||||
function init () {
|
function init () {
|
||||||
// No tray icon on OSX
|
// No tray icon on OSX
|
||||||
if (process.platform === 'darwin') return
|
if (process.platform === 'darwin') return
|
||||||
|
|
||||||
var trayIcon = new electron.Tray(path.join(__dirname, '..', 'static', 'WebTorrentSmall.png'))
|
trayIcon = new electron.Tray(path.join(__dirname, '..', 'static', 'WebTorrentSmall.png'))
|
||||||
|
|
||||||
// On Windows, left click to open the app, right click for context menu
|
// On Windows, left click to open the app, right click for context menu
|
||||||
// On Linux, any click (right or left) opens the context menu
|
// On Linux, any click (right or left) opens the context menu
|
||||||
trayIcon.on('click', showApp)
|
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 updateTrayMenu () {
|
||||||
|
var showHideMenuItem
|
||||||
|
if (windows.main.isVisible()) {
|
||||||
|
showHideMenuItem = { label: 'Hide to tray', click: hideApp }
|
||||||
|
} else {
|
||||||
|
showHideMenuItem = { label: 'Show', click: showApp }
|
||||||
|
}
|
||||||
var contextMenu = electron.Menu.buildFromTemplate([
|
var contextMenu = electron.Menu.buildFromTemplate([
|
||||||
{ label: 'Show', click: showApp },
|
showHideMenuItem,
|
||||||
{ label: 'Quit', click: quitApp }
|
{ label: 'Quit', click: quitApp }
|
||||||
])
|
])
|
||||||
trayIcon.setContextMenu(contextMenu)
|
trayIcon.setContextMenu(contextMenu)
|
||||||
@@ -26,6 +42,12 @@ function showApp () {
|
|||||||
windows.main.show()
|
windows.main.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
function quitApp () {
|
function hideApp () {
|
||||||
electron.app.quit()
|
windows.main.hide()
|
||||||
|
}
|
||||||
|
|
||||||
|
function quitApp () {
|
||||||
|
windows.main.send('dispatch', 'saveState') /* try to save state on exit */
|
||||||
|
electron.ipcMain.once('savedState', () => electron.app.quit())
|
||||||
|
setTimeout(() => electron.app.quit(), 2000) /* exit after at most 2 secs */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,9 +77,6 @@ function init () {
|
|||||||
})
|
})
|
||||||
document.body.appendChild(vdomLoop.target)
|
document.body.appendChild(vdomLoop.target)
|
||||||
|
|
||||||
// Save state on exit
|
|
||||||
window.addEventListener('beforeunload', saveState)
|
|
||||||
|
|
||||||
// OS integrations:
|
// OS integrations:
|
||||||
// ...drag and drop a torrent or video file to play or seed
|
// ...drag and drop a torrent or video file to play or seed
|
||||||
dragDrop('body', (files) => dispatch('onOpen', files))
|
dragDrop('body', (files) => dispatch('onOpen', files))
|
||||||
@@ -316,6 +313,9 @@ function dispatch (action, ...args) {
|
|||||||
state.saved.skippedVersions.push(args[0] /* version */)
|
state.saved.skippedVersions.push(args[0] /* version */)
|
||||||
saveState()
|
saveState()
|
||||||
}
|
}
|
||||||
|
if (action === 'saveState') {
|
||||||
|
saveState()
|
||||||
|
}
|
||||||
|
|
||||||
// Update the virtual-dom, unless it's just a mouse move event
|
// Update the virtual-dom, unless it's just a mouse move event
|
||||||
if (action !== 'mediaMouseMoved') {
|
if (action !== 'mediaMouseMoved') {
|
||||||
@@ -431,6 +431,7 @@ function saveState () {
|
|||||||
|
|
||||||
cfg.write(copy, function (err) {
|
cfg.write(copy, function (err) {
|
||||||
if (err) console.error(err)
|
if (err) console.error(err)
|
||||||
|
ipcRenderer.send('savedState')
|
||||||
update()
|
update()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user