summaryrefslogtreecommitdiffhomepage
path: root/gui/src/main/tray-icon-controller.ts
blob: a16c2ad9fc41223f5218342d55091a707ae01bd0 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { exec as execAsync } from 'child_process';
import { nativeImage, NativeImage, Tray } from 'electron';
import path from 'path';
import { promisify } from 'util';
import log from '../shared/logging';
import KeyframeAnimation from './keyframe-animation';

const exec = promisify(execAsync);

export type TrayIconType = 'unsecured' | 'securing' | 'secured';

type IconSets = {
  regular: NativeImage[];
  template: NativeImage[];
  white: NativeImage[];
  black: NativeImage[];
};

export default class TrayIconController {
  private animation?: KeyframeAnimation;
  private iconSets: IconSets = { regular: [], template: [], white: [], black: [] };
  private iconSet: NativeImage[] = [];

  constructor(
    private tray: Tray,
    private iconTypeValue: TrayIconType,
    private useMonochromaticIconValue: boolean,
  ) {
    this.loadImages();
  }

  public dispose() {
    if (this.animation) {
      this.animation.stop();
      this.animation = undefined;
    }
  }

  get iconType(): TrayIconType {
    return this.iconTypeValue;
  }

  public async updateTheme() {
    if (this.useMonochromaticIconValue) {
      switch (process.platform) {
        case 'darwin':
          this.iconSet = this.iconSets.template;
          break;
        case 'win32': {
          if (await this.getSystemUsesLightTheme()) {
            this.iconSet = this.iconSets.black;
          } else {
            this.iconSet = this.iconSets.white;
          }
          break;
        }
        case 'linux':
        default:
          this.iconSet = this.iconSets.white;
          break;
      }
    } else {
      this.iconSet = this.iconSets.regular;
    }

    if (this.animation === undefined) {
      this.initAnimation();
    } else if (!this.animation.isRunning) {
      this.animation.play({ end: this.targetFrame() });
    }
  }

  public async setUseMonochromaticIcon(useMonochromaticIcon: boolean) {
    this.useMonochromaticIconValue = useMonochromaticIcon;
    await this.updateTheme();
  }

  public animateToIcon(type: TrayIconType) {
    if (this.iconTypeValue === type || !this.animation) {
      return;
    }

    this.iconTypeValue = type;

    const animation = this.animation;
    const frame = this.targetFrame();

    animation.play({ end: frame });
  }

  private initAnimation() {
    const initialFrame = this.targetFrame();
    const animation = new KeyframeAnimation();
    animation.speed = 100;
    animation.onFrame = this.onFrame;
    animation.play({ start: initialFrame, end: initialFrame });

    this.animation = animation;
  }

  private onFrame = (frameNumber: number) => {
    const frame = this.iconSet[frameNumber];
    if (frame === undefined) {
      log.error('Failed to show tray icon due to the icon being undefined');
    } else {
      this.tray.setImage(frame);
    }
  };

  private loadImages() {
    this.iconSets.regular = this.loadImageSet('');

    switch (process.platform) {
      case 'darwin':
        this.iconSets.template = this.loadImageSet('Template');
        break;
      case 'win32':
        this.iconSets.white = this.loadImageSet('_white');
        this.iconSets.black = this.loadImageSet('_black');
        break;
      case 'linux':
      default:
        this.iconSets.white = this.loadImageSet('_white');
        break;
    }
  }

  private loadImageSet(suffix: string): NativeImage[] {
    const frames = Array.from({ length: 10 }, (_, i) => i + 1);
    return frames.map((frame) => nativeImage.createFromPath(this.getImagePath(frame, suffix)));
  }

  private getImagePath(frame: number, suffix?: string) {
    const basePath = path.resolve(path.join(__dirname, '../../assets/images/menubar icons'));
    const extension = process.platform === 'win32' ? 'ico' : 'png';
    return path.join(basePath, process.platform, `lock-${frame}${suffix}.${extension}`);
  }

  private async getSystemUsesLightTheme(): Promise<boolean | undefined> {
    try {
      // This registry entry contains information about the tray background color. This is
      // needed to decide between white and black icons.
      const { stdout, stderr } = await exec(
        'reg query HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\\ /v SystemUsesLightTheme',
      );

      if (!stderr && stdout) {
        // Split the output into rows
        const rows = stdout.split('\n');
        // Select the row that contains the registry entry result
        const resultRow = rows.find((row) => row.includes('SystemUsesLightTheme'))?.trim();
        // Split the row into words
        const resultRowWords = resultRow?.split(' ').filter((word) => word !== '');
        // Grab value which is last word on the result row
        const value = resultRowWords && resultRowWords[resultRowWords.length - 1];

        if (value) {
          const parsedValue = parseInt(value);
          return parsedValue === 1 ? true : false;
        }
      }

      return undefined;
    } catch (e) {
      const error = e as Error;
      log.error('Failed to read SystemUsesLightTheme,', error.message);
      return undefined;
    }
  }

  private targetFrame(): number {
    switch (this.iconTypeValue) {
      case 'unsecured':
        return 0;
      case 'securing':
        return 9;
      case 'secured':
        return 8;
    }
  }
}