-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (74 loc) · 1.76 KB
/
main.go
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
package main
import (
"fmt"
"log"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/getlantern/systray"
"github.com/markbates/pkger"
)
func main() {
systray.Run(onReady, onExit)
}
func playGong(times int) {
f, err := pkger.Open("/audio/gong.mp3")
if err != nil {
log.Fatal(err)
}
streamer, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
defer streamer.Close()
done := make(chan bool)
speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
for i := 0; i < times; i++ {
speaker.Play(beep.Seq(streamer, beep.Callback(func() {
done <- true
})))
<-done
streamer.Seek(0)
}
}
func onReady() {
systray.SetTitle("Ting Tong")
systray.SetTooltip("Go Ting Tong")
mTimeLabel := systray.AddMenuItem("0 mins", "0 mins")
systray.AddSeparator()
mBreak := systray.AddMenuItem("Start break timer", "Come on, life's short")
mQuitOrig := systray.AddMenuItem("Quit", "Quit the whole app")
counter := newCounter()
go counter.start(1 * time.Minute)
state := "working"
for {
select {
case count := <-counter.count:
if state == "working" {
title := fmt.Sprintf("Working for %d mins", count)
mTimeLabel.SetTitle(title)
if (count % 60) == 0 {
go playGong(count / 60)
}
} else if state == "resting" {
title := fmt.Sprintf("Resting for %d mins", count)
mTimeLabel.SetTitle(title)
}
case <-mTimeLabel.ClickedCh:
mTimeLabel.SetTitle("Working for 0 mins")
state = "working"
counter.reset <- true
case <-mBreak.ClickedCh:
mTimeLabel.SetTitle("Resting for 0 mins")
state = "resting"
counter.reset <- true
case <-mQuitOrig.ClickedCh:
counter.done <- true
systray.Quit()
}
}
}
func onExit() {
fmt.Println("On exit")
}