layout: add WeightSum to Flex for overriding the Flexed total weight

Updates gio#139

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-17 09:43:11 +02:00
parent a3f7eaae08
commit 3b54c665ca
2 changed files with 8 additions and 4 deletions
+1 -4
View File
@@ -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:
+7
View File
@@ -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