From 70ac6b216b14f0f495d244a40481012b342da5dd Mon Sep 17 00:00:00 2001 From: Aleksey Belchenkov Date: Tue, 30 Jun 2020 13:18:29 +0300 Subject: [PATCH] AppTray Class --- AppTray.js | 38 ++++++++++++++++++++++++++++++++++++++ main.js | 28 +++------------------------- 2 files changed, 41 insertions(+), 25 deletions(-) create mode 100644 AppTray.js diff --git a/AppTray.js b/AppTray.js new file mode 100644 index 0000000..3ee4938 --- /dev/null +++ b/AppTray.js @@ -0,0 +1,38 @@ +const { app, Menu, Tray } = require('electron'); + +class AppTray extends Tray { + constructor(icon, mainWindow) { + super(icon); + + this.setToolTip('SysTop'); + + this.mainWindow = mainWindow; + + this.on('click', this.onClick.bind(this)); + this.on('right-click', this.onRightClick.bind(this)); + } + + onClick() { + if (this.mainWindow.isVisible() === true) { + this.mainWindow.hide(); + } else { + this.mainWindow.show(); + } + } + + onRightClick() { + const contextMenu = Menu.buildFromTemplate([ + { + label: 'Quit', + click: () => { + app.isQuitting = true + app.quit() + } + } + ]); + + this.popUpContextMenu(contextMenu); + } +} + +module.exports = AppTray; \ No newline at end of file diff --git a/main.js b/main.js index 554fa0f..827adb6 100644 --- a/main.js +++ b/main.js @@ -1,9 +1,9 @@ -const { app, BrowserWindow, Menu, ipcMain, Tray } = require('electron'); -const log = require('electron-log'); +const { app, BrowserWindow, Menu, ipcMain } = require('electron'); const path = require('path'); const Store = require('./Store'); const MainWindow = require('./MainWindow'); +const AppTray = require('./AppTray'); // Set env process.env.NODE_ENV = 'development' @@ -50,29 +50,7 @@ app.on('ready', () => { const icon = path.join(__dirname, 'assets', 'icons', 'tray_icon.png'); - tray = new Tray(icon); - - tray.on('click', () => { - if (mainWindow.isVisible() === true) { - mainWindow.hide(); - } else { - mainWindow.show(); - } - }); - - tray.on('right-click', () => { - const contextMenu = Menu.buildFromTemplate([ - { - label: 'Quit', - click: () => { - app.isQuitting = true - app.quit() - } - } - ]); - - tray.popUpContextMenu(contextMenu); - }); + tray = new AppTray(icon, mainWindow); mainWindow.on('ready', () => (mainWindow = null)); })