texttable.go raw
1 package gel
2
3 import l "github.com/p9c/gio/layout"
4
5 type TextTableHeader []string
6
7 type TextTableRow []string
8
9 type TextTableBody []TextTableRow
10
11 // TextTable is a widget that renders a scrolling list of rows of data labeled by a header. Note that for the reasons of
12 // expedience and performance this widget assumes a growing but immutable list of rows of items. If this is used on
13 // data that is not immutable, nilling the body will cause it to be wholly regenerated, updating older content than the
14 // longest length the list has reached.
15 type TextTable struct {
16 *Window
17 Header TextTableHeader
18 Body TextTableBody
19 HeaderColor string
20 HeaderDarkTheme bool
21 HeaderBackground string
22 HeaderFont string
23 HeaderFontScale float32
24 CellColor string
25 CellBackground string
26 CellFont string
27 CellFontScale float32
28 CellInset float32
29 List *List
30 Table *Table
31 }
32
33 // Regenerate the text table.
34 func (tt *TextTable) Regenerate(fully bool) {
35 if len(tt.Header) == 0 || len(tt.Body) == 0 {
36 return
37 }
38 // // set defaults if unset
39 tt.SetDefaults()
40 if tt.Table.header == nil || len(tt.Table.header) < 1 || tt.HeaderDarkTheme != tt.Theme.Dark.True() {
41 tt.HeaderDarkTheme = tt.Theme.Dark.True()
42 // if this is being regenerated due to theme change
43 tt.Table.header = tt.Table.header[:0]
44 // this only has to be created once
45 for i := range tt.Header {
46 tt.Table.header = append(tt.Table.header, Cell{
47 Widget: // tt.Theme.Fill(tt.HeaderBackground,
48 tt.Inset(tt.CellInset,
49 tt.Body1(tt.Header[i]).
50 Color(tt.HeaderColor).
51 TextScale(tt.HeaderFontScale).
52 Font(tt.HeaderFont).MaxLines(1).
53 Fn,
54 ).Fn,
55 // ).Fn,
56 })
57 }
58 }
59 // var startIndex int
60 // if tt.Table.body == nil || len(tt.Table.body) < 1 {
61 // // tt.Table.body = tt.Table.body[:0]
62 // } else {
63 // if fully {
64 // tt.Body = tt.Body[:0]
65 // tt.Table.body = tt.Table.body[:0]
66 // }
67 // startIndex = len(tt.Table.body)
68 // D.Ln("startIndex", startIndex, len(tt.Body))
69 // if startIndex < len(tt.Body) {
70
71 // bd := tt.Body // [startIndex:]
72 diff := len(tt.Body) - len(tt.Table.body)
73 // D.Ln(len(tt.Table.body), len(tt.Body), diff)
74 if diff > 0 {
75 cg := make(CellGrid, diff)
76 for i := range cg {
77 cg[i] = make(CellRow, len(tt.Header))
78 }
79 tt.Table.body = append(tt.Table.body, cg...)
80 }
81 // D.Ln(len(tt.Table.body), len(tt.Body))
82 var body CellGrid
83 for i := range tt.Body {
84 var row CellRow
85 for j := range tt.Body[i] {
86 tt.Table.body[i][j] = Cell{
87 Widget: tt.Inset(0.25,
88 tt.Body1(tt.Body[i][j]).
89 Color(tt.CellColor).
90 TextScale(tt.CellFontScale).
91 Font(tt.CellFont).MaxLines(1).
92 Fn,
93 ).Fn,
94 }
95 }
96 body = append(body, row)
97 }
98 // tt.Table.body = append(tt.Table.body, body...)
99 // }
100 // }
101 }
102
103 func (tt *TextTable) SetReverse() *TextTable {
104 tt.Table.reverse = true
105 return tt
106 }
107
108 func (tt *TextTable) SetDefaults() *TextTable {
109 if tt.HeaderColor == "" {
110 tt.HeaderColor = "PanelText"
111 }
112 if tt.HeaderBackground == "" {
113 tt.HeaderBackground = "PanelBg"
114 }
115 if tt.HeaderFont == "" {
116 tt.HeaderFont = "bariol bold"
117 }
118 if tt.HeaderFontScale == 0 {
119 tt.HeaderFontScale = Scales["Caption"]
120 }
121 if tt.CellColor == "" {
122 tt.CellColor = "DocText"
123 }
124 if tt.CellBackground == "" {
125 tt.CellBackground = "DocBg"
126 }
127 if tt.CellFont == "" {
128 tt.CellFont = "go regular"
129 }
130 if tt.CellFontScale == 0 {
131 tt.CellFontScale = Scales["Caption"]
132 }
133 // we assume the caller has intended a zero inset if it is zero
134 if tt.Table == nil {
135 tt.Table = &Table{
136 Window: tt.Window,
137 list: tt.List,
138 headerBackground: tt.HeaderBackground,
139 cellBackground: tt.CellBackground,
140 }
141 }
142 return tt
143 }
144
145 func (tt *TextTable) Fn(gtx l.Context) l.Dimensions {
146 return tt.Table.Fn(gtx)
147 }
148