/
main.go
165 lines (138 loc) · 3.42 KB
/
main.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
package main
import (
"image"
"image/color"
"log"
"os"
"gioui.org/app"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget/material"
)
func main() {
go func() {
w := app.NewWindow(app.Size(150*6+50, 150*6-50))
if err := loop(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
func loop(w *app.Window) error {
theme := material.NewTheme()
var ops op.Ops
for {
switch e := w.NextEvent().(type) {
case app.DestroyEvent:
return e.Err
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
layout.UniformInset(unit.Dp(theme.TextSize)).Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
return Wrap{Gap: unit.Dp(theme.TextSize)}.Layout(gtx, 'Z'-'A',
func(gtx layout.Context, index int) layout.Dimensions {
rec := op.Record(gtx.Ops)
dims := layout.UniformInset(unit.Dp(2)).Layout(gtx,
material.H1(theme, string(rune('A'+index))).Layout)
call := rec.Stop()
stack := clip.Rect{Max: dims.Size}.Push(gtx.Ops)
paint.Fill(gtx.Ops, color.NRGBA{R: byte(index), G: byte(index * index), B: byte(index * index * index), A: 0xFF})
stack.Pop()
call.Add(gtx.Ops)
return dims
})
})
e.Frame(gtx.Ops)
w.Invalidate()
}
}
}
type Wrap struct {
Gap unit.Dp
}
func (wrap Wrap) Layout(gtx layout.Context, itemCount int, w layout.ListElement) layout.Dimensions {
// calculate the pixel size of the gap.
gap := gtx.Dp(wrap.Gap)
// well use the constraints as the max width rather than automatically determining.
width := gtx.Constraints.Max.X
// measured keeps track of a measured element.
type measured struct {
dims layout.Dimensions
call op.CallOp
}
// line keeps track of the current line.
var line []measured
// lineWidth keeps track of the total width of the line + gap
var lineWidth int
// maxHeight is the highest item in the line.
var maxHeight int
// y keeps track of the y position of the line + gap.
var y int
// flush flushes the current line.
flush := func() {
if len(line) == 0 {
return
}
// x keeps track of the current x position of the widgets
x := 0
for _, w := range line {
// adjust the drawing to the correct location.
stack := op.Offset(image.Pt(
x,
// we center each item on the Y axis,
y+maxHeight/2-w.dims.Size.Y/2,
)).Push(gtx.Ops)
// draw the widget
w.call.Add(gtx.Ops)
// restore previous offset
stack.Pop()
// update the x position.
x += w.dims.Size.X + gap
}
// clear the line
line = line[:0]
// update the y position.
y += maxHeight + gap
maxHeight = 0
lineWidth = 0
}
// create a child context that does not have a minimum constraint
cgtx := gtx
cgtx.Constraints.Min = image.Point{}
for i := 0; i < itemCount; i++ {
// record the drawing of the item at position i
macro := op.Record(gtx.Ops)
dims := w(cgtx, i)
call := macro.Stop()
// update the max height
if dims.Size.Y > maxHeight {
maxHeight = dims.Size.Y
}
// when the line would overflow, then flush the current line.
if lineWidth+dims.Size.X > width {
flush()
}
// update the lineWidth with the new widget and add item to list
lineWidth += dims.Size.X + gap
line = append(line, measured{
call: call,
dims: dims,
})
}
// flush the last line
flush()
if y > 0 {
y -= gap
}
return layout.Dimensions{
Size: image.Point{
X: width,
Y: y,
},
Baseline: 0,
}
}