change: added option to span selection highlight across width of list view

pull/220/head
ardnew 5 years ago
parent 36893a6697
commit e0f6e89550

@ -47,6 +47,9 @@ type List struct {
// If true, the selection is only shown when the list has focus.
selectedFocusOnly bool
// If true, the selected background color spans the entire view.
spanSelectionHighlight 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)
@ -139,6 +142,15 @@ func (l *List) SetSelectedFocusOnly(focusOnly bool) *List {
return l
}
// SetSpanSelectedBackgroundColor sets a flag which determines whether the
// colored background of selected items spans the entire width of the view. If
// set to true, the highlight spans the entire view. If set to false, only the
// text of the selected item from beginning to end is highlighted.
func (l *List) SetSpanSelectedBackgroundColor(span bool) *List {
l.spanSelectionHighlight = span
return l
}
// ShowSecondaryText determines whether or not to show secondary item texts.
func (l *List) ShowSecondaryText(show bool) *List {
l.showSecondaryText = show
@ -275,8 +287,16 @@ func (l *List) Draw(screen tcell.Screen) {
// Background color of selected text.
if index == l.currentItem && (!l.selectedFocusOnly || l.HasFocus()) {
textWidth := StringWidth(item.MainText)
for bx := 0; bx < textWidth && bx < width; bx++ {
// Width of background color of selected item.
var textWidth int = width
if !l.spanSelectionHighlight {
if w := StringWidth(item.MainText); w < textWidth {
textWidth = w
}
}
for bx := 0; bx < textWidth; bx++ {
m, c, style, _ := screen.GetContent(x+bx, y)
fg, _, _ := style.Decompose()
if fg == l.mainTextColor {

Loading…
Cancel
Save