~pierrec/giox

790e8d9e708a370aa9b6c5c24d86553737fd8f24 — pierre 8 months ago 2b1e5bc
widgetx: removed Min and Max in Resizable since the ratio is always between 0 and 1

Signed-off-by: pierre <pierre.curto@gmail.com>
2 files changed, 8 insertions(+), 32 deletions(-)

M widgetx/materialx/resizable.go
M widgetx/resizable.go
M widgetx/materialx/resizable.go => widgetx/materialx/resizable.go +1 -1
@@ 9,8 9,8 @@ import (
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/widget/material"
	"git.sr.ht/~pierrec/giox/layoutx"

	"git.sr.ht/~pierrec/giox/layoutx"
	"git.sr.ht/~pierrec/giox/widgetx"
)


M widgetx/resizable.go => widgetx/resizable.go +7 -31
@@ 15,11 15,7 @@ type Resizable struct {
	// Ratio defines the ratio between the current position
	// of the handle and the widget main axis. Ratio is in [0, 1].
	Ratio float32
	// Min and Max specify the minimum and maximum values for the Ratio.
	// Zero means no minimum or maximum.
	Min  float32
	Max  float32
	drag gesture.Drag
	drag  gesture.Drag
}

// Lay out the handle and make it draggable. Dragging actions update the Ratio value.


@@ 47,21 43,12 @@ func (rs *Resizable) Layout(gtx layout.Context, handle layout.Widget) layout.Dim
			a, b = pos.Y, float32(gtx.Constraints.Max.Y)
			pos.X = 0
		}
		// In case the min and max have changed, keep it in range and in the right order.
		rs.Min = ratioInRange(rs.Min)
		rs.Max = ratioInRange(rs.Max)
		if rs.Max < rs.Min {
			rs.Max, rs.Min = rs.Min, rs.Max
		}
		// Update the current ratio with the scrolling delta.
		ratio := (rs.Ratio*b + a) / b
		ratio = ratioInRange(ratio)
		// Stay within the specified min and max, if any.
		switch {
		case rs.Min > 0 && ratio < rs.Min:
			rs.Ratio = rs.Min
		case rs.Max > 0 && ratio > rs.Max:
			rs.Ratio = rs.Max
		switch ratio := (rs.Ratio*b + a) / b; {
		case ratio < 0:
			rs.Ratio = 0
		case ratio > 1:
			rs.Ratio = 1
		default:
			rs.Ratio = ratio
		}


@@ 73,8 60,7 @@ func (rs *Resizable) Layout(gtx layout.Context, handle layout.Widget) layout.Dim
	// Allow events caught for the scroll handling to flow down to the handle.
	pointer.PassOp{Pass: true}.Add(gtx.Ops)
	// Overlay the scroll handling ops.
	r := image.Rectangle{Max: dims.Size}
	pointer.Rect(r).Add(gtx.Ops)
	pointer.Rect(image.Rectangle{Max: dims.Size}).Add(gtx.Ops)
	rs.drag.Add(gtx.Ops)
	cursor := pointer.CursorRowResize
	if rs.Axis == layout.Horizontal {


@@ 84,13 70,3 @@ func (rs *Resizable) Layout(gtx layout.Context, handle layout.Widget) layout.Dim

	return dims
}

func ratioInRange(a float32) float32 {
	if a < 0 {
		return 0
	}
	if a > 1 {
		return 1
	}
	return a
}