M widget/float.go => widget/float.go +7 -9
@@ 13,9 13,7 @@ import (
// Float is for selecting a value in a range.
type Float struct {
- Value float32
- Axis layout.Axis
- Invert bool
+ Value float32
drag gesture.Drag
pos float32 // position normalized to [0, 1]
@@ 29,12 27,12 @@ func (f *Float) Dragging() bool { return f.drag.Dragging() }
// Layout updates the value according to drag events along the f's main axis.
//
// The range of f is set by the minimum constraints main axis value.
-func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32) layout.Dimensions {
+func (f *Float) Layout(gtx layout.Context, axis layout.Axis, min, max float32, invert bool, pointerMargin int) layout.Dimensions {
size := gtx.Constraints.Min
- f.length = float32(f.Axis.Convert(size).X)
+ f.length = float32(axis.Convert(size).X)
var de *pointer.Event
- for _, e := range f.drag.Events(gtx.Metric, gtx, gesture.Axis(f.Axis)) {
+ for _, e := range f.drag.Events(gtx.Metric, gtx, gesture.Axis(axis)) {
if e.Type == pointer.Press || e.Type == pointer.Drag {
de = &e
}
@@ 43,10 41,10 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
value := f.Value
if de != nil {
xy := de.Position.X
- if f.Axis == layout.Vertical {
+ if axis == layout.Vertical {
xy = f.length - de.Position.Y
}
- if f.Invert {
+ if invert {
xy = f.length - xy
}
f.pos = xy / f.length
@@ 63,7 61,7 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
f.pos = 1
}
- margin := f.Axis.Convert(image.Pt(pointerMargin, 0))
+ margin := axis.Convert(image.Pt(pointerMargin, 0))
rect := image.Rectangle{
Min: margin.Mul(-1),
Max: size.Add(margin),
M widget/material/slider.go => widget/material/slider.go +5 -3
@@ 27,7 27,9 @@ func Slider(th *Theme, float *widget.Float, min, max float32) SliderStyle {
}
type SliderStyle struct {
+ Axis layout.Axis
Min, Max float32
+ Invert bool
Color color.NRGBA
Float *widget.Float
@@ 38,7 40,7 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
thumbRadius := gtx.Dp(6)
trackWidth := gtx.Dp(2)
- axis := s.Float.Axis
+ axis := s.Axis
// Keep a minimum length so that the track is always visible.
minLength := thumbRadius + 3*thumbRadius + thumbRadius
// Try to expand to finger size, but only if the constraints
@@ 51,7 53,7 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
o := axis.Convert(image.Pt(thumbRadius, 0))
trans := op.Offset(o).Push(gtx.Ops)
gtx.Constraints.Min = axis.Convert(image.Pt(sizeMain-2*thumbRadius, sizeCross))
- s.Float.Layout(gtx, thumbRadius, s.Min, s.Max)
+ s.Float.Layout(gtx, axis, s.Min, s.Max, s.Invert, thumbRadius)
gtx.Constraints.Min = gtx.Constraints.Min.Add(axis.Convert(image.Pt(0, sizeCross)))
thumbPos := thumbRadius + int(s.Float.Pos())
trans.Pop()
@@ 63,7 65,7 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
rect := func(minx, miny, maxx, maxy int) image.Rectangle {
r := image.Rect(minx, miny, maxx, maxy)
- if s.Float.Invert != (axis == layout.Vertical) {
+ if s.Invert != (axis == layout.Vertical) {
r.Max.X, r.Min.X = sizeMain-r.Min.X, sizeMain-r.Max.X
}
r.Min = axis.Convert(r.Min)