github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/layout/example_test.go (about) 1 package layout_test 2 3 import ( 4 "fmt" 5 "image" 6 7 "github.com/gop9/olt/gio/layout" 8 "github.com/gop9/olt/gio/unit" 9 ) 10 11 func ExampleInset() { 12 gtx := new(layout.Context) 13 gtx.Reset(nil, image.Point{X: 100, Y: 100}) 14 // Loose constraints with no minimal size. 15 gtx.Constraints.Width.Min = 0 16 gtx.Constraints.Height.Min = 0 17 18 // Inset all edges by 10. 19 inset := layout.UniformInset(unit.Dp(10)) 20 inset.Layout(gtx, func() { 21 // Lay out a 50x50 sized widget. 22 layoutWidget(gtx, 50, 50) 23 fmt.Println(gtx.Dimensions.Size) 24 }) 25 26 fmt.Println(gtx.Dimensions.Size) 27 28 // Output: 29 // (50,50) 30 // (70,70) 31 } 32 33 func ExampleAlign() { 34 gtx := new(layout.Context) 35 // Rigid constraints with both minimum and maximum set. 36 gtx.Reset(nil, image.Point{X: 100, Y: 100}) 37 38 align := layout.Align(layout.Center) 39 align.Layout(gtx, func() { 40 // Lay out a 50x50 sized widget. 41 layoutWidget(gtx, 50, 50) 42 fmt.Println(gtx.Dimensions.Size) 43 }) 44 45 fmt.Println(gtx.Dimensions.Size) 46 47 // Output: 48 // (50,50) 49 // (100,100) 50 } 51 52 func ExampleFlex() { 53 gtx := new(layout.Context) 54 gtx.Reset(nil, image.Point{X: 100, Y: 100}) 55 56 layout.Flex{}.Layout(gtx, 57 // Rigid 10x10 widget. 58 layout.Rigid(func() { 59 fmt.Printf("Rigid: %v\n", gtx.Constraints.Width) 60 layoutWidget(gtx, 10, 10) 61 }), 62 // Child with 50% space allowance. 63 layout.Flexed(0.5, func() { 64 fmt.Printf("50%%: %v\n", gtx.Constraints.Width) 65 layoutWidget(gtx, 10, 10) 66 }), 67 ) 68 69 // Output: 70 // Rigid: {0 100} 71 // 50%: {45 45} 72 } 73 74 func ExampleStack() { 75 gtx := new(layout.Context) 76 gtx.Reset(nil, image.Point{X: 100, Y: 100}) 77 gtx.Constraints.Width.Min = 0 78 gtx.Constraints.Height.Min = 0 79 80 layout.Stack{}.Layout(gtx, 81 // Force widget to the same size as the second. 82 layout.Expanded(func() { 83 fmt.Printf("Expand: %v\n", gtx.Constraints) 84 layoutWidget(gtx, 10, 10) 85 }), 86 // Rigid 50x50 widget. 87 layout.Stacked(func() { 88 layoutWidget(gtx, 50, 50) 89 }), 90 ) 91 92 // Output: 93 // Expand: {{50 100} {50 100}} 94 } 95 96 func ExampleList() { 97 gtx := new(layout.Context) 98 gtx.Reset(nil, image.Point{X: 100, Y: 100}) 99 100 // The list is 1e6 elements, but only 5 fit the constraints. 101 const listLen = 1e6 102 103 var list layout.List 104 count := 0 105 list.Layout(gtx, listLen, func(i int) { 106 count++ 107 layoutWidget(gtx, 20, 20) 108 }) 109 110 fmt.Println(count) 111 112 // Output: 113 // 5 114 } 115 116 func layoutWidget(ctx *layout.Context, width, height int) { 117 ctx.Dimensions = layout.Dimensions{ 118 Size: image.Point{ 119 X: width, 120 Y: height, 121 }, 122 } 123 }