This repository has been archived by the owner on Oct 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
grid.go
462 lines (395 loc) · 8.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package tui
import (
"image"
)
var _ Widget = &Grid{}
// Grid is a widget that lays out widgets in a grid.
type Grid struct {
WidgetBase
rows, cols int
rowHeights []int
colWidths []int
hasBorder bool
cells map[image.Point]Widget
columnStretch map[int]int
rowStretch map[int]int
}
// NewGrid returns a new Grid.
func NewGrid(cols, rows int) *Grid {
return &Grid{
cols: cols,
rows: rows,
cells: make(map[image.Point]Widget),
columnStretch: make(map[int]int),
rowStretch: make(map[int]int),
}
}
// Draw draws the grid.
func (g *Grid) Draw(p *Painter) {
s := g.Size()
if g.hasBorder {
border := 1
// Draw outmost border.
p.DrawRect(0, 0, s.X, s.Y)
// Draw column dividers.
var coloff int
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawVerticalLine(x, 0, s.Y-1)
p.DrawRune(x, 0, '┬')
p.DrawRune(x, s.Y-1, '┴')
coloff = x
}
// Draw row dividers.
var rowoff int
for j := 0; j < g.rows-1; j++ {
y := g.rowHeights[j] + rowoff + border
p.DrawHorizontalLine(0, s.X-1, y)
p.DrawRune(0, y, '├')
p.DrawRune(s.X-1, y, '┤')
rowoff = y
}
// Polish the intersections.
rowoff = 0
for j := 0; j < g.rows-1; j++ {
y := g.rowHeights[j] + rowoff + border
coloff = 0
for i := 0; i < g.cols-1; i++ {
x := g.colWidths[i] + coloff + border
p.DrawRune(x, y, '┼')
coloff = x
}
rowoff = y
}
}
// Draw cell content.
for i := 0; i < g.cols; i++ {
for j := 0; j < g.rows; j++ {
pos := image.Point{i, j}
wp := g.mapCellToLocal(pos)
if w, ok := g.cells[pos]; ok {
p.Translate(wp.X, wp.Y)
p.WithMask(image.Rectangle{
Min: image.Point{},
Max: w.Size(),
}, func(p *Painter) {
w.Draw(p)
})
p.Restore()
}
}
}
}
// MinSizeHint returns the minimum size hint for the grid.
func (g *Grid) MinSizeHint() image.Point {
if g.cols == 0 || g.rows == 0 {
return image.Point{}
}
var width int
for i := 0; i < g.cols; i++ {
width += g.minColumnWidth(i)
}
var height int
for j := 0; j < g.rows; j++ {
height += g.minRowHeight(j)
}
if g.hasBorder {
width += g.cols + 1
height += g.rows + 1
}
return image.Point{width, height}
}
// SizeHint returns the recommended size hint for the grid.
func (g *Grid) SizeHint() image.Point {
if g.cols == 0 || g.rows == 0 {
return image.Point{}
}
var maxWidth int
for i := 0; i < g.cols; i++ {
if w := g.columnWidth(i); w > maxWidth {
maxWidth = w
}
}
var maxHeight int
for j := 0; j < g.rows; j++ {
if h := g.rowHeight(j); h > maxHeight {
maxHeight = h
}
}
width := maxWidth * g.cols
height := maxHeight * g.rows
if g.hasBorder {
width += g.cols + 1
height += g.rows + 1
}
return image.Point{width, height}
}
// Resize recursively updates the size of the Grid and all the widgets it
// contains. This is a potentially expensive operation and should be invoked
// with restraint.
//
// Resize is called by the layout engine and is not intended to be used by end
// users.
func (g *Grid) Resize(size image.Point) {
g.size = size
inner := g.size
if g.hasBorder {
inner.X = g.size.X - (g.cols + 1)
inner.Y = g.size.Y - (g.rows + 1)
}
g.layoutChildren(inner)
}
func (g *Grid) layoutChildren(size image.Point) {
g.colWidths = g.doLayout(dim(Horizontal, size), Horizontal)
g.rowHeights = g.doLayout(dim(Vertical, size), Vertical)
for pos, w := range g.cells {
w.Resize(image.Point{g.colWidths[pos.X], g.rowHeights[pos.Y]})
}
}
func (g *Grid) doLayout(space int, a Alignment) []int {
var sizes []int
var stretch map[int]int
if a == Horizontal {
sizes = make([]int, g.cols)
stretch = g.columnStretch
} else if a == Vertical {
sizes = make([]int, g.rows)
stretch = g.rowStretch
}
var nonZeroStretchFactors int
for _, s := range stretch {
if s > 0 {
nonZeroStretchFactors++
}
}
remaining := space
// Distribute MinSizeHint
for {
var changed bool
for i, sz := range sizes {
if stretch[i] > 0 {
continue
}
ws := g.rowcol(i, a)
var sizeHint int
for _, w := range ws {
s := dim(a, w.MinSizeHint())
if sizeHint < s {
sizeHint = s
}
}
if sz < sizeHint {
sizes[i] = sz + 1
remaining--
if remaining <= 0 {
goto Resize
}
changed = true
}
}
if !changed {
break
}
}
// Distribute Minimum
for {
var changed bool
for i, sz := range sizes {
if stretch[i] > 0 {
continue
}
ws := g.rowcol(i, a)
var sizeHint int
for _, w := range ws {
s := dim(a, w.SizeHint())
p := alignedSizePolicy(a, w)
if p == Minimum && sizeHint < s {
sizeHint = s
}
}
if sz < sizeHint {
sizes[i] = sz + 1
remaining--
if remaining <= 0 {
goto Resize
}
changed = true
}
}
if !changed {
break
}
}
// Distribute remaining space
for {
var changed bool
if nonZeroStretchFactors == 0 {
min := maxInt
for _, sz := range sizes {
if sz < min {
min = sz
}
}
for i, sz := range sizes {
if sz == min {
sizes[i] = sz + 1
remaining--
if remaining <= 0 {
goto Resize
}
changed = true
}
}
} else {
for i, sz := range sizes {
s := stretch[i]
if s > 0 {
if remaining-s < 0 {
s = remaining
}
sizes[i] = sz + s
remaining -= s
if remaining <= 0 {
goto Resize
}
changed = true
}
}
}
if !changed {
break
}
}
Resize:
return sizes
}
func (g *Grid) rowcol(i int, a Alignment) []Widget {
cells := make([]Widget, 0)
for p, w := range g.cells {
if dim(a, p) == i {
cells = append(cells, w)
}
}
return cells
}
func (g *Grid) mapCellToLocal(p image.Point) image.Point {
var lx, ly int
for x := 0; x < p.X; x++ {
lx += g.colWidths[x]
}
for y := 0; y < p.Y; y++ {
ly += g.rowHeights[y]
}
if g.hasBorder {
lx += p.X + 1
ly += p.Y + 1
}
return image.Point{lx, ly}
}
func (g *Grid) rowHeight(i int) int {
result := 0
for pos, w := range g.cells {
if pos.Y != i {
continue
}
if w.SizeHint().Y > result {
result = w.SizeHint().Y
}
}
return result
}
func (g *Grid) columnWidth(i int) int {
result := 0
for pos, w := range g.cells {
if pos.X != i {
continue
}
if w.SizeHint().X > result {
result = w.SizeHint().X
}
}
return result
}
func (g *Grid) minRowHeight(i int) int {
result := 0
for pos, w := range g.cells {
if pos.Y != i {
continue
}
if w.MinSizeHint().Y > result {
result = w.MinSizeHint().Y
}
}
return result
}
func (g *Grid) minColumnWidth(i int) int {
result := 0
for pos, w := range g.cells {
if pos.X != i {
continue
}
if w.MinSizeHint().X > result {
result = w.MinSizeHint().X
}
}
return result
}
// OnKeyEvent handles key events.
func (g *Grid) OnKeyEvent(ev KeyEvent) {
for _, w := range g.cells {
w.OnKeyEvent(ev)
}
}
// SetCell sets or replaces the contents of a cell.
func (g *Grid) SetCell(pos image.Point, w Widget) {
g.cells[pos] = w
}
// SetBorder sets whether the border is visible or not.
func (g *Grid) SetBorder(enabled bool) {
g.hasBorder = enabled
}
// AppendRow adds a new row at the end.
func (g *Grid) AppendRow(row ...Widget) {
g.rows++
if len(row) > g.cols {
g.cols = len(row)
}
for i, cell := range row {
pos := image.Point{i, g.rows - 1}
g.SetCell(pos, cell)
}
}
// RemoveRow removes the row ( at index ) from the grid
func (g *Grid) RemoveRow(index int) {
if index < g.rows {
g.rows--
for i := index; i <= g.rows; i++ {
for j := 0; j < g.cols; j++ {
if i == g.rows {
delete(g.cells, image.Point{j, g.rows})
} else {
g.cells[image.Point{j, i}] = g.cells[image.Point{j, i + 1}]
}
}
}
}
}
// RemoveRows will remove all the rows in grid
func (g *Grid) RemoveRows() {
g.rows = 0
g.cells = make(map[image.Point]Widget)
}
// SetColumnStretch sets the stretch factor for a given column. If stretch > 0,
// the column will expand to fill up available space. If multiple columns have
// a stretch factor > 0, stretch determines how much space the column get in
// respect to the others. E.g. by setting SetColumnStretch(0, 1) and
// SetColumnStretch(1, 2), the second column will fill up twice as much space
// as the first one.
func (g *Grid) SetColumnStretch(col, stretch int) {
g.columnStretch[col] = stretch
}
// SetRowStretch sets the stretch factor for a given row. For more on stretch
// factors, see SetColumnStretch.
func (g *Grid) SetRowStretch(row, stretch int) {
g.rowStretch[row] = stretch
}