mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 09:25:38 +00:00
widget/material: [API] move widget.Float.{Axis,Invert} into material.SliderStyle
Signed-off-by: Gordon Klaus <gordon.klaus@gmail.com>
This commit is contained in:
+7
-9
@@ -13,9 +13,7 @@ import (
|
|||||||
|
|
||||||
// Float is for selecting a value in a range.
|
// Float is for selecting a value in a range.
|
||||||
type Float struct {
|
type Float struct {
|
||||||
Value float32
|
Value float32
|
||||||
Axis layout.Axis
|
|
||||||
Invert bool
|
|
||||||
|
|
||||||
drag gesture.Drag
|
drag gesture.Drag
|
||||||
pos float32 // position normalized to [0, 1]
|
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.
|
// 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.
|
// 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
|
size := gtx.Constraints.Min
|
||||||
f.length = float32(f.Axis.Convert(size).X)
|
f.length = float32(axis.Convert(size).X)
|
||||||
|
|
||||||
var de *pointer.Event
|
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 {
|
if e.Type == pointer.Press || e.Type == pointer.Drag {
|
||||||
de = &e
|
de = &e
|
||||||
}
|
}
|
||||||
@@ -43,10 +41,10 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
|
|||||||
value := f.Value
|
value := f.Value
|
||||||
if de != nil {
|
if de != nil {
|
||||||
xy := de.Position.X
|
xy := de.Position.X
|
||||||
if f.Axis == layout.Vertical {
|
if axis == layout.Vertical {
|
||||||
xy = f.length - de.Position.Y
|
xy = f.length - de.Position.Y
|
||||||
}
|
}
|
||||||
if f.Invert {
|
if invert {
|
||||||
xy = f.length - xy
|
xy = f.length - xy
|
||||||
}
|
}
|
||||||
f.pos = xy / f.length
|
f.pos = xy / f.length
|
||||||
@@ -63,7 +61,7 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
|
|||||||
f.pos = 1
|
f.pos = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
margin := f.Axis.Convert(image.Pt(pointerMargin, 0))
|
margin := axis.Convert(image.Pt(pointerMargin, 0))
|
||||||
rect := image.Rectangle{
|
rect := image.Rectangle{
|
||||||
Min: margin.Mul(-1),
|
Min: margin.Mul(-1),
|
||||||
Max: size.Add(margin),
|
Max: size.Add(margin),
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ func Slider(th *Theme, float *widget.Float, min, max float32) SliderStyle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SliderStyle struct {
|
type SliderStyle struct {
|
||||||
|
Axis layout.Axis
|
||||||
Min, Max float32
|
Min, Max float32
|
||||||
|
Invert bool
|
||||||
Color color.NRGBA
|
Color color.NRGBA
|
||||||
Float *widget.Float
|
Float *widget.Float
|
||||||
|
|
||||||
@@ -38,7 +40,7 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|||||||
thumbRadius := gtx.Dp(6)
|
thumbRadius := gtx.Dp(6)
|
||||||
trackWidth := gtx.Dp(2)
|
trackWidth := gtx.Dp(2)
|
||||||
|
|
||||||
axis := s.Float.Axis
|
axis := s.Axis
|
||||||
// Keep a minimum length so that the track is always visible.
|
// Keep a minimum length so that the track is always visible.
|
||||||
minLength := thumbRadius + 3*thumbRadius + thumbRadius
|
minLength := thumbRadius + 3*thumbRadius + thumbRadius
|
||||||
// Try to expand to finger size, but only if the constraints
|
// 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))
|
o := axis.Convert(image.Pt(thumbRadius, 0))
|
||||||
trans := op.Offset(o).Push(gtx.Ops)
|
trans := op.Offset(o).Push(gtx.Ops)
|
||||||
gtx.Constraints.Min = axis.Convert(image.Pt(sizeMain-2*thumbRadius, sizeCross))
|
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)))
|
gtx.Constraints.Min = gtx.Constraints.Min.Add(axis.Convert(image.Pt(0, sizeCross)))
|
||||||
thumbPos := thumbRadius + int(s.Float.Pos())
|
thumbPos := thumbRadius + int(s.Float.Pos())
|
||||||
trans.Pop()
|
trans.Pop()
|
||||||
@@ -63,7 +65,7 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
|
|||||||
|
|
||||||
rect := func(minx, miny, maxx, maxy int) image.Rectangle {
|
rect := func(minx, miny, maxx, maxy int) image.Rectangle {
|
||||||
r := image.Rect(minx, miny, maxx, maxy)
|
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.Max.X, r.Min.X = sizeMain-r.Min.X, sizeMain-r.Max.X
|
||||||
}
|
}
|
||||||
r.Min = axis.Convert(r.Min)
|
r.Min = axis.Convert(r.Min)
|
||||||
|
|||||||
Reference in New Issue
Block a user