aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/TouchBar.ts
blob: 417e20411f112383a22175d0d51149e10232c2c4 (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
import semver from 'semver';
import { TouchBar, getCurrentWindow } from '@electron/remote';
import { autorun } from 'mobx';

import { isMac, osRelease } from '../environment';

export default class FranzTouchBar {
  stores: any;

  actions: any;

  build: any;

  constructor(stores: any, actions: any) {
    this.stores = stores;
    this.actions = actions;

    // Temporary fix for https://github.com/electron/electron/issues/10442
    // TODO: remove when we upgrade to electron 1.8.2 or later
    try {
      if (isMac && semver.gt(osRelease, '16.6.0')) {
        this.build = autorun(this._build.bind(this));
      }
    } catch (error) {
      console.error(error);
    }
  }

  _build() {
    const currentWindow = getCurrentWindow();

    if (this.stores.user.isLoggedIn) {
      const { TouchBarButton, TouchBarSpacer } = TouchBar;

      const buttons: any[] = [];
      for (const service of this.stores.services.allDisplayed) {
        buttons.push(
          new TouchBarButton({
            label: `${service.name}${
              service.unreadDirectMessageCount > 0 ? ' 🔴' : ''
            } ${
              service.unreadDirectMessageCount === 0 &&
              service.unreadIndirectMessageCount > 0
                ? ' ⚪️'
                : ''
            }`,
            backgroundColor: service.isActive && '#3498DB',
            click: () => {
              this.actions.service.setActive({ serviceId: service.id });
            },
          }),
          new TouchBarSpacer({ size: 'small' }),
        );
      }

      const touchBar = new TouchBar({ items: buttons });
      currentWindow.setTouchBar(touchBar);
    } else {
      currentWindow.setTouchBar(null);
    }
  }
}