aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/ipc-api/appIndicator.js
blob: 576234d256159f90782b5c958069e2e894eee956 (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
import { app, ipcMain, Tray, Menu } from 'electron';
import path from 'path';

const INDICATOR_TRAY_PLAIN = 'tray';
const INDICATOR_TRAY_UNREAD = 'tray-unread';
const INDICATOR_TASKBAR = 'taskbar';

const FILE_EXTENSION = process.platform === 'win32' ? 'ico' : 'png';
let trayIcon;

function getAsset(type, asset) {
  return path.join(
    __dirname, '..', '..', 'assets', 'images', type, process.platform, `${asset}.${FILE_EXTENSION}`,
  );
}

export default (params) => {
  trayIcon = new Tray(getAsset('tray', INDICATOR_TRAY_PLAIN));
  const trayMenuTemplate = [
    {
      label: 'Show Franz',
      click() {
        params.mainWindow.show();
      },
    }, {
      label: 'Quit Franz',
      click() {
        app.quit();
      },
    },
  ];

  const trayMenu = Menu.buildFromTemplate(trayMenuTemplate);
  trayIcon.setContextMenu(trayMenu);

  trayIcon.on('click', () => {
    params.mainWindow.show();
  });

  ipcMain.on('updateAppIndicator', (event, args) => {
    // Update badge
    if (process.platform === 'darwin'
      && typeof (args.indicator) === 'string') {
      app.dock.setBadge(args.indicator);
    }

    if ((process.platform === 'darwin'
      || process.platform === 'linux')
      && typeof (args.indicator) === 'number'
    ) {
      app.setBadgeCount(args.indicator);
    }

    if (process.platform === 'win32') {
      if (typeof args.indicator === 'number'
        && args.indicator !== 0) {
        params.mainWindow.setOverlayIcon(
          getAsset('taskbar', `${INDICATOR_TASKBAR}-${(args.indicator >= 10 ? 10 : args.indicator)}`),
          '',
        );
      } else if (typeof args.indicator === 'string') {
        params.mainWindow.setOverlayIcon(
          getAsset('taskbar', `${INDICATOR_TASKBAR}-alert`),
          '',
        );
      } else {
        params.mainWindow.setOverlayIcon(null, '');
      }
    }

    // Update system tray
    trayIcon.setImage(getAsset('tray', args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN));

    if (process.platform === 'darwin') {
      trayIcon.setPressedImage(
        getAsset('tray', `${args.indicator !== 0 ? INDICATOR_TRAY_UNREAD : INDICATOR_TRAY_PLAIN}-active`),
      );
    }
  });
};