2017-01-19 15:29:07 +01: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.
|
|
|
|
*/
|
|
|
|
|
2017-05-13 13:37:13 +02:00
|
|
|
const {app, Tray, Menu, nativeImage} = require('electron');
|
2017-06-04 15:24:22 +02:00
|
|
|
const pngToIco = require('png-to-ico');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
2017-01-19 15:29:07 +01:00
|
|
|
|
|
|
|
let trayIcon = null;
|
|
|
|
|
|
|
|
exports.hasTray = function hasTray() {
|
|
|
|
return (trayIcon !== null);
|
2017-05-13 13:41:13 +02:00
|
|
|
};
|
2017-01-19 15:29:07 +01:00
|
|
|
|
2017-06-12 14:47:29 +02:00
|
|
|
exports.create = function(config) {
|
2017-01-19 15:29:07 +01:00
|
|
|
// no trays on darwin
|
2017-05-22 20:21:52 +02:00
|
|
|
if (process.platform === 'darwin' || trayIcon) return;
|
2017-01-19 15:29:07 +01:00
|
|
|
|
2017-05-13 13:41:13 +02:00
|
|
|
const toggleWin = function() {
|
2017-06-12 14:47:29 +02:00
|
|
|
if (global.mainWindow.isVisible() && !global.mainWindow.isMinimized()) {
|
|
|
|
global.mainWindow.hide();
|
2017-01-19 15:29:07 +01:00
|
|
|
} else {
|
2017-06-12 14:47:29 +02:00
|
|
|
if (global.mainWindow.isMinimized()) global.mainWindow.restore();
|
|
|
|
if (!global.mainWindow.isVisible()) global.mainWindow.show();
|
|
|
|
global.mainWindow.focus();
|
2017-01-19 15:29:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-13 13:37:13 +02:00
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
2017-01-19 15:29:07 +01:00
|
|
|
{
|
2017-05-22 20:21:52 +02:00
|
|
|
label: `Show/Hide ${config.brand}`,
|
2017-05-13 13:41:13 +02:00
|
|
|
click: toggleWin,
|
2017-01-19 15:29:07 +01:00
|
|
|
},
|
2017-05-22 20:21:52 +02:00
|
|
|
{ type: 'separator' },
|
2017-01-19 15:29:07 +01:00
|
|
|
{
|
|
|
|
label: 'Quit',
|
2017-05-13 13:41:13 +02:00
|
|
|
click: function() {
|
2017-01-19 15:29:07 +01:00
|
|
|
app.quit();
|
2017-05-13 13:41:13 +02:00
|
|
|
},
|
|
|
|
},
|
2017-01-19 15:29:07 +01:00
|
|
|
]);
|
|
|
|
|
2017-06-19 12:58:44 +02:00
|
|
|
const defaultIcon = nativeImage.createFromPath(config.icon_path);
|
|
|
|
|
|
|
|
trayIcon = new Tray(defaultIcon);
|
2017-01-19 15:29:07 +01:00
|
|
|
trayIcon.setToolTip(config.brand);
|
|
|
|
trayIcon.setContextMenu(contextMenu);
|
|
|
|
trayIcon.on('click', toggleWin);
|
2017-05-13 13:37:13 +02:00
|
|
|
|
2017-05-17 11:39:43 +02:00
|
|
|
let lastFavicon = null;
|
2017-06-12 14:47:29 +02:00
|
|
|
global.mainWindow.webContents.on('page-favicon-updated', async function(ev, favicons) {
|
2017-06-19 12:58:44 +02:00
|
|
|
if (!favicons || favicons.length <= 0 || !favicons[0].startsWith('data:')) {
|
|
|
|
if (lastFavicon !== null) {
|
2019-06-18 14:59:13 +02:00
|
|
|
global.mainWindow.setIcon(defaultIcon);
|
2017-06-19 12:58:44 +02:00
|
|
|
trayIcon.setImage(defaultIcon);
|
|
|
|
lastFavicon = null;
|
|
|
|
}
|
|
|
|
return;
|
2017-05-17 11:05:50 +02:00
|
|
|
}
|
2017-05-17 11:39:43 +02:00
|
|
|
|
|
|
|
// No need to change, shortcut
|
2017-06-19 12:58:44 +02:00
|
|
|
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 {
|
|
|
|
const icoPath = path.join(app.getPath('temp'), 'win32_riot_icon.ico');
|
|
|
|
fs.writeFileSync(icoPath, await pngToIco(newFavicon.toPNG()));
|
|
|
|
newFavicon = nativeImage.createFromPath(icoPath);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to make win32 ico", e);
|
2017-06-04 12:03:12 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 15:24:22 +02:00
|
|
|
|
|
|
|
trayIcon.setImage(newFavicon);
|
2017-06-12 14:47:29 +02:00
|
|
|
global.mainWindow.setIcon(newFavicon);
|
2017-05-13 13:37:13 +02:00
|
|
|
});
|
2017-05-13 13:39:55 +02:00
|
|
|
|
2017-06-12 14:47:29 +02:00
|
|
|
global.mainWindow.webContents.on('page-title-updated', function(ev, title) {
|
2017-05-13 13:39:55 +02:00
|
|
|
trayIcon.setToolTip(title);
|
|
|
|
});
|
2017-01-19 15:29:07 +01:00
|
|
|
};
|