Skip to content

Commit

Permalink
AppTray Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Belchenkov committed Jun 30, 2020
1 parent 9c063fb commit 70ac6b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
38 changes: 38 additions & 0 deletions AppTray.js
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 3 additions & 25 deletions main.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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));
})
Expand Down

0 comments on commit 70ac6b2

Please sign in to comment.