44d16d04e9e76cc6b75e6394830a99a8906a7053 — Elias Naur a month ago 94f2752
ui/layout: add Flex example

Signed-off-by: Elias Naur <mail@eliasnaur.com>
1 files changed, 29 insertions(+), 0 deletions(-)

M ui/layout/layout_test.go
M ui/layout/layout_test.go => ui/layout/layout_test.go +29 -0
@@ 39,10 39,12 @@ func ExampleAlign() {
  	ops := new(ui.Ops)
  
+ 	// Rigid constraints with both minimum and maximum set.
  	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
  
  	align := layout.Align{Alignment: layout.Center}
  	cs = align.Begin(ops, cs)
+ 
  	// Lay out a 50x50 sized widget.
  	dims := layoutWidget(50, 50, cs)
  	fmt.Println(dims.Size)


@@ 56,6 58,33 @@ // (100,100)
  }
  
+ func ExampleFlex() {
+ 	ops := new(ui.Ops)
+ 
+ 	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
+ 
+ 	flex := layout.Flex{}
+ 	flex.Init(ops, cs)
+ 
+ 	// Rigid 10x10 widget.
+ 	cs = flex.Rigid()
+ 	fmt.Printf("Rigid: %v\n", cs.Width)
+ 	dims := layoutWidget(10, 10, cs)
+ 	child1 := flex.End(dims)
+ 
+ 	// Child with 50% space allowance.
+ 	cs = flex.Flexible(0.5)
+ 	fmt.Printf("50%%: %v\n", cs.Width)
+ 	dims = layoutWidget(10, 10, cs)
+ 	child2 := flex.End(dims)
+ 
+ 	dims = flex.Layout(child1, child2)
+ 
+ 	// Output:
+ 	// Rigid: {0 100}
+ 	// 50%: {0 45}
+ }
+ 
  func layoutWidget(width, height int, cs layout.Constraints) layout.Dimens {
  	return layout.Dimens{
  		Size: image.Point{