-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
slider.go
344 lines (280 loc) · 7.47 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package widget
import (
"fmt"
"math"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
// Orientation controls the horizontal/vertical layout of a widget
type Orientation int
// Orientation constants to control widget layout
const (
Horizontal Orientation = 0
Vertical Orientation = 1
)
var _ fyne.Draggable = (*Slider)(nil)
// Slider is a widget that can slide between two fixed values.
type Slider struct {
BaseWidget
Value float64
Min float64
Max float64
Step float64
Orientation Orientation
OnChanged func(float64)
valueSource binding.Float
valueListener binding.DataListener
}
// NewSlider returns a basic slider.
func NewSlider(min, max float64) *Slider {
slider := &Slider{
Value: 0,
Min: min,
Max: max,
Step: 1,
Orientation: Horizontal,
}
slider.ExtendBaseWidget(slider)
return slider
}
// NewSliderWithData returns a slider connected with the specified data source.
//
// Since: 2.0
func NewSliderWithData(min, max float64, data binding.Float) *Slider {
slider := NewSlider(min, max)
slider.Bind(data)
return slider
}
// Bind connects the specified data source to this Slider.
// The current value will be displayed and any changes in the data will cause the widget to update.
// User interactions with this Slider will set the value into the data source.
//
// Since: 2.0
func (s *Slider) Bind(data binding.Float) {
s.Unbind()
s.valueSource = data
s.valueListener = binding.NewDataListener(func() {
val, err := data.Get()
if err != nil {
fyne.LogError("Error getting current data value", err)
return
}
s.Value = val
if cache.IsRendered(s) { // don't invalidate values set after constructor like Step
s.Refresh()
}
})
data.AddListener(s.valueListener)
s.OnChanged = func(f float64) {
err := data.Set(f)
if err != nil {
fyne.LogError(fmt.Sprintf("Failed to set binding value to %f", f), err)
}
}
}
// DragEnd function.
func (s *Slider) DragEnd() {
}
// Dragged function.
func (s *Slider) Dragged(e *fyne.DragEvent) {
ratio := s.getRatio(&(e.PointEvent))
lastValue := s.Value
s.updateValue(ratio)
if lastValue == s.Value {
return
}
s.Refresh()
if s.OnChanged != nil {
s.OnChanged(s.Value)
}
}
func (s *Slider) buttonDiameter() float32 {
return theme.Padding() * standardScale
}
func (s *Slider) endOffset() float32 {
return s.buttonDiameter()/2 + theme.Padding()
}
func (s *Slider) getRatio(e *fyne.PointEvent) float64 {
pad := s.endOffset()
x := e.Position.X
y := e.Position.Y
switch s.Orientation {
case Vertical:
if y > s.size.Height-pad {
return 0.0
} else if y < pad {
return 1.0
} else {
return 1 - float64(y-pad)/float64(s.size.Height-pad*2)
}
case Horizontal:
if x > s.size.Width-pad {
return 1.0
} else if x < pad {
return 0.0
} else {
return float64(x-pad) / float64(s.size.Width-pad*2)
}
}
return 0.0
}
func (s *Slider) clampValueToRange() {
if s.Value >= s.Max {
s.Value = s.Max
return
} else if s.Value <= s.Min {
s.Value = s.Min
return
}
if s.Step == 0 { // extended Slider may not have this set - assume value is not adjusted
return
}
i := -(math.Log10(s.Step))
p := math.Pow(10, i)
s.Value = float64(int(s.Value*p)) / p
}
func (s *Slider) updateValue(ratio float64) {
s.Value = s.Min + ratio*(s.Max-s.Min)
s.clampValueToRange()
}
// SetValue updates the value of the slider and clamps the value to be within the range.
func (s *Slider) SetValue(value float64) {
if s.Value == value {
return
}
lastValue := s.Value
s.Value = value
s.clampValueToRange()
if lastValue == s.Value {
return
}
if s.OnChanged != nil {
s.OnChanged(s.Value)
}
s.Refresh()
}
// MinSize returns the size that this widget should not shrink below
func (s *Slider) MinSize() fyne.Size {
s.ExtendBaseWidget(s)
return s.BaseWidget.MinSize()
}
// CreateRenderer links this widget to its renderer.
func (s *Slider) CreateRenderer() fyne.WidgetRenderer {
s.ExtendBaseWidget(s)
track := canvas.NewRectangle(theme.ShadowColor())
active := canvas.NewRectangle(theme.ForegroundColor())
thumb := &canvas.Circle{
FillColor: theme.ForegroundColor(),
StrokeWidth: 0}
objects := []fyne.CanvasObject{track, active, thumb}
slide := &sliderRenderer{widget.NewBaseRenderer(objects), track, active, thumb, s}
slide.Refresh() // prepare for first draw
return slide
}
// Unbind disconnects any configured data source from this Slider.
// The current value will remain at the last value of the data source.
//
// Since: 2.0
func (s *Slider) Unbind() {
s.OnChanged = nil
if s.valueSource == nil || s.valueListener == nil {
return
}
s.valueSource.RemoveListener(s.valueListener)
s.valueListener = nil
s.valueSource = nil
}
const (
standardScale = float32(4)
minLongSide = float32(50)
)
type sliderRenderer struct {
widget.BaseRenderer
track *canvas.Rectangle
active *canvas.Rectangle
thumb *canvas.Circle
slider *Slider
}
// Refresh updates the widget state for drawing.
func (s *sliderRenderer) Refresh() {
s.track.FillColor = theme.ShadowColor()
s.thumb.FillColor = theme.ForegroundColor()
s.active.FillColor = theme.ForegroundColor()
s.slider.clampValueToRange()
s.Layout(s.slider.Size())
canvas.Refresh(s.slider.super())
}
// Layout the components of the widget.
func (s *sliderRenderer) Layout(size fyne.Size) {
trackWidth := theme.Padding()
diameter := s.slider.buttonDiameter()
endPad := s.slider.endOffset()
var trackPos, activePos, thumbPos fyne.Position
var trackSize, activeSize fyne.Size
// some calculations are relative to trackSize, so we must update that first
switch s.slider.Orientation {
case Vertical:
trackPos = fyne.NewPos(size.Width/2, endPad)
trackSize = fyne.NewSize(trackWidth, size.Height-endPad*2)
case Horizontal:
trackPos = fyne.NewPos(endPad, size.Height/2)
trackSize = fyne.NewSize(size.Width-endPad*2, trackWidth)
}
s.track.Move(trackPos)
s.track.Resize(trackSize)
activeOffset := s.getOffset() // TODO based on old size...0
switch s.slider.Orientation {
case Vertical:
activePos = fyne.NewPos(trackPos.X, activeOffset)
activeSize = fyne.NewSize(trackWidth, trackSize.Height-activeOffset+endPad)
thumbPos = fyne.NewPos(
trackPos.X-(diameter-trackSize.Width)/2, activeOffset-((diameter-theme.Padding())/2))
case Horizontal:
activePos = trackPos
activeSize = fyne.NewSize(activeOffset-endPad, trackWidth)
thumbPos = fyne.NewPos(
activeOffset-((diameter-theme.Padding())/2), trackPos.Y-(diameter-trackSize.Height)/2)
}
s.active.Move(activePos)
s.active.Resize(activeSize)
s.thumb.Move(thumbPos)
s.thumb.Resize(fyne.NewSize(diameter, diameter))
}
// MinSize calculates the minimum size of a widget.
func (s *sliderRenderer) MinSize() fyne.Size {
s1, s2 := minLongSide, s.slider.buttonDiameter()
switch s.slider.Orientation {
case Vertical:
return fyne.NewSize(s2, s1)
case Horizontal:
return fyne.NewSize(s1, s2)
}
return fyne.Size{Width: 0, Height: 0}
}
func (s *sliderRenderer) getOffset() float32 {
endPad := s.slider.endOffset()
w := s.slider
size := s.track.Size()
if w.Value == w.Min || w.Min == w.Max {
switch w.Orientation {
case Vertical:
return size.Height + endPad
case Horizontal:
return endPad
}
}
ratio := float32((w.Value - w.Min) / (w.Max - w.Min))
switch w.Orientation {
case Vertical:
y := size.Height - ratio*size.Height + endPad
return y
case Horizontal:
x := ratio*size.Width + endPad
return x
}
return endPad
}