gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/layout/example_test.go (about) 1 package layout_test 2 3 import ( 4 "fmt" 5 "image" 6 7 "gioui.org/layout" 8 "gioui.org/op" 9 ) 10 11 func ExampleInset() { 12 gtx := layout.Context{ 13 Ops: new(op.Ops), 14 // Loose constraints with no minimal size. 15 Constraints: layout.Constraints{ 16 Max: image.Point{X: 100, Y: 100}, 17 }, 18 } 19 20 // Inset all edges by 10. 21 inset := layout.UniformInset(10) 22 dims := inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions { 23 // Lay out a 50x50 sized widget. 24 dims := layoutWidget(gtx, 50, 50) 25 fmt.Println(dims.Size) 26 return dims 27 }) 28 29 fmt.Println(dims.Size) 30 31 // Output: 32 // (50,50) 33 // (70,70) 34 } 35 36 func ExampleDirection() { 37 gtx := layout.Context{ 38 Ops: new(op.Ops), 39 // Rigid constraints with both minimum and maximum set. 40 Constraints: layout.Exact(image.Point{X: 100, Y: 100}), 41 } 42 43 dims := layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions { 44 // Lay out a 50x50 sized widget. 45 dims := layoutWidget(gtx, 50, 50) 46 fmt.Println(dims.Size) 47 return dims 48 }) 49 50 fmt.Println(dims.Size) 51 52 // Output: 53 // (50,50) 54 // (100,100) 55 } 56 57 func ExampleFlex() { 58 gtx := layout.Context{ 59 Ops: new(op.Ops), 60 // Rigid constraints with both minimum and maximum set. 61 Constraints: layout.Exact(image.Point{X: 100, Y: 100}), 62 } 63 64 layout.Flex{WeightSum: 2}.Layout(gtx, 65 // Rigid 10x10 widget. 66 layout.Rigid(func(gtx layout.Context) layout.Dimensions { 67 fmt.Printf("Rigid: %v\n", gtx.Constraints) 68 return layoutWidget(gtx, 10, 10) 69 }), 70 // Child with 50% space allowance. 71 layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { 72 fmt.Printf("50%%: %v\n", gtx.Constraints) 73 return layoutWidget(gtx, 10, 10) 74 }), 75 ) 76 77 // Output: 78 // Rigid: {(0,100) (100,100)} 79 // 50%: {(45,100) (45,100)} 80 } 81 82 func ExampleStack() { 83 gtx := layout.Context{ 84 Ops: new(op.Ops), 85 Constraints: layout.Constraints{ 86 Max: image.Point{X: 100, Y: 100}, 87 }, 88 } 89 90 layout.Stack{}.Layout(gtx, 91 // Force widget to the same size as the second. 92 layout.Expanded(func(gtx layout.Context) layout.Dimensions { 93 fmt.Printf("Expand: %v\n", gtx.Constraints) 94 return layoutWidget(gtx, 10, 10) 95 }), 96 // Rigid 50x50 widget. 97 layout.Stacked(func(gtx layout.Context) layout.Dimensions { 98 return layoutWidget(gtx, 50, 50) 99 }), 100 ) 101 102 // Output: 103 // Expand: {(50,50) (100,100)} 104 } 105 106 func ExampleBackground() { 107 gtx := layout.Context{ 108 Ops: new(op.Ops), 109 Constraints: layout.Constraints{ 110 Max: image.Point{X: 100, Y: 100}, 111 }, 112 } 113 114 layout.Background{}.Layout(gtx, 115 // Force widget to the same size as the second. 116 func(gtx layout.Context) layout.Dimensions { 117 fmt.Printf("Expand: %v\n", gtx.Constraints) 118 return layoutWidget(gtx, 10, 10) 119 }, 120 // Rigid 50x50 widget. 121 func(gtx layout.Context) layout.Dimensions { 122 return layoutWidget(gtx, 50, 50) 123 }, 124 ) 125 126 // Output: 127 // Expand: {(50,50) (100,100)} 128 } 129 130 func ExampleList() { 131 gtx := layout.Context{ 132 Ops: new(op.Ops), 133 // Rigid constraints with both minimum and maximum set. 134 Constraints: layout.Exact(image.Point{X: 100, Y: 100}), 135 } 136 137 // The list is 1e6 elements, but only 5 fit the constraints. 138 const listLen = 1e6 139 140 var list layout.List 141 list.Layout(gtx, listLen, func(gtx layout.Context, i int) layout.Dimensions { 142 return layoutWidget(gtx, 20, 20) 143 }) 144 145 fmt.Println(list.Position.Count) 146 147 // Output: 148 // 5 149 } 150 151 func layoutWidget(ctx layout.Context, width, height int) layout.Dimensions { 152 return layout.Dimensions{ 153 Size: image.Point{ 154 X: width, 155 Y: height, 156 }, 157 } 158 }