Hmm probably not enough. Let's leave it out for now. Sending a patch for this.
Le mer. 13 janv. 2021 à 19:52, Elias Naur <mail@eliasnaur.com> a écrit :
From: pierre <pierre.curto@gmail.com>
Adding an axis to the Float widget, allows positioning the Slider one not only horizontally but also vertically.
Also update the fill ops while there.
Signed-off-by: pierre <pierre.curto@gmail.com>
---
widget/float.go | 27 +++++++---widget/material/slider.go | 109 ++++++++++++++++++++------------------
2 files changed, 78 insertions(+), 58 deletions(-)
diff --git a/widget/float.go b/widget/float.go
index 3c59a26..a467646 100644
--- a/widget/float.go+++ b/widget/float.go
@@ -14,6 +14,7 @@ import (
// Float is for selecting a value in a range.
type Float struct {
Value float32
+ Axis layout.Axis drag gesture.Drag
pos float32 // position normalized to [0, 1]
@@ -24,13 +25,15 @@ type Float struct {
// Dragging returns whether the value is being interacted with.
func (f *Float) Dragging() bool { return f.drag.Dragging() }
-// Layout processes events.+// 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 {
size := gtx.Constraints.Min
- f.length = float32(size.X)+ f.length = float32(f.Axis.Main(size)) var de *pointer.Event
- for _, e := range f.drag.Events(gtx.Metric, gtx, gesture.Horizontal) {+ for _, e := range f.drag.Events(gtx.Metric, gtx, gesture.Axis(f.Axis)) { if e.Type == pointer.Press || e.Type == pointer.Drag {
de = &e
}
@@ -38,7 +41,11 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
value := f.Value
if de != nil {
- f.pos = de.Position.X / f.length+ xy := de.Position.X+ if f.Axis == layout.Vertical {+ xy = de.Position.Y+ }+ f.pos = xy / f.length value = min + (max-min)*f.pos
} else if min != max {
f.pos = value/(max-min) - min
@@ -54,8 +61,9 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
defer op.Save(gtx.Ops).Load()
rect := image.Rectangle{Max: size}
- rect.Min.X -= pointerMargin- rect.Max.X += pointerMargin+ margin := axisPoint(f.Axis, pointerMargin, 0)+ rect.Min = rect.Min.Sub(margin)+ rect.Max = rect.Max.Add(margin) pointer.Rect(rect).Add(gtx.Ops)
f.drag.Add(gtx.Ops)
@@ -89,3 +97,10 @@ func (f *Float) Changed() bool {
f.changed = false
return changed
}
++func axisPoint(a layout.Axis, x, y int) image.Point {+ if a == layout.Horizontal {+ return image.Pt(x, y)+ }+ return image.Pt(y, x)+}
Let's fold this function into layout.Axis, as follows (in a separate
change).
Replace Axis.Cross, Axis.Main, Axis.point with a single method, Convert:
// Convert a point in (x, y) coordinates to (main, cross) coordinates,
// or vice versa. Specifically, Convert((x, y)) returns (x, y) unchanged
// for the Horizontal axis, or (y, x) for the vertical axis.
func (a Axis) Point(pt image.Point) image.Point {
if a == Horizontal {
return pt
}
return image.Pt(pt.Y, py.X)
}