-
Notifications
You must be signed in to change notification settings - Fork 21
/
table.go
158 lines (134 loc) · 3.27 KB
/
table.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
package renderers
import (
"image/color"
"gioui.org/font"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"github.com/crypto-power/cryptopower/ui/cryptomaterial"
)
type cellAlign int
const (
cellAlignLeft cellAlign = iota
cellAlignRight
cellAlignCenter
cellAlignCopyHeader
)
type cell struct {
content string
alignment cellAlign
contentLength float64
}
type row struct {
isHeader bool
cells []cell
}
type table struct {
theme *cryptomaterial.Theme
rows []row
}
func newTable(theme *cryptomaterial.Theme) *table {
return &table{
theme: theme,
}
}
func (t *table) startNextRow() {
t.rows = append(t.rows, row{})
}
func (t *table) addCell(content string, alignment cellAlign, isHeader bool) {
if len(t.rows) == 0 {
return
}
cell := cell{
content: content,
contentLength: float64(len(content)),
alignment: alignment,
}
rowIndex := len(t.rows) - 1
t.rows[rowIndex].isHeader = isHeader
t.rows[rowIndex].cells = append(t.rows[rowIndex].cells, cell)
}
// normalize ensure that the table has the same number of cells
// in each rows, header or not.
func (t *table) normalize() {
}
func (t *table) setAlignment() {
if len(t.rows) == 0 {
return
}
for i := range t.rows {
if i == 0 {
continue
}
for cellIndex := range t.rows[i].cells {
t.rows[i].cells[cellIndex].alignment = t.rows[0].cells[cellIndex].alignment
}
}
}
func (t *table) layoutCellLabel(gtx C, c cell, isHeader bool) D {
var w layout.Direction
switch c.alignment {
case cellAlignLeft:
w = layout.W
case cellAlignRight:
w = layout.E
default:
w = layout.Center
}
lbl := t.theme.Body2(c.content)
if isHeader {
lbl.Font.Weight = font.Bold
}
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.UniformInset(unit.Dp(7)).Layout(gtx, func(gtx C) D {
return w.Layout(gtx, lbl.Layout)
})
}
func (t *table) layoutRow(gtx C, r row) D {
var maxHeight int
width := float64(gtx.Constraints.Max.X) / float64(len(r.cells))
line := t.theme.SeparatorVertical(0, 1)
line.Color = t.theme.Color.Gray2
return (&layout.List{Axis: layout.Horizontal}).Layout(gtx, len(r.cells), func(gtx C, i int) D {
gtx.Constraints.Min.X = int(width)
gtx.Constraints.Max.X = int(width)
return layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx C) D {
dims := t.layoutCellLabel(gtx, r.cells[i], r.isHeader)
if maxHeight < dims.Size.Y {
maxHeight = dims.Size.Y
}
return dims
}),
layout.Expanded(func(gtx C) D {
line.Height = maxHeight
return line.Layout(gtx)
}),
)
})
}
func (t *table) render() layout.Widget {
return func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return (&layout.List{Axis: layout.Vertical}).Layout(gtx, len(t.rows), func(gtx C, i int) D {
var bgCol color.NRGBA
if i == 0 || i%2 != 0 {
bgCol = t.theme.Color.Surface
} else {
bgCol = t.theme.Color.Background
}
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
border := widget.Border{Color: t.theme.Color.Gray2, CornerRadius: unit.Dp(0), Width: unit.Dp(1)}
return border.Layout(gtx, func(gtx C) D {
return cryptomaterial.Fill(gtx, bgCol)
})
}),
layout.Stacked(func(gtx C) D {
return t.layoutRow(gtx, t.rows[i])
}),
)
})
}
}