-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_widget.go
88 lines (69 loc) · 1.43 KB
/
time_widget.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
package main
import (
"time"
"fyne.io/fyne/v2/canvas"
)
type TimeWidget struct {
widget *canvas.Text
timer *Timer
started bool
onFinish func()
}
func createTimeWidget(onFinish func(), startTime time.Duration) *TimeWidget {
tw := new(TimeWidget)
tw.widget = canvas.NewText("", TimerTextColor)
tw.widget.Text = TimerStartText
tw.widget.TextSize = TimerTextSize
tw.onFinish = onFinish
tw.timer = createTimer(tw.onTick, tw.onFinish)
tw.timer.set(startTime)
return tw
}
func (tw *TimeWidget) enable() {
if !tw.timer.paused {
return
}
tw.toggle()
}
func (tw *TimeWidget) start() {
tw.started = true
tw.update()
tw.timer.countDown()
}
func (tw *TimeWidget) restart(startTime time.Duration) {
tw.timer.stop()
tw.timer = createTimer(tw.onTick, tw.onFinish)
tw.timer.set(startTime)
tw.widget.Text = TimerStartText
tw.started = false
if tw.timer.paused {
tw.widget.Color = TimerTextColorPaused
} else {
tw.widget.Color = TimerTextColor
}
}
func (tw *TimeWidget) skip() {
tw.onFinish()
}
func (tw *TimeWidget) onTick() {
tw.update()
}
func (tw *TimeWidget) toggle() {
tw.timer.toggle()
if tw.timer.paused {
tw.widget.Color = TimerTextColorPaused
} else {
tw.widget.Color = TimerTextColor
}
tw.widget.Refresh()
}
func (tw *TimeWidget) set(tm time.Duration) {
tw.timer.set(tm)
if tw.started {
tw.update()
}
}
func (tw *TimeWidget) update() {
tw.widget.Text = formatTime(tw.timer.tl)
tw.widget.Refresh()
}