/
slide_action.go
218 lines (188 loc) · 4.36 KB
/
slide_action.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cryptomaterial
import (
"image"
"time"
"gioui.org/f32"
"gioui.org/gesture"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
)
const (
defaultDuration = 500 * time.Millisecond
defaultdragEffect = 100
)
type SwipeDirection int
const (
SwipeLeft SwipeDirection = iota
SwipeRight
)
type Dragged func(dragDirection SwipeDirection)
type SlideAction struct {
Duration time.Duration
IsReverse bool
push int
next *op.Ops
nextCall op.CallOp
lastCall op.CallOp
t0 time.Time
offset float32
// animation state
dragEffect int
dragStarted f32.Point
dragOffset int
drag gesture.Drag
draged Dragged
isPushing bool
}
func NewSlideAction() *SlideAction {
return &SlideAction{
Duration: defaultDuration,
dragEffect: defaultdragEffect,
}
}
// PushLeft pushes the existing widget to the left.
func (s *SlideAction) PushLeft() { s.push = 1 }
// PushRight pushes the existing widget to the right.
func (s *SlideAction) PushRight() { s.push = -1 }
func (s *SlideAction) SetDragEffect(offset int) { s.dragEffect = offset }
func (s *SlideAction) Draged(drag Dragged) {
s.draged = drag
}
func (s *SlideAction) DragLayout(gtx C, w layout.Widget) D {
if gtx.Queue != nil {
for _, event := range s.drag.Events(gtx.Metric, gtx.Queue, gesture.Horizontal) {
switch event.Type {
case pointer.Press:
s.dragStarted = event.Position
s.dragOffset = 0
case pointer.Drag:
newOffset := int(s.dragStarted.X - event.Position.X)
if newOffset > s.dragEffect {
if !s.isPushing && s.draged != nil {
s.isPushing = true
s.draged(SwipeLeft)
}
} else if newOffset < -s.dragEffect {
if !s.isPushing && s.draged != nil {
s.isPushing = true
s.draged(SwipeRight)
}
}
s.dragOffset = newOffset
case pointer.Release:
fallthrough
case pointer.Cancel:
s.isPushing = false
}
}
}
var dims layout.Dimensions
var call op.CallOp
{
m := op.Record(gtx.Ops)
dims = w(gtx)
call = m.Stop()
}
area := clip.Rect(image.Rect(0, 0, dims.Size.X, dims.Size.Y)).Push(gtx.Ops)
s.drag.Add(gtx.Ops)
defer area.Pop()
call.Add(gtx.Ops)
return dims
}
// TransformLayout perform transition effects between 2 widgets
func (s *SlideAction) TransformLayout(gtx C, w layout.Widget) D {
if s.push != 0 {
s.next = nil
s.lastCall = s.nextCall
s.offset = float32(s.push)
s.t0 = gtx.Now
s.push = 0
}
var delta time.Duration
if !s.t0.IsZero() {
now := gtx.Now
delta = now.Sub(s.t0)
s.t0 = now
}
// Calculate the duration of transition effects
if s.offset != 0 {
duration := s.Duration
if duration == 0 {
duration = defaultDuration
}
movement := float32(delta.Seconds()) / float32(duration.Seconds())
if s.offset < 0 {
s.offset += movement
if s.offset >= 0 {
s.offset = 0
}
} else {
s.offset -= movement
if s.offset <= 0 {
s.offset = 0
}
}
op.InvalidateOp{}.Add(gtx.Ops)
}
// Record the widget presentation
var dims layout.Dimensions
{
if s.next == nil {
s.next = new(op.Ops)
}
gtx := gtx
gtx.Ops = s.next
gtx.Ops.Reset()
m := op.Record(gtx.Ops)
dims = w(gtx)
s.nextCall = m.Stop()
}
if s.offset == 0 {
s.nextCall.Add(gtx.Ops)
return dims
}
offset := smooth(s.offset)
reverse := 1
if s.IsReverse {
reverse = -1
}
// Implement transition effects for widgets
if s.offset > 0 {
defer op.Offset(image.Point{
X: int(float32(dims.Size.X)*(offset-1)) * reverse,
}).Push(gtx.Ops).Pop()
s.lastCall.Add(gtx.Ops)
defer op.Offset(image.Point{
X: dims.Size.X * reverse,
}).Push(gtx.Ops).Pop()
s.nextCall.Add(gtx.Ops)
} else {
defer op.Offset(image.Point{
X: int(float32(dims.Size.X)*(offset+1)) * reverse,
}).Push(gtx.Ops).Pop()
s.lastCall.Add(gtx.Ops)
defer op.Offset(image.Point{
X: -dims.Size.X * reverse,
}).Push(gtx.Ops).Pop()
s.nextCall.Add(gtx.Ops)
}
return dims
}
// smooth handles -1 to 1 with ease-in-out cubic easing func.
func smooth(t float32) float32 {
if t < 0 {
return -easeInOutCubic(-t)
}
return easeInOutCubic(t)
}
// easeInOutCubic maps a linear value to a ease-in-out-cubic easing function.
// It is a mathematical function that describes how a value changes over time.
// It can be applied to adjusting the speed of animation
func easeInOutCubic(t float32) float32 {
if t < 0.5 {
return 4 * t * t * t
}
return (t-1)*(2*t-2)*(2*t-2) + 1
}