2021-01-21 23:58:38 +00:00
|
|
|
// Copyright 2017-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { join } from 'path';
|
|
|
|
import { existsSync } from 'fs';
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
import { BrowserWindow, app, Menu, Tray } from 'electron';
|
|
|
|
import * as DockIcon from '../ts/dock_icon';
|
|
|
|
|
|
|
|
import { LocaleMessagesType } from '../ts/types/I18N';
|
2017-11-27 22:48:09 +00:00
|
|
|
|
|
|
|
let trayContextMenu = null;
|
2021-06-18 17:04:27 +00:00
|
|
|
let tray: Tray | undefined;
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export default function createTrayIcon(
|
|
|
|
getMainWindow: () => BrowserWindow | undefined,
|
|
|
|
messages: LocaleMessagesType
|
|
|
|
): { updateContextMenu: () => void; updateIcon: (count: number) => void } {
|
|
|
|
let iconSize: string;
|
2021-04-02 20:29:04 +00:00
|
|
|
switch (process.platform) {
|
|
|
|
case 'darwin':
|
|
|
|
iconSize = '16';
|
|
|
|
break;
|
|
|
|
case 'win32':
|
|
|
|
iconSize = '32';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
iconSize = '256';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const iconNoNewMessages = join(
|
2018-05-02 01:54:43 +00:00
|
|
|
__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
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const forceOnTop = (mainWindow: BrowserWindow) => {
|
2019-01-02 22:42:31 +00:00
|
|
|
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
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const toggleWindowVisibility = () => {
|
2018-01-08 21:19:25 +00:00
|
|
|
const mainWindow = getMainWindow();
|
2017-11-27 22:48:09 +00:00
|
|
|
if (mainWindow) {
|
|
|
|
if (mainWindow.isVisible()) {
|
|
|
|
mainWindow.hide();
|
2021-06-18 17:04:27 +00:00
|
|
|
DockIcon.hide();
|
2017-11-27 22:48:09 +00:00
|
|
|
} else {
|
|
|
|
mainWindow.show();
|
2021-06-18 17:04:27 +00:00
|
|
|
DockIcon.show();
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
forceOnTop(mainWindow);
|
2019-01-02 22:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
updateContextMenu();
|
2019-01-02 22:42:31 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const showWindow = () => {
|
2019-01-02 22:42:31 +00:00
|
|
|
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
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
forceOnTop(mainWindow);
|
2017-11-27 22:48:09 +00:00
|
|
|
}
|
2021-06-18 17:04:27 +00:00
|
|
|
updateContextMenu();
|
2018-01-08 21:19:25 +00:00
|
|
|
};
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const updateContextMenu = () => {
|
2018-01-08 21:19:25 +00:00
|
|
|
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,
|
2021-06-18 17:04:27 +00:00
|
|
|
click: toggleWindowVisibility,
|
2018-05-02 01:54:43 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'quit',
|
|
|
|
label: messages.quit.message,
|
|
|
|
click: app.quit.bind(app),
|
|
|
|
},
|
|
|
|
]);
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
tray?.setContextMenu(trayContextMenu);
|
2018-01-08 21:19:25 +00:00
|
|
|
};
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
const updateIcon = (unreadCount: number) => {
|
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`;
|
2021-06-18 17:04:27 +00:00
|
|
|
image = 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;
|
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
if (!existsSync(image)) {
|
2019-02-20 01:35:35 +00:00
|
|
|
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 {
|
2021-06-18 17:04:27 +00:00
|
|
|
tray?.setImage(image);
|
2019-06-10 21:41:05 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(
|
|
|
|
'tray.setImage error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
2018-01-17 23:27:58 +00:00
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
tray.on('click', showWindow);
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2019-02-21 22:41:17 +00:00
|
|
|
tray.setToolTip(messages.signalDesktop.message);
|
2021-06-18 17:04:27 +00:00
|
|
|
updateContextMenu();
|
2017-11-27 22:48:09 +00:00
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
return {
|
|
|
|
updateContextMenu,
|
|
|
|
updateIcon,
|
|
|
|
};
|
2017-11-27 22:48:09 +00:00
|
|
|
}
|