Added a style function for form element colours.

This commit is contained in:
Oliver 2024-11-02 16:24:10 +01:00
parent 3e06d118c3
commit 65faf5cfc7
1 changed files with 15 additions and 11 deletions

26
form.go
View File

@ -84,11 +84,8 @@ type Form struct {
// The label color.
labelColor tcell.Color
// The background color of the input area.
fieldBackgroundColor tcell.Color
// The text color of the input area.
fieldTextColor tcell.Color
// The style of the input area.
fieldStyle tcell.Style
// The style of the buttons when they are not focused.
buttonStyle tcell.Style
@ -115,8 +112,7 @@ func NewForm() *Form {
Box: box,
itemPadding: 1,
labelColor: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
fieldStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
buttonStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
buttonActivatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.ContrastBackgroundColor),
buttonDisabledStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.ContrastSecondaryTextColor),
@ -151,13 +147,20 @@ func (f *Form) SetLabelColor(color tcell.Color) *Form {
// SetFieldBackgroundColor sets the background color of the input areas.
func (f *Form) SetFieldBackgroundColor(color tcell.Color) *Form {
f.fieldBackgroundColor = color
f.fieldStyle = f.fieldStyle.Background(color)
return f
}
// SetFieldTextColor sets the text color of the input areas.
func (f *Form) SetFieldTextColor(color tcell.Color) *Form {
f.fieldTextColor = color
f.fieldStyle = f.fieldStyle.Foreground(color)
return f
}
// SetFieldStyle sets the style of the input areas. Attributes are currently
// still ignored to maintain backwards compatibility.
func (f *Form) SetFieldStyle(style tcell.Style) *Form {
f.fieldStyle = style
return f
}
@ -570,12 +573,13 @@ func (f *Form) Draw(screen tcell.Screen) {
if x+itemWidth >= rightLimit {
itemWidth = rightLimit - x
}
fieldTextColor, fieldBackgroundColor, _ := f.fieldStyle.Decompose()
item.SetFormAttributes(
labelWidth,
f.labelColor,
f.backgroundColor,
f.fieldTextColor,
f.fieldBackgroundColor,
fieldTextColor,
fieldBackgroundColor,
)
// Save position.