Add TextView highlight color options

This commit is contained in:
Trevor Slocum 2021-05-30 18:17:17 -07:00
parent 7e8817f20b
commit 011f4decc0
5 changed files with 61 additions and 42 deletions

View File

@ -1,5 +1,6 @@
v1.5.6 (WIP)
- Add TrueColorTags option and do not use TrueColor tag values by default
- Add TextView.SetHighlightForegroundColor and TextView.SetHighlightBackgroundColor
- Draw additional accents when rendering a list divider
- Update List, Table and TreeView to not handle Tab or Backtab
- Allow specifying TabbedPanels switcher height

View File

@ -153,12 +153,12 @@ func NewForm() *Form {
labelColor: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.ContrastPrimaryTextColor,
buttonBackgroundColor: Styles.ContrastBackgroundColor,
buttonBackgroundColorFocused: Styles.PrimaryTextColor,
buttonTextColor: Styles.PrimaryTextColor,
buttonTextColorFocused: Styles.InverseTextColor,
labelColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
}

View File

@ -54,9 +54,9 @@ var Styles = Theme{
PrimaryTextColor: tcell.ColorWhite.TrueColor(),
SecondaryTextColor: tcell.ColorYellow.TrueColor(),
TertiaryTextColor: tcell.ColorGreen.TrueColor(),
InverseTextColor: tcell.ColorBlue.TrueColor(),
InverseTextColor: tcell.ColorBlack.TrueColor(),
ContrastPrimaryTextColor: tcell.ColorBlack.TrueColor(),
ContrastSecondaryTextColor: tcell.ColorDarkCyan.TrueColor(),
ContrastSecondaryTextColor: tcell.ColorDimGray.TrueColor(),
PrimitiveBackgroundColor: tcell.ColorBlack.TrueColor(),
ContrastBackgroundColor: tcell.ColorBlue.TrueColor(),

View File

@ -18,11 +18,6 @@ type TabbedPanels struct {
tabLabels map[string]string
currentTab string
tabTextColor tcell.Color
tabTextColorFocused tcell.Color
tabBackgroundColor tcell.Color
tabBackgroundColorFocused tcell.Color
dividerStart string
dividerMid string
dividerEnd string
@ -41,20 +36,18 @@ type TabbedPanels struct {
// NewTabbedPanels returns a new TabbedPanels object.
func NewTabbedPanels() *TabbedPanels {
t := &TabbedPanels{
Flex: NewFlex(),
Switcher: NewTextView(),
panels: NewPanels(),
tabTextColor: Styles.PrimaryTextColor,
tabTextColorFocused: Styles.InverseTextColor,
tabBackgroundColor: ColorUnset,
tabBackgroundColorFocused: Styles.PrimaryTextColor,
dividerMid: string(BoxDrawingsDoubleVertical),
dividerEnd: string(BoxDrawingsLightVertical),
tabLabels: make(map[string]string),
Flex: NewFlex(),
Switcher: NewTextView(),
panels: NewPanels(),
dividerMid: string(BoxDrawingsDoubleVertical),
dividerEnd: string(BoxDrawingsLightVertical),
tabLabels: make(map[string]string),
}
s := t.Switcher
s.SetDynamicColors(true)
s.SetHighlightForegroundColor(Styles.InverseTextColor)
s.SetHighlightBackgroundColor(Styles.PrimaryTextColor)
s.SetRegions(true)
s.SetScrollable(true)
s.SetWrap(true)
@ -69,7 +62,6 @@ func NewTabbedPanels() *TabbedPanels {
if t.setFocus != nil {
t.setFocus(t.panels)
}
s.Highlight()
})
t.rebuild()
@ -124,7 +116,17 @@ func (t *TabbedPanels) SetCurrentTab(name string) {
t.Unlock()
t.Switcher.Highlight(t.currentTab)
h := t.Switcher.GetHighlights()
var found bool
for _, hl := range h {
if hl == name {
found = true
break
}
}
if !found {
t.Switcher.Highlight(t.currentTab)
}
t.Switcher.ScrollToHighlight()
}
@ -150,31 +152,23 @@ func (t *TabbedPanels) SetTabLabel(name, label string) {
// SetTabTextColor sets the color of the tab text.
func (t *TabbedPanels) SetTabTextColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabTextColor = color
t.Switcher.SetTextColor(color)
}
// SetTabTextColorFocused sets the color of the tab text when the tab is in focus.
func (t *TabbedPanels) SetTabTextColorFocused(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabTextColorFocused = color
t.Switcher.SetHighlightForegroundColor(color)
}
// SetTabBackgroundColor sets the background color of the tab.
func (t *TabbedPanels) SetTabBackgroundColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabBackgroundColor = color
t.Switcher.SetBackgroundColor(color)
}
// SetTabBackgroundColorFocused sets the background color of the tab when the
// tab is in focus.
func (t *TabbedPanels) SetTabBackgroundColorFocused(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.tabBackgroundColorFocused = color
t.Switcher.SetHighlightBackgroundColor(color)
}
// SetTabSwitcherDivider sets the tab switcher divider text. Color tags are supported.
@ -274,13 +268,6 @@ func (t *TabbedPanels) updateTabLabels() {
b.WriteRune(' ')
}
textColor := t.tabTextColor
backgroundColor := t.tabBackgroundColor
if panel.Name == t.currentTab {
textColor = t.tabTextColorFocused
backgroundColor = t.tabBackgroundColorFocused
}
label := t.tabLabels[panel.Name]
if !t.switcherVertical {
label = " " + label
@ -290,7 +277,7 @@ func (t *TabbedPanels) updateTabLabels() {
spacer = bytes.Repeat([]byte(" "), maxWidth-len(label)+1)
}
b.WriteString(fmt.Sprintf(`["%s"][%s:%s]%s%s[-:-][""]`, panel.Name, ColorHex(textColor), ColorHex(backgroundColor), label, spacer))
b.WriteString(fmt.Sprintf(`["%s"]%s%s[""]`, panel.Name, label, spacer))
if i == l-1 && !t.switcherVertical {
b.WriteString(t.dividerEnd)

View File

@ -179,6 +179,12 @@ type TextView struct {
// The (starting) color of the text.
textColor tcell.Color
// The foreground color of highlighted text.
highlightForeground tcell.Color
// The background color of highlighted text.
highlightBackground tcell.Color
// If set to true, the text color can be changed dynamically by piping color
// strings in square brackets to the text view.
dynamicColors bool
@ -222,6 +228,8 @@ func NewTextView() *TextView {
align: AlignLeft,
wrap: true,
textColor: Styles.PrimaryTextColor,
highlightForeground: Styles.PrimitiveBackgroundColor,
highlightBackground: Styles.PrimaryTextColor,
}
}
@ -304,6 +312,22 @@ func (t *TextView) SetTextColor(color tcell.Color) {
t.textColor = color
}
// SetHighlightForegroundColor sets the foreground color of highlighted text.
func (t *TextView) SetHighlightForegroundColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.highlightForeground = color
}
// SetHighlightBackgroundColor sets the foreground color of highlighted text.
func (t *TextView) SetHighlightBackgroundColor(color tcell.Color) {
t.Lock()
defer t.Unlock()
t.highlightBackground = color
}
// SetBytes sets the text of this text view to the provided byte slice.
// Previously contained text will be removed.
func (t *TextView) SetBytes(text []byte) {
@ -1179,7 +1203,14 @@ func (t *TextView) Draw(screen tcell.Screen) {
}
}
if highlighted {
fg, bg, _ := style.Decompose()
fg := t.highlightForeground
bg := t.highlightBackground
if fg == tcell.ColorDefault {
fg = Styles.PrimaryTextColor
if fg == tcell.ColorDefault {
fg = tcell.ColorWhite.TrueColor()
}
}
if bg == tcell.ColorDefault {
r, g, b := fg.RGB()
c := colorful.Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255}
@ -1190,7 +1221,7 @@ func (t *TextView) Draw(screen tcell.Screen) {
bg = tcell.ColorBlack.TrueColor()
}
}
style = style.Background(fg).Foreground(bg)
style = style.Foreground(fg).Background(bg)
}
// Skip to the right.