-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
132 lines (120 loc) · 3.59 KB
/
index.ts
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
import { SysTray, ClickEvent } from './../../src/index';
import { icoIconBase64 } from './../ico-icon';
import { pngIconBase64 } from './../png-icon';
const systray = new SysTray({
menu: {
// you should using .png icon in macOS/Linux, but .ico format in windows
icon:
process.platform === 'win32' || process.platform === 'cygwin'
? icoIconBase64
: pngIconBase64,
title: 'Systray Test',
tooltip: 'Tips',
items: [
{
title: 'Systray Test: Running 00:00:00',
tooltip: 'Systray Test is running...',
// checked is implement by plain text in linux
checked: false,
enabled: false,
},
{
title: 'Simple checkbox: checked',
tooltip: 'A tooltip for the checkbox.',
// checked is implement by plain text in linux
checked: true,
enabled: true,
},
{
title: 'Click counter: 0',
tooltip: 'A tooltip for the click counter.',
checked: false,
enabled: true,
},
{
title: 'A quote by a wise man to test the unicode: 屁股比奶好 🚬',
tooltip: 'A tooltip for the unicode text.',
checked: false,
enabled: true,
},
{
title: 'Exit',
tooltip: 'Exit from the app.',
checked: false,
enabled: true,
},
],
},
debug: true,
copyDir: true, // copy go tray binary to outside directory, useful for packing tool like pkg.
});
systray.onError((err) => {
console.error(err);
});
systray.onReady(() => {
const startTime = new Date();
const updateTimerInterval = setInterval(() => updateRunningDuration(), 1000);
systray.onClick((event) => {
if (event.seq_id === 1) {
onCheckboxClick(event);
} else if (event.seq_id === 2) {
onCounterClick(event);
} else if (event.seq_id === 4) {
console.log(`Clicked the Exit button: ${JSON.stringify(event)}`);
console.log('Killing the systray...');
clearInterval(updateTimerInterval);
systray.kill();
}
});
function onCheckboxClick(event: ClickEvent): void {
console.log(`Clicked the checkbox: ${JSON.stringify(event)}`);
systray.sendAction({
type: 'update-item',
item: {
...event.item,
title: `Simple checkbox: ${
!event.item.checked ? 'checked' : 'unchecked'
}`,
checked: !event.item.checked,
},
seq_id: event.seq_id,
});
}
let clickCounter = 0;
function onCounterClick(event: ClickEvent): void {
console.log(`Clicked the counter: ${JSON.stringify(event)}`);
clickCounter++;
systray.sendAction({
type: 'update-item',
item: {
...event.item,
title: `Click counter: ${clickCounter}`,
},
seq_id: event.seq_id,
});
}
function updateRunningDuration(): void {
const currentTime = new Date();
const secondsElapsed = Math.floor(
(currentTime.valueOf() - startTime.valueOf()) / 1000,
);
const displayedSeconds = secondsElapsed % 60;
const displayedMinutes = Math.floor(secondsElapsed / 60) % 60;
const displayedHours = Math.floor(secondsElapsed / 60 / 60) % 24;
systray.sendAction({
type: 'update-item',
item: {
title: `Systray Test: Running ${displayedHours
.toString()
.padStart(2, '0')}:${displayedMinutes
.toString()
.padStart(2, '0')}:${displayedSeconds.toString().padStart(2, '0')}`,
tooltip: 'Systray Test is running...',
// checked is implement by plain text in linux
checked: false,
enabled: false,
},
seq_id: 0,
});
}
});