Skip to content

Commit

Permalink
rename anim.setStopFlag to anim.setStopped, call isStopped before tic…
Browse files Browse the repository at this point in the history
…king the animation, added early return in tickAnimation function, update tests
  • Loading branch information
fpabl0 authored and andydotxyz committed Feb 26, 2021
1 parent d983d57 commit b4e99a1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
3 changes: 1 addition & 2 deletions internal/animation/animation.go
Expand Up @@ -20,14 +20,13 @@ type anim struct {
}

func newAnim(a *fyne.Animation) *anim {
// TODO should we add a nil check here? (to avoid panic)
animate := &anim{a: a, start: time.Now(), end: time.Now().Add(a.Duration)}
animate.total = animate.end.Sub(animate.start).Nanoseconds() / 1000000 // TODO change this to Milliseconds() when we drop Go 1.12
animate.repeatsLeft = a.RepeatCount
return animate
}

func (a *anim) setStopFlag() {
func (a *anim) setStopped() {
a.mu.Lock()
a.stopped = true
a.mu.Unlock()
Expand Down
2 changes: 2 additions & 0 deletions internal/animation/animation_test.go
Expand Up @@ -86,6 +86,8 @@ func TestGLDriver_StopAnimationImmediatelyAndInsideTick(t *testing.T) {
run.Stop(c)

wg.Wait()
// animations stopped inside tick are really stopped in the next runner cycle
time.Sleep(time.Second/60 + 100*time.Millisecond)
run.animationMutex.RLock()
assert.Zero(t, len(run.animations))
run.animationMutex.RUnlock()
Expand Down
7 changes: 4 additions & 3 deletions internal/animation/runner.go
Expand Up @@ -41,7 +41,7 @@ func (r *Runner) Stop(a *fyne.Animation) {
if item.a != a {
newList = append(newList, item)
} else {
item.setStopFlag()
item.setStopped()
stopped = true
}
}
Expand All @@ -55,7 +55,7 @@ func (r *Runner) Stop(a *fyne.Animation) {
if item.a != a {
newList = append(newList, item)
} else {
item.setStopFlag()
item.setStopped()
}
}
r.pendingAnimations = newList
Expand All @@ -72,7 +72,7 @@ func (r *Runner) runAnimations() {
r.animationMutex.Unlock()
newList := make([]*anim, 0, len(oldList))
for _, a := range oldList {
if r.tickAnimation(a) && !a.isStopped() {
if !a.isStopped() && r.tickAnimation(a) {
newList = append(newList, a)
}
}
Expand Down Expand Up @@ -115,6 +115,7 @@ func (r *Runner) tickAnimation(a *anim) bool {

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
Expand Down

0 comments on commit b4e99a1

Please sign in to comment.