/
slider.go
272 lines (238 loc) · 6.03 KB
/
slider.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// SPDX-License-Identifier: Unlicense OR MIT
package cryptomaterial
import (
"image"
"image/color"
"gioui.org/gesture"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"github.com/crypto-power/cryptopower/ui/values"
)
type sliderItem struct {
widgetItem layout.Widget
button *Clickable
}
type Slider struct {
t *Theme
nextButton *Clickable
prevButton *Clickable
card Card
slideItems []*sliderItem
selected int
isSliderItemsSet bool
// colors of the indicator and navigation button
ButtonBackgroundColor color.NRGBA
IndicatorBackgroundColor color.NRGBA
SelectedIndicatorColor color.NRGBA // this is a full color no opacity
slideAction *SlideAction
clicker gesture.Click
clicked bool
disableButtonDirection bool
ControlInset layout.Inset
}
var m4 = values.MarginPadding4
func (t *Theme) Slider() *Slider {
sl := &Slider{
t: t,
slideItems: make([]*sliderItem, 0),
nextButton: t.NewClickable(false),
prevButton: t.NewClickable(false),
ButtonBackgroundColor: values.TransparentColor(values.TransparentWhite, 0.2),
IndicatorBackgroundColor: values.TransparentColor(values.TransparentWhite, 0.2),
SelectedIndicatorColor: t.Color.White,
slideAction: NewSlideAction(),
}
sl.ControlInset = layout.Inset{
Right: values.MarginPadding16,
Left: values.MarginPadding16,
Bottom: values.MarginPadding16,
}
sl.card = sl.t.Card()
sl.card.Radius = Radius(8)
sl.slideAction.Draged(func(dragDirection SwipeDirection) {
isNext := dragDirection == SwipeLeft
sl.handleActionEvent(isNext)
})
return sl
}
// GetSelectedIndex returns the index of the current slider item.
func (s *Slider) GetSelectedIndex() int {
return s.selected
}
func (s *Slider) SetDisableDirectionBtn(disable bool) {
s.disableButtonDirection = disable
}
func (s *Slider) sliderItems(items []layout.Widget) []*sliderItem {
slideItems := make([]*sliderItem, 0)
for _, item := range items {
slideItems = append(slideItems, &sliderItem{
widgetItem: item,
button: s.t.NewClickable(false),
})
}
return slideItems
}
func (s *Slider) Layout(gtx C, items []layout.Widget) D {
// set slider items once since layout is drawn multiple times per sec.
if !s.isSliderItemsSet {
s.slideItems = s.sliderItems(items)
s.isSliderItemsSet = true
}
if len(s.slideItems) == 0 {
return D{}
}
s.handleClickEvent(gtx)
var dims layout.Dimensions
var call op.CallOp
{
m := op.Record(gtx.Ops)
dims = s.slideAction.DragLayout(gtx, func(gtx C) D {
return layout.Stack{Alignment: layout.S}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return s.slideAction.TransformLayout(gtx, s.slideItems[s.selected].widgetItem)
}),
layout.Stacked(func(gtx C) D {
if len(s.slideItems) == 1 {
return D{}
}
return s.ControlInset.Layout(gtx, func(gtx C) D {
return layout.Flex{
Axis: layout.Horizontal,
}.Layout(gtx,
layout.Rigid(s.selectedItemIndicatorLayout),
layout.Flexed(1, func(gtx C) D {
if s.disableButtonDirection {
return D{}
}
return layout.E.Layout(gtx, s.buttonLayout)
}),
)
})
}),
)
})
call = m.Stop()
}
area := clip.Rect(image.Rect(0, 0, dims.Size.X, dims.Size.Y)).Push(gtx.Ops)
s.clicker.Add(gtx.Ops)
defer area.Pop()
call.Add(gtx.Ops)
return dims
}
func (s *Slider) buttonLayout(gtx C) D {
s.card.Radius = Radius(10)
s.card.Color = s.ButtonBackgroundColor
return s.containerLayout(gtx, func(gtx C) D {
return layout.Inset{
Right: m4,
Left: m4,
}.Layout(gtx, func(gtx C) D {
return LinearLayout{
Width: WrapContent,
Height: WrapContent,
Orientation: layout.Horizontal,
}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return s.prevButton.Layout(gtx, s.t.Icons.ChevronLeft.Layout20dp)
}),
layout.Rigid(func(gtx C) D {
return s.nextButton.Layout(gtx, s.t.Icons.ChevronRight.Layout20dp)
}),
)
})
})
}
func (s *Slider) selectedItemIndicatorLayout(gtx C) D {
m4 := values.MarginPadding4
s.card.Radius = Radius(10)
s.card.Color = s.IndicatorBackgroundColor
return s.containerLayout(gtx, func(gtx C) D {
return layout.Inset{
Right: m4,
Left: m4,
}.Layout(gtx, func(gtx C) D {
list := &layout.List{Axis: layout.Horizontal}
return list.Layout(gtx, len(s.slideItems), func(gtx C, i int) D {
ic := NewIcon(s.t.Icons.DotIcon)
ic.Color = values.TransparentColor(values.TransparentBlack, 0.2)
if i == s.selected {
ic.Color = s.SelectedIndicatorColor
}
return layout.Inset{
Top: m4,
Bottom: m4,
Right: m4,
Left: m4,
}.Layout(gtx, func(gtx C) D {
return s.slideItems[i].button.Layout(gtx, func(gtx C) D {
return ic.Layout(gtx, values.MarginPadding12)
})
})
})
})
})
}
func (s *Slider) containerLayout(gtx C, content layout.Widget) D {
return s.card.Layout(gtx, content)
}
func (s *Slider) RefreshItems() {
s.isSliderItemsSet = false
}
func (s *Slider) Clicked() bool {
clicked := s.clicked
s.clicked = false
return clicked
}
func (s *Slider) handleClickEvent(gtx C) {
if s.nextButton.Clicked() {
s.handleActionEvent(true)
}
if s.prevButton.Clicked() {
s.handleActionEvent(false)
}
for i, item := range s.slideItems {
if item.button.Clicked() {
if i == s.selected {
continue
}
lastSelected := s.selected
s.selected = i
if lastSelected < i {
s.slideAction.PushLeft()
} else {
s.slideAction.PushRight()
}
break
}
}
for _, events := range s.clicker.Events(gtx) {
switch events.Type {
case gesture.TypeClick:
if !s.clicked {
s.clicked = true
}
}
}
}
func (s *Slider) handleActionEvent(isNext bool) {
if len(s.slideItems) == 1 {
return
}
l := len(s.slideItems) - 1 // index starts at 0
if isNext {
if s.selected == l {
s.selected = 0
} else {
s.selected++
}
s.slideAction.PushLeft()
} else {
if s.selected == 0 {
s.selected = l
} else {
s.selected--
}
s.slideAction.PushRight()
}
}