forked from agl/pond
/
listui.go
232 lines (202 loc) · 4.76 KB
/
listui.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
package main
import (
"strconv"
)
// listUI manages the sections in the left-hand side list. It contains a number
// of items, which may have subheadlines and indicators (coloured dots).
type listUI struct {
gui GUI
vboxName string
entries []listItem
selected uint64
nextId int
}
type listItem struct {
id uint64
name, sepName, boxName, imageName, lineName, sublineTextName string
insensitive bool
}
func (cs *listUI) Event(event interface{}) (uint64, bool) {
if click, ok := event.(Click); ok {
for _, entry := range cs.entries {
if click.name == entry.boxName {
if entry.insensitive {
return 0, false
}
return entry.id, true
}
}
}
return 0, false
}
func (cs *listUI) Add(id uint64, name, subline string, indicator Indicator) {
c := listItem{
id: id,
name: name,
sepName: cs.newIdent(),
boxName: cs.newIdent(),
imageName: cs.newIdent(),
lineName: cs.newIdent(),
sublineTextName: cs.newIdent(),
}
cs.entries = append(cs.entries, c)
index := len(cs.entries) - 1
if index > 0 {
// Add the separator bar.
cs.gui.Actions() <- AddToBox{
box: cs.vboxName,
pos: index*2 - 1,
child: EventBox{widgetBase: widgetBase{height: 1, background: 0xe5e6e6, name: c.sepName}},
}
}
children := []Widget{
HBox{
widgetBase: widgetBase{padding: 1},
children: []Widget{
Label{
widgetBase: widgetBase{
name: c.lineName,
padding: 5,
font: fontListEntry,
},
text: name,
},
},
},
}
var sublineChildren []Widget
if len(subline) > 0 {
sublineChildren = append(sublineChildren, Label{
widgetBase: widgetBase{
padding: 5,
foreground: colorSubline,
font: fontListSubline,
name: c.sublineTextName,
},
text: subline,
})
}
sublineChildren = append(sublineChildren, Image{
widgetBase: widgetBase{
padding: 4,
expand: true,
fill: true,
name: c.imageName,
},
image: indicator,
xAlign: 1,
yAlign: 0.5,
})
children = append(children, HBox{
widgetBase: widgetBase{padding: 1},
children: sublineChildren,
})
cs.gui.Actions() <- AddToBox{
box: cs.vboxName,
pos: index * 2,
child: EventBox{
widgetBase: widgetBase{name: c.boxName, background: colorGray},
child: VBox{children: children},
},
}
cs.gui.Signal()
}
func (cs *listUI) SetInsensitive(id uint64) {
for i, entry := range cs.entries {
if entry.id == id {
cs.entries[i].insensitive = true
}
}
}
func (cs *listUI) Remove(id uint64) {
for i, entry := range cs.entries {
if entry.id == id {
if i > 0 {
cs.gui.Actions() <- Destroy{name: entry.sepName}
}
cs.gui.Actions() <- Destroy{name: entry.boxName}
cs.gui.Signal()
if cs.selected == id {
cs.selected = 0
}
return
}
}
panic("unknown id passed to Remove")
}
func (cs *listUI) Deselect() {
if cs.selected == 0 {
return
}
var currentlySelected string
for _, entry := range cs.entries {
if entry.id == cs.selected {
currentlySelected = entry.boxName
break
}
}
cs.gui.Actions() <- SetBackground{name: currentlySelected, color: colorGray}
cs.selected = 0
cs.gui.Signal()
}
func (cs *listUI) Select(id uint64) {
if id == cs.selected {
return
}
var currentlySelected, newSelected string
for _, entry := range cs.entries {
if entry.id == cs.selected {
currentlySelected = entry.boxName
} else if entry.id == id {
newSelected = entry.boxName
}
if len(currentlySelected) > 0 && len(newSelected) > 0 {
break
}
}
if len(newSelected) == 0 {
panic("internal error")
}
if len(currentlySelected) > 0 {
cs.gui.Actions() <- SetBackground{name: currentlySelected, color: colorGray}
}
cs.gui.Actions() <- SetBackground{name: newSelected, color: colorHighlight}
cs.selected = id
cs.gui.Signal()
}
func (cs *listUI) SetIndicator(id uint64, indicator Indicator) {
for _, entry := range cs.entries {
if entry.id == id {
cs.gui.Actions() <- SetImage{name: entry.imageName, image: indicator}
cs.gui.Signal()
break
}
}
}
func (cs *listUI) SetLine(id uint64, line string) {
for _, entry := range cs.entries {
if entry.id == id {
cs.gui.Actions() <- SetText{name: entry.lineName, text: line}
cs.gui.Signal()
break
}
}
}
func (cs *listUI) SetSubline(id uint64, subline string) {
for _, entry := range cs.entries {
if entry.id == id {
if len(subline) > 0 {
cs.gui.Actions() <- SetText{name: entry.sublineTextName, text: subline}
} else {
cs.gui.Actions() <- Destroy{name: entry.sublineTextName}
}
cs.gui.Signal()
break
}
}
}
func (cs *listUI) newIdent() string {
id := cs.vboxName + "-" + strconv.Itoa(cs.nextId)
cs.nextId++
return id
}