-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlistbox.go
More file actions
85 lines (76 loc) · 1.99 KB
/
listbox.go
File metadata and controls
85 lines (76 loc) · 1.99 KB
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
package vtui
import (
"github.com/unxed/vtinput"
)
// listRow wraps a string for Table compatibility.
type listRow struct {
lb *ListBox
idx int
}
func (r listRow) GetCellText(col int) string { return r.lb.Items[r.idx] }
func (r listRow) IsSelected() bool { return r.lb.SelectedMap[r.idx] }
// ListBox is a single-column Table for simple string selection.
type ListBox struct {
Table
Items []string
SelectedMap map[int]bool
MultiSelect bool
}
func NewListBox(x, y, w, h int, items []string) *ListBox {
lb := &ListBox{
Table: *NewTable(x, y, w, h, []TableColumn{{Width: w}}),
Items: items,
SelectedMap: make(map[int]bool),
}
lb.ShowHeader = false
lb.ShowSeparators = false
lb.canFocus = true
lb.UpdateRows()
return lb
}
func (lb *ListBox) UpdateRows() {
rows := make([]TableRow, len(lb.Items))
for i := range lb.Items {
rows[i] = listRow{lb: lb, idx: i}
}
// Sync MarginTop/ViewHeight before calculating widths
lb.Table.SetPosition(lb.X1, lb.Y1, lb.X2, lb.Y2)
lb.SetRows(rows)
if len(lb.Columns) > 0 {
lb.Columns[0].Width = lb.GetContentWidth()
}
}
func (lb *ListBox) GetSelectedIndices() []int {
var res []int
for i := range lb.Items {
if lb.SelectedMap[i] { res = append(res, i) }
}
return res
}
// SelectName searches for an item by its string value and moves the cursor to it.
func (lb *ListBox) SelectName(name string) {
for i, item := range lb.Items {
if item == name {
lb.SetSelectPos(i)
return
}
}
}
func (lb *ListBox) SetPosition(x1, y1, x2, y2 int) {
lb.Table.SetPosition(x1, y1, x2, y2)
if len(lb.Columns) > 0 {
lb.Columns[0].Width = lb.GetContentWidth()
}
}
func (lb *ListBox) ProcessKey(e *vtinput.InputEvent) bool {
if !e.KeyDown || lb.IsDisabled() { return false }
switch e.VirtualKeyCode {
case vtinput.VK_SPACE, vtinput.VK_INSERT:
if lb.MultiSelect {
lb.SelectedMap[lb.SelectPos] = !lb.SelectedMap[lb.SelectPos]
if e.VirtualKeyCode == vtinput.VK_INSERT { lb.MoveRelative(1) }
return true
}
}
return lb.Table.ProcessKey(e)
}