-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
listheader.go
251 lines (218 loc) · 5.97 KB
/
listheader.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
package widgets
import (
"log"
"github.com/dweymouth/supersonic/ui/layouts"
myTheme "github.com/dweymouth/supersonic/ui/theme"
"github.com/dweymouth/supersonic/ui/util"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
type SortType int
const (
SortNone SortType = iota
SortAscending
SortDescending
)
type ListHeaderSort struct {
ColNumber int
Type SortType
}
type ListColumn struct {
Text string
Alignment fyne.TextAlign
CanToggleVisible bool
}
type ListHeader struct {
widget.BaseWidget
DisableSorting bool
OnColumnSortChanged func(ListHeaderSort)
OnColumnVisibilityChanged func(int, bool)
OnColumnVisibilityMenuShown func(*widget.PopUp)
sort ListHeaderSort
columns []ListColumn
columnVisible []bool
columnsLayout *layouts.ColumnsLayout
columnsContainer *fyne.Container
container *fyne.Container
popUpMenu *fyne.Container
}
func NewListHeader(cols []ListColumn, layout *layouts.ColumnsLayout) *ListHeader {
l := &ListHeader{
columns: cols,
columnsLayout: layout,
columnsContainer: container.New(layout),
}
l.columnVisible = make([]bool, len(cols))
for i := range l.columnVisible {
l.columnVisible[i] = true
}
l.container = container.NewMax(myTheme.NewThemedRectangle(myTheme.ColorNameListHeader), l.columnsContainer)
l.ExtendBaseWidget(l)
l.buildColumns()
return l
}
func (l *ListHeader) SetColumnVisible(colNum int, visible bool) {
if colNum >= len(l.columns) {
log.Println("error: ListHeader.SetColumnVisible: column index out of range")
return
}
if visible {
l.columnsContainer.Objects[colNum].Show()
} else {
l.columnsContainer.Objects[colNum].Hide()
}
l.columnVisible[colNum] = visible
l.columnsContainer.Refresh()
}
func (l *ListHeader) buildColumns() {
for i, c := range l.columns {
hdr := newColHeader(c, &l.DisableSorting)
hdr.OnSortChanged = func(i int) func(SortType) {
return func(sort SortType) { l.SetSorting(ListHeaderSort{ColNumber: i, Type: sort}) }
}(i)
hdr.OnTappedSecondary = l.TappedSecondary
l.columnsContainer.Add(hdr)
}
}
// Sets the sorting for the ListHeader. Will invoke
// OnColumnSortChanged if set.
func (l *ListHeader) SetSorting(sort ListHeaderSort) {
if l.sort == sort {
return
}
l.sort = sort
for i, c := range l.columnsContainer.Objects {
if i == sort.ColNumber {
c.(*colHeader).Sort = sort.Type
} else {
c.(*colHeader).Sort = SortNone
}
}
l.Refresh()
if l.OnColumnSortChanged != nil {
l.OnColumnSortChanged(sort)
}
}
func (l *ListHeader) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(l.container)
}
func (l *ListHeader) TappedSecondary(e *fyne.PointEvent) {
l.setupPopUpMenu()
if len(l.popUpMenu.Objects) == 0 {
return
}
pop := widget.NewPopUp(l.popUpMenu, fyne.CurrentApp().Driver().CanvasForObject(l))
pop.ShowAtPosition(e.AbsolutePosition)
if l.OnColumnVisibilityMenuShown != nil {
l.OnColumnVisibilityMenuShown(pop)
}
}
func (l *ListHeader) setupPopUpMenu() {
if l.popUpMenu == nil {
l.popUpMenu = container.New(&layouts.VboxCustomPadding{ExtraPad: -10})
for i, c := range l.columns {
if c.CanToggleVisible {
l.popUpMenu.Add(widget.NewCheck(c.Text, l.createOnChangedCallbk(i)))
}
}
}
objIdx := 0
for i, col := range l.columns {
if col.CanToggleVisible {
l.popUpMenu.Objects[objIdx].(*widget.Check).Checked = l.columnVisible[i]
objIdx++
}
}
}
func (l *ListHeader) createOnChangedCallbk(colNum int) func(bool) {
return func(val bool) {
l.columnVisible[colNum] = val
l.SetColumnVisible(colNum, val)
if l.OnColumnVisibilityChanged != nil {
l.OnColumnVisibilityChanged(colNum, val)
}
}
}
type colHeader struct {
widget.BaseWidget
Sort SortType
OnSortChanged func(SortType)
OnTappedSecondary func(*fyne.PointEvent)
sortDisabled *bool
columnCfg ListColumn
label *widget.RichText
sortIcon *widget.Icon
sortIconNegSpacer fyne.CanvasObject
container *fyne.Container
}
func newColHeader(columnCfg ListColumn, sortDisabled *bool) *colHeader {
c := &colHeader{columnCfg: columnCfg, sortDisabled: sortDisabled}
c.ExtendBaseWidget(c)
c.label = widget.NewRichTextWithText(columnCfg.Text)
c.label.Segments[0].(*widget.TextSegment).Style = widget.RichTextStyle{
TextStyle: fyne.TextStyle{Bold: true},
Alignment: columnCfg.Alignment,
}
c.sortIcon = widget.NewIcon(theme.MenuDropDownIcon())
// hack to remove extra icon space
// should be hidden whenever sortIcon is hidden
c.sortIconNegSpacer = util.NewHSpace(0)
return c
}
func (c *colHeader) Tapped(*fyne.PointEvent) {
if *c.sortDisabled {
return
}
switch c.Sort {
case SortNone:
c.Sort = SortAscending
case SortAscending:
c.Sort = SortDescending
case SortDescending:
c.Sort = SortNone
default:
log.Println("notReached colHeader.Tapped")
}
c.Refresh()
if c.OnSortChanged != nil {
c.OnSortChanged(c.Sort)
}
}
func (c *colHeader) TappedSecondary(e *fyne.PointEvent) {
if c.OnTappedSecondary != nil {
c.OnTappedSecondary(e)
}
}
func (c *colHeader) Refresh() {
if c.Sort == SortDescending {
c.sortIcon.Resource = theme.MenuDropDownIcon()
} else {
c.sortIcon.Resource = theme.MenuDropUpIcon()
}
if c.Sort > 0 && c.sortIcon.Hidden {
c.sortIcon.Show()
c.container.Add(c.sortIconNegSpacer)
} else if (c.Sort == SortNone || *c.sortDisabled) && !c.sortIcon.Hidden {
c.sortIcon.Hide()
c.container.Remove(c.sortIconNegSpacer)
}
c.BaseWidget.Refresh()
}
func (c *colHeader) CreateRenderer() fyne.WidgetRenderer {
if c.container == nil {
c.container = container.New(&layouts.HboxCustomPadding{DisableThemePad: true, ExtraPad: -8})
if c.columnCfg.Alignment != fyne.TextAlignLeading {
c.container.Add(layout.NewSpacer())
}
c.container.Add(c.label)
c.container.Add(c.sortIcon)
c.container.Add(c.sortIconNegSpacer)
if c.columnCfg.Alignment == fyne.TextAlignCenter {
c.container.Add(layout.NewSpacer())
}
}
return widget.NewSimpleRenderer(c.container)
}