feat(treeview): Add `SetSelectedTextColor` and `SetSelectedBackgroundColor`

This commit is contained in:
Andreas Bieber 2020-09-16 14:40:34 +02:00
parent 60cca5d967
commit 49e6b5bfe8
1 changed files with 31 additions and 1 deletions

View File

@ -342,6 +342,12 @@ type TreeView struct {
// If set to true, the tree structure is drawn using lines.
graphics bool
// The text color for selected items.
selectedTextColor *tcell.Color
// The background color for selected items.
selectedBackgroundColor *tcell.Color
// The color of the lines.
graphicsColor tcell.Color
@ -471,6 +477,22 @@ func (t *TreeView) SetGraphics(showGraphics bool) *TreeView {
return t
}
// SetSelectedTextColor sets the text color of selected items.
func (t *TreeView) SetSelectedTextColor(color tcell.Color) *TreeView {
t.Lock()
defer t.Unlock()
t.selectedTextColor = &color
return t
}
// SetSelectedBackgroundColor sets the background color of selected items.
func (t *TreeView) SetSelectedBackgroundColor(color tcell.Color) *TreeView {
t.Lock()
defer t.Unlock()
t.selectedBackgroundColor = &color
return t
}
// SetGraphicsColor sets the colors of the lines used to draw the tree structure.
func (t *TreeView) SetGraphicsColor(color tcell.Color) *TreeView {
t.Lock()
@ -848,7 +870,15 @@ func (t *TreeView) Draw(screen tcell.Screen) {
if node.textX+prefixWidth < width {
style := tcell.StyleDefault.Foreground(node.color)
if node == t.currentNode {
style = tcell.StyleDefault.Background(node.color).Foreground(t.backgroundColor)
backgroundColor := node.color
foregroundColor := t.backgroundColor
if t.selectedTextColor != nil {
foregroundColor = *t.selectedTextColor
}
if t.selectedBackgroundColor != nil {
backgroundColor = *t.selectedBackgroundColor
}
style = tcell.StyleDefault.Background(backgroundColor).Foreground(foregroundColor)
}
printWithStyle(screen, node.text, x+node.textX+prefixWidth, posY, width-node.textX-prefixWidth, AlignLeft, style)
}