2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2022-12-26 20:55:31 +00:00
|
|
|
"image"
|
|
|
|
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-15 14:29:21 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 14:33:49 +00:00
|
|
|
var (
|
|
|
|
// DefaultFormFieldWidth is the default field screen width of form elements
|
|
|
|
// whose field width is flexible (0). This is used in the Form class for
|
|
|
|
// horizontal layouts.
|
|
|
|
DefaultFormFieldWidth = 10
|
|
|
|
|
|
|
|
// DefaultFormFieldHeight is the default field height of multi-line form
|
|
|
|
// elements whose field height is flexible (0).
|
|
|
|
DefaultFormFieldHeight = 5
|
|
|
|
)
|
2018-01-16 19:45:54 +00:00
|
|
|
|
2017-12-18 19:04:52 +00: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.
|
2018-04-09 19:07:03 +00:00
|
|
|
SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem
|
2017-12-18 19:04:52 +00:00
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// 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
|
|
|
|
// required.
|
|
|
|
GetFieldWidth() int
|
2018-01-16 19:45:54 +00:00
|
|
|
|
2022-11-15 14:33:49 +00:00
|
|
|
// GetFieldHeight returns the height of the form item's field (the area which
|
|
|
|
// is manipulated by the user). This value must be greater than 0.
|
|
|
|
GetFieldHeight() int
|
|
|
|
|
2018-08-07 07:25:40 +00:00
|
|
|
// SetFinishedFunc sets the handler function for when the user finished
|
2017-12-18 19:04:52 +00:00
|
|
|
// entering data into the item. The handler may receive events for the
|
2022-12-10 23:46:02 +00:00
|
|
|
// Enter key (we're done), the Escape key (cancel input), the Tab key (move
|
|
|
|
// to next field), the Backtab key (move to previous field), or a negative
|
|
|
|
// value, indicating that the action for the last known key should be
|
|
|
|
// repeated.
|
2017-12-18 19:04:52 +00:00
|
|
|
SetFinishedFunc(handler func(key tcell.Key)) FormItem
|
2023-03-20 06:26:01 +00:00
|
|
|
|
|
|
|
// SetDisabled sets whether or not the item is disabled / read-only. A form
|
|
|
|
// must have at least one item that is not disabled.
|
|
|
|
SetDisabled(disabled bool) FormItem
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 07:37:01 +00:00
|
|
|
// Form allows you to combine multiple one-line form elements into a vertical
|
|
|
|
// or horizontal layout. Form elements include types such as InputField or
|
|
|
|
// Checkbox. These elements can be optionally followed by one or more buttons
|
|
|
|
// for which you can define form-wide actions (e.g. Save, Clear, Cancel).
|
2018-01-07 15:39:06 +00:00
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/Form for an example.
|
2017-12-15 14:29:21 +00:00
|
|
|
type Form struct {
|
2017-12-16 21:48:26 +00:00
|
|
|
*Box
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// The items of the form (one row per item).
|
2017-12-18 19:04:52 +00:00
|
|
|
items []FormItem
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2017-12-15 22:03:01 +00:00
|
|
|
// The buttons of the form.
|
|
|
|
buttons []*Button
|
|
|
|
|
2018-01-16 19:45:54 +00:00
|
|
|
// If set to true, instead of position items and buttons from top to bottom,
|
|
|
|
// they are positioned from left to right.
|
|
|
|
horizontal bool
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// The alignment of the buttons.
|
|
|
|
buttonsAlign int
|
|
|
|
|
2022-11-15 14:33:49 +00:00
|
|
|
// The number of empty cells between items.
|
2017-12-15 14:29:21 +00:00
|
|
|
itemPadding int
|
|
|
|
|
2017-12-15 22:03:01 +00:00
|
|
|
// The index of the item or button which has focus. (Items are counted first,
|
2019-08-29 16:12:55 +00:00
|
|
|
// buttons are counted last.) This is only used when the form itself receives
|
|
|
|
// focus so that the last element that had focus keeps it.
|
2017-12-15 22:03:01 +00:00
|
|
|
focusedElement int
|
2017-12-15 14:29:21 +00: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
|
2017-12-20 19:54:49 +00:00
|
|
|
|
2022-12-17 18:20:43 +00:00
|
|
|
// The style of the buttons when they are not focused.
|
|
|
|
buttonStyle tcell.Style
|
2017-12-20 19:54:49 +00:00
|
|
|
|
2022-12-17 18:20:43 +00:00
|
|
|
// The style of the buttons when they are focused.
|
|
|
|
buttonActivatedStyle tcell.Style
|
2017-12-20 19:54:49 +00:00
|
|
|
|
2023-03-20 06:26:01 +00:00
|
|
|
// The style of the buttons when they are disabled.
|
|
|
|
buttonDisabledStyle tcell.Style
|
|
|
|
|
2022-12-10 23:46:02 +00:00
|
|
|
// The last (valid) key that wsa sent to a "finished" handler or -1 if no
|
|
|
|
// such key is known yet.
|
|
|
|
lastFinishedKey tcell.Key
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// An optional function which is called when the user hits Escape.
|
|
|
|
cancel func()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewForm returns a new form.
|
|
|
|
func NewForm() *Form {
|
2017-12-21 17:08:53 +00:00
|
|
|
box := NewBox().SetBorderPadding(1, 1, 1, 1)
|
2017-12-16 21:48:26 +00:00
|
|
|
|
|
|
|
f := &Form{
|
2022-12-17 18:20:43 +00:00
|
|
|
Box: box,
|
|
|
|
itemPadding: 1,
|
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
|
|
|
buttonStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
|
|
|
|
buttonActivatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.ContrastBackgroundColor),
|
2022-12-26 20:55:31 +00:00
|
|
|
lastFinishedKey: tcell.KeyTab, // To skip over inactive elements at the beginning of the form.
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
|
|
|
|
return f
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 19:45:54 +00:00
|
|
|
// SetItemPadding sets the number of empty rows between form items for vertical
|
|
|
|
// layouts and the number of empty cells between form items for horizontal
|
|
|
|
// layouts.
|
2017-12-15 14:29:21 +00:00
|
|
|
func (f *Form) SetItemPadding(padding int) *Form {
|
|
|
|
f.itemPadding = padding
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-01-16 19:45:54 +00:00
|
|
|
// SetHorizontal sets the direction the form elements are laid out. If set to
|
|
|
|
// true, instead of positioning them from top to bottom (the default), they are
|
|
|
|
// positioned from left to right, moving into the next row if there is not
|
|
|
|
// enough space.
|
|
|
|
func (f *Form) SetHorizontal(horizontal bool) *Form {
|
|
|
|
f.horizontal = horizontal
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// 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-20 19:54:49 +00:00
|
|
|
// SetButtonsAlign sets how the buttons align horizontally, one of AlignLeft
|
2018-01-16 19:45:54 +00:00
|
|
|
// (the default), AlignCenter, and AlignRight. This is only
|
2017-12-20 19:54:49 +00:00
|
|
|
func (f *Form) SetButtonsAlign(align int) *Form {
|
|
|
|
f.buttonsAlign = align
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-12-17 18:20:43 +00:00
|
|
|
// SetButtonBackgroundColor sets the background color of the buttons. This is
|
|
|
|
// also the text color of the buttons when they are focused.
|
2017-12-20 19:54:49 +00:00
|
|
|
func (f *Form) SetButtonBackgroundColor(color tcell.Color) *Form {
|
2022-12-17 18:20:43 +00:00
|
|
|
f.buttonStyle = f.buttonStyle.Background(color)
|
|
|
|
f.buttonActivatedStyle = f.buttonActivatedStyle.Foreground(color)
|
2017-12-20 19:54:49 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-12-17 18:20:43 +00:00
|
|
|
// SetButtonTextColor sets the color of the button texts. This is also the
|
|
|
|
// background of the buttons when they are focused.
|
2017-12-20 19:54:49 +00:00
|
|
|
func (f *Form) SetButtonTextColor(color tcell.Color) *Form {
|
2022-12-17 18:20:43 +00:00
|
|
|
f.buttonStyle = f.buttonStyle.Foreground(color)
|
|
|
|
f.buttonActivatedStyle = f.buttonActivatedStyle.Background(color)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetButtonStyle sets the style of the buttons when they are not focused.
|
|
|
|
func (f *Form) SetButtonStyle(style tcell.Style) *Form {
|
|
|
|
f.buttonStyle = style
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetButtonActivatedStyle sets the style of the buttons when they are focused.
|
|
|
|
func (f *Form) SetButtonActivatedStyle(style tcell.Style) *Form {
|
|
|
|
f.buttonActivatedStyle = style
|
2017-12-20 19:54:49 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-06-02 19:31:59 +00:00
|
|
|
// SetFocus shifts the focus to the form element with the given index, counting
|
|
|
|
// non-button items first and buttons last. Note that this index is only used
|
|
|
|
// when the form itself receives focus.
|
|
|
|
func (f *Form) SetFocus(index int) *Form {
|
|
|
|
if index < 0 {
|
|
|
|
f.focusedElement = 0
|
|
|
|
} else if index >= len(f.items)+len(f.buttons) {
|
|
|
|
f.focusedElement = len(f.items) + len(f.buttons)
|
|
|
|
} else {
|
|
|
|
f.focusedElement = index
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:33:49 +00:00
|
|
|
// AddTextArea adds a text area to the form. It has a label, an optional initial
|
|
|
|
// text, a size (width and height) referring to the actual input area (a
|
|
|
|
// fieldWidth of 0 extends it as far right as possible, a fieldHeight of 0 will
|
|
|
|
// cause it to be [DefaultFormFieldHeight]), and a maximum number of bytes of
|
|
|
|
// text allowed (0 means no limit).
|
|
|
|
//
|
|
|
|
// The optional callback function is invoked when the content of the text area
|
|
|
|
// has changed. Note that especially for larger texts, this is an expensive
|
|
|
|
// operation due to technical constraints of the [TextArea] primitive (every key
|
|
|
|
// stroke leads to a new reallocation of the entire text).
|
|
|
|
func (f *Form) AddTextArea(label, text string, fieldWidth, fieldHeight, maxLength int, changed func(text string)) *Form {
|
|
|
|
if fieldHeight == 0 {
|
|
|
|
fieldHeight = DefaultFormFieldHeight
|
|
|
|
}
|
|
|
|
textArea := NewTextArea().
|
|
|
|
SetLabel(label).
|
|
|
|
SetSize(fieldHeight, fieldWidth).
|
|
|
|
SetMaxLength(maxLength)
|
|
|
|
if text != "" {
|
|
|
|
textArea.SetText(text, true)
|
|
|
|
}
|
|
|
|
if changed != nil {
|
|
|
|
textArea.SetChangedFunc(func() {
|
|
|
|
changed(textArea.GetText())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
f.items = append(f.items, textArea)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-12-10 23:46:02 +00:00
|
|
|
// AddTextView adds a text view to the form. It has a label and text, a size
|
|
|
|
// (width and height) referring to the actual text element (a fieldWidth of 0
|
|
|
|
// extends it as far right as possible, a fieldHeight of 0 will cause it to be
|
|
|
|
// [DefaultFormFieldHeight]), a flag to turn on/off dynamic colors, and a flag
|
|
|
|
// to turn on/off scrolling. If scrolling is turned off, the text view will not
|
|
|
|
// receive focus.
|
|
|
|
func (f *Form) AddTextView(label, text string, fieldWidth, fieldHeight int, dynamicColors, scrollable bool) *Form {
|
|
|
|
if fieldHeight == 0 {
|
|
|
|
fieldHeight = DefaultFormFieldHeight
|
|
|
|
}
|
|
|
|
textArea := NewTextView().
|
|
|
|
SetLabel(label).
|
|
|
|
SetSize(fieldHeight, fieldWidth).
|
|
|
|
SetDynamicColors(dynamicColors).
|
|
|
|
SetScrollable(scrollable).
|
|
|
|
SetText(text)
|
|
|
|
f.items = append(f.items, textArea)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// AddInputField adds an input field to the form. It has a label, an optional
|
2018-01-17 20:17:59 +00:00
|
|
|
// initial value, a field width (a value of 0 extends it as far as possible),
|
2018-01-10 09:40:51 +00:00
|
|
|
// an optional accept function to validate the item's value (set to nil to
|
|
|
|
// accept any text), and an (optional) callback function which is invoked when
|
|
|
|
// the input field's text has changed.
|
2018-01-17 20:17:59 +00:00
|
|
|
func (f *Form) AddInputField(label, value string, fieldWidth int, accept func(textToCheck string, lastChar rune) bool, changed func(text string)) *Form {
|
2017-12-15 14:29:21 +00:00
|
|
|
f.items = append(f.items, NewInputField().
|
|
|
|
SetLabel(label).
|
|
|
|
SetText(value).
|
2018-01-17 20:17:59 +00:00
|
|
|
SetFieldWidth(fieldWidth).
|
2018-01-11 15:13:01 +00:00
|
|
|
SetAcceptanceFunc(accept).
|
|
|
|
SetChangedFunc(changed))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddPasswordField adds a password field to the form. This is similar to an
|
|
|
|
// input field except that the user's input not shown. Instead, a "mask"
|
|
|
|
// character is displayed. The password field has a label, an optional initial
|
2018-01-17 20:17:59 +00:00
|
|
|
// value, a field width (a value of 0 extends it as far as possible), and an
|
2018-01-11 15:13:01 +00:00
|
|
|
// (optional) callback function which is invoked when the input field's text has
|
|
|
|
// changed.
|
2018-01-17 20:17:59 +00:00
|
|
|
func (f *Form) AddPasswordField(label, value string, fieldWidth int, mask rune, changed func(text string)) *Form {
|
2018-01-11 15:13:01 +00:00
|
|
|
if mask == 0 {
|
|
|
|
mask = '*'
|
|
|
|
}
|
|
|
|
f.items = append(f.items, NewInputField().
|
|
|
|
SetLabel(label).
|
|
|
|
SetText(value).
|
2018-01-17 20:17:59 +00:00
|
|
|
SetFieldWidth(fieldWidth).
|
2018-01-11 15:13:01 +00:00
|
|
|
SetMaskCharacter(mask).
|
|
|
|
SetChangedFunc(changed))
|
2017-12-15 14:29:21 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00: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
|
2018-01-24 20:48:53 +00:00
|
|
|
// selected. The initial option may be a negative value to indicate that no
|
|
|
|
// option is currently selected.
|
2017-12-18 19:04:52 +00:00
|
|
|
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).
|
2019-06-04 00:40:09 +00:00
|
|
|
SetOptions(options, selected).
|
|
|
|
SetCurrentOption(initialOption))
|
2017-12-18 19:04:52 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// AddCheckbox adds a checkbox to the form. It has a label, an initial state,
|
|
|
|
// and an (optional) callback function which is invoked when the state of the
|
|
|
|
// checkbox was changed by the user.
|
|
|
|
func (f *Form) AddCheckbox(label string, checked bool, changed func(checked bool)) *Form {
|
|
|
|
f.items = append(f.items, NewCheckbox().
|
|
|
|
SetLabel(label).
|
|
|
|
SetChecked(checked).
|
|
|
|
SetChangedFunc(changed))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2022-12-26 20:55:31 +00:00
|
|
|
// AddImage adds an image to the form. It has a label and the image will fit in
|
|
|
|
// the specified width and height (its aspect ratio is preserved). See
|
|
|
|
// [Image.SetColors] for a description of the "colors" parameter. Images are not
|
|
|
|
// interactive and are skipped over in a form. The "width" value may be 0
|
|
|
|
// (adjust dynamically) but "height" should generally be a positive value.
|
|
|
|
func (f *Form) AddImage(label string, image image.Image, width, height, colors int) *Form {
|
|
|
|
f.items = append(f.items, NewImage().
|
|
|
|
SetLabel(label).
|
|
|
|
SetImage(image).
|
|
|
|
SetSize(height, width).
|
|
|
|
SetAlign(AlignTop, AlignLeft).
|
|
|
|
SetColors(colors))
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:03:01 +00: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
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:35:00 +00:00
|
|
|
// GetButton returns the button at the specified 0-based index. Note that
|
|
|
|
// buttons have been specially prepared for this form and modifying some of
|
|
|
|
// their attributes may have unintended side effects.
|
|
|
|
func (f *Form) GetButton(index int) *Button {
|
|
|
|
return f.buttons[index]
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:18:33 +00:00
|
|
|
// RemoveButton removes the button at the specified position, starting with 0
|
|
|
|
// for the button that was added first.
|
|
|
|
func (f *Form) RemoveButton(index int) *Form {
|
|
|
|
f.buttons = append(f.buttons[:index], f.buttons[index+1:]...)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:35:00 +00:00
|
|
|
// GetButtonCount returns the number of buttons in this form.
|
|
|
|
func (f *Form) GetButtonCount() int {
|
|
|
|
return len(f.buttons)
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:18:33 +00:00
|
|
|
// GetButtonIndex returns the index of the button with the given label, starting
|
|
|
|
// with 0 for the button that was added first. If no such label was found, -1
|
|
|
|
// is returned.
|
|
|
|
func (f *Form) GetButtonIndex(label string) int {
|
|
|
|
for index, button := range f.buttons {
|
|
|
|
if button.GetLabel() == label {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2018-01-15 09:04:03 +00:00
|
|
|
// Clear removes all input elements from the form, including the buttons if
|
|
|
|
// specified.
|
|
|
|
func (f *Form) Clear(includeButtons bool) *Form {
|
|
|
|
f.items = nil
|
|
|
|
if includeButtons {
|
2019-05-14 14:48:29 +00:00
|
|
|
f.ClearButtons()
|
2018-01-15 09:04:03 +00:00
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
f.focusedElement = 0
|
2018-01-15 09:04:03 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:48:29 +00:00
|
|
|
// ClearButtons removes all buttons from the form.
|
|
|
|
func (f *Form) ClearButtons() *Form {
|
|
|
|
f.buttons = nil
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-01-16 19:45:54 +00:00
|
|
|
// AddFormItem adds a new item to the form. This can be used to add your own
|
|
|
|
// objects to the form. Note, however, that the Form class will override some
|
2018-04-19 19:34:03 +00:00
|
|
|
// of its attributes to make it work in the form context. Specifically, these
|
|
|
|
// are:
|
|
|
|
//
|
|
|
|
// - The label width
|
|
|
|
// - The label color
|
|
|
|
// - The background color
|
|
|
|
// - The field text color
|
|
|
|
// - The field background color
|
2018-01-16 19:45:54 +00:00
|
|
|
func (f *Form) AddFormItem(item FormItem) *Form {
|
|
|
|
f.items = append(f.items, item)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:18:27 +00:00
|
|
|
// GetFormItemCount returns the number of items in the form (not including the
|
|
|
|
// buttons).
|
|
|
|
func (f *Form) GetFormItemCount() int {
|
|
|
|
return len(f.items)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFormItem returns the form item at the given position, starting with index
|
|
|
|
// 0. Elements are referenced in the order they were added. Buttons are not
|
|
|
|
// included.
|
2018-01-13 12:07:05 +00:00
|
|
|
func (f *Form) GetFormItem(index int) FormItem {
|
2018-01-10 09:40:51 +00:00
|
|
|
return f.items[index]
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:18:33 +00:00
|
|
|
// RemoveFormItem removes the form element at the given position, starting with
|
|
|
|
// index 0. Elements are referenced in the order they were added. Buttons are
|
|
|
|
// not included.
|
|
|
|
func (f *Form) RemoveFormItem(index int) *Form {
|
|
|
|
f.items = append(f.items[:index], f.items[index+1:]...)
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-04-07 10:58:52 +00:00
|
|
|
// GetFormItemByLabel returns the first form element with the given label. If
|
|
|
|
// no such element is found, nil is returned. Buttons are not searched and will
|
|
|
|
// therefore not be returned.
|
|
|
|
func (f *Form) GetFormItemByLabel(label string) FormItem {
|
|
|
|
for _, item := range f.items {
|
|
|
|
if item.GetLabel() == label {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 18:18:33 +00:00
|
|
|
// GetFormItemIndex returns the index of the first form element with the given
|
|
|
|
// label. If no such element is found, -1 is returned. Buttons are not searched
|
|
|
|
// and will therefore not be returned.
|
|
|
|
func (f *Form) GetFormItemIndex(label string) int {
|
|
|
|
for index, item := range f.items {
|
|
|
|
if item.GetLabel() == label {
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-12-31 10:06:47 +00:00
|
|
|
// GetFocusedItemIndex returns the indices of the form element or button which
|
|
|
|
// currently has focus. If they don't, -1 is returned resepectively.
|
|
|
|
func (f *Form) GetFocusedItemIndex() (formItem, button int) {
|
|
|
|
index := f.focusIndex()
|
|
|
|
if index < 0 {
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
if index < len(f.items) {
|
|
|
|
return index, -1
|
|
|
|
}
|
|
|
|
return -1, index - len(f.items)
|
|
|
|
}
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// SetCancelFunc sets a handler which is called when the user hits the Escape
|
|
|
|
// key.
|
|
|
|
func (f *Form) SetCancelFunc(callback func()) *Form {
|
|
|
|
f.cancel = callback
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (f *Form) Draw(screen tcell.Screen) {
|
2020-11-17 18:33:25 +00:00
|
|
|
f.Box.DrawForSubclass(screen, f)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2019-08-29 16:12:55 +00:00
|
|
|
// Determine the actual item that has focus.
|
|
|
|
if index := f.focusIndex(); index >= 0 {
|
|
|
|
f.focusedElement = index
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Determine the dimensions.
|
2017-12-21 17:08:53 +00:00
|
|
|
x, y, width, height := f.GetInnerRect()
|
2018-03-15 16:54:17 +00:00
|
|
|
topLimit := y
|
2017-12-20 19:54:49 +00:00
|
|
|
bottomLimit := y + height
|
2017-12-15 22:03:01 +00:00
|
|
|
rightLimit := x + width
|
2018-01-16 19:45:54 +00:00
|
|
|
startX := x
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Find the longest label.
|
2018-01-17 20:17:59 +00:00
|
|
|
var maxLabelWidth int
|
2017-12-18 19:04:52 +00:00
|
|
|
for _, item := range f.items {
|
2019-03-19 11:12:40 +00:00
|
|
|
labelWidth := TaggedStringWidth(item.GetLabel())
|
2018-01-17 20:17:59 +00:00
|
|
|
if labelWidth > maxLabelWidth {
|
|
|
|
maxLabelWidth = labelWidth
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
maxLabelWidth++ // Add one space.
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2018-03-15 16:54:17 +00:00
|
|
|
// Calculate positions of form items.
|
2022-11-15 14:33:49 +00:00
|
|
|
type position struct{ x, y, width, height int }
|
|
|
|
positions := make([]position, len(f.items)+len(f.buttons))
|
|
|
|
var (
|
|
|
|
focusedPosition position
|
|
|
|
lineHeight = 1
|
|
|
|
)
|
2018-03-15 16:54:17 +00:00
|
|
|
for index, item := range f.items {
|
2018-01-16 19:45:54 +00:00
|
|
|
// Calculate the space needed.
|
2019-03-19 11:12:40 +00:00
|
|
|
labelWidth := TaggedStringWidth(item.GetLabel())
|
2018-01-16 19:45:54 +00:00
|
|
|
var itemWidth int
|
|
|
|
if f.horizontal {
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth := item.GetFieldWidth()
|
2022-11-15 14:33:49 +00:00
|
|
|
if fieldWidth <= 0 {
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth = DefaultFormFieldWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
labelWidth++
|
2018-01-17 20:17:59 +00:00
|
|
|
itemWidth = labelWidth + fieldWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
} else {
|
|
|
|
// We want all fields to align vertically.
|
2018-04-09 19:07:03 +00:00
|
|
|
labelWidth = maxLabelWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
itemWidth = width
|
|
|
|
}
|
2022-11-15 14:33:49 +00:00
|
|
|
itemHeight := item.GetFieldHeight()
|
|
|
|
if itemHeight <= 0 {
|
|
|
|
itemHeight = DefaultFormFieldHeight
|
|
|
|
}
|
2018-01-16 19:45:54 +00:00
|
|
|
|
|
|
|
// Advance to next line if there is no space.
|
|
|
|
if f.horizontal && x+labelWidth+1 >= rightLimit {
|
|
|
|
x = startX
|
2022-11-15 14:33:49 +00:00
|
|
|
y += lineHeight + 1
|
|
|
|
lineHeight = itemHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update line height.
|
|
|
|
if itemHeight > lineHeight {
|
|
|
|
lineHeight = itemHeight
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the item's attributes.
|
|
|
|
if x+itemWidth >= rightLimit {
|
|
|
|
itemWidth = rightLimit - x
|
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
item.SetFormAttributes(
|
2018-04-09 19:07:03 +00:00
|
|
|
labelWidth,
|
2017-12-18 19:04:52 +00:00
|
|
|
f.labelColor,
|
|
|
|
f.backgroundColor,
|
|
|
|
f.fieldTextColor,
|
|
|
|
f.fieldBackgroundColor,
|
2018-03-15 16:54:17 +00:00
|
|
|
)
|
2018-01-16 19:45:54 +00:00
|
|
|
|
2018-03-15 16:54:17 +00:00
|
|
|
// Save position.
|
|
|
|
positions[index].x = x
|
|
|
|
positions[index].y = y
|
|
|
|
positions[index].width = itemWidth
|
2022-11-15 14:33:49 +00:00
|
|
|
positions[index].height = itemHeight
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.HasFocus() {
|
2018-03-15 16:54:17 +00:00
|
|
|
focusedPosition = positions[index]
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2018-01-16 19:45:54 +00:00
|
|
|
|
|
|
|
// Advance to next item.
|
|
|
|
if f.horizontal {
|
|
|
|
x += itemWidth + f.itemPadding
|
|
|
|
} else {
|
2022-11-15 14:33:49 +00:00
|
|
|
y += itemHeight + f.itemPadding
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-15 22:03:01 +00:00
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// How wide are the buttons?
|
|
|
|
buttonWidths := make([]int, len(f.buttons))
|
|
|
|
buttonsWidth := 0
|
|
|
|
for index, button := range f.buttons {
|
2019-03-19 11:12:40 +00:00
|
|
|
w := TaggedStringWidth(button.GetLabel()) + 4
|
2018-01-17 20:17:59 +00:00
|
|
|
buttonWidths[index] = w
|
|
|
|
buttonsWidth += w + 1
|
2017-12-20 19:54:49 +00:00
|
|
|
}
|
2018-01-16 19:45:54 +00:00
|
|
|
buttonsWidth--
|
2017-12-20 19:54:49 +00:00
|
|
|
|
|
|
|
// Where do we place them?
|
2018-01-16 19:45:54 +00:00
|
|
|
if !f.horizontal && x+buttonsWidth < rightLimit {
|
2017-12-20 19:54:49 +00:00
|
|
|
if f.buttonsAlign == AlignRight {
|
|
|
|
x = rightLimit - buttonsWidth
|
|
|
|
} else if f.buttonsAlign == AlignCenter {
|
|
|
|
x = (x + rightLimit - buttonsWidth) / 2
|
|
|
|
}
|
2018-01-16 19:45:54 +00:00
|
|
|
|
|
|
|
// In vertical layouts, buttons always appear after an empty line.
|
|
|
|
if f.itemPadding == 0 {
|
|
|
|
y++
|
|
|
|
}
|
2017-12-20 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:54:17 +00:00
|
|
|
// Calculate positions of buttons.
|
2017-12-20 19:54:49 +00:00
|
|
|
for index, button := range f.buttons {
|
2018-01-16 19:45:54 +00:00
|
|
|
space := rightLimit - x
|
2017-12-20 19:54:49 +00:00
|
|
|
buttonWidth := buttonWidths[index]
|
2018-01-16 19:45:54 +00:00
|
|
|
if f.horizontal {
|
|
|
|
if space < buttonWidth-4 {
|
|
|
|
x = startX
|
2022-11-15 14:33:49 +00:00
|
|
|
y += lineHeight + 1
|
2018-01-16 19:45:54 +00:00
|
|
|
space = width
|
2022-11-15 14:33:49 +00:00
|
|
|
lineHeight = 1
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if space < 1 {
|
|
|
|
break // No space for this button anymore.
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 22:03:01 +00:00
|
|
|
if buttonWidth > space {
|
|
|
|
buttonWidth = space
|
|
|
|
}
|
2022-12-17 18:20:43 +00:00
|
|
|
button.SetStyle(f.buttonStyle).
|
|
|
|
SetActivatedStyle(f.buttonActivatedStyle)
|
2018-03-15 16:54:17 +00:00
|
|
|
|
|
|
|
buttonIndex := index + len(f.items)
|
|
|
|
positions[buttonIndex].x = x
|
|
|
|
positions[buttonIndex].y = y
|
|
|
|
positions[buttonIndex].width = buttonWidth
|
|
|
|
positions[buttonIndex].height = 1
|
|
|
|
|
|
|
|
if button.HasFocus() {
|
|
|
|
focusedPosition = positions[buttonIndex]
|
|
|
|
}
|
2017-12-15 22:03:01 +00:00
|
|
|
|
2018-01-16 19:45:54 +00:00
|
|
|
x += buttonWidth + 1
|
2017-12-15 22:03:01 +00:00
|
|
|
}
|
2018-03-15 16:54:17 +00:00
|
|
|
|
|
|
|
// Determine vertical offset based on the position of the focused item.
|
|
|
|
var offset int
|
|
|
|
if focusedPosition.y+focusedPosition.height > bottomLimit {
|
|
|
|
offset = focusedPosition.y + focusedPosition.height - bottomLimit
|
|
|
|
if focusedPosition.y-offset < topLimit {
|
|
|
|
offset = focusedPosition.y - topLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw items.
|
|
|
|
for index, item := range f.items {
|
|
|
|
// Set position.
|
|
|
|
y := positions[index].y - offset
|
|
|
|
height := positions[index].height
|
|
|
|
item.SetRect(positions[index].x, y, positions[index].width, height)
|
|
|
|
|
|
|
|
// Is this item visible?
|
|
|
|
if y+height <= topLimit || y >= bottomLimit {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw items with focus last (in case of overlaps).
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.HasFocus() {
|
2018-03-15 16:54:17 +00:00
|
|
|
defer item.Draw(screen)
|
|
|
|
} else {
|
|
|
|
item.Draw(screen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw buttons.
|
|
|
|
for index, button := range f.buttons {
|
|
|
|
// Set position.
|
|
|
|
buttonIndex := index + len(f.items)
|
|
|
|
y := positions[buttonIndex].y - offset
|
|
|
|
height := positions[buttonIndex].height
|
|
|
|
button.SetRect(positions[buttonIndex].x, y, positions[buttonIndex].width, height)
|
|
|
|
|
|
|
|
// Is this button visible?
|
|
|
|
if y+height <= topLimit || y >= bottomLimit {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw button.
|
|
|
|
button.Draw(screen)
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus is called by the application when the primitive receives focus.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (f *Form) Focus(delegate func(p Primitive)) {
|
2017-12-15 22:03:01 +00: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 14:29:21 +00:00
|
|
|
}
|
2022-12-10 23:46:02 +00:00
|
|
|
var handler func(key tcell.Key)
|
|
|
|
handler = func(key tcell.Key) {
|
|
|
|
if key >= 0 {
|
|
|
|
f.lastFinishedKey = key
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
switch key {
|
2017-12-16 21:48:26 +00:00
|
|
|
case tcell.KeyTab, tcell.KeyEnter:
|
2017-12-15 22:03:01 +00:00
|
|
|
f.focusedElement++
|
2017-12-20 19:54:49 +00:00
|
|
|
f.Focus(delegate)
|
2017-12-15 22:03:01 +00:00
|
|
|
case tcell.KeyBacktab:
|
|
|
|
f.focusedElement--
|
|
|
|
if f.focusedElement < 0 {
|
|
|
|
f.focusedElement = len(f.items) + len(f.buttons) - 1
|
|
|
|
}
|
2017-12-20 19:54:49 +00:00
|
|
|
f.Focus(delegate)
|
2017-12-15 22:03:01 +00:00
|
|
|
case tcell.KeyEscape:
|
2017-12-20 19:54:49 +00:00
|
|
|
if f.cancel != nil {
|
|
|
|
f.cancel()
|
|
|
|
} else {
|
|
|
|
f.focusedElement = 0
|
|
|
|
f.Focus(delegate)
|
|
|
|
}
|
2022-12-10 23:46:02 +00:00
|
|
|
default:
|
|
|
|
if key < 0 && f.lastFinishedKey >= 0 {
|
|
|
|
// Repeat the last action.
|
|
|
|
handler(f.lastFinishedKey)
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-15 22:03:01 +00:00
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
|
2023-03-20 06:26:01 +00:00
|
|
|
// Track whether a form item has focus.
|
|
|
|
var itemFocused bool
|
|
|
|
f.hasFocus = false
|
|
|
|
|
|
|
|
// Set the handler and focus for all items and buttons.
|
|
|
|
for index, button := range f.buttons {
|
|
|
|
button.SetExitFunc(handler)
|
|
|
|
if f.focusedElement == index+len(f.items) {
|
|
|
|
if button.IsDisabled() {
|
|
|
|
f.focusedElement++
|
|
|
|
if f.focusedElement >= len(f.items)+len(f.buttons) {
|
|
|
|
f.focusedElement = 0
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
itemFocused = true
|
|
|
|
func(b *Button) { // Wrapping might not be necessary anymore in future Go versions.
|
|
|
|
defer delegate(b)
|
|
|
|
}(button)
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 18:20:43 +00:00
|
|
|
for index, item := range f.items {
|
2017-12-18 19:04:52 +00:00
|
|
|
item.SetFinishedFunc(handler)
|
2022-12-17 18:20:43 +00:00
|
|
|
if f.focusedElement == index {
|
2023-03-20 06:26:01 +00:00
|
|
|
itemFocused = true
|
2022-12-17 18:20:43 +00:00
|
|
|
func(i FormItem) { // Wrapping might not be necessary anymore in future Go versions.
|
|
|
|
defer delegate(i)
|
|
|
|
}(item)
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 06:26:01 +00:00
|
|
|
|
|
|
|
// If no item was focused, focus the form itself.
|
|
|
|
if !itemFocused {
|
|
|
|
f.Box.Focus(delegate)
|
2017-12-15 22:03:01 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 21:48:26 +00:00
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (f *Form) HasFocus() bool {
|
2021-11-08 16:05:49 +00:00
|
|
|
if f.focusIndex() >= 0 {
|
2018-12-03 17:28:54 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-08 16:05:49 +00:00
|
|
|
return f.Box.HasFocus()
|
2019-08-29 16:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// focusIndex returns the index of the currently focused item, counting form
|
|
|
|
// items first, then buttons. A negative value indicates that no containeed item
|
|
|
|
// has focus.
|
|
|
|
func (f *Form) focusIndex() int {
|
|
|
|
for index, item := range f.items {
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.HasFocus() {
|
2019-08-29 16:12:55 +00:00
|
|
|
return index
|
2017-12-16 21:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 16:12:55 +00:00
|
|
|
for index, button := range f.buttons {
|
2020-11-17 18:33:25 +00:00
|
|
|
if button.HasFocus() {
|
2019-08-29 16:12:55 +00:00
|
|
|
return len(f.items) + index
|
2017-12-16 21:48:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 16:12:55 +00:00
|
|
|
return -1
|
2017-12-16 21:48:26 +00:00
|
|
|
}
|
2019-11-04 05:34:46 +00:00
|
|
|
|
2020-01-24 20:40:34 +00:00
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (f *Form) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
|
|
|
return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2020-04-14 13:03:31 +00:00
|
|
|
// At the end, update f.focusedElement and prepare current item/button.
|
|
|
|
defer func() {
|
|
|
|
if consumed {
|
|
|
|
index := f.focusIndex()
|
|
|
|
if index >= 0 {
|
|
|
|
f.focusedElement = index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// Determine items to pass mouse events to.
|
2020-01-24 20:40:34 +00:00
|
|
|
for _, item := range f.items {
|
2023-02-08 21:13:50 +00:00
|
|
|
// Exclude TextView items from mouse-down events as they are
|
|
|
|
// read-only items and thus should not be focused.
|
|
|
|
if _, ok := item.(*TextView); ok && action == MouseLeftDown {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-14 02:09:09 +00:00
|
|
|
consumed, capture = item.MouseHandler()(action, event, setFocus)
|
2020-01-24 20:40:34 +00:00
|
|
|
if consumed {
|
2020-03-27 17:41:44 +00:00
|
|
|
return
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, button := range f.buttons {
|
2020-02-14 02:09:09 +00:00
|
|
|
consumed, capture = button.MouseHandler()(action, event, setFocus)
|
2020-01-24 20:40:34 +00:00
|
|
|
if consumed {
|
2020-03-27 17:41:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 15:49:12 +00:00
|
|
|
// A mouse down anywhere else will return the focus to the last selected
|
2020-03-27 17:41:44 +00:00
|
|
|
// element.
|
2022-09-02 15:49:12 +00:00
|
|
|
if action == MouseLeftDown && f.InRect(event.Position()) {
|
2023-03-20 09:52:35 +00:00
|
|
|
f.Focus(setFocus)
|
2020-03-27 17:41:44 +00:00
|
|
|
consumed = true
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
return
|
2020-01-24 20:40:34 +00:00
|
|
|
})
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
2020-08-18 10:05:43 +00:00
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
func (f *Form) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return f.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
for _, item := range f.items {
|
2020-11-17 18:33:25 +00:00
|
|
|
if item != nil && item.HasFocus() {
|
2020-08-18 10:05:43 +00:00
|
|
|
if handler := item.InputHandler(); handler != nil {
|
|
|
|
handler(event, setFocus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, button := range f.buttons {
|
2020-11-17 18:33:25 +00:00
|
|
|
if button.HasFocus() {
|
2020-08-18 10:05:43 +00:00
|
|
|
if handler := button.InputHandler(); handler != nil {
|
|
|
|
handler(event, setFocus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|