~eliasnaur/gio

ref: 44d16d04e9e76cc6b75e6394830a99a8906a7053 gio/ui/layout/layout_test.go -rw-r--r-- 1.8 KiB View raw
44d16d04Elias Naur ui/layout: add Flex example 7 months 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
package layout_test

import (
	"fmt"
	"image"
	"time"

	"gioui.org/ui"
	"gioui.org/ui/layout"
)

type config struct{}

var cfg = new(config)

func ExampleInset() {
	ops := new(ui.Ops)

	// Loose constraints with no minimal size.
	var cs layout.Constraints
	cs.Width.Max = 100
	cs.Height.Max = 100

	// Inset all edges by 10.
	inset := layout.UniformInset(ui.Dp(10))
	cs = inset.Begin(cfg, ops, cs)
	// Lay out a 50x50 sized widget.
	dims := layoutWidget(50, 50, cs)
	fmt.Println(dims.Size)
	dims = inset.End(dims)

	fmt.Println(dims.Size)

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

func ExampleAlign() {
	ops := new(ui.Ops)

	// Rigid constraints with both minimum and maximum set.
	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})

	align := layout.Align{Alignment: layout.Center}
	cs = align.Begin(ops, cs)

	// Lay out a 50x50 sized widget.
	dims := layoutWidget(50, 50, cs)
	fmt.Println(dims.Size)

	dims = align.End(dims)

	fmt.Println(dims.Size)

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

func ExampleFlex() {
	ops := new(ui.Ops)

	cs := layout.RigidConstraints(image.Point{X: 100, Y: 100})

	flex := layout.Flex{}
	flex.Init(ops, cs)

	// Rigid 10x10 widget.
	cs = flex.Rigid()
	fmt.Printf("Rigid: %v\n", cs.Width)
	dims := layoutWidget(10, 10, cs)
	child1 := flex.End(dims)

	// Child with 50% space allowance.
	cs = flex.Flexible(0.5)
	fmt.Printf("50%%: %v\n", cs.Width)
	dims = layoutWidget(10, 10, cs)
	child2 := flex.End(dims)

	dims = flex.Layout(child1, child2)

	// Output:
	// Rigid: {0 100}
	// 50%: {0 45}
}

func layoutWidget(width, height int, cs layout.Constraints) layout.Dimens {
	return layout.Dimens{
		Size: image.Point{
			X: width,
			Y: height,
		},
	}
}

func (config) Now() time.Time {
	return time.Now()
}

func (config) Px(v ui.Value) int {
	return int(v.V + .5)
}