From 49e6b5bfe843a1a67368ef104d980a98561afb21 Mon Sep 17 00:00:00 2001 From: Andreas Bieber Date: Wed, 16 Sep 2020 14:40:34 +0200 Subject: [PATCH] feat(treeview): Add `SetSelectedTextColor` and `SetSelectedBackgroundColor` --- treeview.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/treeview.go b/treeview.go index cb060bf..aeda64a 100644 --- a/treeview.go +++ b/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) }