2
0
mirror of https://github.com/rivo/tview.git synced 2024-11-15 06:12:46 +00:00

add new method for list

add new method for list

remove "last" member
This commit is contained in:
raziman 2020-07-26 20:17:32 +08:00
parent c65badfc3d
commit 4df6307f77

25
list.go
View File

@ -57,6 +57,9 @@ type List struct {
// The number of list items skipped at the top before the first item is drawn.
offset int
// Number of items drawn
itemsDrawn int
// An optional function which is called when the user has navigated to a list
// item.
changed func(index int, mainText, secondaryText string, shortcut rune)
@ -67,6 +70,7 @@ type List struct {
// An optional function which is called when the user presses the Escape key.
done func()
}
// NewList returns a new form.
@ -399,6 +403,7 @@ func (l *List) Draw(screen tcell.Screen) {
bottomLimit = totalHeight
}
// Do we show any shortcuts?
var showShortcuts bool
for _, item := range l.items {
@ -423,6 +428,8 @@ func (l *List) Draw(screen tcell.Screen) {
}
}
l.itemsDrawn = 0
// Draw the list items.
for index, item := range l.items {
if index < l.offset {
@ -440,6 +447,7 @@ func (l *List) Draw(screen tcell.Screen) {
// Main text.
Print(screen, item.MainText, x, y, width, AlignLeft, l.mainTextColor)
l.itemsDrawn++
// Background color of selected text.
if index == l.currentItem && (!l.selectedFocusOnly || l.HasFocus()) {
@ -472,9 +480,26 @@ func (l *List) Draw(screen tcell.Screen) {
Print(screen, item.SecondaryText, x, y, width, AlignLeft, l.secondaryTextColor)
y++
}
}
}
// GetFirst returns the first index of item drawn
func (l *List) GetFirst() int {
return l.offset
}
// GetLast returns the last index of item drawn. If showSecondaryText set to true,
// GetLast returns the second last index of item drawn to prevent from scrolling
func (l *List) GetLast() int {
lastIndex := l.itemsDrawn - 1 + l.offset
if l.showSecondaryText {
return lastIndex - 1
}
return lastIndex
}
// InputHandler returns the handler for this primitive.
func (l *List) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return l.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {