|
|
|
@ -49,12 +49,21 @@ type DropDown struct {
|
|
|
|
|
// The label color.
|
|
|
|
|
labelColor tcell.Color
|
|
|
|
|
|
|
|
|
|
// The label color when focused.
|
|
|
|
|
labelColorFocused tcell.Color
|
|
|
|
|
|
|
|
|
|
// The background color of the input area.
|
|
|
|
|
fieldBackgroundColor tcell.Color
|
|
|
|
|
|
|
|
|
|
// The background color of the input area when focused.
|
|
|
|
|
fieldBackgroundColorFocused tcell.Color
|
|
|
|
|
|
|
|
|
|
// The text color of the input area.
|
|
|
|
|
fieldTextColor tcell.Color
|
|
|
|
|
|
|
|
|
|
// The text color of the input area when focused.
|
|
|
|
|
fieldTextColorFocused tcell.Color
|
|
|
|
|
|
|
|
|
|
// The color for prefixes.
|
|
|
|
|
prefixTextColor tcell.Color
|
|
|
|
|
|
|
|
|
@ -96,13 +105,16 @@ func NewDropDown() *DropDown {
|
|
|
|
|
SetBackgroundColor(Styles.MoreContrastBackgroundColor)
|
|
|
|
|
|
|
|
|
|
d := &DropDown{
|
|
|
|
|
Box: NewBox(),
|
|
|
|
|
currentOption: -1,
|
|
|
|
|
list: list,
|
|
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
|
|
|
|
prefixTextColor: Styles.ContrastSecondaryTextColor,
|
|
|
|
|
Box: NewBox(),
|
|
|
|
|
currentOption: -1,
|
|
|
|
|
list: list,
|
|
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
|
labelColorFocused: Styles.SecondaryTextColor,
|
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
|
fieldBackgroundColorFocused: Styles.ContrastBackgroundColor,
|
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
|
|
|
|
fieldTextColorFocused: Styles.PrimaryTextColor,
|
|
|
|
|
prefixTextColor: Styles.ContrastSecondaryTextColor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
d.focus = d
|
|
|
|
@ -211,6 +223,15 @@ func (d *DropDown) SetLabelColor(color tcell.Color) *DropDown {
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetLabelColorFocused sets the color of the label when focused.
|
|
|
|
|
func (d *DropDown) SetLabelColorFocused(color tcell.Color) *DropDown {
|
|
|
|
|
d.Lock()
|
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
|
|
d.labelColorFocused = color
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFieldBackgroundColor sets the background color of the options area.
|
|
|
|
|
func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown {
|
|
|
|
|
d.Lock()
|
|
|
|
@ -220,6 +241,15 @@ func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown {
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFieldBackgroundColorFocused sets the background color of the options area when focused.
|
|
|
|
|
func (d *DropDown) SetFieldBackgroundColorFocused(color tcell.Color) *DropDown {
|
|
|
|
|
d.Lock()
|
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
|
|
d.fieldBackgroundColorFocused = color
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the options area.
|
|
|
|
|
func (d *DropDown) SetFieldTextColor(color tcell.Color) *DropDown {
|
|
|
|
|
d.Lock()
|
|
|
|
@ -229,6 +259,15 @@ func (d *DropDown) SetFieldTextColor(color tcell.Color) *DropDown {
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFieldTextColorFocused sets the text color of the options area when focused.
|
|
|
|
|
func (d *DropDown) SetFieldTextColorFocused(color tcell.Color) *DropDown {
|
|
|
|
|
d.Lock()
|
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
|
|
d.fieldTextColorFocused = color
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
@ -241,15 +280,18 @@ func (d *DropDown) SetPrefixTextColor(color tcell.Color) *DropDown {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFormAttributes sets attributes shared by all form items.
|
|
|
|
|
func (d *DropDown) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
|
|
|
|
func (d *DropDown) SetFormAttributes(labelWidth int, bgColor, labelColor, labelColorFocused, fieldTextColor, fieldTextColorFocused, fieldBgColor, fieldBgColorFocused tcell.Color) FormItem {
|
|
|
|
|
d.Lock()
|
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
|
|
d.labelWidth = labelWidth
|
|
|
|
|
d.labelColor = labelColor
|
|
|
|
|
d.backgroundColor = bgColor
|
|
|
|
|
d.labelColor = labelColor
|
|
|
|
|
d.labelColorFocused = labelColorFocused
|
|
|
|
|
d.fieldTextColor = fieldTextColor
|
|
|
|
|
d.fieldTextColorFocused = fieldTextColorFocused
|
|
|
|
|
d.fieldBackgroundColor = fieldBgColor
|
|
|
|
|
d.fieldBackgroundColorFocused = fieldBgColorFocused
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -360,6 +402,16 @@ func (d *DropDown) Draw(screen tcell.Screen) {
|
|
|
|
|
d.Lock()
|
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
|
|
// Select colors
|
|
|
|
|
labelColor := d.labelColor
|
|
|
|
|
fieldBackgroundColor := d.fieldBackgroundColor
|
|
|
|
|
fieldTextColor := d.fieldTextColor
|
|
|
|
|
if hasFocus {
|
|
|
|
|
labelColor = d.labelColorFocused
|
|
|
|
|
fieldBackgroundColor = d.fieldBackgroundColorFocused
|
|
|
|
|
fieldTextColor = d.fieldTextColorFocused
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare.
|
|
|
|
|
x, y, width, height := d.GetInnerRect()
|
|
|
|
|
rightLimit := x + width
|
|
|
|
@ -373,10 +425,10 @@ func (d *DropDown) Draw(screen tcell.Screen) {
|
|
|
|
|
if labelWidth > rightLimit-x {
|
|
|
|
|
labelWidth = rightLimit - x
|
|
|
|
|
}
|
|
|
|
|
Print(screen, d.label, x, y, labelWidth, AlignLeft, d.labelColor)
|
|
|
|
|
Print(screen, d.label, x, y, labelWidth, AlignLeft, labelColor)
|
|
|
|
|
x += labelWidth
|
|
|
|
|
} else {
|
|
|
|
|
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, d.labelColor)
|
|
|
|
|
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, labelColor)
|
|
|
|
|
x += drawnWidth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -409,10 +461,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
|
|
|
|
|
if rightLimit-x < fieldWidth {
|
|
|
|
|
fieldWidth = rightLimit - x
|
|
|
|
|
}
|
|
|
|
|
fieldStyle := tcell.StyleDefault.Background(d.fieldBackgroundColor)
|
|
|
|
|
if hasFocus && !d.open {
|
|
|
|
|
fieldStyle = fieldStyle.Background(d.fieldTextColor)
|
|
|
|
|
}
|
|
|
|
|
fieldStyle := tcell.StyleDefault.Background(fieldBackgroundColor)
|
|
|
|
|
for index := 0; index < fieldWidth; index++ {
|
|
|
|
|
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
|
|
|
|
|
}
|
|
|
|
@ -423,21 +472,18 @@ func (d *DropDown) Draw(screen tcell.Screen) {
|
|
|
|
|
currentOptionPrefixWidth := TaggedStringWidth(d.currentOptionPrefix)
|
|
|
|
|
prefixWidth := stringWidth(d.prefix)
|
|
|
|
|
listItemText := d.options[d.list.GetCurrentItem()].Text
|
|
|
|
|
Print(screen, d.currentOptionPrefix, x, y, fieldWidth, AlignLeft, d.fieldTextColor)
|
|
|
|
|
Print(screen, d.currentOptionPrefix, x, y, fieldWidth, AlignLeft, 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)
|
|
|
|
|
Print(screen, listItemText[len(d.prefix):]+d.currentOptionSuffix, x+prefixWidth+currentOptionPrefixWidth, y, fieldWidth-prefixWidth-currentOptionPrefixWidth, AlignLeft, fieldTextColor)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
color := d.fieldTextColor
|
|
|
|
|
color := fieldTextColor
|
|
|
|
|
text := d.noSelection
|
|
|
|
|
if d.currentOption >= 0 && d.currentOption < len(d.options) {
|
|
|
|
|
text = d.currentOptionPrefix + d.options[d.currentOption].Text + d.currentOptionSuffix
|
|
|
|
|
}
|
|
|
|
|
// Just show the current selection.
|
|
|
|
|
if hasFocus && !d.open {
|
|
|
|
|
color = d.fieldBackgroundColor
|
|
|
|
|
}
|
|
|
|
|
Print(screen, text, x, y, fieldWidth, AlignLeft, color)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|