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