-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
135 lines (119 loc) · 2.82 KB
/
runner.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
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
131
132
133
134
135
package animation
import (
"sync"
"time"
"github.com/danielbaenabird/fyne/v2"
)
// Runner is the main driver for animations package
type Runner struct {
animationMutex sync.RWMutex
animations []*anim
pendingAnimations []*anim
runnerStarted bool
}
// Start will register the passed application and initiate its ticking.
func (r *Runner) Start(a *fyne.Animation) {
r.animationMutex.Lock()
defer r.animationMutex.Unlock()
if !r.runnerStarted {
r.runnerStarted = true
r.animations = append(r.animations, newAnim(a))
r.runAnimations()
} else {
r.pendingAnimations = append(r.pendingAnimations, newAnim(a))
}
}
// Stop causes an animation to stop ticking (if it was still running) and removes it from the runner.
func (r *Runner) Stop(a *fyne.Animation) {
r.animationMutex.Lock()
defer r.animationMutex.Unlock()
newList := make([]*anim, 0, len(r.animations))
stopped := false
for _, item := range r.animations {
if item.a != a {
newList = append(newList, item)
} else {
item.setStopped()
stopped = true
}
}
r.animations = newList
if stopped {
return
}
newList = make([]*anim, 0, len(r.pendingAnimations))
for _, item := range r.pendingAnimations {
if item.a != a {
newList = append(newList, item)
} else {
item.setStopped()
}
}
r.pendingAnimations = newList
}
func (r *Runner) runAnimations() {
draw := time.NewTicker(time.Second / 60)
go func() {
for done := false; !done; {
<-draw.C
r.animationMutex.Lock()
oldList := r.animations
r.animationMutex.Unlock()
newList := make([]*anim, 0, len(oldList))
for _, a := range oldList {
if !a.isStopped() && r.tickAnimation(a) {
newList = append(newList, a)
}
}
r.animationMutex.Lock()
r.animations = append(newList, r.pendingAnimations...)
r.pendingAnimations = nil
done = len(r.animations) == 0
r.animationMutex.Unlock()
}
r.animationMutex.Lock()
r.runnerStarted = false
r.animationMutex.Unlock()
draw.Stop()
}()
}
// tickAnimation will process a frame of animation and return true if this should continue animating
func (r *Runner) tickAnimation(a *anim) bool {
if time.Now().After(a.end) {
if a.reverse {
a.a.Tick(0.0)
if a.repeatsLeft == 0 {
return false
}
a.reverse = false
} else {
a.a.Tick(1.0)
if a.a.AutoReverse {
a.reverse = true
}
}
if !a.reverse {
if a.repeatsLeft == 0 {
return false
}
if a.repeatsLeft > 0 {
a.repeatsLeft--
}
}
a.start = time.Now()
a.end = a.start.Add(a.a.Duration)
return true
}
delta := time.Since(a.start).Nanoseconds() / 1000000 // TODO change this to Milliseconds() when we drop Go 1.12
val := float32(delta) / float32(a.total)
curve := a.a.Curve
if curve == nil {
curve = fyne.AnimationEaseInOut
}
if a.reverse {
a.a.Tick(curve(1 - val))
} else {
a.a.Tick(curve(val))
}
return true
}