summaryrefslogtreecommitdiffhomepage
path: root/app/tray-icon-controller.js
blob: c32421b20fdbcb6a5b2c91ef85a7580518e77b8b (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
// @flow
import path from 'path';
import KeyframeAnimation from './lib/keyframe-animation';
import type { Tray } from 'electron';

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

export default class TrayIconController {
  _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;
  }

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

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

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

    const animation = this._animation;
    if (type === 'secured') {
      animation.reverse = true;
      animation.play({ beginFromCurrentState: true, startFrame: 8, endFrame: 9 });
    } else {
      animation.reverse = this._isReverseAnimation(type);
      animation.play({ beginFromCurrentState: true });
    }

    this._iconType = type;
  }

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

  _isReverseAnimation(type: TrayIconType): boolean {
    return type === 'unsecured';
  }
}