-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (82 loc) · 2.09 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)
//go:embed notification.mp3
var notificationBytes []byte
var (
ui *UI
isBreak bool
breakNum int
sound *Sound
pref fyne.Preferences
)
func formatTime(tm time.Duration) string {
s := int(tm.Seconds())
minutes := int(s/60) % 60
seconds := int(s % 60)
return fmt.Sprintf("%02d:%02d", minutes, seconds)
}
func onMainTimerFinish() {
isBreak = !isBreak
newTime := parseTimeFromString(pref.StringWithFallback("timer", formatTime(DefaultSettings.timer)))
s := BackgroundColor
e := BackgroundColorBreak
// Update breaks guts.
if isBreak {
newTime = DefaultBreaks[breakNum]
ui.breaks[breakNum].enable()
breakNum = (breakNum + 1) % len(DefaultBreaks)
} else {
s = BackgroundColorBreak
e = BackgroundColor
}
// Update main timer.
ui.timer.timer.stop()
ui.timer.set(newTime)
ui.timer.update()
ui.bg.animate(s, e, BackgroundAnimationTime)
if pref.BoolWithFallback("auto-start", DefaultSettings.autoStartEnabled) {
ui.timer.started = true
ui.timer.update()
ui.timer.timer.countDown()
} else {
ui.timer.started = false
}
// Dim all the break widgets when we cycle back to the beginning.
if !isBreak && breakNum == 0 {
ui.disableBreaks()
}
if pref.BoolWithFallback("sound", DefaultSettings.soundEnabled) {
sound.play(sound.decodeFromBytes(notificationBytes))
}
}
func parseTimeFromString(s string) time.Duration {
res := strings.Split(s, ":")
min, _ := strconv.Atoi(res[0])
sec, _ := strconv.Atoi(res[1])
return time.Duration(min)*time.Minute + time.Duration(sec)*time.Second
}
func main() {
sound = new(Sound)
sound.init()
ui = new(UI)
ui.app = app.NewWithID("gopomodoro.preferences")
pref = ui.app.Preferences()
ui.app.Settings().SetTheme(&newTheme{})
ui.window = ui.app.NewWindow(WindowTitle)
ui.createTray()
ui.window.SetContent(ui.createContent())
ui.window.Resize(fyne.NewSize(WindowWidth, WindowHeight))
ui.window.SetMaster()
ui.window.CenterOnScreen()
ui.window.RequestFocus()
ui.window.SetFixedSize(true)
ui.window.ShowAndRun()
}