-
Notifications
You must be signed in to change notification settings - Fork 21
/
gridlayout.go
45 lines (36 loc) · 1.03 KB
/
gridlayout.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
package cryptomaterial
import (
"gioui.org/layout"
)
type GridLayout struct {
List *layout.List
HorizontalSpacing layout.Spacing
Alignment layout.Alignment
Direction layout.Direction
RowCount int
}
func (g GridLayout) Layout(gtx layout.Context, num int, el GridElement) layout.Dimensions {
rows := make([]layout.Widget, 0)
currentRow := make([]layout.FlexChild, 0)
appendRow := func(row []layout.FlexChild) {
rows = append(rows, func(gtx C) D {
return layout.Flex{Alignment: g.Alignment, Spacing: g.HorizontalSpacing}.Layout(gtx, row...)
})
currentRow = make([]layout.FlexChild, 0)
}
for i := 0; i < num; i++ {
index := i
currentRow = append(currentRow, layout.Rigid(func(gtx C) D { return el(gtx, index) }))
if len(currentRow) >= g.RowCount {
appendRow(currentRow)
}
}
if len(currentRow) > 0 {
appendRow(currentRow)
}
return g.List.Layout(gtx, len(rows), func(gtx C, index int) D {
return g.Direction.Layout(gtx, func(gtx C) D {
return rows[index](gtx)
})
})
}