lbry-desktop/electron/createTray.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-02-27 05:03:01 +01:00
import { app, Menu, Tray } from 'electron';
2018-02-24 00:20:12 +01:00
import path from 'path';
2019-03-16 00:16:11 +01:00
let tray;
2018-02-24 00:20:12 +01:00
export default window => {
let iconPath;
2019-03-15 23:09:14 +01:00
/*
* A maximized window can't be properly
* restored when minimized to the taskbar
* (it will be restored/showed as unmaximized).
2021-01-28 00:25:17 +01:00
*
* window.isMaximized() will also return
* false when minimizing a maximized window.
2021-01-28 00:25:17 +01:00
*
* The safest way to keep track of the
* maximized state using maximize and
* unmaximize events.
*/
let isWindowMaximized = false;
window.on('maximize', () => {
isWindowMaximized = true;
});
window.on('unmaximize', () => {
isWindowMaximized = false;
});
2018-02-24 00:20:12 +01:00
switch (process.platform) {
case 'darwin': {
2019-03-05 05:46:57 +01:00
iconPath = 'static/img/tray/mac/trayTemplate.png';
2018-02-24 00:20:12 +01:00
break;
}
case 'win32': {
2019-03-05 05:46:57 +01:00
iconPath = 'static/img/tray/windows/tray.ico';
2018-02-24 00:20:12 +01:00
break;
}
default: {
2019-03-05 05:46:57 +01:00
iconPath = 'static/img/tray/default/tray.png';
2018-02-24 00:20:12 +01:00
}
}
2019-05-07 23:38:29 +02:00
tray = new Tray(process.env.NODE_ENV === 'development' ? iconPath : path.join(process.resourcesPath, iconPath));
2018-02-24 00:20:12 +01:00
const restoreFromTray = () => {
if (isWindowMaximized) {
window.maximize();
}
2018-02-24 00:20:12 +01:00
window.show();
};
tray.on('double-click', restoreFromTray);
2018-02-24 00:20:12 +01:00
tray.setToolTip('LBRY App');
const template = [
{
label: `Open ${app.name}`,
click: restoreFromTray,
2018-02-24 00:20:12 +01:00
},
{ role: 'quit' },
];
const contextMenu = Menu.buildFromTemplate(template);
tray.setContextMenu(contextMenu);
return tray;
};