mirror of
https://github.com/Llewellynvdm/nativefier.git
synced 2024-09-21 17:29:00 +00:00
e664bc6af8
This PR allows us to code playwright integration tests that can potentially replace some of our manual tests and allow us automated testing of the app itself. Current technical limitations: * No app level keyboard simulation support (e.g, zoom in, zoom out, etc.) * No code coverage support, so even though we are testing the app, the code coverage does not reflect this fact
89 lines
2.3 KiB
TypeScript
89 lines
2.3 KiB
TypeScript
import { app, Tray, Menu, ipcMain, nativeImage, BrowserWindow } from 'electron';
|
|
|
|
import { getAppIcon, getCounterValue, isOSX } from '../helpers/helpers';
|
|
import * as log from '../helpers/loggingHelper';
|
|
import { OutputOptions } from '../../../shared/src/options/model';
|
|
|
|
export function createTrayIcon(
|
|
nativefierOptions: OutputOptions,
|
|
mainWindow: BrowserWindow,
|
|
): Tray | undefined {
|
|
const options = { ...nativefierOptions };
|
|
|
|
if (options.tray && options.tray !== 'false') {
|
|
const iconPath = getAppIcon();
|
|
if (!iconPath) {
|
|
throw new Error('Icon path not found found to use with tray option.');
|
|
}
|
|
const nimage = nativeImage.createFromPath(iconPath);
|
|
const appIcon = new Tray(nativeImage.createEmpty());
|
|
|
|
if (isOSX()) {
|
|
//sets the icon to the height of the tray.
|
|
appIcon.setImage(
|
|
nimage.resize({ height: appIcon.getBounds().height - 2 }),
|
|
);
|
|
} else {
|
|
appIcon.setImage(nimage);
|
|
}
|
|
|
|
const onClick = (): void => {
|
|
log.debug('onClick');
|
|
if (mainWindow.isVisible()) {
|
|
mainWindow.hide();
|
|
} else {
|
|
mainWindow.show();
|
|
}
|
|
};
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
{
|
|
label: options.name,
|
|
click: onClick,
|
|
},
|
|
{
|
|
label: 'Quit',
|
|
click: (): void => app.exit(0),
|
|
},
|
|
]);
|
|
|
|
appIcon.on('click', onClick);
|
|
|
|
if (options.counter) {
|
|
mainWindow.on('page-title-updated', (event, title) => {
|
|
log.debug('mainWindow.page-title-updated', { event, title });
|
|
const counterValue = getCounterValue(title);
|
|
if (counterValue) {
|
|
appIcon.setToolTip(
|
|
`(${counterValue}) ${options.name ?? 'Nativefier'}`,
|
|
);
|
|
} else {
|
|
appIcon.setToolTip(options.name ?? '');
|
|
}
|
|
});
|
|
} else {
|
|
ipcMain.on('notification', () => {
|
|
log.debug('ipcMain.notification');
|
|
if (mainWindow.isFocused()) {
|
|
return;
|
|
}
|
|
if (options.name) {
|
|
appIcon.setToolTip(`• ${options.name}`);
|
|
}
|
|
});
|
|
|
|
mainWindow.on('focus', () => {
|
|
log.debug('mainWindow.focus');
|
|
appIcon.setToolTip(options.name ?? '');
|
|
});
|
|
}
|
|
|
|
appIcon.setToolTip(options.name ?? '');
|
|
appIcon.setContextMenu(contextMenu);
|
|
|
|
return appIcon;
|
|
}
|
|
|
|
return undefined;
|
|
}
|