-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (81 loc) · 1.82 KB
/
main.go
File metadata and controls
98 lines (81 loc) · 1.82 KB
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
package main
import (
"fmt"
"time"
"github.com/getlantern/systray"
)
var (
timerActive bool
workDuration = 25 * time.Minute
breakDuration = 5 * time.Minute
timeRemaining = workDuration
timerTicker *time.Ticker
currentMenuItem *systray.MenuItem
)
func main() {
systray.Run(onReady, onExit)
}
func onReady() {
systray.SetTitle("Pomodoro Timer")
systray.SetTooltip("Simple Pomodoro Timer")
startWork := systray.AddMenuItem("Start Work", "Start a work session")
stopWork := systray.AddMenuItem("Stop Work", "Stop the current work session")
startBreak := systray.AddMenuItem("Start Break", "Start a break session")
stopBreak := systray.AddMenuItem("Stop Break", "Stop the current break session")
exit := systray.AddMenuItem("Exit", "Exit the application")
for {
select {
case <-startWork.ClickedCh:
startTimer(workDuration)
currentMenuItem = startWork
case <-stopWork.ClickedCh:
stopTimer()
case <-startBreak.ClickedCh:
startTimer(breakDuration)
currentMenuItem = startBreak
case <-stopBreak.ClickedCh:
stopTimer()
case <-exit.ClickedCh:
systray.Quit()
return
}
}
}
func onExit() {
// Clean up here if needed
}
func startTimer(duration time.Duration) {
if timerActive {
return
}
timeRemaining = duration
timerTicker = time.NewTicker(1 * time.Second)
timerActive = true
go func() {
for range timerTicker.C {
if timerActive {
updateTimer()
}
}
}()
updateTimer()
}
func stopTimer() {
if !timerActive {
return
}
timerTicker.Stop()
timerActive = false
if currentMenuItem != nil {
currentMenuItem.Uncheck()
}
}
func updateTimer() {
minutes := int(timeRemaining.Minutes())
seconds := int(timeRemaining.Seconds()) % 60
systray.SetTitle(fmt.Sprintf("%02d:%02d", minutes, seconds))
if timeRemaining <= 0 {
stopTimer()
}
timeRemaining -= time.Second
}