Do not set focus-related style attributes by default

This commit is contained in:
Trevor Slocum 2020-09-29 13:21:53 -07:00
parent a709a929d9
commit 357a49e9e7
7 changed files with 81 additions and 37 deletions

View File

@ -1,5 +1,10 @@
This document lists architectural details of cview.
# Focus-related style attributes are unset by default
This applies to all widgets except Buttons, which require a style change to
indicate focus. See [ColorUnset](https://docs.rocketnine.space/gitlab.com/tslocum/cview#pkg-variables).
# Widgets always use `sync.RWMutex`
See [#30](https://gitlab.com/tslocum/cview/-/issues/30).

4
box.go
View File

@ -81,8 +81,8 @@ func NewBox() *Box {
innerX: -1, // Mark as uninitialized.
backgroundColor: Styles.PrimitiveBackgroundColor,
borderColor: Styles.BorderColor,
borderColorFocused: Styles.BorderColor,
titleColor: Styles.TitleColor,
borderColorFocused: ColorUnset,
titleAlign: AlignCenter,
showFocus: true,
}
@ -441,7 +441,7 @@ func (b *Box) Draw(screen tcell.Screen) {
hasFocus = b.focus.HasFocus()
}
if hasFocus {
if hasFocus && b.borderColorFocused != ColorUnset {
border = SetAttributes(background.Foreground(b.borderColorFocused), b.borderAttributes)
}

View File

@ -66,12 +66,12 @@ func NewCheckBox() *CheckBox {
return &CheckBox{
Box: NewBox(),
labelColor: Styles.SecondaryTextColor,
labelColorFocused: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldBackgroundColorFocused: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.PrimaryTextColor,
checkedRune: Styles.CheckBoxCheckedRune,
labelColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
}
}
@ -285,9 +285,15 @@ func (c *CheckBox) Draw(screen tcell.Screen) {
fieldBackgroundColor := c.fieldBackgroundColor
fieldTextColor := c.fieldTextColor
if c.GetFocusable().HasFocus() {
labelColor = c.labelColorFocused
fieldBackgroundColor = c.fieldBackgroundColorFocused
fieldTextColor = c.fieldTextColorFocused
if c.labelColorFocused != ColorUnset {
labelColor = c.labelColorFocused
}
if c.fieldBackgroundColorFocused != ColorUnset {
fieldBackgroundColor = c.fieldBackgroundColorFocused
}
if c.fieldTextColorFocused != ColorUnset {
fieldTextColor = c.fieldTextColorFocused
}
}
// Prepare

View File

@ -157,14 +157,14 @@ func NewDropDown() *DropDown {
currentOption: -1,
list: list,
labelColor: Styles.SecondaryTextColor,
labelColorFocused: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldBackgroundColorFocused: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.PrimaryTextColor,
prefixTextColor: Styles.ContrastSecondaryTextColor,
dropDownSymbol: Styles.DropDownSymbol,
abbreviationChars: Styles.DropDownAbbreviationChars,
labelColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
}
d.focus = d
@ -551,9 +551,15 @@ func (d *DropDown) Draw(screen tcell.Screen) {
fieldBackgroundColor := d.fieldBackgroundColor
fieldTextColor := d.fieldTextColor
if hasFocus {
labelColor = d.labelColorFocused
fieldBackgroundColor = d.fieldBackgroundColorFocused
fieldTextColor = d.fieldTextColorFocused
if d.labelColorFocused != ColorUnset {
labelColor = d.labelColorFocused
}
if d.fieldBackgroundColorFocused != ColorUnset {
fieldBackgroundColor = d.fieldBackgroundColorFocused
}
if d.fieldTextColorFocused != ColorUnset {
fieldTextColor = d.fieldTextColorFocused
}
}
// Prepare.

43
form.go
View File

@ -20,10 +20,10 @@ type FormItemAttributes struct {
BackgroundColor tcell.Color
LabelColor tcell.Color
LabelColorFocused tcell.Color
FieldTextColor tcell.Color
FieldTextColorFocused tcell.Color
FieldBackgroundColor tcell.Color
FieldBackgroundColorFocused tcell.Color
FieldTextColor tcell.Color
FieldTextColorFocused tcell.Color
FinishedFunc func(key tcell.Key)
}
@ -124,15 +124,15 @@ func NewForm() *Form {
Box: box,
itemPadding: 1,
labelColor: Styles.SecondaryTextColor,
labelColorFocused: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldBackgroundColorFocused: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.PrimaryTextColor,
buttonBackgroundColor: Styles.ContrastBackgroundColor,
buttonBackgroundColorFocused: Styles.PrimaryTextColor,
buttonTextColor: Styles.PrimaryTextColor,
buttonTextColorFocused: Styles.ContrastBackgroundColor,
buttonBackgroundColorFocused: Styles.PrimaryTextColor,
buttonTextColorFocused: Styles.InverseTextColor,
labelColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
}
f.focus = f
@ -580,15 +580,28 @@ func (f *Form) GetAttributes() *FormItemAttributes {
}
func (f *Form) getAttributes() *FormItemAttributes {
return &FormItemAttributes{
BackgroundColor: f.backgroundColor,
LabelColor: f.labelColor,
LabelColorFocused: f.labelColorFocused,
FieldTextColor: f.fieldTextColor,
FieldTextColorFocused: f.fieldTextColorFocused,
FieldBackgroundColor: f.fieldBackgroundColor,
FieldBackgroundColorFocused: f.fieldBackgroundColorFocused,
attrs := &FormItemAttributes{
BackgroundColor: f.backgroundColor,
LabelColor: f.labelColor,
FieldBackgroundColor: f.fieldBackgroundColor,
FieldTextColor: f.fieldTextColor,
}
if f.labelColorFocused == ColorUnset {
attrs.LabelColorFocused = f.labelColor
} else {
attrs.LabelColorFocused = f.labelColorFocused
}
if f.fieldBackgroundColorFocused == ColorUnset {
attrs.FieldBackgroundColorFocused = f.fieldBackgroundColor
} else {
attrs.FieldBackgroundColorFocused = f.fieldBackgroundColorFocused
}
if f.fieldTextColorFocused == ColorUnset {
attrs.FieldTextColorFocused = f.fieldTextColor
} else {
attrs.FieldTextColorFocused = f.fieldTextColorFocused
}
return attrs
}
// Draw draws this primitive onto the screen.

View File

@ -141,19 +141,19 @@ func NewInputField() *InputField {
return &InputField{
Box: NewBox(),
labelColor: Styles.SecondaryTextColor,
labelColorFocused: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldBackgroundColorFocused: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.PrimaryTextColor,
placeholderTextColor: Styles.ContrastSecondaryTextColor,
placeholderTextColorFocused: Styles.ContrastSecondaryTextColor,
autocompleteListTextColor: Styles.PrimitiveBackgroundColor,
autocompleteListBackgroundColor: Styles.MoreContrastBackgroundColor,
autocompleteListSelectedTextColor: Styles.PrimitiveBackgroundColor,
autocompleteListSelectedBackgroundColor: Styles.PrimaryTextColor,
autocompleteSuggestionTextColor: Styles.ContrastPrimaryTextColor,
fieldNoteTextColor: Styles.SecondaryTextColor,
labelColorFocused: ColorUnset,
fieldBackgroundColorFocused: ColorUnset,
fieldTextColorFocused: ColorUnset,
placeholderTextColorFocused: ColorUnset,
}
}
@ -583,9 +583,15 @@ func (i *InputField) Draw(screen tcell.Screen) {
fieldBackgroundColor := i.fieldBackgroundColor
fieldTextColor := i.fieldTextColor
if i.GetFocusable().HasFocus() {
labelColor = i.labelColorFocused
fieldBackgroundColor = i.fieldBackgroundColorFocused
fieldTextColor = i.fieldTextColorFocused
if i.labelColorFocused != ColorUnset {
labelColor = i.labelColorFocused
}
if i.fieldBackgroundColorFocused != ColorUnset {
fieldBackgroundColor = i.fieldBackgroundColorFocused
}
if i.fieldTextColorFocused != ColorUnset {
fieldTextColor = i.fieldTextColorFocused
}
}
// Prepare
@ -627,7 +633,11 @@ func (i *InputField) Draw(screen tcell.Screen) {
text := i.text
if text == "" && i.placeholder != "" {
// Draw placeholder text.
Print(screen, Escape(i.placeholder), x, y, fieldWidth, AlignLeft, i.placeholderTextColor)
placeholderTextColor := i.placeholderTextColor
if i.GetFocusable().HasFocus() && i.placeholderTextColorFocused != ColorUnset {
placeholderTextColor = i.placeholderTextColorFocused
}
Print(screen, Escape(i.placeholder), x, y, fieldWidth, AlignLeft, placeholderTextColor)
i.offset = 0
} else {
// Draw entered text.

View File

@ -11,6 +11,10 @@ import (
"github.com/rivo/uniseg"
)
// ColorUnset represents an unset color. This is necessary because the zero
// value of color, ColorDefault, results in default terminal colors.
var ColorUnset = tcell.ColorSpecial | 108
// Text alignment within a box.
const (
AlignLeft = iota