index.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { Tray, ipcMain, BrowserWindow, app, Notification } from 'electron';
  2. import type { NotificationConstructorOptions, IpcMainInvokeEvent } from 'electron';
  3. import { openInBrowser } from '../utils';
  4. import { omit } from 'lodash-es';
  5. ipcMain.on('open-in-browser', (event: IpcMainInvokeEvent, url: string) => openInBrowser(url));
  6. // 处理任务栏闪烁
  7. ipcMain.on('notify-flash', (event: IpcMainInvokeEvent, count: number = 0) => {
  8. const win = BrowserWindow.getAllWindows()[0];
  9. if (!win) return;
  10. if (win.isFocused()) return;
  11. if (process.platform === 'win32') {
  12. // windows
  13. win.flashFrame(true);
  14. } else if (process.platform === 'darwin') {
  15. // Mac
  16. if (app.dock) {
  17. app.dock.bounce('informational');
  18. // 设置角标(未读消息)
  19. if (count > 0) {
  20. app.dock.setBadge(count.toString());
  21. } else {
  22. app.dock.setBadge('');
  23. }
  24. }
  25. }
  26. });
  27. // 通知 (点击通知打开指定页面)
  28. ipcMain.on('notify-with-path', (event: IpcMainInvokeEvent, options: NotificationConstructorOptions & { path: string }) => {
  29. const win = BrowserWindow.getAllWindows()[0];
  30. if (!win) return;
  31. if (win.isFocused()) return;
  32. const notification = new Notification({
  33. ...omit(options, 'path'),
  34. });
  35. notification.on('click', () => {
  36. if (win.isMinimized()) win.restore();
  37. win.show();
  38. win.focus();
  39. // win.webContents.send('navigate-to', options.path);
  40. });
  41. notification.show();
  42. });