~eliasnaur/gio

gio/layout/example_test.go -rw-r--r-- 2.9 KiB
918a5da3Egon Elbre Many operations do not pass refs to Write. Similarly adding 5 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package layout_test

import (
	"fmt"
	"image"

	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/unit"
)

func ExampleInset() {
	gtx := layout.Context{
		Ops: new(op.Ops),
		// Loose constraints with no minimal size.
		Constraints: layout.Constraints{
			Max: image.Point{X: 100, Y: 100},
		},
	}

	// Inset all edges by 10.
	inset := layout.UniformInset(unit.Dp(10))
	dims := inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
		// Lay out a 50x50 sized widget.
		dims := layoutWidget(gtx, 50, 50)
		fmt.Println(dims.Size)
		return dims
	})

	fmt.Println(dims.Size)

	// Output:
	// (50,50)
	// (70,70)
}

func ExampleDirection() {
	gtx := layout.Context{
		Ops: new(op.Ops),
		// Rigid constraints with both minimum and maximum set.
		Constraints: layout.Exact(image.Point{X: 100, Y: 100}),
	}

	dims := layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
		// Lay out a 50x50 sized widget.
		dims := layoutWidget(gtx, 50, 50)
		fmt.Println(dims.Size)
		return dims
	})

	fmt.Println(dims.Size)

	// Output:
	// (50,50)
	// (100,100)
}

func ExampleFlex() {
	gtx := layout.Context{
		Ops: new(op.Ops),
		// Rigid constraints with both minimum and maximum set.
		Constraints: layout.Exact(image.Point{X: 100, Y: 100}),
	}

	layout.Flex{WeightSum: 2}.Layout(gtx,
		// Rigid 10x10 widget.
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			fmt.Printf("Rigid: %v\n", gtx.Constraints)
			return layoutWidget(gtx, 10, 10)
		}),
		// Child with 50% space allowance.
		layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
			fmt.Printf("50%%: %v\n", gtx.Constraints)
			return layoutWidget(gtx, 10, 10)
		}),
	)

	// Output:
	// Rigid: {(0,100) (100,100)}
	// 50%: {(45,100) (45,100)}
}

func ExampleStack() {
	gtx := layout.Context{
		Ops: new(op.Ops),
		Constraints: layout.Constraints{
			Max: image.Point{X: 100, Y: 100},
		},
	}

	layout.Stack{}.Layout(gtx,
		// Force widget to the same size as the second.
		layout.Expanded(func(gtx layout.Context) layout.Dimensions {
			fmt.Printf("Expand: %v\n", gtx.Constraints)
			return layoutWidget(gtx, 10, 10)
		}),
		// Rigid 50x50 widget.
		layout.Stacked(func(gtx layout.Context) layout.Dimensions {
			return layoutWidget(gtx, 50, 50)
		}),
	)

	// Output:
	// Expand: {(50,50) (100,100)}
}

func ExampleList() {
	gtx := layout.Context{
		Ops: new(op.Ops),
		// Rigid constraints with both minimum and maximum set.
		Constraints: layout.Exact(image.Point{X: 100, Y: 100}),
	}

	// The list is 1e6 elements, but only 5 fit the constraints.
	const listLen = 1e6

	var list layout.List
	count := 0
	list.Layout(gtx, listLen, func(gtx layout.Context, i int) layout.Dimensions {
		count++
		return layoutWidget(gtx, 20, 20)
	})

	fmt.Println(count)

	// Output:
	// 5
}

func layoutWidget(ctx layout.Context, width, height int) layout.Dimensions {
	return layout.Dimensions{
		Size: image.Point{
			X: width,
			Y: height,
		},
	}
}