Windows/Linux: Fix broken Quit option in Tray icon
This also cleans up the code by handling quit in the same way for all platforms, removing the special case in tray.js for darwin. We already have a 'before-quit' handler in main/index.js, so this is now handled there :)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
var electron = require('electron')
|
var electron = require('electron')
|
||||||
|
|
||||||
var app = electron.app
|
var app = electron.app
|
||||||
|
var ipcMain = electron.ipcMain
|
||||||
|
|
||||||
var autoUpdater = require('./auto-updater')
|
var autoUpdater = require('./auto-updater')
|
||||||
var config = require('../config')
|
var config = require('../config')
|
||||||
@@ -62,8 +63,14 @@ function init () {
|
|||||||
processArgv(argv)
|
processArgv(argv)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('before-quit', function () {
|
app.on('before-quit', function (e) {
|
||||||
|
if (app.isQuitting) return
|
||||||
|
|
||||||
app.isQuitting = true
|
app.isQuitting = true
|
||||||
|
e.preventDefault()
|
||||||
|
windows.main.send('dispatch', 'saveState') /* try to save state on exit */
|
||||||
|
ipcMain.once('savedState', () => app.quit())
|
||||||
|
setTimeout(() => app.quit(), 2000) /* quit after 2 secs, at most */
|
||||||
})
|
})
|
||||||
|
|
||||||
app.on('activate', function () {
|
app.on('activate', function () {
|
||||||
|
|||||||
27
main/tray.js
27
main/tray.js
@@ -4,20 +4,20 @@ module.exports = {
|
|||||||
|
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
var electron = require('electron')
|
var electron = require('electron')
|
||||||
|
|
||||||
|
var app = electron.app
|
||||||
|
var Menu = electron.Menu
|
||||||
|
var Tray = electron.Tray
|
||||||
|
|
||||||
var windows = require('./windows')
|
var windows = require('./windows')
|
||||||
|
|
||||||
var trayIcon
|
var trayIcon
|
||||||
|
|
||||||
function init () {
|
function init () {
|
||||||
if (process.platform === 'darwin') {
|
// OS X has no tray icon
|
||||||
// Instead of relying on the tray icon quit button, listen for Cmd+Q
|
if (process.platform === 'darwin') return
|
||||||
electron.app.once('before-quit', quitApp)
|
|
||||||
|
|
||||||
// OS X has no tray icon
|
trayIcon = new Tray(path.join(__dirname, '..', 'static', 'WebTorrentSmall.png'))
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
@@ -36,9 +36,9 @@ function updateTrayMenu () {
|
|||||||
} else {
|
} else {
|
||||||
showHideMenuItem = { label: 'Show', click: showApp }
|
showHideMenuItem = { label: 'Show', click: showApp }
|
||||||
}
|
}
|
||||||
var contextMenu = electron.Menu.buildFromTemplate([
|
var contextMenu = Menu.buildFromTemplate([
|
||||||
showHideMenuItem,
|
showHideMenuItem,
|
||||||
{ label: 'Quit', click: quitApp }
|
{ label: 'Quit', click: () => app.quit() }
|
||||||
])
|
])
|
||||||
trayIcon.setContextMenu(contextMenu)
|
trayIcon.setContextMenu(contextMenu)
|
||||||
}
|
}
|
||||||
@@ -50,10 +50,3 @@ function showApp () {
|
|||||||
function hideApp () {
|
function hideApp () {
|
||||||
windows.main.hide()
|
windows.main.hide()
|
||||||
}
|
}
|
||||||
|
|
||||||
function quitApp (e) {
|
|
||||||
e.preventDefault()
|
|
||||||
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 */
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user