From 4ac48a97482909773e98f6139c4f69cdebfea8b7 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Fri, 9 Oct 2020 08:37:54 -0700 Subject: [PATCH] Replace FormItem.SetAttributes with individual methods --- CHANGELOG | 1 - checkbox.go | 16 ---------------- dropdown.go | 16 ---------------- form.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- inputfield.go | 16 ---------------- 5 files changed, 44 insertions(+), 53 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 4e6728b..767d4ef 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/checkbox.go b/checkbox.go index 6288db0..9d15713 100644 --- a/checkbox.go +++ b/checkbox.go @@ -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) diff --git a/dropdown.go b/dropdown.go index b8a6192..3f1225a 100644 --- a/dropdown.go +++ b/dropdown.go @@ -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) diff --git a/form.go b/form.go index 00cfcfa..87e7730 100644 --- a/form.go +++ b/form.go @@ -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) + } +} diff --git a/inputfield.go b/inputfield.go index d137830..dfed8e1 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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)