diff --git a/electron/tray.js b/electron/tray.js index 75a524ae..5ff849e2 100644 --- a/electron/tray.js +++ b/electron/tray.js @@ -5,40 +5,31 @@ const app = electron.app; const Tray = electron.Tray; const MenuItem = electron.MenuItem; -let tray = null; +var appIcon = null; exports.create = win => { - if (process.platform === 'darwin' || tray) { + if (process.platform === 'darwin' || appIcon) { return; } const icon = process.platform === 'linux' || process.platform === 'darwin' ? 'IconTray.png' : 'Icon.ico'; const iconPath = path.join(__dirname, `../resources/${icon}`); - let showMB = new MenuItem({ - label: 'Show Rambox' - ,position: '1' - ,visible: false - ,click(btn) { - win.show(); - contextMenu.items[0].visible = false; - contextMenu.items[1].visible = true; - } - }); - - let hideMB = new MenuItem({ - label: 'Minimize Rambox' - ,position: '2' - ,click(btn) { + const toggleWin = () => { + if (win.isVisible()) { win.hide(); - contextMenu.items[1].visible = false; - contextMenu.items[0].visible = true; + } else { + win.show(); } - }); + }; const contextMenu = electron.Menu.buildFromTemplate([ - showMB, - hideMB, + { + label: 'Show/Hide Window' + ,click() { + toggleWin(); + } + }, { type: 'separator' }, @@ -50,29 +41,16 @@ exports.create = win => { } ]); - tray = new Tray(iconPath); - tray.setToolTip('Rambox'); - tray.setContextMenu(contextMenu); - tray.on('click', function() { - if ( win.isVisible() ) { - win.hide(); - contextMenu.items[1].visible = false; - contextMenu.items[0].visible = true; - } else { - win.show(); - contextMenu.items[0].visible = false; - contextMenu.items[1].visible = true; - } - }); - - win.on('hide', function() { - contextMenu.items[1].visible = false; - contextMenu.items[0].visible = true; + appIcon = new Tray(iconPath); + appIcon.setToolTip('Rambox'); + appIcon.setContextMenu(contextMenu); + appIcon.on('double-click', () => { + win.show(); }); }; exports.setBadge = shouldDisplayUnread => { - if (process.platform === 'darwin' || !tray) { + if (process.platform === 'darwin' || !appIcon) { return; } @@ -84,5 +62,5 @@ exports.setBadge = shouldDisplayUnread => { } const iconPath = path.join(__dirname, `../resources/${icon}`); - tray.setImage(iconPath); + appIcon.setImage(iconPath); };