2017-12-21 03:54:49 +08:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
2018-01-07 23:39:06 +08:00
|
|
|
// Checkbox implements a simple box for boolean values which can be checked and
|
|
|
|
// unchecked.
|
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/Checkbox for an example.
|
2017-12-21 03:54:49 +08:00
|
|
|
type Checkbox struct {
|
|
|
|
*Box
|
|
|
|
|
|
|
|
// Whether or not this box is checked.
|
|
|
|
checked bool
|
|
|
|
|
|
|
|
// The text to be displayed before the input area.
|
|
|
|
label string
|
|
|
|
|
2018-04-10 03:07:03 +08:00
|
|
|
// The screen width of the label area. A value of 0 means use the width of
|
|
|
|
// the label text.
|
|
|
|
labelWidth int
|
|
|
|
|
2017-12-21 03:54:49 +08:00
|
|
|
// The label color.
|
|
|
|
labelColor tcell.Color
|
|
|
|
|
|
|
|
// The background color of the input area.
|
|
|
|
fieldBackgroundColor tcell.Color
|
|
|
|
|
|
|
|
// The text color of the input area.
|
|
|
|
fieldTextColor tcell.Color
|
|
|
|
|
|
|
|
// An optional function which is called when the user changes the checked
|
|
|
|
// state of this checkbox.
|
|
|
|
changed func(checked bool)
|
|
|
|
|
|
|
|
// An optional function which is called when the user indicated that they
|
|
|
|
// are done entering text. The key which was pressed is provided (tab,
|
|
|
|
// shift-tab, or escape).
|
|
|
|
done func(tcell.Key)
|
2018-04-20 03:34:03 +08:00
|
|
|
|
|
|
|
// A callback function set by the Form class and called when the user leaves
|
|
|
|
// this form item.
|
|
|
|
finished func(tcell.Key)
|
2017-12-21 03:54:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox returns a new input field.
|
|
|
|
func NewCheckbox() *Checkbox {
|
|
|
|
return &Checkbox{
|
|
|
|
Box: NewBox(),
|
2018-01-10 16:43:32 +08:00
|
|
|
labelColor: Styles.SecondaryTextColor,
|
|
|
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
|
|
|
fieldTextColor: Styles.PrimaryTextColor,
|
2017-12-21 03:54:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetChecked sets the state of the checkbox.
|
|
|
|
func (c *Checkbox) SetChecked(checked bool) *Checkbox {
|
|
|
|
c.checked = checked
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-01-10 17:40:51 +08:00
|
|
|
// IsChecked returns whether or not the box is checked.
|
|
|
|
func (c *Checkbox) IsChecked() bool {
|
|
|
|
return c.checked
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:54:49 +08:00
|
|
|
// SetLabel sets the text to be displayed before the input area.
|
|
|
|
func (c *Checkbox) SetLabel(label string) *Checkbox {
|
|
|
|
c.label = label
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabel returns the text to be displayed before the input area.
|
|
|
|
func (c *Checkbox) GetLabel() string {
|
|
|
|
return c.label
|
|
|
|
}
|
|
|
|
|
2018-04-10 03:07:03 +08:00
|
|
|
// 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 {
|
|
|
|
c.labelWidth = width
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:54:49 +08:00
|
|
|
// SetLabelColor sets the color of the label.
|
|
|
|
func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
|
|
|
|
c.labelColor = color
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldBackgroundColor sets the background color of the input area.
|
|
|
|
func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
|
|
|
|
c.fieldBackgroundColor = color
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFieldTextColor sets the text color of the input area.
|
|
|
|
func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
|
|
|
|
c.fieldTextColor = color
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFormAttributes sets attributes shared by all form items.
|
2018-04-10 03:07:03 +08:00
|
|
|
func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
|
|
|
c.labelWidth = labelWidth
|
2017-12-21 03:54:49 +08:00
|
|
|
c.labelColor = labelColor
|
|
|
|
c.backgroundColor = bgColor
|
|
|
|
c.fieldTextColor = fieldTextColor
|
|
|
|
c.fieldBackgroundColor = fieldBgColor
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:17:59 +08:00
|
|
|
// GetFieldWidth returns this primitive's field width.
|
|
|
|
func (c *Checkbox) GetFieldWidth() int {
|
2018-01-17 03:45:54 +08:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:54:49 +08:00
|
|
|
// 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 {
|
|
|
|
c.changed = handler
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-09-05 16:39:21 +08:00
|
|
|
// SetDoneFunc sets a handler which is called when the user is done using the
|
|
|
|
// checkbox. The callback function is provided with the key that was pressed,
|
|
|
|
// which is one of the following:
|
2017-12-21 03:54:49 +08:00
|
|
|
//
|
|
|
|
// - 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 {
|
|
|
|
c.done = handler
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-04-20 03:34:03 +08:00
|
|
|
// SetFinishedFunc sets a callback invoked when the user leaves this form item.
|
2017-12-21 03:54:49 +08:00
|
|
|
func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {
|
2018-04-20 03:34:03 +08:00
|
|
|
c.finished = handler
|
|
|
|
return c
|
2017-12-21 03:54:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (c *Checkbox) Draw(screen tcell.Screen) {
|
|
|
|
c.Box.Draw(screen)
|
|
|
|
|
|
|
|
// Prepare
|
2017-12-22 01:08:53 +08:00
|
|
|
x, y, width, height := c.GetInnerRect()
|
|
|
|
rightLimit := x + width
|
2017-12-21 03:54:49 +08:00
|
|
|
if height < 1 || rightLimit <= x {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw label.
|
2018-04-10 03:07:03 +08:00
|
|
|
if c.labelWidth > 0 {
|
|
|
|
labelWidth := c.labelWidth
|
|
|
|
if labelWidth > rightLimit-x {
|
|
|
|
labelWidth = rightLimit - x
|
|
|
|
}
|
|
|
|
Print(screen, c.label, x, y, labelWidth, AlignLeft, c.labelColor)
|
|
|
|
x += labelWidth
|
|
|
|
} else {
|
|
|
|
_, drawnWidth := Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor)
|
|
|
|
x += drawnWidth
|
|
|
|
}
|
2017-12-21 03:54:49 +08:00
|
|
|
|
|
|
|
// Draw checkbox.
|
|
|
|
fieldStyle := tcell.StyleDefault.Background(c.fieldBackgroundColor).Foreground(c.fieldTextColor)
|
|
|
|
if c.focus.HasFocus() {
|
|
|
|
fieldStyle = fieldStyle.Background(c.fieldTextColor).Foreground(c.fieldBackgroundColor)
|
|
|
|
}
|
|
|
|
checkedRune := 'X'
|
|
|
|
if !c.checked {
|
|
|
|
checkedRune = ' '
|
|
|
|
}
|
|
|
|
screen.SetContent(x, y, checkedRune, nil, fieldStyle)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
func (c *Checkbox) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-20 04:25:30 +08:00
|
|
|
return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2017-12-21 03:54:49 +08:00
|
|
|
// Process key event.
|
|
|
|
switch key := event.Key(); key {
|
|
|
|
case tcell.KeyRune, tcell.KeyEnter: // Check.
|
|
|
|
if key == tcell.KeyRune && event.Rune() != ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.checked = !c.checked
|
|
|
|
if c.changed != nil {
|
|
|
|
c.changed(c.checked)
|
|
|
|
}
|
|
|
|
case tcell.KeyTab, tcell.KeyBacktab, tcell.KeyEscape: // We're done.
|
|
|
|
if c.done != nil {
|
|
|
|
c.done(key)
|
|
|
|
}
|
2018-04-20 03:34:03 +08:00
|
|
|
if c.finished != nil {
|
|
|
|
c.finished(key)
|
|
|
|
}
|
2017-12-21 03:54:49 +08:00
|
|
|
}
|
2018-01-15 04:29:34 +08:00
|
|
|
})
|
2017-12-21 03:54:49 +08:00
|
|
|
}
|
2019-11-04 14:30:25 +08:00
|
|
|
|
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 10:09:09 +08:00
|
|
|
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) {
|
2020-03-28 01:41:44 +08:00
|
|
|
x, y := event.Position()
|
|
|
|
_, rectY, _, _ := c.GetInnerRect()
|
|
|
|
if !c.InRect(x, y) {
|
2020-02-14 10:09:09 +08:00
|
|
|
return false, nil
|
2020-01-25 04:40:34 +08:00
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
|
2019-11-04 14:30:25 +08:00
|
|
|
// Process mouse event.
|
2020-03-28 01:41:44 +08:00
|
|
|
if action == MouseLeftClick && y == rectY {
|
|
|
|
setFocus(c)
|
2019-11-04 14:30:25 +08:00
|
|
|
c.checked = !c.checked
|
|
|
|
if c.changed != nil {
|
|
|
|
c.changed(c.checked)
|
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
consumed = true
|
2019-11-04 14:30:25 +08:00
|
|
|
}
|
2020-03-28 01:41:44 +08:00
|
|
|
|
|
|
|
return
|
2019-11-04 14:30:25 +08:00
|
|
|
})
|
|
|
|
}
|