-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_view.go
74 lines (65 loc) · 1.45 KB
/
list_view.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
package main
type ListView struct {
view View
ViewPosition
ViewSize
views []*LabelView
emptyListLabel *LabelView
model *ListModel
}
func NewListView(view View, x, y, width, height int) *ListView {
return &ListView{
view,
ViewPosition{x, y},
ViewSize{width, height},
buildViews(view, x, y, width, height),
NewLabelView(view, "", false, x, y, width),
NewListModel([]Path{}, 0),
}
}
func (l *ListView) Items(list []Path) error {
l.Clear()
l.model = NewListModel(list, l.height)
l.sync()
if len(list) == 0 {
l.emptyListLabel.TextAndSelect("No items", false)
} else {
l.emptyListLabel.Clear()
}
return nil
}
func (l *ListView) Select(index int) {
l.model.Select(index)
l.sync()
}
func (l *ListView) SelectNext(step int) {
l.model.SelectNext(step)
l.sync()
}
func (l *ListView) Selected() Path {
return l.model.Selected()
}
func (l *ListView) Clear() {
for _, view := range l.views {
view.Clear()
}
}
func buildViews(view View, x, y, width, height int) []*LabelView {
views := make([]*LabelView, height)
for i := 0; i < height; i++ {
views[i] = NewLabelView(view, "", false, x, y+i, width)
}
return views
}
func (l *ListView) sync() {
if l.model == nil {
l.Clear()
return
}
visibleItems := l.model.VisibleItems()
selectedIndex := l.model.VisibleSelectedIndex()
max := Min(len(l.views), len(visibleItems))
for i := 0; i < max; i++ {
l.views[i].TextAndSelect(visibleItems[i].Label(), i == selectedIndex)
}
}