aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/appIndicator.ts
blob: f4a5ba480e4bb5d04bf315e6318f8e6dc3faab83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { join } from 'node:path';
import { app, ipcMain, BrowserWindow } from 'electron';
import { autorun } from 'mobx';
import { isMac, isWindows, isLinux } from '../../environment';
import TrayIcon from '../../lib/Tray';

const INDICATOR_TASKBAR = 'taskbar';
const FILE_EXTENSION = isWindows ? 'ico' : 'png';

let isTrayIconEnabled: boolean;

function getAsset(type: 'tray' | 'taskbar', asset: string) {
  return join(
    __dirname,
    '..',
    '..',
    'assets',
    'images',
    type,
    process.platform,
    `${asset}.${FILE_EXTENSION}`,
  );
}

export default (params: {
  mainWindow: BrowserWindow;
  settings: any;
  trayIcon: TrayIcon;
}) => {
  autorun(() => {
    isTrayIconEnabled = params.settings.app.get('enableSystemTray');

    if (!isTrayIconEnabled) {
      params.trayIcon.hide();
    } else if (isTrayIconEnabled) {
      params.trayIcon.show();
    }
  });

  ipcMain.on('updateAppIndicator', (_event, args) => {
    // Flash TaskBar for windows, bounce Dock on Mac
    if (
      !params.mainWindow.isFocused() &&
      params.settings.app.get('notifyTaskBarOnMessage')
    ) {
      if (isWindows) {
        params.mainWindow.flashFrame(true);
        params.mainWindow.once('focus', () =>
          params.mainWindow.flashFrame(false),
        );
      } else if (isMac) {
        app.dock.bounce('informational');
      }
    }

    // Update badge
    if (isMac && typeof args.indicator === 'string') {
      app.dock.setBadge(args.indicator);
    }

    if ((isMac || isLinux) && typeof args.indicator === 'number') {
      app.badgeCount = args.indicator;
    }

    if (isWindows) {
      if (typeof args.indicator === 'number' && args.indicator !== 0) {
        params.mainWindow.setOverlayIcon(
          // @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'NativeImage | null'.
          getAsset(
            'taskbar',
            `${INDICATOR_TASKBAR}-${
              args.indicator >= 10 ? 10 : args.indicator
            }`,
          ),
          '',
        );
      } else if (typeof args.indicator === 'string') {
        params.mainWindow.setOverlayIcon(
          // @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'NativeImage | null'.
          getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`),
          '',
        );
      } else {
        params.mainWindow.setOverlayIcon(null, '');
      }
    }

    // Update Tray
    params.trayIcon.setIndicator(args.indicator);
  });
};