From 6e9f09aec1cc4d8ba4dd1ae2278f96faf02f0865 Mon Sep 17 00:00:00 2001 From: Andreas Bieber Date: Thu, 17 Sep 2020 13:03:43 +0200 Subject: [PATCH] feat(dropdown): Draw rune at the end of the field to indicate that this field is a dropdown --- dropdown.go | 17 +++++++++++++++++ styles.go | 2 ++ 2 files changed, 19 insertions(+) diff --git a/dropdown.go b/dropdown.go index f6f61e1..45fa833 100644 --- a/dropdown.go +++ b/dropdown.go @@ -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? diff --git a/styles.go b/styles.go index 646101a..c15f609 100644 --- a/styles.go +++ b/styles.go @@ -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: '▼', }