forked from tslocum/cview
Rename CheckBox
This commit is contained in:
parent
e8b2604dcb
commit
de0eb1ea89
|
@ -34,7 +34,7 @@ v1.4.0 (2020-01-16)
|
|||
- Bump version to resolve issues with "go get"
|
||||
|
||||
v0.2.2 (2020-01-06)
|
||||
- Add optional message displayed after Checkbox
|
||||
- Add optional message displayed after CheckBox
|
||||
- Fix Dropdown mouse capture behavior
|
||||
- Fix TextView region highlighting on last line
|
||||
|
||||
|
|
46
checkbox.go
46
checkbox.go
|
@ -6,11 +6,11 @@ import (
|
|||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// Checkbox implements a simple box for boolean values which can be checked and
|
||||
// CheckBox implements a simple box for boolean values which can be checked and
|
||||
// unchecked.
|
||||
//
|
||||
// See https://gitlab.com/tslocum/cview/wiki/Checkbox for an example.
|
||||
type Checkbox struct {
|
||||
type CheckBox struct {
|
||||
*Box
|
||||
|
||||
// Whether or not this box is checked.
|
||||
|
@ -51,9 +51,9 @@ type Checkbox struct {
|
|||
sync.Mutex
|
||||
}
|
||||
|
||||
// NewCheckbox returns a new input field.
|
||||
func NewCheckbox() *Checkbox {
|
||||
return &Checkbox{
|
||||
// NewCheckBox returns a new input field.
|
||||
func NewCheckBox() *CheckBox {
|
||||
return &CheckBox{
|
||||
Box: NewBox(),
|
||||
labelColor: Styles.SecondaryTextColor,
|
||||
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
||||
|
@ -62,7 +62,7 @@ func NewCheckbox() *Checkbox {
|
|||
}
|
||||
|
||||
// SetChecked sets the state of the checkbox.
|
||||
func (c *Checkbox) SetChecked(checked bool) *Checkbox {
|
||||
func (c *CheckBox) SetChecked(checked bool) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -71,7 +71,7 @@ func (c *Checkbox) SetChecked(checked bool) *Checkbox {
|
|||
}
|
||||
|
||||
// IsChecked returns whether or not the box is checked.
|
||||
func (c *Checkbox) IsChecked() bool {
|
||||
func (c *CheckBox) IsChecked() bool {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -79,7 +79,7 @@ func (c *Checkbox) IsChecked() bool {
|
|||
}
|
||||
|
||||
// SetLabel sets the text to be displayed before the input area.
|
||||
func (c *Checkbox) SetLabel(label string) *Checkbox {
|
||||
func (c *CheckBox) SetLabel(label string) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -88,7 +88,7 @@ func (c *Checkbox) SetLabel(label string) *Checkbox {
|
|||
}
|
||||
|
||||
// GetLabel returns the text to be displayed before the input area.
|
||||
func (c *Checkbox) GetLabel() string {
|
||||
func (c *CheckBox) GetLabel() string {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -96,7 +96,7 @@ func (c *Checkbox) GetLabel() string {
|
|||
}
|
||||
|
||||
// SetMessage sets the text to be displayed after the checkbox
|
||||
func (c *Checkbox) SetMessage(message string) *Checkbox {
|
||||
func (c *CheckBox) SetMessage(message string) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -105,7 +105,7 @@ func (c *Checkbox) SetMessage(message string) *Checkbox {
|
|||
}
|
||||
|
||||
// GetMessage returns the text to be displayed after the checkbox
|
||||
func (c *Checkbox) GetMessage() string {
|
||||
func (c *CheckBox) GetMessage() string {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -114,7 +114,7 @@ func (c *Checkbox) GetMessage() string {
|
|||
|
||||
// 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 (c *Checkbox) SetLabelWidth(width int) *Checkbox {
|
||||
func (c *CheckBox) SetLabelWidth(width int) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -123,7 +123,7 @@ func (c *Checkbox) SetLabelWidth(width int) *Checkbox {
|
|||
}
|
||||
|
||||
// SetLabelColor sets the color of the label.
|
||||
func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
|
||||
func (c *CheckBox) SetLabelColor(color tcell.Color) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -132,7 +132,7 @@ func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
|
|||
}
|
||||
|
||||
// SetFieldBackgroundColor sets the background color of the input area.
|
||||
func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
|
||||
func (c *CheckBox) SetFieldBackgroundColor(color tcell.Color) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -141,7 +141,7 @@ func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
|
|||
}
|
||||
|
||||
// SetFieldTextColor sets the text color of the input area.
|
||||
func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
|
||||
func (c *CheckBox) SetFieldTextColor(color tcell.Color) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -150,7 +150,7 @@ func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
|
|||
}
|
||||
|
||||
// SetFormAttributes sets attributes shared by all form items.
|
||||
func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
||||
func (c *CheckBox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -163,7 +163,7 @@ func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldT
|
|||
}
|
||||
|
||||
// GetFieldWidth returns this primitive's field width.
|
||||
func (c *Checkbox) GetFieldWidth() int {
|
||||
func (c *CheckBox) GetFieldWidth() int {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -177,7 +177,7 @@ func (c *Checkbox) GetFieldWidth() int {
|
|||
// SetChangedFunc sets a handler which is called when the checked state of this
|
||||
// checkbox was changed by the user. The handler function receives the new
|
||||
// state.
|
||||
func (c *Checkbox) SetChangedFunc(handler func(checked bool)) *Checkbox {
|
||||
func (c *CheckBox) SetChangedFunc(handler func(checked bool)) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -192,7 +192,7 @@ func (c *Checkbox) SetChangedFunc(handler func(checked bool)) *Checkbox {
|
|||
// - KeyEscape: Abort text input.
|
||||
// - KeyTab: Move to the next field.
|
||||
// - KeyBacktab: Move to the previous field.
|
||||
func (c *Checkbox) SetDoneFunc(handler func(key tcell.Key)) *Checkbox {
|
||||
func (c *CheckBox) SetDoneFunc(handler func(key tcell.Key)) *CheckBox {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -201,7 +201,7 @@ func (c *Checkbox) SetDoneFunc(handler func(key tcell.Key)) *Checkbox {
|
|||
}
|
||||
|
||||
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
|
||||
func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
||||
func (c *CheckBox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
||||
c.Lock()
|
||||
defer c.Unlock()
|
||||
|
||||
|
@ -210,7 +210,7 @@ func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
|||
}
|
||||
|
||||
// Draw draws this primitive onto the screen.
|
||||
func (c *Checkbox) Draw(screen tcell.Screen) {
|
||||
func (c *CheckBox) Draw(screen tcell.Screen) {
|
||||
c.Box.Draw(screen)
|
||||
|
||||
c.Lock()
|
||||
|
@ -253,7 +253,7 @@ func (c *Checkbox) Draw(screen tcell.Screen) {
|
|||
}
|
||||
|
||||
// InputHandler returns the handler for this primitive.
|
||||
func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
func (c *CheckBox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
||||
// Process key event.
|
||||
switch key := event.Key(); key {
|
||||
|
@ -279,7 +279,7 @@ func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
|
|||
}
|
||||
|
||||
// MouseHandler returns the mouse handler for this primitive.
|
||||
func (c *Checkbox) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
||||
func (c *CheckBox) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
||||
return c.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
||||
x, y := event.Position()
|
||||
_, rectY, _, _ := c.GetInnerRect()
|
||||
|
|
|
@ -7,50 +7,50 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
testCheckboxLabelA = "Hello, world!"
|
||||
testCheckboxLabelB = "Goodnight, moon!"
|
||||
testCheckBoxLabelA = "Hello, world!"
|
||||
testCheckBoxLabelB = "Goodnight, moon!"
|
||||
)
|
||||
|
||||
func TestCheckbox(t *testing.T) {
|
||||
func TestCheckBox(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
c, sc, err := testCheckbox()
|
||||
c, sc, err := testCheckBox()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if c.IsChecked() {
|
||||
t.Errorf("failed to initalize checkbox: incorrect initial state: expected unchecked, got checked")
|
||||
} else if c.GetLabel() != testCheckboxLabelA {
|
||||
t.Errorf("failed to initalize checkbox: incorrect label: expected %s, got %s", testCheckboxLabelA, c.GetLabel())
|
||||
t.Errorf("failed to initalize CheckBox: incorrect initial state: expected unchecked, got checked")
|
||||
} else if c.GetLabel() != testCheckBoxLabelA {
|
||||
t.Errorf("failed to initalize CheckBox: incorrect label: expected %s, got %s", testCheckBoxLabelA, c.GetLabel())
|
||||
}
|
||||
|
||||
c.SetLabel(testCheckboxLabelB)
|
||||
if c.GetLabel() != testCheckboxLabelB {
|
||||
t.Errorf("failed to set checkbox label: incorrect label: expected %s, got %s", testCheckboxLabelA, c.GetLabel())
|
||||
c.SetLabel(testCheckBoxLabelB)
|
||||
if c.GetLabel() != testCheckBoxLabelB {
|
||||
t.Errorf("failed to set CheckBox label: incorrect label: expected %s, got %s", testCheckBoxLabelB, c.GetLabel())
|
||||
}
|
||||
|
||||
c.SetChecked(true)
|
||||
if !c.IsChecked() {
|
||||
t.Errorf("failed to update checkbox state: incorrect state: expected checked, got unchecked")
|
||||
t.Errorf("failed to update CheckBox state: incorrect state: expected checked, got unchecked")
|
||||
}
|
||||
|
||||
c.SetChecked(false)
|
||||
if c.IsChecked() {
|
||||
t.Errorf("failed to update checkbox state: incorrect state: expected unchecked, got checked")
|
||||
t.Errorf("failed to update CheckBox state: incorrect state: expected unchecked, got checked")
|
||||
}
|
||||
|
||||
c.Draw(sc)
|
||||
}
|
||||
|
||||
func testCheckbox() (*Checkbox, tcell.Screen, error) {
|
||||
c := NewCheckbox()
|
||||
func testCheckBox() (*CheckBox, tcell.Screen, error) {
|
||||
c := NewCheckBox()
|
||||
|
||||
sc := tcell.NewSimulationScreen("UTF-8")
|
||||
sc.SetSize(80, 24)
|
||||
|
||||
_ = NewApplication().SetRoot(c, true).SetScreen(sc)
|
||||
|
||||
c.SetLabel(testCheckboxLabelA)
|
||||
c.SetLabel(testCheckBoxLabelA)
|
||||
|
||||
return c, sc, nil
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Demo code for the Checkbox primitive.
|
||||
// Demo code for the CheckBox primitive.
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -7,7 +7,7 @@ import (
|
|||
|
||||
func main() {
|
||||
app := cview.NewApplication()
|
||||
checkbox := cview.NewCheckbox().SetLabel("Hit Enter to check box: ")
|
||||
checkbox := cview.NewCheckBox().SetLabel("Hit Enter to check box: ")
|
||||
if err := app.SetRoot(checkbox, true).EnableMouse(true).Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ func main() {
|
|||
AddInputField("First name", "", 20, nil, nil).
|
||||
AddInputField("Last name", "", 20, nil, nil).
|
||||
AddPasswordField("Password", "", 10, '*', nil).
|
||||
AddCheckbox("", "Age 18+", false, nil).
|
||||
AddCheckBox("", "Age 18+", false, nil).
|
||||
AddButton("Save", nil).
|
||||
AddButton("Quit", func() {
|
||||
app.Stop()
|
||||
|
|
|
@ -19,7 +19,7 @@ const form = `[green]package[white] main
|
|||
[red]"Manager"[white],
|
||||
[red]"Administration"[white],
|
||||
}, [red]0[white], nil).
|
||||
[yellow]AddCheckbox[white]([red]"On vacation:"[white], false, nil).
|
||||
[yellow]AddCheckBox[white]([red]"On vacation:"[white], false, nil).
|
||||
[yellow]AddPasswordField[white]([red]"Password:"[white], [red]""[white], [red]10[white], [red]'*'[white], nil).
|
||||
[yellow]AddButton[white]([red]"Save"[white], [yellow]func[white]() { [blue]/* Save data */[white] }).
|
||||
[yellow]AddButton[white]([red]"Cancel"[white], [yellow]func[white]() { [blue]/* Cancel */[white] })
|
||||
|
@ -35,7 +35,7 @@ func Form(nextSlide func()) (title string, content cview.Primitive) {
|
|||
AddInputField("Last name:", "", 20, nil, nil).
|
||||
AddDropDown("Role:", []string{"Engineer", "Manager", "Administration"}, 0, nil).
|
||||
AddPasswordField("Password:", "", 10, '*', nil).
|
||||
AddCheckbox("", "On vacation", false, nil).
|
||||
AddCheckBox("", "On vacation", false, nil).
|
||||
AddButton("Save", nextSlide).
|
||||
AddButton("Cancel", nextSlide)
|
||||
f.SetBorder(true).SetTitle("Employee Information")
|
||||
|
|
|
@ -15,7 +15,7 @@ func main() {
|
|||
form.AddDropDown("称谓", []string{"先生", "女士", "博士", "老师", "师傅"}, 0, nil).
|
||||
AddInputField("姓名", "", 20, nil, nil).
|
||||
AddPasswordField("密码", "", 10, '*', nil).
|
||||
AddCheckbox("", "年龄 18+", false, nil).
|
||||
AddCheckBox("", "年龄 18+", false, nil).
|
||||
AddButton("保存", func() {
|
||||
_, title := form.GetFormItem(0).(*cview.DropDown).GetCurrentOption()
|
||||
userName := form.GetFormItem(1).(*cview.InputField).GetText()
|
||||
|
|
2
doc.go
2
doc.go
|
@ -9,7 +9,7 @@ Widgets
|
|||
The following widgets are available:
|
||||
|
||||
Button - Button which is activated when the user selects it.
|
||||
Checkbox - Selectable checkbox for boolean values.
|
||||
CheckBox - Selectable checkbox for boolean values.
|
||||
DropDown - Drop-down selection field.
|
||||
Flex - A Flexbox based layout manager.
|
||||
Form - Form composed of input fields, drop down selections, checkboxes, and
|
||||
|
|
8
form.go
8
form.go
|
@ -37,7 +37,7 @@ type FormItem interface {
|
|||
|
||||
// Form allows you to combine multiple one-line form elements into a vertical
|
||||
// or horizontal layout. Form elements include types such as InputField or
|
||||
// Checkbox. These elements can be optionally followed by one or more buttons
|
||||
// CheckBox. These elements can be optionally followed by one or more buttons
|
||||
// for which you can define form-wide actions (e.g. Save, Clear, Cancel).
|
||||
//
|
||||
// See https://gitlab.com/tslocum/cview/wiki/Form for an example.
|
||||
|
@ -258,14 +258,14 @@ func (f *Form) AddDropDown(label string, options []string, initialOption int, se
|
|||
return f
|
||||
}
|
||||
|
||||
// AddCheckbox adds a checkbox to the form. It has a label, a message, an
|
||||
// AddCheckBox adds a checkbox to the form. It has a label, a message, an
|
||||
// initial state, and an (optional) callback function which is invoked when the
|
||||
// state of the checkbox was changed by the user.
|
||||
func (f *Form) AddCheckbox(label string, message string, checked bool, changed func(checked bool)) *Form {
|
||||
func (f *Form) AddCheckBox(label string, message string, checked bool, changed func(checked bool)) *Form {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
|
||||
f.items = append(f.items, NewCheckbox().
|
||||
f.items = append(f.items, NewCheckBox().
|
||||
SetLabel(label).
|
||||
SetMessage(message).
|
||||
SetChecked(checked).
|
||||
|
|
Loading…
Reference in New Issue