/
animation.go
143 lines (122 loc) · 3.7 KB
/
animation.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
136
137
138
139
140
141
142
143
package tutorials
import (
"image/color"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func makeAnimationScreen(_ fyne.Window) fyne.CanvasObject {
curves := makeAnimationCurves()
curves.Move(fyne.NewPos(0, 140+theme.Padding()))
return fyne.NewContainerWithoutLayout(makeAnimationCanvas(), curves)
}
func makeAnimationCanvas() fyne.CanvasObject {
rect := canvas.NewRectangle(color.Black)
rect.Resize(fyne.NewSize(410, 140))
a := canvas.NewColorRGBAAnimation(theme.PrimaryColorNamed(theme.ColorBlue), theme.PrimaryColorNamed(theme.ColorGreen),
time.Second*3, func(c color.Color) {
rect.FillColor = c
canvas.Refresh(rect)
})
a.RepeatCount = fyne.AnimationRepeatForever
a.AutoReverse = true
a.Start()
var a2 *fyne.Animation
i := widget.NewIcon(theme.CheckButtonCheckedIcon())
a2 = canvas.NewPositionAnimation(fyne.NewPos(0, 0), fyne.NewPos(350, 80), time.Second*3, func(p fyne.Position) {
i.Move(p)
width := 10 + (p.X / 7)
i.Resize(fyne.NewSize(width, width))
})
a2.RepeatCount = fyne.AnimationRepeatForever
a2.AutoReverse = true
a2.Curve = fyne.AnimationLinear
a2.Start()
running := true
var toggle *widget.Button
toggle = widget.NewButton("Stop", func() {
if running {
a.Stop()
a2.Stop()
toggle.SetText("Start")
} else {
a.Start()
a2.Start()
toggle.SetText("Stop")
}
running = !running
})
toggle.Resize(toggle.MinSize())
toggle.Move(fyne.NewPos(152, 54))
return fyne.NewContainerWithoutLayout(rect, i, toggle)
}
func makeAnimationCurves() fyne.CanvasObject {
label1, box1, a1 := makeAnimationCurveItem("EaseInOut", fyne.AnimationEaseInOut, 0)
label2, box2, a2 := makeAnimationCurveItem("EaseIn", fyne.AnimationEaseIn, 30+theme.Padding())
label3, box3, a3 := makeAnimationCurveItem("EaseOut", fyne.AnimationEaseOut, 60+theme.Padding()*2)
label4, box4, a4 := makeAnimationCurveItem("Linear", fyne.AnimationLinear, 90+theme.Padding()*3)
start := widget.NewButton("Compare", func() {
a1.Start()
a2.Start()
a3.Start()
a4.Start()
})
start.Resize(start.MinSize())
start.Move(fyne.NewPos(0, 120+theme.Padding()*4))
return fyne.NewContainerWithoutLayout(label1, label2, label3, label4, box1, box2, box3, box4, start)
}
func makeAnimationCurveItem(label string, curve fyne.AnimationCurve, yOff float32) (
text *widget.Label, box fyne.CanvasObject, anim *fyne.Animation) {
text = widget.NewLabel(label)
text.Alignment = fyne.TextAlignCenter
text.Resize(fyne.NewSize(380, 30))
text.Move(fyne.NewPos(0, yOff))
box = newThemedBox()
box.Resize(fyne.NewSize(30, 30))
box.Move(fyne.NewPos(0, yOff))
anim = canvas.NewPositionAnimation(
fyne.NewPos(0, yOff), fyne.NewPos(380, yOff), time.Second, func(p fyne.Position) {
box.Move(p)
box.Refresh()
})
anim.Curve = curve
anim.AutoReverse = true
anim.RepeatCount = 1
return
}
// themedBox is a simple box that change its background color according
// to the selected theme
type themedBox struct {
widget.BaseWidget
}
func newThemedBox() *themedBox {
b := &themedBox{}
b.ExtendBaseWidget(b)
return b
}
func (b *themedBox) CreateRenderer() fyne.WidgetRenderer {
b.ExtendBaseWidget(b)
bg := canvas.NewRectangle(theme.ForegroundColor())
return &themedBoxRenderer{bg: bg, objects: []fyne.CanvasObject{bg}}
}
type themedBoxRenderer struct {
bg *canvas.Rectangle
objects []fyne.CanvasObject
}
func (r *themedBoxRenderer) Destroy() {
}
func (r *themedBoxRenderer) Layout(size fyne.Size) {
r.bg.Resize(size)
}
func (r *themedBoxRenderer) MinSize() fyne.Size {
return r.bg.MinSize()
}
func (r *themedBoxRenderer) Objects() []fyne.CanvasObject {
return r.objects
}
func (r *themedBoxRenderer) Refresh() {
r.bg.FillColor = theme.ForegroundColor()
r.bg.Refresh()
}