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

Use styles in list.go

This commit is contained in:
Mario Domgoergen 2021-05-20 22:13:11 +02:00
parent 3ff48db0b5
commit 8f759a0c7a

69
list.go
View File

@ -30,23 +30,17 @@ type List struct {
// Whether or not to show the secondary item texts. // Whether or not to show the secondary item texts.
showSecondaryText bool showSecondaryText bool
// The item main text color. // The item main text style.
mainTextColor tcell.Color mainTextStyle tcell.Style
// The item secondary text color. // The item secondary text style.
secondaryTextColor tcell.Color secondaryTextStyle tcell.Style
// The item shortcut text color. // The item shortcut text style.
shortcutColor tcell.Color shortcutStyle tcell.Style
// The text color for selected items. // The text style for selected items.
selectedTextColor tcell.Color selectedTextStyle tcell.Style
// The background color for selected items.
selectedBackgroundColor tcell.Color
// Whether or not to reverse color for selected items.
selectedReverseColor bool
// If true, the selection is only shown when the list has focus. // If true, the selection is only shown when the list has focus.
selectedFocusOnly bool selectedFocusOnly bool
@ -84,15 +78,15 @@ type List struct {
// NewList returns a new form. // NewList returns a new form.
func NewList() *List { func NewList() *List {
base := tcell.StyleDefault.Background(Styles.PrimitiveBackgroundColor)
return &List{ return &List{
Box: NewBox(), Box: NewBox(),
showSecondaryText: true, showSecondaryText: true,
wrapAround: true, wrapAround: true,
mainTextColor: Styles.PrimaryTextColor, mainTextStyle: base.Foreground(Styles.PrimaryTextColor),
secondaryTextColor: Styles.TertiaryTextColor, secondaryTextStyle: base.Foreground(Styles.TertiaryTextColor),
shortcutColor: Styles.SecondaryTextColor, shortcutStyle: base.Foreground(Styles.SecondaryTextColor),
selectedTextColor: Styles.PrimitiveBackgroundColor, selectedTextStyle: base.Foreground(Styles.PrimitiveBackgroundColor).Background(Styles.PrimaryTextColor),
selectedBackgroundColor: Styles.PrimaryTextColor,
} }
} }
@ -199,39 +193,31 @@ func (l *List) RemoveItem(index int) *List {
// SetMainTextColor sets the color of the items' main text. // SetMainTextColor sets the color of the items' main text.
func (l *List) SetMainTextColor(color tcell.Color) *List { func (l *List) SetMainTextColor(color tcell.Color) *List {
l.mainTextColor = color l.mainTextStyle.Foreground(color)
return l return l
} }
// SetSecondaryTextColor sets the color of the items' secondary text. // SetSecondaryTextColor sets the color of the items' secondary text.
func (l *List) SetSecondaryTextColor(color tcell.Color) *List { func (l *List) SetSecondaryTextColor(color tcell.Color) *List {
l.secondaryTextColor = color l.secondaryTextStyle.Foreground(color)
return l return l
} }
// SetShortcutColor sets the color of the items' shortcut. // SetShortcutColor sets the color of the items' shortcut.
func (l *List) SetShortcutColor(color tcell.Color) *List { func (l *List) SetShortcutColor(color tcell.Color) *List {
l.shortcutColor = color l.shortcutStyle.Foreground(color)
return l return l
} }
// SetSelectedTextColor sets the text color of selected items. // SetSelectedTextColor sets the text color of selected items.
func (l *List) SetSelectedTextColor(color tcell.Color) *List { func (l *List) SetSelectedTextColor(color tcell.Color) *List {
l.selectedTextColor = color l.selectedTextStyle.Foreground(color)
return l return l
} }
// SetSelectedBackgroundColor sets the background color of selected items. // SetSelectedBackgroundColor sets the background color of selected items.
func (l *List) SetSelectedBackgroundColor(color tcell.Color) *List { func (l *List) SetSelectedBackgroundColor(color tcell.Color) *List {
l.selectedBackgroundColor = color l.selectedTextStyle.Background(color)
return l
}
// SetSelectedReverseColor sets a flag which determines if the colors
// of the selected items are reversed. If set to true, the colors set by
// SetSelectedTextColor and SetSelectedBackgroundColor are ignored.
func (l *List) SetSelectedReverseColor(reverse bool) *List {
l.selectedReverseColor = reverse
return l return l
} }
@ -484,11 +470,11 @@ func (l *List) Draw(screen tcell.Screen) {
// Shortcuts. // Shortcuts.
if showShortcuts && item.Shortcut != 0 { if showShortcuts && item.Shortcut != 0 {
Print(screen, fmt.Sprintf("(%s)", string(item.Shortcut)), x-5, y, 4, AlignRight, l.shortcutColor) printWithStyle(screen, fmt.Sprintf("(%s)", string(item.Shortcut)), x-5, y, 0, 4, AlignRight, l.shortcutStyle, false)
} }
// Main text. // Main text.
_, printedWidth, _, end := printWithStyle(screen, item.MainText, x, y, l.horizontalOffset, width, AlignLeft, tcell.StyleDefault.Foreground(l.mainTextColor), true) _, printedWidth, _, end := printWithStyle(screen, item.MainText, x, y, l.horizontalOffset, width, AlignLeft, l.mainTextStyle, false)
if printedWidth > maxWidth { if printedWidth > maxWidth {
maxWidth = printedWidth maxWidth = printedWidth
} }
@ -506,17 +492,8 @@ func (l *List) Draw(screen tcell.Screen) {
} }
for bx := 0; bx < textWidth; bx++ { for bx := 0; bx < textWidth; bx++ {
m, c, style, _ := screen.GetContent(x+bx, y) m, c, _, _ := screen.GetContent(x+bx, y)
fg, _, _ := style.Decompose() screen.SetContent(x+bx, y, m, c, l.selectedTextStyle)
if fg == l.mainTextColor {
fg = l.selectedTextColor
}
if l.selectedReverseColor {
style = style.Reverse(true)
} else {
style = style.Background(l.selectedBackgroundColor).Foreground(fg)
}
screen.SetContent(x+bx, y, m, c, style)
} }
} }
@ -528,7 +505,7 @@ func (l *List) Draw(screen tcell.Screen) {
// Secondary text. // Secondary text.
if l.showSecondaryText { if l.showSecondaryText {
_, printedWidth, _, end := printWithStyle(screen, item.SecondaryText, x, y, l.horizontalOffset, width, AlignLeft, tcell.StyleDefault.Foreground(l.secondaryTextColor), true) _, printedWidth, _, end := printWithStyle(screen, item.SecondaryText, x, y, l.horizontalOffset, width, AlignLeft, l.secondaryTextStyle, false)
if printedWidth > maxWidth { if printedWidth > maxWidth {
maxWidth = printedWidth maxWidth = printedWidth
} }