Skip to content

Commit

Permalink
Merge pull request #40 from rawzone/linux-tray-icon
Browse files Browse the repository at this point in the history
Added workaround for trayicon not working under Linux
  • Loading branch information
bcalik authored Apr 9, 2017
2 parents c1a050c + f65460b commit 48d20f0
Show file tree
Hide file tree
Showing 2 changed files with 1,274 additions and 0 deletions.
55 changes: 55 additions & 0 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@
createTray() {
whatsApp.tray = new AppTray(__dirname + '/assets/img/trayTemplate.png');

// Setting up a trayicon context menu
whatsApp.trayContextMenu = AppMenu.buildFromTemplate([
{label: 'Show',
visible: false, // Hide this option on start
click: function() {
whatsApp.window.show();
}},

{label: 'Hide',
visible: true, // Show this option on start
click: function() {
whatsApp.window.hide();
}},

// Quit WhatsApp
{label: 'Quit', click: function() {
app.quit();
}}
]);
whatsApp.tray.setContextMenu(whatsApp.trayContextMenu);

// Normal this will show the main window, but electron under Linux
// dosent work with the clicked event so we are using the above
// contextmenu insted - Rightclick the trayicon and pick Show
// WhatsApp
// More info:
// https://github.com/electron/electron/blob/master/docs/api/tray.md
// See the Platform limitations section.
whatsApp.tray.on('clicked', () => {
whatsApp.window.show();
});
Expand Down Expand Up @@ -213,6 +241,13 @@
}
}));

whatsApp.window.on('close', onlyLinux((e) => {
if (whatsApp.window.forceClose !== true) {
e.preventDefault();
whatsApp.window.hide();
}
}));

whatsApp.window.on("close", function(){
if (settings.window) {
settings.window.close();
Expand All @@ -227,6 +262,26 @@
config.saveConfiguration();
});

// Toggle contextmenu content when window is shown
whatsApp.window.on("show", function() {
whatsApp.trayContextMenu.items[0].visible = false;
whatsApp.trayContextMenu.items[1].visible = true;

// Need to re-set the contextmenu for this to work under Linux
// TODO: Only trigger this under Linux
whatsApp.tray.setContextMenu(whatsApp.trayContextMenu);
});

// Toggle contextmenu content when window is hidden
whatsApp.window.on("hide", function() {
whatsApp.trayContextMenu.items[0].visible = true;
whatsApp.trayContextMenu.items[1].visible = false;

// Need to re-set the contextmenu for this to work under Linux
// TODO: Only trigger this under Linux
whatsApp.tray.setContextMenu(whatsApp.trayContextMenu);
});

app.on('before-quit', onlyOSX(() => {
whatsApp.window.forceClose = true;
}));
Expand Down
Loading

0 comments on commit 48d20f0

Please sign in to comment.