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