From ad0fcaed465165530b88930d039f71b85011353d Mon Sep 17 00:00:00 2001 From: DC Date: Wed, 13 Apr 2016 00:23:18 -0700 Subject: [PATCH] Fix two tray icon bugs (#395) * Stop media on Tray Icon > Hide * Linux tray support: check for libappindicator1 Fixes #383 --- main/tray.js | 32 +++++++++++++++++++++++++++++++- main/windows.js | 10 ++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/main/tray.js b/main/tray.js index 4178a316..92454d91 100644 --- a/main/tray.js +++ b/main/tray.js @@ -1,7 +1,9 @@ module.exports = { - init + init, + hasTray } +var cp = require('child_process') var path = require('path') var electron = require('electron') @@ -17,6 +19,22 @@ 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() +} + +function hasTray () { + return !!trayIcon +} + +function createTrayIcon () { trayIcon = new Tray(path.join(__dirname, '..', 'static', 'WebTorrentSmall.png')) // On Windows, left click to open the app, right click for context menu @@ -29,6 +47,18 @@ function init () { 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 () { var showHideMenuItem if (windows.main.isVisible()) { diff --git a/main/windows.js b/main/windows.js index 6caa8551..9326c5e4 100644 --- a/main/windows.js +++ b/main/windows.js @@ -11,6 +11,7 @@ var electron = require('electron') var config = require('../config') var menu = require('./menu') +var tray = require('./tray') function createAboutWindow () { if (windows.about) { @@ -108,13 +109,18 @@ function createMainWindow () { win.on('leave-full-screen', () => menu.onToggleFullScreen(false)) win.on('close', function (e) { - if (!electron.app.isQuitting) { + if (process.platform !== 'darwin' && !tray.hasTray()) { + electron.app.quit() + } else if (!electron.app.isQuitting) { e.preventDefault() - win.send('dispatch', 'backToList') win.hide() } }) + win.on('hide', function () { + win.send('dispatch', 'backToList') + }) + win.once('closed', function () { windows.main = null })