gioui.org/ui@v0.0.0-20190926171558-ce74bc0cbaea/layout/layout_test.go (about)

     1  package layout_test
     2  
     3  import (
     4  	"fmt"
     5  	"image"
     6  	"time"
     7  
     8  	"gioui.org/ui"
     9  	"gioui.org/ui/layout"
    10  )
    11  
    12  type queue struct{}
    13  
    14  type config struct{}
    15  
    16  var q queue
    17  var cfg = new(config)
    18  
    19  func ExampleInset() {
    20  	gtx := &layout.Context{Queue: q}
    21  	// Loose constraints with no minimal size.
    22  	var cs layout.Constraints
    23  	cs.Width.Max = 100
    24  	cs.Height.Max = 100
    25  	gtx.Reset(cfg, cs)
    26  
    27  	// Inset all edges by 10.
    28  	inset := layout.UniformInset(ui.Dp(10))
    29  	inset.Layout(gtx, func() {
    30  		// Lay out a 50x50 sized widget.
    31  		layoutWidget(gtx, 50, 50)
    32  		fmt.Println(gtx.Dimensions.Size)
    33  	})
    34  
    35  	fmt.Println(gtx.Dimensions.Size)
    36  
    37  	// Output:
    38  	// (50,50)
    39  	// (70,70)
    40  }
    41  
    42  func ExampleAlign() {
    43  	gtx := &layout.Context{Queue: q}
    44  	// Rigid constraints with both minimum and maximum set.
    45  	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
    46  	gtx.Reset(cfg, cs)
    47  
    48  	align := layout.Align(layout.Center)
    49  	align.Layout(gtx, func() {
    50  		// Lay out a 50x50 sized widget.
    51  		layoutWidget(gtx, 50, 50)
    52  		fmt.Println(gtx.Dimensions.Size)
    53  	})
    54  
    55  	fmt.Println(gtx.Dimensions.Size)
    56  
    57  	// Output:
    58  	// (50,50)
    59  	// (100,100)
    60  }
    61  
    62  func ExampleFlex() {
    63  	gtx := &layout.Context{Queue: q}
    64  	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
    65  	gtx.Reset(cfg, cs)
    66  
    67  	flex := layout.Flex{}
    68  	flex.Init(gtx)
    69  
    70  	// Rigid 10x10 widget.
    71  	child1 := flex.Rigid(func() {
    72  		fmt.Printf("Rigid: %v\n", gtx.Constraints.Width)
    73  		layoutWidget(gtx, 10, 10)
    74  	})
    75  
    76  	// Child with 50% space allowance.
    77  	child2 := flex.Flexible(0.5, func() {
    78  		fmt.Printf("50%%: %v\n", gtx.Constraints.Width)
    79  		layoutWidget(gtx, 10, 10)
    80  	})
    81  
    82  	flex.Layout(child1, child2)
    83  
    84  	// Output:
    85  	// Rigid: {0 100}
    86  	// 50%: {0 45}
    87  }
    88  
    89  func ExampleStack() {
    90  	gtx := &layout.Context{Queue: q}
    91  	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
    92  	gtx.Reset(cfg, cs)
    93  
    94  	stack := layout.Stack{}
    95  	stack.Init(gtx)
    96  
    97  	// Rigid 50x50 widget.
    98  	child1 := stack.Rigid(func() {
    99  		layoutWidget(gtx, 50, 50)
   100  	})
   101  
   102  	// Force widget to the same size as the first.
   103  	child2 := stack.Expand(func() {
   104  		fmt.Printf("Expand: %v\n", gtx.Constraints)
   105  		layoutWidget(gtx, 10, 10)
   106  	})
   107  
   108  	stack.Layout(child1, child2)
   109  
   110  	// Output:
   111  	// Expand: {{50 50} {50 50}}
   112  }
   113  
   114  func ExampleList() {
   115  	gtx := &layout.Context{Queue: q}
   116  	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})
   117  	gtx.Reset(cfg, cs)
   118  
   119  	// The list is 1e6 elements, but only 5 fit the constraints.
   120  	const listLen = 1e6
   121  
   122  	var list layout.List
   123  	count := 0
   124  	list.Layout(gtx, listLen, func(i int) {
   125  		count++
   126  		layoutWidget(gtx, 20, 20)
   127  	})
   128  
   129  	fmt.Println(count)
   130  
   131  	// Output:
   132  	// 5
   133  }
   134  
   135  func layoutWidget(ctx *layout.Context, width, height int) {
   136  	ctx.Dimensions = layout.Dimensions{
   137  		Size: image.Point{
   138  			X: width,
   139  			Y: height,
   140  		},
   141  	}
   142  }
   143  
   144  func (config) Now() time.Time {
   145  	return time.Now()
   146  }
   147  
   148  func (config) Px(v ui.Value) int {
   149  	return int(v.V + .5)
   150  }
   151  
   152  func (queue) Events(k ui.Key) []ui.Event {
   153  	return nil
   154  }