From 0b9ac2f71d284e7fcaaaed3be3fb9b700f25966f Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Wed, 11 Jan 2017 15:22:51 -0200 Subject: [PATCH] Fix try context menu --- src/scripts/tray.js | 61 ++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/src/scripts/tray.js b/src/scripts/tray.js index eb1bbd6e3e43..c60ae1e47834 100644 --- a/src/scripts/tray.js +++ b/src/scripts/tray.js @@ -3,12 +3,11 @@ import { remote } from 'electron'; import path from 'path'; -var Tray = remote.Tray; -var Menu = remote.Menu; +const { Tray, Menu } = remote; -let mainWindow = remote.getCurrentWindow(); +const mainWindow = remote.getCurrentWindow(); -var icons = { +const icons = { win32: { dir: 'windows' }, @@ -23,39 +22,55 @@ var icons = { } }; -let _iconTray = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon || 'icon-tray.png'); -let _iconTrayAlert = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert || 'icon-tray-alert.png'); +const _iconTray = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].icon || 'icon-tray.png'); +const _iconTrayAlert = path.join(__dirname, 'images', icons[process.platform].dir, icons[process.platform].iconAlert || 'icon-tray-alert.png'); function createAppTray () { - let _tray = new Tray(_iconTray); - var contextMenu = Menu.buildFromTemplate([{ + const _tray = new Tray(_iconTray); + const contextMenuShow = Menu.buildFromTemplate([{ + label: 'Show', + click () { + mainWindow.show(); + } + }, { label: 'Quit', - click: function () { + click () { remote.app.quit(); } + }]); + const contextMenuHide = Menu.buildFromTemplate([{ + label: 'Hide', + click () { + mainWindow.hide(); + } + }, { + label: 'Quit', + click () { + remote.app.quit(); + } }]); - _tray.setToolTip(remote.app.getName()); + if (!mainWindow.isMinimized() && !mainWindow.isVisible()) { + _tray.setContextMenu(contextMenuShow); + } else { + _tray.setContextMenu(contextMenuHide); + } - _tray.on('right-click', function (e, b) { - _tray.popUpContextMenu(contextMenu, b); + mainWindow.on('show', () => { + _tray.setContextMenu(contextMenuHide); }); - _tray.on('click', function (e, b) { - if (e.ctrlKey === true) { - _tray.popUpContextMenu(contextMenu, b); - return; - } + mainWindow.on('hide', () => { + _tray.setContextMenu(contextMenuShow); + }); - if (mainWindow.isVisible()) { - mainWindow.hide(); - } else { - mainWindow.show(); - } + _tray.setToolTip(remote.app.getName()); + + _tray.on('right-click', function (e, b) { + _tray.popUpContextMenu(undefined, b); }); - mainWindow = mainWindow; mainWindow.tray = _tray; }