2019-12-06 18:17:34 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 Karl Glatz <karl@glatz.biz>
|
|
|
|
Copyright 2017 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
import { app, Tray, Menu, nativeImage } from "electron";
|
|
|
|
import pngToIco from "png-to-ico";
|
|
|
|
import path from "path";
|
|
|
|
import fs from "fs";
|
2022-01-10 12:57:33 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
import { _t } from "./language-helper";
|
2019-12-06 18:17:34 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
let trayIcon: Tray = null;
|
2019-12-06 18:17:34 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
export function hasTray(): boolean {
|
2019-12-06 18:17:34 +00:00
|
|
|
return (trayIcon !== null);
|
2021-06-25 13:35:58 +00:00
|
|
|
}
|
2019-12-06 18:17:34 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
export function destroy(): void {
|
2019-12-06 18:17:34 +00:00
|
|
|
if (trayIcon) {
|
|
|
|
trayIcon.destroy();
|
|
|
|
trayIcon = null;
|
|
|
|
}
|
2021-06-25 13:35:58 +00:00
|
|
|
}
|
2019-12-06 18:17:34 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
function toggleWin(): void {
|
2021-04-26 12:58:29 +00:00
|
|
|
if (global.mainWindow.isVisible() && !global.mainWindow.isMinimized()) {
|
|
|
|
global.mainWindow.hide();
|
|
|
|
} else {
|
|
|
|
if (global.mainWindow.isMinimized()) global.mainWindow.restore();
|
|
|
|
if (!global.mainWindow.isVisible()) global.mainWindow.show();
|
|
|
|
global.mainWindow.focus();
|
|
|
|
}
|
2021-06-25 13:35:58 +00:00
|
|
|
}
|
2021-04-26 12:58:29 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
interface IConfig {
|
2021-07-01 08:22:57 +00:00
|
|
|
icon_path: string; // eslint-disable-line camelcase
|
2021-06-25 13:35:58 +00:00
|
|
|
brand: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function create(config: IConfig): void {
|
2019-12-06 18:17:34 +00:00
|
|
|
// no trays on darwin
|
|
|
|
if (process.platform === 'darwin' || trayIcon) return;
|
|
|
|
const defaultIcon = nativeImage.createFromPath(config.icon_path);
|
|
|
|
|
|
|
|
trayIcon = new Tray(defaultIcon);
|
|
|
|
trayIcon.setToolTip(config.brand);
|
2021-04-26 12:58:29 +00:00
|
|
|
initApplicationMenu();
|
2019-12-06 18:17:34 +00:00
|
|
|
trayIcon.on('click', toggleWin);
|
|
|
|
|
|
|
|
let lastFavicon = null;
|
|
|
|
global.mainWindow.webContents.on('page-favicon-updated', async function(ev, favicons) {
|
|
|
|
if (!favicons || favicons.length <= 0 || !favicons[0].startsWith('data:')) {
|
|
|
|
if (lastFavicon !== null) {
|
|
|
|
global.mainWindow.setIcon(defaultIcon);
|
|
|
|
trayIcon.setImage(defaultIcon);
|
|
|
|
lastFavicon = null;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to change, shortcut
|
|
|
|
if (favicons[0] === lastFavicon) return;
|
|
|
|
lastFavicon = favicons[0];
|
|
|
|
|
|
|
|
let newFavicon = nativeImage.createFromDataURL(favicons[0]);
|
|
|
|
|
|
|
|
// Windows likes ico's too much.
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
try {
|
2020-07-01 14:30:53 +00:00
|
|
|
const icoPath = path.join(app.getPath('temp'), 'win32_element_icon.ico');
|
2019-12-06 18:17:34 +00:00
|
|
|
fs.writeFileSync(icoPath, await pngToIco(newFavicon.toPNG()));
|
|
|
|
newFavicon = nativeImage.createFromPath(icoPath);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to make win32 ico", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trayIcon.setImage(newFavicon);
|
|
|
|
global.mainWindow.setIcon(newFavicon);
|
|
|
|
});
|
|
|
|
|
|
|
|
global.mainWindow.webContents.on('page-title-updated', function(ev, title) {
|
|
|
|
trayIcon.setToolTip(title);
|
|
|
|
});
|
2021-06-25 13:35:58 +00:00
|
|
|
}
|
2021-04-26 12:58:29 +00:00
|
|
|
|
2021-06-25 13:35:58 +00:00
|
|
|
export function initApplicationMenu(): void {
|
2021-04-26 12:58:29 +00:00
|
|
|
if (!trayIcon) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
2021-04-26 13:50:18 +00:00
|
|
|
label: _t('Show/Hide'),
|
2021-04-26 12:58:29 +00:00
|
|
|
click: toggleWin,
|
|
|
|
},
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
2021-04-26 13:50:18 +00:00
|
|
|
label: _t('Quit'),
|
2021-04-26 12:58:29 +00:00
|
|
|
click: function() {
|
|
|
|
app.quit();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
trayIcon.setContextMenu(contextMenu);
|
|
|
|
}
|