~pierrec/giox

ab8c69f045ac58410e855e22208a19ae38cd4b53 — pierre 9 months ago cb72b77
layoutx: added Flex to support cross constraint Rigids

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

M layoutx/layoutx.go
M layoutx/layoutx.go => layoutx/layoutx.go +31 -1
@@ 1,6 1,8 @@
package layoutx

import "gioui.org/layout"
import (
	"gioui.org/layout"
)

func max(a, b int) int {
	if a > b {


@@ 19,3 21,31 @@ func min(a, b int) int {
func Swap(a layout.Axis) layout.Axis {
	return (a + 1) % 2
}

// Flex wraps a layout.Flex to support cross constraint rigids.
type Flex struct {
	layout.Flex
	cross int
}

func (f *Flex) Rigid(w layout.Widget) layout.FlexChild {
	return layout.Rigid(func(gtx layout.Context) layout.Dimensions {
		dims := w(gtx)
		f.cross = max(f.cross, f.Flex.Axis.Convert(dims.Size).Y)
		return dims
	})
}

// RigidCross returns a Rigid which cross axis constraint is set to the
// largest one from other non RigidCross.
func (f *Flex) RigidCross(w layout.Widget) layout.FlexChild {
	// Use a Flexed so that they are run after Rigids have been evaluated.
	return layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
		if f.Flex.Axis == layout.Horizontal {
			gtx.Constraints.Max.Y = f.cross
		} else {
			gtx.Constraints.Max.X = f.cross
		}
		return w(gtx)
	})
}