-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
130 lines (111 loc) · 2.71 KB
/
main.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
import path from "path";
import {
app,
Menu,
Tray,
Notification,
MenuItemConstructorOptions,
nativeTheme,
} from "electron";
const appName = "52-17";
const minutesPerWorkSession = 52;
const minutesPerBreak = 17;
const msPerMinute = 60000;
const isWindows = process.platform === "win32";
const defaultMenuItems: MenuItemConstructorOptions[] = [
{
label: "Start working",
click: startWorking,
},
{
label: "Start break",
click: startBreak,
},
{
type: "separator",
},
{
label: "Quit",
click: () => app.quit(),
},
];
let tray: Tray;
let currentTimer: NodeJS.Timer;
app.whenReady().then(startApplication);
function startApplication(): void {
if (app.dock) {
app.dock.hide();
}
tray = new Tray(getTrayIconPath());
tray.setToolTip(appName);
updateTrayMenu(defaultMenuItems);
nativeTheme.on("updated", () => tray.setImage(getTrayIconPath()));
if (isWindows) {
tray.on("click", () => tray.popUpContextMenu());
app.setAppUserModelId(appName);
}
}
function updateTrayMenu(menuItems: MenuItemConstructorOptions[]): void {
tray.setContextMenu(Menu.buildFromTemplate(menuItems));
}
function getTrayIconPath(): string {
const extension = isWindows ? "ico" : "png";
const theme = nativeTheme.shouldUseDarkColors ? "light" : "dark";
return path.join(__dirname, "..", "images", `icon-${theme}.${extension}`);
}
function startWorking(): void {
createNewTimer(
(timeString) => `Get your work done! Next break at ${timeString}.`,
minutesPerWorkSession,
startBreak
);
}
function startBreak(): void {
createNewTimer(
(timeString) => `Take a break! Work starts again at ${timeString}.`,
minutesPerBreak,
startWorking
);
}
function createNewTimer(
getMessage: (time: string) => string,
minutes: number,
timerCallback: () => void
) {
clearInterval(currentTimer);
const now = new Date();
const endTime = new Date(
now.getTime() - now.getSeconds() + minutes * msPerMinute
);
const timeString = getFormattedTime(endTime);
const message = getMessage(timeString);
updateTrayMenu([
{
label: message,
enabled: false,
},
{ type: "separator" },
{ label: "Stop timer", click: stopTimer },
{ type: "separator" },
...defaultMenuItems,
]);
new Notification({
title: message,
}).show();
currentTimer = setInterval(() => {
if (new Date() >= endTime) {
timerCallback();
}
}, 1000);
}
function getFormattedTime(date: Date): string {
return date.toLocaleTimeString(undefined, {
hour: "2-digit",
minute: "2-digit",
});
}
function stopTimer(): void {
clearInterval(currentTimer);
updateTrayMenu(defaultMenuItems);
new Notification({ title: "Timer stopped" }).show();
}