Browse Source

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

tablepad
Andreas Bieber 2 years ago
parent
commit
49e6b5bfe8
  1. 32
      treeview.go

32
treeview.go

@ -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)
}

Loading…
Cancel
Save