Skip to content

Commit

Permalink
Don't store time function in each cursor animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 17, 2024
1 parent 5ee9979 commit 307be7d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/cache/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
skippedCleanWithCanvasRefresh = false

// testing purpose only
timeNow func() time.Time = time.Now
timeNow = time.Now
)

func init() {
Expand Down
13 changes: 6 additions & 7 deletions widget/entry_cursor_anim.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,23 @@ import (
"fyne.io/fyne/v2/theme"
)

var timeNow = time.Now // used in tests

const (
cursorInterruptTime = 300 * time.Millisecond
cursorFadeAlpha = uint8(0x16)
cursorFadeRatio = 0.1
)

type entryCursorAnimation struct {
mu *sync.RWMutex
mu sync.RWMutex
cursor *canvas.Rectangle
anim *fyne.Animation
lastInterruptTime time.Time

timeNow func() time.Time // useful for testing
}

func newEntryCursorAnimation(cursor *canvas.Rectangle) *entryCursorAnimation {
a := &entryCursorAnimation{mu: &sync.RWMutex{}, cursor: cursor, timeNow: time.Now}
return a
return &entryCursorAnimation{cursor: cursor}
}

// creates fyne animation
Expand All @@ -55,7 +54,7 @@ func (a *entryCursorAnimation) createAnim(inverted bool) *fyne.Animation {
interrupted := false
anim := fyne.NewAnimation(time.Second/2, func(f float32) {
a.mu.RLock()
shouldInterrupt := a.timeNow().Sub(a.lastInterruptTime) <= cursorInterruptTime
shouldInterrupt := timeNow().Sub(a.lastInterruptTime) <= cursorInterruptTime
a.mu.RUnlock()
if shouldInterrupt {
if !interrupted {
Expand Down Expand Up @@ -127,7 +126,7 @@ func (a *entryCursorAnimation) start() {
// temporarily stops the animation by "cursorInterruptTime".
func (a *entryCursorAnimation) interrupt() {
a.mu.Lock()
a.lastInterruptTime = a.timeNow()
a.lastInterruptTime = timeNow()
a.mu.Unlock()
}

Expand Down
6 changes: 3 additions & 3 deletions widget/entry_cursor_anim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestEntryCursorAnim(t *testing.T) {
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))

a.timeNow = func() time.Time {
timeNow = func() time.Time {
return time.Now().Add(cursorInterruptTime)
}
// animation should be restarted inverting the colors
Expand All @@ -54,12 +54,12 @@ func TestEntryCursorAnim(t *testing.T) {
a.anim.Tick(1.0)
assert.True(t, alphaEquals(cursorDim, a.cursor.FillColor))

a.timeNow = time.Now
timeNow = time.Now
a.interrupt()
a.anim.Tick(0.0)
assert.True(t, alphaEquals(cursorOpaque, a.cursor.FillColor))

a.timeNow = func() time.Time {
timeNow = func() time.Time {
return time.Now().Add(cursorInterruptTime)
}
a.anim.Tick(0.0)
Expand Down

0 comments on commit 307be7d

Please sign in to comment.