feat(dropdown): Draw rune at the end of the field to indicate that this field is a dropdown

This commit is contained in:
Andreas Bieber 2020-09-17 13:03:43 +02:00
parent 6fa4370332
commit 6e9f09aec1
2 changed files with 19 additions and 0 deletions

View File

@ -127,6 +127,9 @@ type DropDown struct {
// The chars to show when the option's text gets shortened.
abbreviationChars string
// The symbol to draw at the end of the field.
dropDownSymbol rune
sync.RWMutex
}
@ -151,6 +154,7 @@ func NewDropDown() *DropDown {
fieldTextColor: Styles.PrimaryTextColor,
fieldTextColorFocused: Styles.PrimaryTextColor,
prefixTextColor: Styles.ContrastSecondaryTextColor,
dropDownSymbol: Styles.DropDownSymbol,
abbreviationChars: Styles.DropDownAbbreviationChars,
}
@ -159,6 +163,15 @@ func NewDropDown() *DropDown {
return d
}
// SetDropDownSymbolRune sets the rune to be drawn at the end of the dropdown field
// to indicate that this field is a dropdown.
func (d *DropDown) SetDropDownSymbolRune(symbol rune) *DropDown {
d.Lock()
defer d.Unlock()
d.dropDownSymbol = symbol
return d
}
// SetCurrentOption sets the index of the currently selected option. This may
// 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).
@ -398,6 +411,7 @@ func (d *DropDown) getFieldWidth() int {
}
fieldWidth += len(d.optionPrefix) + len(d.optionSuffix)
fieldWidth += len(d.currentOptionPrefix) + len(d.currentOptionSuffix)
fieldWidth += 3 // space + dropDownSymbol + space
return fieldWidth
}
@ -589,6 +603,9 @@ func (d *DropDown) Draw(screen tcell.Screen) {
Print(screen, text, x, y, fieldWidth, AlignLeft, color)
}
// Draw drop down symbol
screen.SetContent(x+fieldWidth-2, y, d.dropDownSymbol, nil, new(tcell.Style).Foreground(fieldTextColor).Background(fieldBackgroundColor))
// Draw options list.
if hasFocus && d.open {
// We prefer to drop down but if there is no space, maybe drop up?

View File

@ -36,6 +36,7 @@ type Theme struct {
// Drop down
DropDownAbbreviationChars string // The chars to show when the option's text gets shortened.
DropDownSymbol rune // The symbol to draw at the end of the field.
}
// Styles defines the appearance of an application. The default is for a black
@ -67,4 +68,5 @@ var Styles = Theme{
CheckBoxCheckedRune: 'X',
DropDownAbbreviationChars: "...",
DropDownSymbol: '▼',
}