lbry-desktop/electron/createTray.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

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