2017-12-18 19:04:52 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2018-03-18 19:42:51 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2022-09-11 19:02:40 +00:00
|
|
|
"github.com/rivo/uniseg"
|
2017-12-18 19:04:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// dropDownOption is one option that can be selected in a drop-down primitive.
|
|
|
|
type dropDownOption struct {
|
|
|
|
Text string // The text to be displayed in the drop-down.
|
|
|
|
Selected func() // The (optional) callback for when this option was selected.
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:42:51 +00:00
|
|
|
// DropDown implements a selection widget whose options become visible in a
|
|
|
|
// drop-down list when activated.
|
2018-01-07 15:39:06 +00:00
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/DropDown for an example.
|
2017-12-18 19:04:52 +00:00
|
|
|
type DropDown struct {
|
|
|
|
*Box
|
|
|
|
|
2023-03-20 06:26:01 +00:00
|
|
|
// Whether or not this drop-down is disabled/read-only.
|
|
|
|
disabled bool
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// The options from which the user can choose.
|
|
|
|
options []*dropDownOption
|
|
|
|
|
2019-07-05 09:48:25 +00:00
|
|
|
// Strings to be placed before and after each drop-down option.
|
|
|
|
optionPrefix, optionSuffix string
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// The index of the currently selected option. Negative if no option is
|
|
|
|
// currently selected.
|
|
|
|
currentOption int
|
|
|
|
|
2021-07-06 03:03:28 +00:00
|
|
|
// Strings to be placed before and after the current option.
|
2019-07-05 09:48:25 +00:00
|
|
|
currentOptionPrefix, currentOptionSuffix string
|
|
|
|
|
|
|
|
// The text to be displayed when no option has yet been selected.
|
|
|
|
noSelection string
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// Set to true if the options are visible and selectable.
|
|
|
|
open bool
|
|
|
|
|
2018-03-18 19:42:51 +00:00
|
|
|
// The runes typed so far to directly access one of the list items.
|
|
|
|
prefix string
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// The list element for the options.
|
|
|
|
list *List
|
|
|
|
|
|
|
|
// The text to be displayed before the input area.
|
|
|
|
label string
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2018-03-18 19:42:51 +00:00
|
|
|
// The color for prefixes.
|
|
|
|
prefixTextColor tcell.Color
|
|
|
|
|
2018-04-09 19:07:03 +00:00
|
|
|
// The screen width of the label area. A value of 0 means use the width of
|
|
|
|
// the label text.
|
|
|
|
labelWidth int
|
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// The screen width of the input area. A value of 0 means extend as much as
|
2017-12-18 19:04:52 +00:00
|
|
|
// possible.
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth int
|
2017-12-18 19:04:52 +00:00
|
|
|
|
|
|
|
// An optional function which is called when the user indicated that they
|
|
|
|
// are done selecting options. The key which was pressed is provided (tab,
|
|
|
|
// shift-tab, or escape).
|
|
|
|
done func(tcell.Key)
|
2018-04-19 19:34:03 +00:00
|
|
|
|
|
|
|
// A callback function set by the Form class and called when the user leaves
|
|
|
|
// this form item.
|
|
|
|
finished func(tcell.Key)
|
2019-01-23 15:51:16 +00:00
|
|
|
|
|
|
|
// A callback function which is called when the user changes the drop-down's
|
|
|
|
// selection.
|
|
|
|
selected func(text string, index int)
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
dragging bool // Set to true when mouse dragging is in progress.
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDropDown returns a new drop-down.
|
|
|
|
func NewDropDown() *DropDown {
|
2019-07-08 08:34:06 +00:00
|
|
|
list := NewList()
|
|
|
|
list.ShowSecondaryText(false).
|
|
|
|
SetMainTextColor(Styles.PrimitiveBackgroundColor).
|
2018-01-10 08:43:32 +00:00
|
|
|
SetSelectedTextColor(Styles.PrimitiveBackgroundColor).
|
|
|
|
SetSelectedBackgroundColor(Styles.PrimaryTextColor).
|
2019-07-05 09:48:25 +00:00
|
|
|
SetHighlightFullLine(true).
|
2018-01-10 08:43:32 +00:00
|
|
|
SetBackgroundColor(Styles.MoreContrastBackgroundColor)
|
2017-12-18 19:04:52 +00:00
|
|
|
|
|
|
|
d := &DropDown{
|
|
|
|
Box: NewBox(),
|
|
|
|
currentOption: -1,
|
|
|
|
list: list,
|
2018-01-10 08:43:32 +00:00
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
2018-03-18 19:42:51 +00:00
|
|
|
prefixTextColor: Styles.ContrastSecondaryTextColor,
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-01-24 20:48:53 +00:00
|
|
|
// SetCurrentOption sets the index of the currently selected option. This may
|
2019-04-05 21:20:22 +00:00
|
|
|
// be a negative value to indicate that no option is currently selected. Calling
|
|
|
|
// this function will also trigger the "selected" callback (if there is one).
|
2017-12-18 19:04:52 +00:00
|
|
|
func (d *DropDown) SetCurrentOption(index int) *DropDown {
|
2019-04-05 21:20:22 +00:00
|
|
|
if index >= 0 && index < len(d.options) {
|
|
|
|
d.currentOption = index
|
|
|
|
d.list.SetCurrentItem(index)
|
|
|
|
if d.selected != nil {
|
|
|
|
d.selected(d.options[index].Text, index)
|
|
|
|
}
|
|
|
|
if d.options[index].Selected != nil {
|
|
|
|
d.options[index].Selected()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.currentOption = -1
|
|
|
|
d.list.SetCurrentItem(0) // Set to 0 because -1 means "last item".
|
|
|
|
if d.selected != nil {
|
|
|
|
d.selected("", -1)
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-01-10 09:40:51 +00:00
|
|
|
// GetCurrentOption returns the index of the currently selected option as well
|
|
|
|
// as its text. If no option was selected, -1 and an empty string is returned.
|
|
|
|
func (d *DropDown) GetCurrentOption() (int, string) {
|
|
|
|
var text string
|
|
|
|
if d.currentOption >= 0 && d.currentOption < len(d.options) {
|
|
|
|
text = d.options[d.currentOption].Text
|
|
|
|
}
|
|
|
|
return d.currentOption, text
|
|
|
|
}
|
|
|
|
|
2019-07-05 09:48:25 +00:00
|
|
|
// SetTextOptions sets the text to be placed before and after each drop-down
|
|
|
|
// option (prefix/suffix), the text placed before and after the currently
|
|
|
|
// selected option (currentPrefix/currentSuffix) as well as the text to be
|
|
|
|
// displayed when no option is currently selected. Per default, all of these
|
|
|
|
// strings are empty.
|
|
|
|
func (d *DropDown) SetTextOptions(prefix, suffix, currentPrefix, currentSuffix, noSelection string) *DropDown {
|
|
|
|
d.currentOptionPrefix = currentPrefix
|
|
|
|
d.currentOptionSuffix = currentSuffix
|
|
|
|
d.noSelection = noSelection
|
|
|
|
d.optionPrefix = prefix
|
|
|
|
d.optionSuffix = suffix
|
|
|
|
for index := 0; index < d.list.GetItemCount(); index++ {
|
|
|
|
d.list.SetItemText(index, prefix+d.options[index].Text+suffix, "")
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// SetLabel sets the text to be displayed before the input area.
|
|
|
|
func (d *DropDown) SetLabel(label string) *DropDown {
|
|
|
|
d.label = label
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the text to be displayed before the input area.
|
|
|
|
func (d *DropDown) GetLabel() string {
|
|
|
|
return d.label
|
|
|
|
}
|
|
|
|
|
2018-04-09 19:07:03 +00:00
|
|
|
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
|
|
|
|
// primitive to use the width of the label string.
|
|
|
|
func (d *DropDown) SetLabelWidth(width int) *DropDown {
|
|
|
|
d.labelWidth = width
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// SetLabelColor sets the color of the label.
|
|
|
|
func (d *DropDown) SetLabelColor(color tcell.Color) *DropDown {
|
|
|
|
d.labelColor = color
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldBackgroundColor sets the background color of the options area.
|
|
|
|
func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown {
|
|
|
|
d.fieldBackgroundColor = color
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the options area.
|
|
|
|
func (d *DropDown) SetFieldTextColor(color tcell.Color) *DropDown {
|
|
|
|
d.fieldTextColor = color
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-03-18 19:42:51 +00:00
|
|
|
// SetPrefixTextColor sets the color of the prefix string. The prefix string is
|
|
|
|
// shown when the user starts typing text, which directly selects the first
|
|
|
|
// option that starts with the typed string.
|
|
|
|
func (d *DropDown) SetPrefixTextColor(color tcell.Color) *DropDown {
|
|
|
|
d.prefixTextColor = color
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:53:35 +00:00
|
|
|
// SetListStyles sets the styles of the items in the drop-down list (unselected
|
|
|
|
// as well as selected items). Style attributes are currently ignored but may be
|
|
|
|
// used in the future.
|
|
|
|
func (d *DropDown) SetListStyles(unselected, selected tcell.Style) *DropDown {
|
|
|
|
fg, bg, _ := unselected.Decompose()
|
|
|
|
d.list.SetMainTextColor(fg).SetBackgroundColor(bg)
|
|
|
|
fg, bg, _ = selected.Decompose()
|
|
|
|
d.list.SetSelectedTextColor(fg).SetSelectedBackgroundColor(bg)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// SetFormAttributes sets attributes shared by all form items.
|
2018-04-09 19:07:03 +00:00
|
|
|
func (d *DropDown) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
|
|
|
d.labelWidth = labelWidth
|
2017-12-18 19:04:52 +00:00
|
|
|
d.labelColor = labelColor
|
|
|
|
d.backgroundColor = bgColor
|
|
|
|
d.fieldTextColor = fieldTextColor
|
|
|
|
d.fieldBackgroundColor = fieldBgColor
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// SetFieldWidth sets the screen width of the options area. A value of 0 means
|
|
|
|
// extend to as long as the longest option text.
|
|
|
|
func (d *DropDown) SetFieldWidth(width int) *DropDown {
|
|
|
|
d.fieldWidth = width
|
2017-12-18 19:04:52 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:17:59 +00:00
|
|
|
// GetFieldWidth returns this primitive's field screen width.
|
|
|
|
func (d *DropDown) GetFieldWidth() int {
|
|
|
|
if d.fieldWidth > 0 {
|
|
|
|
return d.fieldWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth := 0
|
2018-01-16 19:45:54 +00:00
|
|
|
for _, option := range d.options {
|
2019-03-19 11:12:40 +00:00
|
|
|
width := TaggedStringWidth(option.Text)
|
2018-01-17 20:17:59 +00:00
|
|
|
if width > fieldWidth {
|
|
|
|
fieldWidth = width
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
return fieldWidth
|
2018-01-16 19:45:54 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:33:49 +00:00
|
|
|
// GetFieldHeight returns this primitive's field height.
|
|
|
|
func (d *DropDown) GetFieldHeight() int {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:26:01 +00:00
|
|
|
// SetDisabled sets whether or not the item is disabled / read-only.
|
|
|
|
func (d *DropDown) SetDisabled(disabled bool) FormItem {
|
|
|
|
d.disabled = disabled
|
|
|
|
if d.finished != nil {
|
|
|
|
d.finished(-1)
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// AddOption adds a new selectable option to this drop-down. The "selected"
|
|
|
|
// callback is called when this option was selected. It may be nil.
|
|
|
|
func (d *DropDown) AddOption(text string, selected func()) *DropDown {
|
|
|
|
d.options = append(d.options, &dropDownOption{Text: text, Selected: selected})
|
2019-07-05 09:48:25 +00:00
|
|
|
d.list.AddItem(d.optionPrefix+text+d.optionSuffix, "", 0, nil)
|
2017-12-18 19:04:52 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions replaces all current options with the ones provided and installs
|
|
|
|
// one callback function which is called when one of the options is selected.
|
|
|
|
// It will be called with the option's text and its index into the options
|
|
|
|
// slice. The "selected" parameter may be nil.
|
|
|
|
func (d *DropDown) SetOptions(texts []string, selected func(text string, index int)) *DropDown {
|
2018-01-01 16:17:20 +00:00
|
|
|
d.list.Clear()
|
2017-12-18 19:04:52 +00:00
|
|
|
d.options = nil
|
|
|
|
for index, text := range texts {
|
|
|
|
func(t string, i int) {
|
2019-01-23 15:51:16 +00:00
|
|
|
d.AddOption(text, nil)
|
2017-12-18 19:04:52 +00:00
|
|
|
}(text, index)
|
|
|
|
}
|
2019-01-23 15:51:16 +00:00
|
|
|
d.selected = selected
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2022-02-16 10:14:05 +00:00
|
|
|
// GetOptionCount returns the number of options in the drop-down.
|
|
|
|
func (d *DropDown) GetOptionCount() int {
|
|
|
|
return len(d.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveOption removes the specified option from the drop-down. Panics if the
|
|
|
|
// index is out of range.
|
|
|
|
func (d *DropDown) RemoveOption(index int) *DropDown {
|
|
|
|
d.options = append(d.options[:index], d.options[index+1:]...)
|
|
|
|
d.list.RemoveItem(index)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2019-01-23 15:51:16 +00:00
|
|
|
// SetSelectedFunc sets a handler which is called when the user changes the
|
|
|
|
// drop-down's option. This handler will be called in addition and prior to
|
|
|
|
// an option's optional individual handler. The handler is provided with the
|
2019-04-05 21:20:22 +00:00
|
|
|
// selected option's text and index. If "no option" was selected, these values
|
|
|
|
// are an empty string and -1.
|
2019-01-23 15:51:16 +00:00
|
|
|
func (d *DropDown) SetSelectedFunc(handler func(text string, index int)) *DropDown {
|
|
|
|
d.selected = handler
|
2017-12-18 19:04:52 +00:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDoneFunc sets a handler which is called when the user is done selecting
|
|
|
|
// options. The callback function is provided with the key that was pressed,
|
|
|
|
// which is one of the following:
|
|
|
|
//
|
|
|
|
// - KeyEscape: Abort selection.
|
|
|
|
// - KeyTab: Move to the next field.
|
|
|
|
// - KeyBacktab: Move to the previous field.
|
|
|
|
func (d *DropDown) SetDoneFunc(handler func(key tcell.Key)) *DropDown {
|
|
|
|
d.done = handler
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:34:03 +00:00
|
|
|
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (d *DropDown) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
2018-04-19 19:34:03 +00:00
|
|
|
d.finished = handler
|
|
|
|
return d
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (d *DropDown) Draw(screen tcell.Screen) {
|
2020-11-17 18:33:25 +00:00
|
|
|
d.Box.DrawForSubclass(screen, d)
|
2017-12-18 19:04:52 +00:00
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// Prepare.
|
|
|
|
x, y, width, height := d.GetInnerRect()
|
|
|
|
rightLimit := x + width
|
2017-12-18 19:04:52 +00:00
|
|
|
if height < 1 || rightLimit <= x {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw label.
|
2018-04-09 19:07:03 +00:00
|
|
|
if d.labelWidth > 0 {
|
|
|
|
labelWidth := d.labelWidth
|
|
|
|
if labelWidth > rightLimit-x {
|
|
|
|
labelWidth = rightLimit - x
|
|
|
|
}
|
|
|
|
Print(screen, d.label, x, y, labelWidth, AlignLeft, d.labelColor)
|
|
|
|
x += labelWidth
|
|
|
|
} else {
|
|
|
|
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, d.labelColor)
|
|
|
|
x += drawnWidth
|
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
|
|
|
|
// What's the longest option text?
|
2018-01-17 20:17:59 +00:00
|
|
|
maxWidth := 0
|
2019-07-05 09:48:25 +00:00
|
|
|
optionWrapWidth := TaggedStringWidth(d.optionPrefix + d.optionSuffix)
|
2017-12-18 19:04:52 +00:00
|
|
|
for _, option := range d.options {
|
2019-07-05 09:48:25 +00:00
|
|
|
strWidth := TaggedStringWidth(option.Text) + optionWrapWidth
|
2018-01-17 20:17:59 +00:00
|
|
|
if strWidth > maxWidth {
|
|
|
|
maxWidth = strWidth
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw selection area.
|
2018-01-17 20:17:59 +00:00
|
|
|
fieldWidth := d.fieldWidth
|
|
|
|
if fieldWidth == 0 {
|
|
|
|
fieldWidth = maxWidth
|
2019-07-05 09:48:25 +00:00
|
|
|
if d.currentOption < 0 {
|
|
|
|
noSelectionWidth := TaggedStringWidth(d.noSelection)
|
|
|
|
if noSelectionWidth > fieldWidth {
|
|
|
|
fieldWidth = noSelectionWidth
|
|
|
|
}
|
|
|
|
} else if d.currentOption < len(d.options) {
|
|
|
|
currentOptionWidth := TaggedStringWidth(d.currentOptionPrefix + d.options[d.currentOption].Text + d.currentOptionSuffix)
|
|
|
|
if currentOptionWidth > fieldWidth {
|
|
|
|
fieldWidth = currentOptionWidth
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
if rightLimit-x < fieldWidth {
|
|
|
|
fieldWidth = rightLimit - x
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
fieldStyle := tcell.StyleDefault.Background(d.fieldBackgroundColor)
|
2020-11-17 18:33:25 +00:00
|
|
|
if d.HasFocus() && !d.open {
|
2017-12-18 19:04:52 +00:00
|
|
|
fieldStyle = fieldStyle.Background(d.fieldTextColor)
|
|
|
|
}
|
2023-03-20 06:26:01 +00:00
|
|
|
if d.disabled {
|
|
|
|
fieldStyle = fieldStyle.Background(d.backgroundColor)
|
|
|
|
}
|
2018-01-17 20:17:59 +00:00
|
|
|
for index := 0; index < fieldWidth; index++ {
|
2017-12-18 19:04:52 +00:00
|
|
|
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw selected text.
|
2018-03-18 19:42:51 +00:00
|
|
|
if d.open && len(d.prefix) > 0 {
|
|
|
|
// Show the prefix.
|
2019-07-05 09:48:25 +00:00
|
|
|
currentOptionPrefixWidth := TaggedStringWidth(d.currentOptionPrefix)
|
2022-09-11 19:02:40 +00:00
|
|
|
prefixWidth := uniseg.StringWidth(d.prefix)
|
2018-03-18 19:42:51 +00:00
|
|
|
listItemText := d.options[d.list.GetCurrentItem()].Text
|
2019-07-05 09:48:25 +00:00
|
|
|
Print(screen, d.currentOptionPrefix, x, y, fieldWidth, AlignLeft, d.fieldTextColor)
|
|
|
|
Print(screen, d.prefix, x+currentOptionPrefixWidth, y, fieldWidth-currentOptionPrefixWidth, AlignLeft, d.prefixTextColor)
|
|
|
|
if len(d.prefix) < len(listItemText) {
|
|
|
|
Print(screen, listItemText[len(d.prefix):]+d.currentOptionSuffix, x+prefixWidth+currentOptionPrefixWidth, y, fieldWidth-prefixWidth-currentOptionPrefixWidth, AlignLeft, d.fieldTextColor)
|
2018-03-18 19:42:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-05 09:48:25 +00:00
|
|
|
color := d.fieldTextColor
|
|
|
|
text := d.noSelection
|
2018-03-18 19:42:51 +00:00
|
|
|
if d.currentOption >= 0 && d.currentOption < len(d.options) {
|
2019-07-05 09:48:25 +00:00
|
|
|
text = d.currentOptionPrefix + d.options[d.currentOption].Text + d.currentOptionSuffix
|
|
|
|
}
|
|
|
|
// Just show the current selection.
|
2023-03-20 06:26:01 +00:00
|
|
|
if d.HasFocus() && !d.open && !d.disabled {
|
2019-07-05 09:48:25 +00:00
|
|
|
color = d.fieldBackgroundColor
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2019-07-05 09:48:25 +00:00
|
|
|
Print(screen, text, x, y, fieldWidth, AlignLeft, color)
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw options list.
|
|
|
|
if d.HasFocus() && d.open {
|
|
|
|
lx := x
|
|
|
|
ly := y + 1
|
2018-01-17 20:17:59 +00:00
|
|
|
lwidth := maxWidth
|
2017-12-18 19:04:52 +00:00
|
|
|
lheight := len(d.options)
|
2022-04-24 08:25:17 +00:00
|
|
|
swidth, sheight := screen.Size()
|
|
|
|
// We prefer to align the left sides of the list and the main widget, but
|
|
|
|
// if there is no space to the right, then shift the list to the left.
|
|
|
|
if lx+lwidth >= swidth {
|
|
|
|
lx = swidth - lwidth
|
|
|
|
if lx < 0 {
|
|
|
|
lx = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We prefer to drop down but if there is no space, maybe drop up?
|
2018-03-11 08:51:15 +00:00
|
|
|
if ly+lheight >= sheight && ly-2 > lheight-ly {
|
2017-12-18 19:04:52 +00:00
|
|
|
ly = y - lheight
|
2018-03-11 08:51:15 +00:00
|
|
|
if ly < 0 {
|
|
|
|
ly = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ly+lheight >= sheight {
|
|
|
|
lheight = sheight - ly
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
d.list.SetRect(lx, ly, lwidth, lheight)
|
|
|
|
d.list.Draw(screen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-19 20:25:30 +00:00
|
|
|
return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2023-03-20 06:26:01 +00:00
|
|
|
if d.disabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:05:43 +00:00
|
|
|
// If the list has focus, let it process its own key events.
|
2020-11-17 18:33:25 +00:00
|
|
|
if d.list.HasFocus() {
|
2020-08-18 10:05:43 +00:00
|
|
|
if handler := d.list.InputHandler(); handler != nil {
|
|
|
|
handler(event, setFocus)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// Process key event.
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyEnter, tcell.KeyRune, tcell.KeyDown:
|
2018-03-18 19:42:51 +00:00
|
|
|
d.prefix = ""
|
|
|
|
|
|
|
|
// If the first key was a letter already, it becomes part of the prefix.
|
|
|
|
if r := event.Rune(); key == tcell.KeyRune && r != ' ' {
|
|
|
|
d.prefix += string(r)
|
2019-11-05 01:11:21 +00:00
|
|
|
d.evalPrefix()
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2018-03-18 19:42:51 +00:00
|
|
|
|
2020-01-24 20:40:34 +00:00
|
|
|
d.openList(setFocus)
|
2017-12-18 19:04:52 +00:00
|
|
|
case tcell.KeyEscape, tcell.KeyTab, tcell.KeyBacktab:
|
|
|
|
if d.done != nil {
|
|
|
|
d.done(key)
|
|
|
|
}
|
2018-04-19 19:34:03 +00:00
|
|
|
if d.finished != nil {
|
|
|
|
d.finished(key)
|
|
|
|
}
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2018-01-14 20:29:34 +00:00
|
|
|
})
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:13:03 +00:00
|
|
|
// evalPrefix selects an item in the drop-down list based on the current prefix.
|
2019-11-05 01:11:21 +00:00
|
|
|
func (d *DropDown) evalPrefix() {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
for index, option := range d.options {
|
|
|
|
if strings.HasPrefix(strings.ToLower(option.Text), d.prefix) {
|
|
|
|
d.list.SetCurrentItem(index)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2020-03-27 20:13:03 +00:00
|
|
|
// Prefix does not match any item. Remove last rune.
|
2019-11-05 01:11:21 +00:00
|
|
|
r := []rune(d.prefix)
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// openList hands control over to the embedded List primitive.
|
2020-01-24 20:40:34 +00:00
|
|
|
func (d *DropDown) openList(setFocus func(Primitive)) {
|
2019-11-05 01:11:21 +00:00
|
|
|
d.open = true
|
|
|
|
optionBefore := d.currentOption
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2019-11-05 01:11:21 +00:00
|
|
|
d.list.SetSelectedFunc(func(index int, mainText, secondaryText string, shortcut rune) {
|
2020-03-27 17:41:44 +00:00
|
|
|
if d.dragging {
|
|
|
|
return // If we're dragging the mouse, we don't want to trigger any events.
|
|
|
|
}
|
|
|
|
|
2019-11-05 01:11:21 +00:00
|
|
|
// An option was selected. Close the list again.
|
|
|
|
d.currentOption = index
|
|
|
|
d.closeList(setFocus)
|
|
|
|
|
|
|
|
// Trigger "selected" event.
|
|
|
|
if d.selected != nil {
|
|
|
|
d.selected(d.options[d.currentOption].Text, d.currentOption)
|
|
|
|
}
|
|
|
|
if d.options[d.currentOption].Selected != nil {
|
|
|
|
d.options[d.currentOption].Selected()
|
|
|
|
}
|
|
|
|
}).SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
if event.Key() == tcell.KeyRune {
|
|
|
|
d.prefix += string(event.Rune())
|
|
|
|
d.evalPrefix()
|
|
|
|
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
|
|
|
|
if len(d.prefix) > 0 {
|
|
|
|
r := []rune(d.prefix)
|
|
|
|
d.prefix = string(r[:len(r)-1])
|
|
|
|
}
|
|
|
|
d.evalPrefix()
|
|
|
|
} else if event.Key() == tcell.KeyEscape {
|
|
|
|
d.currentOption = optionBefore
|
|
|
|
d.closeList(setFocus)
|
|
|
|
} else {
|
|
|
|
d.prefix = ""
|
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2019-11-05 01:11:21 +00:00
|
|
|
return event
|
|
|
|
})
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2019-11-05 01:11:21 +00:00
|
|
|
setFocus(d.list)
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// closeList closes the embedded List element by hiding it and removing focus
|
|
|
|
// from it.
|
2019-11-05 01:11:21 +00:00
|
|
|
func (d *DropDown) closeList(setFocus func(Primitive)) {
|
|
|
|
d.open = false
|
2019-11-05 01:43:35 +00:00
|
|
|
if d.list.HasFocus() {
|
2019-11-05 01:38:47 +00:00
|
|
|
setFocus(d)
|
|
|
|
}
|
2019-11-05 01:11:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-25 19:14:17 +00:00
|
|
|
// IsOpen returns true if the drop-down list is currently open.
|
|
|
|
func (d *DropDown) IsOpen() bool {
|
|
|
|
return d.open
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
// Focus is called by the application when the primitive receives focus.
|
|
|
|
func (d *DropDown) Focus(delegate func(p Primitive)) {
|
2023-03-20 06:26:01 +00:00
|
|
|
// If we're part of a form and this item is disabled, there's nothing the
|
|
|
|
// user can do here so we're finished.
|
|
|
|
if d.finished != nil && d.disabled {
|
|
|
|
d.finished(-1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-18 19:04:52 +00:00
|
|
|
if d.open {
|
|
|
|
delegate(d.list)
|
2021-11-08 16:05:49 +00:00
|
|
|
} else {
|
|
|
|
d.Box.Focus(delegate)
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (d *DropDown) HasFocus() bool {
|
|
|
|
if d.open {
|
|
|
|
return d.list.HasFocus()
|
|
|
|
}
|
2021-11-08 16:05:49 +00:00
|
|
|
return d.Box.HasFocus()
|
2017-12-18 19:04:52 +00:00
|
|
|
}
|
2019-11-04 06:30:25 +00:00
|
|
|
|
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (d *DropDown) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
|
|
|
return d.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2023-03-20 06:26:01 +00:00
|
|
|
if d.disabled {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// Was the mouse event in the drop-down box itself (or on its label)?
|
|
|
|
x, y := event.Position()
|
2021-05-28 18:06:28 +00:00
|
|
|
rectX, rectY, rectWidth, _ := d.GetInnerRect()
|
|
|
|
inRect := y == rectY && x >= rectX && x < rectX+rectWidth
|
2020-01-24 20:40:34 +00:00
|
|
|
if !d.open && !inRect {
|
2020-03-27 17:41:44 +00:00
|
|
|
return d.InRect(x, y), nil // No, and it's not expanded either. Ignore.
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
2022-09-16 08:15:18 +00:00
|
|
|
// As long as the drop-down is open, we capture all mouse events.
|
|
|
|
if d.open {
|
|
|
|
capture = d
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
switch action {
|
|
|
|
case MouseLeftDown:
|
|
|
|
consumed = d.open || inRect
|
|
|
|
capture = d
|
|
|
|
if !d.open {
|
2020-02-28 01:19:36 +00:00
|
|
|
d.openList(setFocus)
|
2020-03-27 17:41:44 +00:00
|
|
|
d.dragging = true
|
|
|
|
} else if consumed, _ := d.list.MouseHandler()(MouseLeftClick, event, setFocus); !consumed {
|
|
|
|
d.closeList(setFocus) // Close drop-down if clicked outside of it.
|
|
|
|
}
|
|
|
|
case MouseMove:
|
|
|
|
if d.dragging {
|
|
|
|
// We pretend it's a left click so we can see the selection during
|
|
|
|
// dragging. Because we don't act upon it, it's not a problem.
|
|
|
|
d.list.MouseHandler()(MouseLeftClick, event, setFocus)
|
|
|
|
consumed = true
|
|
|
|
}
|
|
|
|
case MouseLeftUp:
|
|
|
|
if d.dragging {
|
|
|
|
d.dragging = false
|
|
|
|
d.list.MouseHandler()(MouseLeftClick, event, setFocus)
|
|
|
|
consumed = true
|
2019-11-05 01:11:21 +00:00
|
|
|
}
|
2020-02-28 01:19:36 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
return
|
2019-11-04 06:30:25 +00:00
|
|
|
})
|
|
|
|
}
|