1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-26 13:49:06 +08:00

Add SelectItem to list view

This allows the user to programatically call an items select function as
if the Enter key had been pressed without requiring the use of fake key
signals that act as a sort of "shadow" API on top of the actual Go API.

Fixes #1023

Signed-off-by: Sam Whited <sam@samwhited.com>
This commit is contained in:
Sam Whited 2024-09-13 07:45:04 -04:00
parent fd649dbf12
commit 0be52430c6
No known key found for this signature in database
GPG Key ID: 16D5138E52B849B3

23
list.go
View File

@ -101,6 +101,19 @@ func NewList() *List {
}
}
// SelectItem calls the given item's select function (if defined).
func (l *List) SelectItem(index int) {
if index >= 0 && index < len(l.items) {
item := l.items[index]
if item.Selected != nil {
item.Selected()
}
if l.selected != nil {
l.selected(index, item.MainText, item.SecondaryText, item.Shortcut)
}
}
}
// SetCurrentItem sets the currently selected item by its index, starting at 0
// for the first item. If a negative index is provided, items are referred to
// from the back (-1 = last item, -2 = second-to-last item, and so on). Out of
@ -607,15 +620,7 @@ func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
l.currentItem = 0
}
case tcell.KeyEnter:
if l.currentItem >= 0 && l.currentItem < len(l.items) {
item := l.items[l.currentItem]
if item.Selected != nil {
item.Selected()
}
if l.selected != nil {
l.selected(l.currentItem, item.MainText, item.SecondaryText, item.Shortcut)
}
}
l.SelectItem(l.currentItem)
case tcell.KeyRune:
ch := event.Rune()
if ch != ' ' {