blob: bb1d4f58683de77555d878adf41356ff1b823755 (
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
|
import { nativeImage, NativeImage, Tray } from 'electron';
import path from 'path';
import KeyframeAnimation from './keyframe-animation';
export type TrayIconType = 'unsecured' | 'securing' | 'secured';
export default class TrayIconController {
private animation?: KeyframeAnimation;
private iconImages: NativeImage[] = [];
constructor(
tray: Tray,
private iconTypeValue: TrayIconType,
private useMonochromaticIconValue: boolean,
) {
this.loadImages();
const initialFrame = this.targetFrame();
const animation = new KeyframeAnimation();
animation.speed = 100;
animation.onFrame = (frameNumber) => tray.setImage(this.iconImages[frameNumber]);
animation.play({ start: initialFrame, end: initialFrame });
this.animation = animation;
}
public dispose() {
if (this.animation) {
this.animation.stop();
this.animation = undefined;
}
}
get iconType(): TrayIconType {
return this.iconTypeValue;
}
set useMonochromaticIcon(useMonochromaticIcon: boolean) {
this.useMonochromaticIconValue = useMonochromaticIcon;
this.loadImages();
if (this.animation && !this.animation.isRunning) {
this.animation.play({ end: this.targetFrame() });
}
}
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 loadImages() {
const frames = Array.from({ length: 10 }, (_, i) => i + 1);
this.iconImages = frames.map((frame) => nativeImage.createFromPath(this.getImagePath(frame)));
}
private getImagePath(frame: number) {
const basePath = path.resolve(path.join(__dirname, '../../assets/images/menubar icons'));
const extension = process.platform === 'win32' ? 'ico' : 'png';
let suffix = '';
if (this.useMonochromaticIconValue) {
suffix = process.platform === 'darwin' ? 'Template' : '_white';
}
return path.join(basePath, process.platform, `lock-${frame}${suffix}.${extension}`);
}
private targetFrame(): number {
switch (this.iconTypeValue) {
case 'unsecured':
return 0;
case 'securing':
return 9;
case 'secured':
return 8;
}
}
}
|