forked from marcetin/parallelcoin
/
slider.go
132 lines (115 loc) · 2.74 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
package p9
import (
"image"
"image/color"
"gioui.org/f32"
l "gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"github.com/p9c/pod/pkg/gui/f32color"
)
type Slider struct {
th *Theme
min, max float32
color color.NRGBA
float *Float
}
// Slider is for selecting a value in a range.
func (th *Theme) Slider() *Slider {
return &Slider{
th: th,
color: th.Colors.Get("Primary"),
}
}
// Min sets the value at the left hand side
func (s *Slider) Min(min float32) *Slider {
s.min = min
return s
}
// Max sets the value at the right hand side
func (s *Slider) Max(max float32) *Slider {
s.max = max
return s
}
// Color sets the color to draw the slider in
func (s *Slider) Color(color string) *Slider {
s.color = s.th.Colors.Get(color)
return s
}
// Float sets the initial value
func (s *Slider) Float(f *Float) *Slider {
s.float = f
return s
}
// Fn renders the slider
func (s *Slider) Fn(gtx l.Context) l.Dimensions {
thumbRadiusInt := gtx.Px(unit.Dp(6))
trackWidth := float32(gtx.Px(unit.Dp(2)))
thumbRadius := float32(thumbRadiusInt)
halfWidthInt := 2 * thumbRadiusInt
halfWidth := float32(halfWidthInt)
size := gtx.Constraints.Min
// Keep a minimum length so that the track is always visible.
minLength := halfWidthInt + 3*thumbRadiusInt + halfWidthInt
if size.X < minLength {
size.X = minLength
}
size.Y = 2 * halfWidthInt
st := op.Push(gtx.Ops)
op.Offset(f32.Pt(halfWidth, 0)).Add(gtx.Ops)
gtx.Constraints.Min = image.Pt(size.X-2*halfWidthInt, size.Y)
s.float.Fn(gtx, halfWidthInt, s.min, s.max)
thumbPos := halfWidth + s.float.Pos()
st.Pop()
color := s.color
if gtx.Queue == nil {
color = f32color.MulAlpha(color, 150)
}
// Draw track before thumb.
st = op.Push(gtx.Ops)
track := f32.Rectangle{
Min: f32.Point{
X: halfWidth,
Y: halfWidth - trackWidth/2,
},
Max: f32.Point{
X: thumbPos,
Y: halfWidth + trackWidth/2,
},
}
clip.RRect{Rect: track}.Add(gtx.Ops)
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
st.Pop()
// Draw track after thumb.
st = op.Push(gtx.Ops)
track.Min.X = thumbPos
track.Max.X = float32(size.X) - halfWidth
clip.RRect{Rect: track}.Add(gtx.Ops)
paint.ColorOp{Color: f32color.MulAlpha(color, 96)}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
st.Pop()
// Draw thumb.
st = op.Push(gtx.Ops)
thumb := f32.Rectangle{
Min: f32.Point{
X: thumbPos - thumbRadius,
Y: halfWidth - thumbRadius,
},
Max: f32.Point{
X: thumbPos + thumbRadius,
Y: halfWidth + thumbRadius,
},
}
rr := thumbRadius
clip.RRect{
Rect: thumb,
NE: rr, NW: rr, SE: rr, SW: rr,
}.Add(gtx.Ops)
paint.ColorOp{Color: color}.Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
st.Pop()
return l.Dimensions{Size: size}
}