2017-12-15 22:29:21 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// FormItem is the interface all form items must implement to be able to be
|
|
|
|
// included in a form.
|
|
|
|
type FormItem interface {
|
|
|
|
Primitive
|
|
|
|
|
|
|
|
// GetLabel returns the item's label text.
|
|
|
|
GetLabel() string
|
|
|
|
|
|
|
|
// SetFormAttributes sets a number of item attributes at once.
|
|
|
|
SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem
|
|
|
|
|
|
|
|
// SetEnteredFunc sets the handler function for when the user finished
|
|
|
|
// entering data into the item. The handler may receive events for the
|
|
|
|
// Enter key (we're done), the Escape key (cancel input), the Tab key (move to
|
|
|
|
// next field), and the Backtab key (move to previous field).
|
|
|
|
SetFinishedFunc(handler func(key tcell.Key)) FormItem
|
|
|
|
|
|
|
|
// GetFocusable returns the item's Focusable.
|
|
|
|
GetFocusable() Focusable
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// Form is a Box which contains multiple input fields, one per row.
|
|
|
|
type Form struct {
|
2017-12-17 05:48:26 +08:00
|
|
|
*Box
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// The items of the form (one row per item).
|
2017-12-19 03:04:52 +08:00
|
|
|
items []FormItem
|
2017-12-15 22:29:21 +08:00
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// The buttons of the form.
|
|
|
|
buttons []*Button
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// The number of empty rows between items.
|
|
|
|
itemPadding int
|
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// The index of the item or button which has focus. (Items are counted first,
|
|
|
|
// buttons are counted last.)
|
|
|
|
focusedElement int
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewForm returns a new form.
|
|
|
|
func NewForm() *Form {
|
2017-12-17 05:48:26 +08:00
|
|
|
box := NewBox()
|
|
|
|
|
|
|
|
f := &Form{
|
|
|
|
Box: box,
|
2017-12-15 22:29:21 +08:00
|
|
|
itemPadding: 1,
|
|
|
|
labelColor: tcell.ColorYellow,
|
|
|
|
fieldBackgroundColor: tcell.ColorBlue,
|
|
|
|
fieldTextColor: tcell.ColorWhite,
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
|
|
|
|
f.focus = f
|
|
|
|
|
|
|
|
return f
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetItemPadding sets the number of empty rows between form items.
|
|
|
|
func (f *Form) SetItemPadding(padding int) *Form {
|
|
|
|
f.itemPadding = padding
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLabelColor sets the color of the labels.
|
|
|
|
func (f *Form) SetLabelColor(color tcell.Color) *Form {
|
|
|
|
f.labelColor = color
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldBackgroundColor sets the background color of the input areas.
|
|
|
|
func (f *Form) SetFieldBackgroundColor(color tcell.Color) *Form {
|
|
|
|
f.fieldBackgroundColor = color
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the input areas.
|
|
|
|
func (f *Form) SetFieldTextColor(color tcell.Color) *Form {
|
|
|
|
f.fieldTextColor = color
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// AddInputField adds an input field to the form. It has a label, an optional
|
|
|
|
// initial value, a field length (a value of 0 extends it as far as possible),
|
|
|
|
// and an optional accept function to validate the item's value (set to nil to
|
2017-12-15 22:29:21 +08:00
|
|
|
// accept any text).
|
2017-12-19 03:04:52 +08:00
|
|
|
func (f *Form) AddInputField(label, value string, fieldLength int, accept func(textToCheck string, lastChar rune) bool) *Form {
|
2017-12-15 22:29:21 +08:00
|
|
|
f.items = append(f.items, NewInputField().
|
|
|
|
SetLabel(label).
|
|
|
|
SetText(value).
|
|
|
|
SetFieldLength(fieldLength).
|
|
|
|
SetAcceptanceFunc(accept))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:04:52 +08:00
|
|
|
// AddDropDown adds a drop-down element to the form. It has a label, options,
|
|
|
|
// and an (optional) callback function which is invoked when an option was
|
|
|
|
// selected.
|
|
|
|
func (f *Form) AddDropDown(label string, options []string, initialOption int, selected func(option string, optionIndex int)) *Form {
|
|
|
|
f.items = append(f.items, NewDropDown().
|
|
|
|
SetLabel(label).
|
|
|
|
SetCurrentOption(initialOption).
|
|
|
|
SetOptions(options, selected))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// AddButton adds a new button to the form. The "selected" function is called
|
|
|
|
// when the user selects this button. It may be nil.
|
|
|
|
func (f *Form) AddButton(label string, selected func()) *Form {
|
|
|
|
f.buttons = append(f.buttons, NewButton(label).SetSelectedFunc(selected))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (f *Form) Draw(screen tcell.Screen) {
|
|
|
|
f.Box.Draw(screen)
|
|
|
|
|
|
|
|
// Determine the dimensions.
|
|
|
|
x := f.x
|
|
|
|
y := f.y
|
|
|
|
width := f.width
|
|
|
|
bottomLimit := f.y + f.height
|
|
|
|
if f.border {
|
|
|
|
x++
|
|
|
|
y++
|
|
|
|
width -= 2
|
|
|
|
bottomLimit -= 2
|
|
|
|
}
|
2017-12-16 06:03:01 +08:00
|
|
|
rightLimit := x + width
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// Find the longest label.
|
|
|
|
var labelLength int
|
2017-12-19 03:04:52 +08:00
|
|
|
for _, item := range f.items {
|
|
|
|
label := strings.TrimSpace(item.GetLabel())
|
2017-12-15 22:29:21 +08:00
|
|
|
if len([]rune(label)) > labelLength {
|
|
|
|
labelLength = len([]rune(label))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
labelLength++ // Add one space.
|
|
|
|
|
|
|
|
// Set up and draw the input fields.
|
2017-12-19 03:04:52 +08:00
|
|
|
for _, item := range f.items {
|
2017-12-15 22:29:21 +08:00
|
|
|
if y >= bottomLimit {
|
2017-12-16 06:03:01 +08:00
|
|
|
return // Stop here.
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
label := strings.TrimSpace(item.GetLabel())
|
|
|
|
item.SetFormAttributes(
|
|
|
|
label+strings.Repeat(" ", labelLength-len([]rune(label))),
|
|
|
|
f.labelColor,
|
|
|
|
f.backgroundColor,
|
|
|
|
f.fieldTextColor,
|
|
|
|
f.fieldBackgroundColor,
|
|
|
|
).SetRect(x, y, width, 1)
|
|
|
|
if item.GetFocusable().HasFocus() {
|
|
|
|
defer item.Draw(screen)
|
|
|
|
} else {
|
|
|
|
item.Draw(screen)
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
y += 1 + f.itemPadding
|
|
|
|
}
|
2017-12-16 06:03:01 +08:00
|
|
|
|
|
|
|
// Draw the buttons.
|
|
|
|
if f.itemPadding == 0 {
|
|
|
|
y++
|
|
|
|
}
|
|
|
|
if y >= bottomLimit {
|
|
|
|
return // Stop here.
|
|
|
|
}
|
|
|
|
for _, button := range f.buttons {
|
|
|
|
space := rightLimit - x
|
|
|
|
if space < 1 {
|
|
|
|
return // No space for this button anymore.
|
|
|
|
}
|
|
|
|
buttonWidth := len([]rune(button.GetLabel())) + 4
|
|
|
|
if buttonWidth > space {
|
|
|
|
buttonWidth = space
|
|
|
|
}
|
|
|
|
button.SetRect(x, y, buttonWidth, 1)
|
|
|
|
button.Draw(screen)
|
|
|
|
|
|
|
|
x += buttonWidth + 2
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus is called by the application when the primitive receives focus.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (f *Form) Focus(delegate func(p Primitive)) {
|
2017-12-16 06:03:01 +08:00
|
|
|
if len(f.items)+len(f.buttons) == 0 {
|
2017-12-15 22:29:21 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
// Hand on the focus to one of our child elements.
|
|
|
|
if f.focusedElement < 0 || f.focusedElement >= len(f.items)+len(f.buttons) {
|
|
|
|
f.focusedElement = 0
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-16 06:03:01 +08:00
|
|
|
handler := func(key tcell.Key) {
|
2017-12-15 22:29:21 +08:00
|
|
|
switch key {
|
2017-12-17 05:48:26 +08:00
|
|
|
case tcell.KeyTab, tcell.KeyEnter:
|
2017-12-16 06:03:01 +08:00
|
|
|
f.focusedElement++
|
|
|
|
case tcell.KeyBacktab:
|
|
|
|
f.focusedElement--
|
|
|
|
if f.focusedElement < 0 {
|
|
|
|
f.focusedElement = len(f.items) + len(f.buttons) - 1
|
|
|
|
}
|
|
|
|
case tcell.KeyEscape:
|
|
|
|
f.focusedElement = 0
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
f.Focus(delegate)
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
2017-12-19 03:04:52 +08:00
|
|
|
|
2017-12-16 06:03:01 +08:00
|
|
|
if f.focusedElement < len(f.items) {
|
|
|
|
// We're selecting an item.
|
2017-12-19 03:04:52 +08:00
|
|
|
item := f.items[f.focusedElement]
|
|
|
|
item.SetFinishedFunc(handler)
|
|
|
|
delegate(item)
|
2017-12-16 06:03:01 +08:00
|
|
|
} else {
|
|
|
|
// We're selecting a button.
|
|
|
|
button := f.buttons[f.focusedElement-len(f.items)]
|
|
|
|
button.SetBlurFunc(handler)
|
2017-12-19 03:04:52 +08:00
|
|
|
delegate(button)
|
2017-12-16 06:03:01 +08:00
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (f *Form) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return func(event *tcell.EventKey, setFocus func(p Primitive)) {}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
|
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (f *Form) HasFocus() bool {
|
|
|
|
for _, item := range f.items {
|
2017-12-19 03:04:52 +08:00
|
|
|
if item.GetFocusable().HasFocus() {
|
2017-12-17 05:48:26 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, button := range f.buttons {
|
|
|
|
if button.focus.HasFocus() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|