-
Notifications
You must be signed in to change notification settings - Fork 5
/
listview.go
211 lines (182 loc) · 4.02 KB
/
listview.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
package widgets
import (
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/views"
)
type item struct {
name string
text *views.Text
view *views.ViewPort
}
// ListView is a Widget with containing multiple items as a list
type ListView struct {
view views.View
items []item
selected int
changed bool
width int
height int
views.WidgetWatchers
}
// NewListView returns a new ListView
func NewListView() *ListView {
return &ListView{
selected: -1,
}
}
// AddItem adds a new item with the text and its style. The text must be
// unique in the list view. It panics when the text is already exists in the
// list
func (w *ListView) AddItem(text string, style tcell.Style) {
if w.getItemIndex(text) != -1 {
panic("item " + text + " already exists")
}
v := &views.ViewPort{}
v.SetView(w.view)
t := &views.Text{}
t.SetText(text)
t.SetStyle(style)
t.SetView(v)
t.Watch(w)
item := item{name: text, text: t, view: v}
w.items = append(w.items, item)
w.changed = true
w.layout()
w.PostEventWidgetContent(w)
}
// SetStyle updates the style of the text. It panics when the text does not
// exist in the list.
func (w *ListView) SetStyle(text string, style tcell.Style) {
idx := w.getItemIndex(text)
if idx == -1 {
panic("item " + text + " not fount")
}
w.items[idx].text.SetStyle(style)
w.changed = true
w.layout()
w.PostEventWidgetContent(w)
}
// DeleteItem deletes a item with the text. It panics when the text does not
// exist in the list
func (w *ListView) DeleteItem(text string) {
idx := w.getItemIndex(text)
if idx == -1 {
panic("item " + text + " not fount")
}
item := w.items[idx]
item.text.Unwatch(w)
w.items = append(w.items[:idx], w.items[idx+1:]...)
}
// ItemCount returns the count of the items.
func (w *ListView) ItemCount() int {
return len(w.items)
}
func (w *ListView) getItemIndex(name string) int {
for i, item := range w.items {
if item.name == name {
return i
}
}
return -1
}
// SelectNext selects next item of the current
func (w *ListView) SelectNext() {
index := w.selected + 1
if index >= len(w.items) {
index = 0
}
w.SelectAt(index)
}
// SelectPrev selects previous item of the current
func (w *ListView) SelectPrev() {
index := w.selected - 1
if index < 0 {
index = len(w.items) - 1
}
w.SelectAt(index)
}
// SelectAt selects nth items by the index.
func (w *ListView) SelectAt(index int) {
if index == w.selected {
return
}
if w.selected >= 0 {
i := w.items[w.selected]
i.text.SetStyle(i.text.Style().Reverse(false))
}
if index < 0 || index >= len(w.items) {
return
}
w.selected = index
i := w.items[index]
i.text.SetStyle(i.text.Style().Reverse(true))
w.PostEventWidgetContent(w)
ev := &EventItemSelected{
Name: i.name,
Index: index,
widget: w,
}
ev.SetEventNow()
w.PostEvent(ev)
}
// Draw draws the ListView
func (w *ListView) Draw() {
if w.view == nil {
return
}
if w.changed {
w.layout()
}
for _, i := range w.items {
i.text.Draw()
}
}
// Resize is called when our View changes sizes.
func (w *ListView) Resize() {
w.layout()
w.PostEventWidgetResize(w)
}
// HandleEvent handles events on tcell
func (w *ListView) HandleEvent(ev tcell.Event) bool {
switch ev.(type) {
case *views.EventWidgetContent:
w.changed = true
w.PostEventWidgetContent(w)
return true
}
for _, item := range w.items {
if item.text.HandleEvent(ev) {
return true
}
}
return false
}
// SetView sets the View object used for the list view
func (w *ListView) SetView(view views.View) {
w.view = view
for _, item := range w.items {
item.view.SetView(view)
}
w.changed = true
}
// Size returns the width and height in vertical line.
func (w *ListView) Size() (int, int) {
return w.width, w.height
}
func (w *ListView) layout() {
vieww, _ := w.view.Size()
w.width, w.height = 0, 0
for y, item := range w.items {
textw, texth := item.text.Size()
if textw > w.width {
w.width = textw
}
if textw < vieww {
textw = vieww
}
item.view.Resize(0, y, textw, texth)
item.text.Resize()
}
w.height = len(w.items)
w.changed = false
}