From 357a49e9e7f716f4140be8f5e769d03b8f0618ff Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 29 Sep 2020 13:21:53 -0700 Subject: [PATCH] Do not set focus-related style attributes by default --- DESIGN.md | 5 +++++ box.go | 4 ++-- checkbox.go | 18 ++++++++++++------ dropdown.go | 18 ++++++++++++------ form.go | 43 ++++++++++++++++++++++++++++--------------- inputfield.go | 26 ++++++++++++++++++-------- util.go | 4 ++++ 7 files changed, 81 insertions(+), 37 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index a68eae6..e6e9010 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -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). diff --git a/box.go b/box.go index 7f77a29..5bb2016 100644 --- a/box.go +++ b/box.go @@ -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) } diff --git a/checkbox.go b/checkbox.go index 8b96d83..c88af24 100644 --- a/checkbox.go +++ b/checkbox.go @@ -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 diff --git a/dropdown.go b/dropdown.go index af29586..4128734 100644 --- a/dropdown.go +++ b/dropdown.go @@ -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. diff --git a/form.go b/form.go index 4996bc2..64524de 100644 --- a/form.go +++ b/form.go @@ -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. diff --git a/inputfield.go b/inputfield.go index 6bdff76..e9931e2 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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. diff --git a/util.go b/util.go index 644069f..256d4e9 100644 --- a/util.go +++ b/util.go @@ -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