From 3b54c665ca4dea212b8f639023e22915738f4a94 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 17 Jun 2020 09:43:11 +0200 Subject: [PATCH] layout: add WeightSum to Flex for overriding the Flexed total weight Updates gio#139 Signed-off-by: Elias Naur --- layout/example_test.go | 5 +---- layout/flex.go | 7 +++++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/layout/example_test.go b/layout/example_test.go index 03ba44df..4be88fbb 100644 --- a/layout/example_test.go +++ b/layout/example_test.go @@ -62,7 +62,7 @@ func ExampleFlex() { Constraints: layout.Exact(image.Point{X: 100, Y: 100}), } - layout.Flex{}.Layout(gtx, + layout.Flex{WeightSum: 2}.Layout(gtx, // Rigid 10x10 widget. layout.Rigid(func(gtx layout.Context) layout.Dimensions { fmt.Printf("Rigid: %v\n", gtx.Constraints) @@ -73,9 +73,6 @@ func ExampleFlex() { fmt.Printf("50%%: %v\n", gtx.Constraints) return layoutWidget(gtx, 10, 10) }), - layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { - return layout.Dimensions{} - }), ) // Output: diff --git a/layout/flex.go b/layout/flex.go index 76a018d9..77b3d49d 100644 --- a/layout/flex.go +++ b/layout/flex.go @@ -18,6 +18,10 @@ type Flex struct { Spacing Spacing // Alignment is the alignment in the cross axis. Alignment Alignment + // WeightSum is the sum of weights used for the weighted + // size of Flexed children. If WeightSum is zero, the sum + // of all Flexed weights is used. + WeightSum float32 } // FlexChild is the descriptor for a Flex child. @@ -102,6 +106,9 @@ func (f Flex) Layout(gtx Context, children ...FlexChild) Dimensions { children[i].call = c children[i].dims = dims } + if w := f.WeightSum; w != 0 { + totalWeight = w + } // fraction is the rounding error from a Flex weighting. var fraction float32 flexTotal := remaining