Customization of strings displayed when a checkbox is checked. Resolves #518

pull/532/head
Oliver 4 years ago
parent f9f2182520
commit f007e9ad38

@ -1,6 +1,8 @@
package tview
import (
"strings"
"github.com/gdamore/tcell/v2"
)
@ -30,6 +32,9 @@ type Checkbox struct {
// The text color of the input area.
fieldTextColor tcell.Color
// The string use to display a checked box.
checkedString string
// An optional function which is called when the user changes the checked
// state of this checkbox.
changed func(checked bool)
@ -51,6 +56,7 @@ func NewCheckbox() *Checkbox {
labelColor: Styles.SecondaryTextColor,
fieldBackgroundColor: Styles.ContrastBackgroundColor,
fieldTextColor: Styles.PrimaryTextColor,
checkedString: "X",
}
}
@ -101,6 +107,13 @@ func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
return c
}
// SetCheckedString sets the string to be displayed when the checkbox is
// checked (defaults to "X").
func (c *Checkbox) SetCheckedString(checked string) *Checkbox {
c.checkedString = checked
return c
}
// SetFormAttributes sets attributes shared by all form items.
func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
c.labelWidth = labelWidth
@ -171,11 +184,12 @@ func (c *Checkbox) Draw(screen tcell.Screen) {
if c.HasFocus() {
fieldStyle = fieldStyle.Background(c.fieldTextColor).Foreground(c.fieldBackgroundColor)
}
checkedRune := 'X'
checkboxWidth := stringWidth(c.checkedString)
checkedString := c.checkedString
if !c.checked {
checkedRune = ' '
checkedString = strings.Repeat(" ", checkboxWidth)
}
screen.SetContent(x, y, checkedRune, nil, fieldStyle)
printWithStyle(screen, checkedString, x, y, checkboxWidth, AlignLeft, fieldStyle)
}
// InputHandler returns the handler for this primitive.

Loading…
Cancel
Save