Replace FormItem.SetAttributes with individual methods

This commit is contained in:
Trevor Slocum 2020-10-09 08:37:54 -07:00
parent 94351006a1
commit 4ac48a9748
5 changed files with 44 additions and 53 deletions

View File

@ -15,7 +15,6 @@ v1.5.0 (2020-10-03)
- Add arrow symbol to DropDown
- Add Makefile
- Allow autocomplete selection value customization
- Revamp FormItem styling as FormItem.SetAttributes
- Provide DropDownOption in DropDown handlers
- Provide ListItem in List handlers
- Panic when attempting to add an invalid FormItem

View File

@ -237,22 +237,6 @@ func (c *CheckBox) SetFinishedFunc(handler func(key tcell.Key)) {
c.finished = handler
}
// SetAttributes applies attribute settings to a form item.
func (c *CheckBox) SetAttributes(attrs *FormItemAttributes) {
c.SetLabelWidth(attrs.LabelWidth)
c.SetBackgroundColor(attrs.BackgroundColor)
c.SetLabelColor(attrs.LabelColor)
c.SetLabelColorFocused(attrs.LabelColorFocused)
c.SetFieldTextColor(attrs.FieldTextColor)
c.SetFieldTextColorFocused(attrs.FieldTextColorFocused)
c.SetFieldBackgroundColor(attrs.FieldBackgroundColor)
c.SetFieldBackgroundColorFocused(attrs.FieldBackgroundColorFocused)
if attrs.FinishedFunc != nil {
c.SetFinishedFunc(attrs.FinishedFunc)
}
}
// Draw draws this primitive onto the screen.
func (c *CheckBox) Draw(screen tcell.Screen) {
c.Box.Draw(screen)

View File

@ -489,22 +489,6 @@ func (d *DropDown) SetFinishedFunc(handler func(key tcell.Key)) {
d.finished = handler
}
// SetAttributes applies attribute settings to a form item.
func (d *DropDown) SetAttributes(attrs *FormItemAttributes) {
d.SetLabelWidth(attrs.LabelWidth)
d.SetBackgroundColor(attrs.BackgroundColor)
d.SetLabelColor(attrs.LabelColor)
d.SetLabelColorFocused(attrs.LabelColorFocused)
d.SetFieldTextColor(attrs.FieldTextColor)
d.SetFieldTextColorFocused(attrs.FieldTextColorFocused)
d.SetFieldBackgroundColor(attrs.FieldBackgroundColor)
d.SetFieldBackgroundColorFocused(attrs.FieldBackgroundColorFocused)
if attrs.FinishedFunc != nil {
d.SetFinishedFunc(attrs.FinishedFunc)
}
}
// Draw draws this primitive onto the screen.
func (d *DropDown) Draw(screen tcell.Screen) {
d.Box.Draw(screen)

48
form.go
View File

@ -37,6 +37,16 @@ type FormItem interface {
// GetLabel returns the item's label text.
GetLabel() string
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
// primitive to use the width of the label string.
SetLabelWidth(int)
// SetLabelColor sets the color of the label.
SetLabelColor(tcell.Color)
// SetLabelColor sets the color of the label when focused.
SetLabelColorFocused(tcell.Color)
// GetFieldWidth returns the width of the form item's field (the area which
// is manipulated by the user) in number of screen cells. A value of 0
// indicates the the field width is flexible and may use as much space as
@ -46,8 +56,23 @@ type FormItem interface {
// GetFieldHeight returns the height of the form item.
GetFieldHeight() int
// SetAttributes applies attributes to the form item.
SetAttributes(attrs *FormItemAttributes)
// SetFieldTextColor sets the text color of the input area.
SetFieldTextColor(tcell.Color)
// SetFieldTextColorFocused sets the text color of the input area when focused.
SetFieldTextColorFocused(tcell.Color)
// SetFieldBackgroundColor sets the background color of the input area.
SetFieldBackgroundColor(tcell.Color)
// SetFieldBackgroundColor sets the background color of the input area when focused.
SetFieldBackgroundColorFocused(tcell.Color)
// SetBackgroundColor sets the background color of the form item.
SetBackgroundColor(tcell.Color)
// SetFinishedFunc sets a callback invoked when the user leaves the form item.
SetFinishedFunc(func(key tcell.Key))
}
// Form allows you to combine multiple one-line form elements into a vertical
@ -658,7 +683,7 @@ func (f *Form) Draw(screen tcell.Screen) {
attributes := f.getAttributes()
attributes.LabelWidth = labelWidth
item.SetAttributes(attributes)
setFormItemAttributes(item, attributes)
// Save position.
positions[index].x = x
@ -851,7 +876,7 @@ func (f *Form) Focus(delegate func(p Primitive)) {
f.Unlock()
item.SetAttributes(attributes)
setFormItemAttributes(item, attributes)
delegate(item)
} else {
// We're selecting a button.
@ -927,3 +952,18 @@ func (f *Form) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
return
})
}
func setFormItemAttributes(item FormItem, attrs *FormItemAttributes) {
item.SetLabelWidth(attrs.LabelWidth)
item.SetBackgroundColor(attrs.BackgroundColor)
item.SetLabelColor(attrs.LabelColor)
item.SetLabelColorFocused(attrs.LabelColorFocused)
item.SetFieldTextColor(attrs.FieldTextColor)
item.SetFieldTextColorFocused(attrs.FieldTextColorFocused)
item.SetFieldBackgroundColor(attrs.FieldBackgroundColor)
item.SetFieldBackgroundColorFocused(attrs.FieldBackgroundColorFocused)
if attrs.FinishedFunc != nil {
item.SetFinishedFunc(attrs.FinishedFunc)
}
}

View File

@ -536,22 +536,6 @@ func (i *InputField) SetFinishedFunc(handler func(key tcell.Key)) {
i.finished = handler
}
// SetAttributes applies attribute settings to a form item.
func (i *InputField) SetAttributes(attrs *FormItemAttributes) {
i.SetLabelWidth(attrs.LabelWidth)
i.SetBackgroundColor(attrs.BackgroundColor)
i.SetLabelColor(attrs.LabelColor)
i.SetLabelColorFocused(attrs.LabelColorFocused)
i.SetFieldTextColor(attrs.FieldTextColor)
i.SetFieldTextColorFocused(attrs.FieldTextColorFocused)
i.SetFieldBackgroundColor(attrs.FieldBackgroundColor)
i.SetFieldBackgroundColorFocused(attrs.FieldBackgroundColorFocused)
if attrs.FinishedFunc != nil {
i.SetFinishedFunc(attrs.FinishedFunc)
}
}
// Draw draws this primitive onto the screen.
func (i *InputField) Draw(screen tcell.Screen) {
i.Box.Draw(screen)