From db6b4de0f71bbde5ff84973c064ce72f7c17a89e Mon Sep 17 00:00:00 2001 From: Gordon Klaus Date: Tue, 24 Jan 2023 08:04:45 +0100 Subject: [PATCH] widget/material: [API] move widget.Float.{Axis,Invert} into material.SliderStyle Signed-off-by: Gordon Klaus --- widget/float.go | 16 +++++++--------- widget/material/slider.go | 8 +++++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/widget/float.go b/widget/float.go index 1581e7de..f9bb9d34 100644 --- a/widget/float.go +++ b/widget/float.go @@ -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), diff --git a/widget/material/slider.go b/widget/material/slider.go index d05d2dd9..d5783f34 100644 --- a/widget/material/slider.go +++ b/widget/material/slider.go @@ -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)