improve tray algorithm, requires manual update stuff
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
5d66a539b7
commit
908a7bab7c
1 changed files with 32 additions and 26 deletions
|
@ -26,17 +26,17 @@ exports.hasTray = function hasTray() {
|
|||
return (trayIcon !== null);
|
||||
};
|
||||
|
||||
exports.create = function(win, config) {
|
||||
exports.create = function(config) {
|
||||
// no trays on darwin
|
||||
if (process.platform === 'darwin' || trayIcon) return;
|
||||
|
||||
const toggleWin = function() {
|
||||
if (win.isVisible() && !win.isMinimized()) {
|
||||
win.hide();
|
||||
if (global.mainWindow.isVisible() && !global.mainWindow.isMinimized()) {
|
||||
global.mainWindow.hide();
|
||||
} else {
|
||||
if (win.isMinimized()) win.restore();
|
||||
if (!win.isVisible()) win.show();
|
||||
win.focus();
|
||||
if (global.mainWindow.isMinimized()) global.mainWindow.restore();
|
||||
if (!global.mainWindow.isVisible()) global.mainWindow.show();
|
||||
global.mainWindow.focus();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -59,36 +59,42 @@ exports.create = function(win, config) {
|
|||
trayIcon.setContextMenu(contextMenu);
|
||||
trayIcon.on('click', toggleWin);
|
||||
|
||||
const defaultIcon = nativeImage.createFromPath(config.icon_path);
|
||||
|
||||
let lastFavicon = null;
|
||||
win.webContents.on('page-favicon-updated', async function(ev, favicons) {
|
||||
let newFavicon = config.icon_path;
|
||||
if (favicons && favicons.length > 0 && favicons[0].startsWith('data:')) {
|
||||
newFavicon = favicons[0];
|
||||
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 (newFavicon === lastFavicon) return;
|
||||
lastFavicon = newFavicon;
|
||||
if (favicons[0] === lastFavicon) return;
|
||||
lastFavicon = favicons[0];
|
||||
|
||||
// if its not default we have to construct into nativeImage
|
||||
if (newFavicon !== config.icon_path) {
|
||||
newFavicon = nativeImage.createFromDataURL(favicons[0]);
|
||||
let newFavicon = nativeImage.createFromDataURL(favicons[0]);
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
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);}
|
||||
}
|
||||
// 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(e);}
|
||||
}
|
||||
|
||||
trayIcon.setImage(newFavicon);
|
||||
win.setIcon(newFavicon);
|
||||
global.mainWindow.setIcon(newFavicon);
|
||||
});
|
||||
|
||||
win.webContents.on('page-title-updated', function(ev, title) {
|
||||
global.mainWindow.webContents.on('page-title-updated', function(ev, title) {
|
||||
trayIcon.setToolTip(title);
|
||||
});
|
||||
|
||||
global.mainWindow.on('closed', () => {
|
||||
trayIcon.destroy();
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue