From b4e99a18186dfa8ac787ec7fdc0ee8360bd252f4 Mon Sep 17 00:00:00 2001 From: FPabl0 Date: Sun, 21 Feb 2021 10:05:57 -0500 Subject: [PATCH] rename anim.setStopFlag to anim.setStopped, call isStopped before ticking the animation, added early return in tickAnimation function, update tests --- internal/animation/animation.go | 3 +-- internal/animation/animation_test.go | 2 ++ internal/animation/runner.go | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/animation/animation.go b/internal/animation/animation.go index 539cff204f..9c4656bf65 100644 --- a/internal/animation/animation.go +++ b/internal/animation/animation.go @@ -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() diff --git a/internal/animation/animation_test.go b/internal/animation/animation_test.go index 266c8f0d1f..61f90bfd96 100644 --- a/internal/animation/animation_test.go +++ b/internal/animation/animation_test.go @@ -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() diff --git a/internal/animation/runner.go b/internal/animation/runner.go index 434b893ba6..764b062d53 100644 --- a/internal/animation/runner.go +++ b/internal/animation/runner.go @@ -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 } } @@ -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 @@ -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) } } @@ -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