From ef503a1e29a540c7318efb5f2018efbf00706198 Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Thu, 19 Oct 2017 11:15:25 +0200 Subject: Add option to disable system tray icon --- src/lib/Tray.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/lib/Tray.js (limited to 'src/lib/Tray.js') diff --git a/src/lib/Tray.js b/src/lib/Tray.js new file mode 100644 index 000000000..631342b24 --- /dev/null +++ b/src/lib/Tray.js @@ -0,0 +1,64 @@ +import { app, Tray, Menu } from 'electron'; +import path from 'path'; + +const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png'; +const INDICATOR_TRAY_PLAIN = 'tray'; +const INDICATOR_TRAY_UNREAD = 'tray-unread'; + +function getAsset(type, asset) { + return path.join( + __dirname, '..', 'assets', 'images', type, process.platform, `${asset}.${FILE_EXTENSION}`, + ); +} + +export default class TrayIcon { + mainWindow = null; + trayIcon = null; + + constructor(mainWindow) { + this.mainWindow = mainWindow; + } + + show() { + this.trayIcon = new Tray(getAsset('tray', INDICATOR_TRAY_PLAIN)); + const trayMenuTemplate = [ + { + label: 'Show Franz', + click() { + this.mainWindow.show(); + }, + }, { + label: 'Quit Franz', + click() { + app.quit(); + }, + }, + ]; + + const trayMenu = Menu.buildFromTemplate(trayMenuTemplate); + this.trayIcon.setContextMenu(trayMenu); + + this.trayIcon.on('click', () => { + this.mainWindow.show(); + }); + } + + hide() { + if (this.trayIcon) { + this.trayIcon.destroy(); + this.trayIcon = null; + } + } + + setIndicator(indicator) { + if (!this.trayIcon) return; + + this.trayIcon.setImage(getAsset('tray', indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN)); + + if (process.platform === 'darwin') { + this.trayIcon.setPressedImage( + getAsset('tray', `${indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`), + ); + } + } +} -- cgit v1.2.3-54-g00ecf