Documentation
¶
Overview ¶
Package checkbox is one box that is checked or not, with a label: Space checks and unchecks it when it has focus, and with the mouse on so does a click. For a list of them with one cursor, see multiselect.
Stability: experimental. Its API may change in any minor release.
Example ¶
Space checks a focused checkbox.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/checkbox"
)
func main() {
keep := checkbox.New("Keep metadata")
keep.Focus()
keep, _ = keep.Update(tui.Key{Type: tui.KeySpace})
fmt.Println(ansi.StripANSI(keep.View()))
fmt.Println(keep.Checked())
}
Output: <x> Keep metadata true
Index ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Checked() bool
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m *Model) SetChecked(checked bool)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) Width() int
- func (m Model) WithTokens(tok theme.Tokens) Model
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChangedMsg ¶
type ChangedMsg struct {
// ID is the checkbox's ID.
ID string
// Checked is the box's state after the change.
Checked bool
}
ChangedMsg is delivered (via the Cmd Update returns) when a key or a click checks or unchecks the box.
type KeyMap ¶
KeyMap names the key of a Model's one action.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Space. Enter is left out, since in a form it moves to the next field.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one checkbox's change from
// another's.
ID string
// Label is the text after the box. An empty Label draws the box alone.
Label string
// Indeterminate draws the box as "[-]": neither checked nor unchecked,
// as the box over a list some of whose items are checked. The next press
// checks it and clears Indeterminate.
Indeterminate bool
// Disabled stops the box being changed; it is drawn faint.
Disabled bool
Theme theme.Theme
// KeyMap holds the key that checks and unchecks the box. New fills it
// with DefaultKeyMap; a Model built as a struct literal with a zero
// KeyMap behaves as if it held DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left press on
// the box or its label checks or unchecks it. Off (the default) ignores
// the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the checkbox.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one checkbox. The zero value is not ready to use; build one with New.
func (Model) Bindings ¶
Bindings returns the action the checkbox honours, for help text. A disabled one has none.
func (*Model) Focus ¶
Focus gives the checkbox keyboard focus. It returns no Cmd; the result is there so a checkbox can stand where any focusable widget does.
func (Model) LayoutNode ¶
LayoutNode adapts the widget to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the checkbox as plain text for accessible output (see tui.Linearizer): "Keep metadata, checkbox, checked", with "not checked" or "partly checked", and ", unavailable" when disabled.
func (*Model) SetChecked ¶
SetChecked checks or unchecks the box without a key press: no ChangedMsg is delivered. It clears Indeterminate.
func (Model) SetTheme ¶
SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentCheckbox, ...) and then by WithTokens.
func (Model) Update ¶
Update checks or unchecks the box on Space when it has focus, and, with Mouse on, on a left press inside Bounds. A disabled checkbox ignores both.