-
Notifications
You must be signed in to change notification settings - Fork 21
/
grid.go
211 lines (191 loc) · 4.88 KB
/
grid.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cryptomaterial
import (
"image"
"gioui.org/layout"
"gioui.org/op"
)
// inf is an infinite axis constraint.
const inf = 1e6
// GridElement lays out the ith element of a Grid.
type GridElement func(gtx layout.Context, i int) layout.Dimensions
// Grid lays out at most Num elements along the main axis.
// The number of cross axis elements depend on the total number of elements.
type Grid struct {
Num int
Axis layout.Axis
Alignment layout.Alignment
list layout.List
}
// GridWrap lays out as many elements as possible along the main axis
// before wrapping to the cross axis.
type GridWrap struct {
Axis layout.Axis
Alignment layout.Alignment
}
type wrapData struct {
dims layout.Dimensions
call op.CallOp
}
func (g GridWrap) Layout(gtx layout.Context, num int, el GridElement) layout.Dimensions {
csMax := gtx.Constraints.Max
var mainSize, crossSize, mainPos, crossPos, base int
gtx.Constraints.Min = image.Point{}
mainCs := axisMain(g.Axis, csMax)
crossCs := axisCross(g.Axis, gtx.Constraints.Max)
var els []wrapData
for i := 0; i < num; i++ {
macro := op.Record(gtx.Ops)
dims, okMain, okCross := g.place(gtx, i, el)
call := macro.Stop()
if !okMain && !okCross {
break
}
main := axisMain(g.Axis, dims.Size)
cross := axisCross(g.Axis, dims.Size)
if okMain {
els = append(els, wrapData{dims, call})
mainCs := axisMain(g.Axis, gtx.Constraints.Max)
gtx.Constraints.Max = axisPoint(g.Axis, mainCs-main, crossCs)
mainPos += main
crossPos = max(crossPos, cross)
base = max(base, dims.Baseline)
continue
}
// okCross
mainSize = max(mainSize, mainPos)
crossSize += crossPos
g.placeAll(gtx.Ops, els, crossPos, base)
els = append(els[:0], wrapData{dims, call})
gtx.Constraints.Max = axisPoint(g.Axis, mainCs-main, crossCs-crossPos)
mainPos = main
crossPos = cross
base = dims.Baseline
}
mainSize = max(mainSize, mainPos)
crossSize += crossPos
g.placeAll(gtx.Ops, els, crossPos, base)
sz := axisPoint(g.Axis, mainSize, crossSize)
return layout.Dimensions{Size: sz}
}
func (g GridWrap) place(gtx layout.Context, i int, el GridElement) (dims layout.Dimensions, okMain, okCross bool) {
cs := gtx.Constraints
if g.Axis == layout.Horizontal {
gtx.Constraints.Max.X = inf
} else {
gtx.Constraints.Max.Y = inf
}
dims = el(gtx, i)
okMain = dims.Size.X <= cs.Max.X
okCross = dims.Size.Y <= cs.Max.Y
if g.Axis == layout.Vertical {
okMain, okCross = okCross, okMain
}
return
}
func (g GridWrap) placeAll(ops *op.Ops, els []wrapData, crossMax, baseMax int) {
var mainPos int
var pt image.Point
for i, el := range els {
cross := axisCross(g.Axis, el.dims.Size)
switch g.Alignment {
case layout.Start:
cross = 0
case layout.End:
cross = crossMax - cross
case layout.Middle:
cross = (crossMax - cross) / 2
case layout.Baseline:
if g.Axis == layout.Horizontal {
cross = baseMax - el.dims.Baseline
} else {
cross = 0
}
}
if cross == 0 {
el.call.Add(ops)
} else {
pt = axisPoint(g.Axis, 0, cross)
op.Offset(pt).Add(ops)
el.call.Add(ops)
op.Offset(pt.Mul(-1)).Add(ops)
}
if i == len(els)-1 {
pt = axisPoint(g.Axis, -mainPos, crossMax)
} else {
main := axisMain(g.Axis, el.dims.Size)
pt = axisPoint(g.Axis, main, 0)
mainPos += main
}
op.Offset(pt).Add(ops)
}
}
func (g *Grid) Layout(gtx layout.Context, num int, el GridElement) layout.Dimensions {
if g.Num == 0 {
return layout.Dimensions{Size: gtx.Constraints.Min}
}
if g.Axis == g.list.Axis {
if g.Axis == layout.Horizontal {
g.list.Axis = layout.Vertical
} else {
g.list.Axis = layout.Horizontal
}
g.list.Alignment = g.Alignment
}
csMax := gtx.Constraints.Max
return g.list.Layout(gtx, (num+g.Num-1)/g.Num, func(gtx layout.Context, idx int) layout.Dimensions {
if g.Axis == layout.Horizontal {
gtx.Constraints.Max.Y = inf
} else {
gtx.Constraints.Max.X = inf
}
gtx.Constraints.Min = image.Point{}
var mainMax, crossMax int
left := axisMain(g.Axis, csMax)
i := idx * g.Num
n := min(num, i+g.Num)
for ; i < n; i++ {
dims := el(gtx, i)
main := axisMain(g.Axis, dims.Size)
crossMax = max(crossMax, axisCross(g.Axis, dims.Size))
left -= main
if left <= 0 {
mainMax = axisMain(g.Axis, csMax)
break
}
pt := axisPoint(g.Axis, main, 0)
op.Offset(pt).Add(gtx.Ops)
mainMax += main
}
return layout.Dimensions{Size: axisPoint(g.Axis, mainMax, crossMax)}
})
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func axisPoint(a layout.Axis, main, cross int) image.Point {
if a == layout.Horizontal {
return image.Point{main, cross}
}
return image.Point{cross, main}
}
func axisMain(a layout.Axis, sz image.Point) int {
if a == layout.Horizontal {
return sz.X
}
return sz.Y
}
func axisCross(a layout.Axis, sz image.Point) int {
if a == layout.Horizontal {
return sz.Y
}
return sz.X
}