~whereswaldon/gio-x

gio-x/outlay/anim.go -rw-r--r-- 968 bytes
24857b8dChris Waldon component: document tooltip in README 19 hours ago
                                                                                
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
package outlay

import (
	"time"

	"gioui.org/layout"
	"gioui.org/op"
)

// Animation holds state for an Animation between two states that
// is not invertible.
type Animation struct {
	time.Duration
	StartTime time.Time
}

// Progress returns the current progress through the animation
// as a value in the range [0,1]
func (n *Animation) Progress(gtx layout.Context) float32 {
	if n.Duration == time.Duration(0) {
		return 0
	}
	progressDur := gtx.Now.Sub(n.StartTime)
	if progressDur > n.Duration {
		return 1
	}
	op.InvalidateOp{}.Add(gtx.Ops)
	progress := float32(progressDur.Milliseconds()) / float32(n.Duration.Milliseconds())
	return progress
}

func (n *Animation) Start(now time.Time) {
	n.StartTime = now
}

func (n *Animation) SetDuration(d time.Duration) {
	n.Duration = d
}

func (n *Animation) Animating(gtx layout.Context) bool {
	if n.Duration == 0 {
		return false
	}
	if gtx.Now.After(n.StartTime.Add(n.Duration)) {
		return false
	}
	return true
}