-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlayout.go
More file actions
146 lines (130 loc) · 3.5 KB
/
Copy pathlayout.go
File metadata and controls
146 lines (130 loc) · 3.5 KB
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
138
139
140
141
142
143
144
145
146
package gova
// Alignment describes cross-axis alignment for VStack/HStack and full 2D
// alignment for ZStack. The same constants serve both; stacks ignore the
// axis that doesn't apply (VStack ignores vertical; HStack ignores horizontal).
type Alignment int
const (
AlignCenter Alignment = iota
AlignLeading
AlignTrailing
AlignTop
AlignBottom
AlignTopLeading
AlignTopTrailing
AlignBottomLeading
AlignBottomTrailing
)
// Legacy-friendly aliases so callers can write g.Leading instead of g.AlignLeading.
const (
Leading = AlignLeading
Trailing = AlignTrailing
Top = AlignTop
Bottom = AlignBottom
Center = AlignCenter
TopLeading = AlignTopLeading
TopTrailing = AlignTopTrailing
BottomLeading = AlignBottomLeading
BottomTrailing = AlignBottomTrailing
)
func VStack(children ...any) *viewNode {
return stackNode(viewKindVStack, children)
}
func HStack(children ...any) *viewNode {
return stackNode(viewKindHStack, children)
}
// ZStack layers children on top of each other at the same position.
// Default alignment is Center; change with .Align(g.BottomTrailing) etc.
func ZStack(children ...any) *viewNode {
return stackNode(viewKindZStack, children)
}
// Group flattens its children into the parent layout. Use to satisfy
// variadic spread limitations (Go forbids mixing spread with named args).
func Group(children ...any) *viewNode {
return stackNode(viewKindGroup, children)
}
func stackNode(kind viewKind, children []any) *viewNode {
node := &viewNode{
kind: kind,
children: make([]*viewNode, 0, len(children)),
layout: &layoutData{},
}
for _, c := range children {
v := asView(c)
if v == nil {
continue
}
node.children = append(node.children, v.viewNode())
}
return node
}
// Spacing sets inter-child gap in pixels.
func (n *viewNode) Spacing(s float32) *viewNode {
if n.layout != nil {
n.layout.spacing = s
}
return n
}
// Align sets the cross-axis alignment for VStack/HStack, or full 2D
// alignment for ZStack.
func (n *viewNode) Align(a Alignment) *viewNode {
if n.layout != nil {
n.layout.alignment = a
n.layout.hasAlignment = true
}
return n
}
func Spacer() *viewNode {
return &viewNode{kind: viewKindSpacer}
}
// Scaffold is the border-layout primitive. Center is always required;
// edges are optional and attached via chain methods.
//
// g.Scaffold(g.ScrollView(content)).
// Top(header).
// Bottom(footer)
func Scaffold(center any) *viewNode {
node := &viewNode{
kind: viewKindScaffold,
scaffold: &scaffoldData{},
}
if v := asView(center); v != nil {
node.children = append(node.children, v.viewNode())
}
return node
}
// Top attaches a view to the top edge of a Scaffold.
func (n *viewNode) Top(v any) *viewNode {
if n.scaffold != nil {
if view := asView(v); view != nil {
n.scaffold.top = view
}
}
return n
}
// Bottom attaches a view to the bottom edge of a Scaffold.
func (n *viewNode) Bottom(v any) *viewNode {
if n.scaffold != nil {
if view := asView(v); view != nil {
n.scaffold.bottom = view
}
}
return n
}
// Leading attaches a view to the leading (left in LTR) edge of a Scaffold.
func (n *viewNode) Leading(v any) *viewNode {
if n.scaffold != nil {
if view := asView(v); view != nil {
n.scaffold.leading = view
}
}
return n
}
// Trailing attaches a view to the trailing (right in LTR) edge of a Scaffold.
func (n *viewNode) Trailing(v any) *viewNode {
if n.scaffold != nil {
if view := asView(v); view != nil {
n.scaffold.trailing = view
}
}
return n
}