2017-11-27 22:48:09 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2019-02-20 01:35:35 +00:00
|
|
|
const fs = require('fs');
|
2018-05-02 01:54:43 +00:00
|
|
|
const { app, Menu, Tray } = require('electron');
|
2018-07-21 16:37:39 +00:00
|
|
|
const dockIcon = require('./dock_icon');
|
2017-11-27 22:48:09 +00:00
|
|
|
|
|
|
|
let trayContextMenu = null;
|
|
|
|
let tray = null;
|
|
|
|
|
|
|
|
function createTrayIcon(getMainWindow, messages) {
|
|
|
|
// A smaller icon is needed on macOS
|
2018-01-17 23:27:58 +00:00
|
|
|
const iconSize = process.platform === 'darwin' ? '16' : '256';
|
2018-05-02 01:54:43 +00:00
|
|
|
const iconNoNewMessages = path.join(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'images',
|
|
|
|
`icon_${iconSize}.png`
|
|
|
|
);
|
2018-01-17 23:27:58 +00:00
|
|
|
|
|
|
|
tray = new Tray(iconNoNewMessages);
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2019-01-02 22:42:31 +00:00
|
|
|
tray.forceOnTop = mainWindow => {
|
|
|
|
if (mainWindow) {
|
|
|
|
// On some versions of GNOME the window may not be on top when restored.
|
|
|
|
// This trick should fix it.
|
|
|
|
// Thanks to: https://github.com/Enrico204/Whatsapp-Desktop/commit/6b0dc86b64e481b455f8fce9b4d797e86d000dc1
|
|
|
|
mainWindow.setAlwaysOnTop(true);
|
|
|
|
mainWindow.focus();
|
|
|
|
mainWindow.setAlwaysOnTop(false);
|
|
|
|
}
|
2019-01-03 21:44:42 +00:00
|
|
|
};
|
2019-01-02 22:42:31 +00:00
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
tray.toggleWindowVisibility = () => {
|
|
|
|
const mainWindow = getMainWindow();
|
2017-11-27 22:48:09 +00:00
|
|
|
if (mainWindow) {
|
|
|
|
if (mainWindow.isVisible()) {
|
|
|
|
mainWindow.hide();
|
2018-07-21 16:37:39 +00:00
|
|
|
dockIcon.hide();
|
2017-11-27 22:48:09 +00:00
|
|
|
} else {
|
|
|
|
mainWindow.show();
|
2018-07-21 16:37:39 +00:00
|
|
|
dockIcon.show();
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2019-01-02 22:42:31 +00:00
|
|
|
tray.forceOnTop(mainWindow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tray.updateContextMenu();
|
|
|
|
};
|
|
|
|
|
|
|
|
tray.showWindow = () => {
|
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (mainWindow) {
|
|
|
|
if (!mainWindow.isVisible()) {
|
|
|
|
mainWindow.show();
|
2017-11-27 22:48:09 +00:00
|
|
|
}
|
2019-01-02 22:42:31 +00:00
|
|
|
|
|
|
|
tray.forceOnTop(mainWindow);
|
2017-11-27 22:48:09 +00:00
|
|
|
}
|
|
|
|
tray.updateContextMenu();
|
2018-01-08 21:19:25 +00:00
|
|
|
};
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
tray.updateContextMenu = () => {
|
|
|
|
const mainWindow = getMainWindow();
|
2017-11-27 22:48:09 +00:00
|
|
|
|
|
|
|
// NOTE: we want to have the show/hide entry available in the tray icon
|
|
|
|
// context menu, since the 'click' event may not work on all platforms.
|
|
|
|
// For details please refer to:
|
|
|
|
// https://github.com/electron/electron/blob/master/docs/api/tray.md.
|
2018-05-02 01:54:43 +00:00
|
|
|
trayContextMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
id: 'toggleWindowVisibility',
|
2020-03-25 23:45:37 +00:00
|
|
|
label:
|
|
|
|
messages[mainWindow && mainWindow.isVisible() ? 'hide' : 'show']
|
|
|
|
.message,
|
2018-05-02 01:54:43 +00:00
|
|
|
click: tray.toggleWindowVisibility,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'quit',
|
|
|
|
label: messages.quit.message,
|
|
|
|
click: app.quit.bind(app),
|
|
|
|
},
|
|
|
|
]);
|
2017-11-27 22:48:09 +00:00
|
|
|
|
|
|
|
tray.setContextMenu(trayContextMenu);
|
2018-01-08 21:19:25 +00:00
|
|
|
};
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
tray.updateIcon = unreadCount => {
|
2019-02-20 01:35:35 +00:00
|
|
|
let image;
|
|
|
|
|
2018-01-17 23:27:58 +00:00
|
|
|
if (unreadCount > 0) {
|
|
|
|
const filename = `${String(unreadCount >= 10 ? 10 : unreadCount)}.png`;
|
2019-02-20 01:35:35 +00:00
|
|
|
image = path.join(__dirname, '..', 'images', 'alert', iconSize, filename);
|
2018-01-17 23:27:58 +00:00
|
|
|
} else {
|
2019-02-20 01:35:35 +00:00
|
|
|
image = iconNoNewMessages;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(image)) {
|
|
|
|
console.log('tray.updateIcon: Image for tray update does not exist!');
|
|
|
|
return;
|
2018-01-17 23:27:58 +00:00
|
|
|
}
|
2019-06-10 21:41:05 +00:00
|
|
|
try {
|
|
|
|
tray.setImage(image);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(
|
|
|
|
'tray.setImage error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
2018-01-17 23:27:58 +00:00
|
|
|
};
|
|
|
|
|
2019-01-02 22:42:31 +00:00
|
|
|
tray.on('click', tray.showWindow);
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2019-02-21 22:41:17 +00:00
|
|
|
tray.setToolTip(messages.signalDesktop.message);
|
2017-11-27 22:48:09 +00:00
|
|
|
tray.updateContextMenu();
|
|
|
|
|
|
|
|
return tray;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createTrayIcon;
|