summaryrefslogtreecommitdiffhomepage
path: root/app/lib/tray-icon-manager.js
blob: 73424cb7ef5de978af526b42de42022568bf4c7e (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
// @flow
import path from 'path';
import KeyframeAnimation from './keyframe-animation';

import type { Tray } from 'electron';

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

export default class TrayIconManager {

  _animation: ?KeyframeAnimation;
  _iconType: TrayIconType;

  constructor(tray: Tray, initialType: TrayIconType) {
    const animation = this._createAnimation();
    animation.onFrame = (img) => tray.setImage(img);
    animation.reverse = this._isReverseAnimation(initialType);
    animation.play({ advanceTo: 'end' });

    this._animation = animation;
    this._iconType = initialType;
  }

  destroy() {
    if(this._animation) {
      this._animation.stop();
      this._animation = null;
    }
  }

  _createAnimation(): KeyframeAnimation {
    const basePath = path.join(path.resolve(__dirname, '..'), 'assets/images/menubar icons');
    const filePath = path.join(basePath, 'lock-{}.png');
    const animation = KeyframeAnimation.fromFilePattern(filePath, [1, 9]);
    animation.speed = 100;
    return animation;
  }

  _isReverseAnimation(type: TrayIconType): bool {
    // unsecured & securing are treated as one
    return type !== 'secured';
  }

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

  set iconType(type: TrayIconType) {
    if(this._iconType === type || !this._animation) { return; }

    const animation = this._animation;
    animation.reverse = this._isReverseAnimation(type);
    animation.play({ beginFromCurrentState: true });

    this._iconType = type;
  }

}