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
|
package layout
import (
"image"
"image/color"
"image/draw"
"github.com/faiface/gui"
)
type grid struct {
Contents [][]*gui.Env
Background color.Color
Gap int
SplitX SplitFunc
SplitY SplitFunc
}
// NewGrid creates a familiar flexbox-like grid layout.
// Each row can be a different length.
func NewGrid(contents [][]*gui.Env, options ...func(*grid)) Layout {
ret := &grid{
Background: image.Black,
Gap: 0,
Contents: contents,
SplitX: EvenSplit,
SplitY: EvenSplit,
}
for _, f := range options {
f(ret)
}
return ret
}
// GridBackground changes the background of the grid to a uniform color.
func GridBackground(c color.Color) func(*grid) {
return func(grid *grid) {
grid.Background = c
}
}
// GridGap changes the grid gap.
// The gap is identical everywhere (top, left, bottom, right).
func GridGap(g int) func(*grid) {
return func(grid *grid) {
grid.Gap = g
}
}
// GridSplitX changes the way the space is divided among the columns in each row.
func GridSplitX(split SplitFunc) func(*grid) {
return func(grid *grid) {
grid.SplitX = split
}
}
// GridSplitY changes the way the space is divided among the rows.
func GridSplitY(split SplitFunc) func(*grid) {
return func(grid *grid) {
grid.SplitY = split
}
}
func (g *grid) Redraw(drw draw.Image, bounds image.Rectangle) {
draw.Draw(drw, bounds, image.NewUniform(g.Background), image.ZP, draw.Src)
}
func (g *grid) Items() []*gui.Env {
// 32 should be more than enough for most grids
ret := make([]*gui.Env, 0, 32)
for _, row := range g.Contents {
ret = append(ret, row...)
}
return ret
}
func (g *grid) Lay(bounds image.Rectangle) []image.Rectangle {
gap := g.Gap
ret := make([]image.Rectangle, 0)
rows := len(g.Contents)
rowsH := g.SplitY(rows, bounds.Dy()-(g.Gap*(rows+1)))
X := gap + bounds.Min.X
Y := gap + bounds.Min.Y
for y, row := range g.Contents {
cols := len(row)
h := rowsH[y]
colsW := g.SplitX(cols, bounds.Dx()-(g.Gap*(cols+1)))
X = gap + bounds.Min.X
for x := range row {
w := colsW[x]
ret = append(ret, image.Rect(X, Y, X+w, Y+h))
X += gap + w
}
Y += gap + h
}
return ret
}
|