Added an option to List which hides the selection when List has no focus. Resolves #193

This commit is contained in:
Oliver 2018-11-26 11:00:48 +01:00
parent f1536e67fa
commit 08411f6e81
1 changed files with 12 additions and 1 deletions

13
list.go
View File

@ -44,6 +44,9 @@ type List struct {
// The background color for selected items.
selectedBackgroundColor tcell.Color
// If true, the selection is only shown when the list has focus.
selectedFocusOnly bool
// An optional function which is called when the user has navigated to a list
// item.
changed func(index int, mainText, secondaryText string, shortcut rune)
@ -128,6 +131,14 @@ func (l *List) SetSelectedBackgroundColor(color tcell.Color) *List {
return l
}
// SetSelectedFocusOnly sets a flag which determines when the currently selected
// list item is highlighted. If set to true, selected items are only highlighted
// when the list has focus. If set to false, they are always highlighted.
func (l *List) SetSelectedFocusOnly(focusOnly bool) *List {
l.selectedFocusOnly = focusOnly
return l
}
// ShowSecondaryText determines whether or not to show secondary item texts.
func (l *List) ShowSecondaryText(show bool) *List {
l.showSecondaryText = show
@ -263,7 +274,7 @@ func (l *List) Draw(screen tcell.Screen) {
Print(screen, item.MainText, x, y, width, AlignLeft, l.mainTextColor)
// Background color of selected text.
if index == l.currentItem {
if index == l.currentItem && (!l.selectedFocusOnly || l.HasFocus()) {
textWidth := StringWidth(item.MainText)
for bx := 0; bx < textWidth && bx < width; bx++ {
m, c, style, _ := screen.GetContent(x+bx, y)