Documentation
¶
Overview ¶
Package toggle is an on/off switch with a label, the Switch of web component libraries ("switch" is a Go keyword and cannot name a package). Space or Enter flips it when it has focus, Left turns it off and Right turns it on, and with the mouse on so does a click.
Stability: experimental. Its API may change in any minor release.
Example ¶
Right turns a focused switch on.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/toggle"
)
func main() {
overwrite := toggle.New("Overwrite")
overwrite.Focus()
overwrite, _ = overwrite.Update(tui.Key{Type: tui.KeyRight})
fmt.Println(ansi.StripANSI(overwrite.View()))
fmt.Println(overwrite.On())
}
Output: < ●> Overwrite true
Index ¶
- type ChangedMsg
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- 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) On() bool
- func (m *Model) SetOn(on 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 switch's ID.
ID string
// On is the switch's state after the change.
On bool
}
ChangedMsg is delivered (via the Cmd Update returns) when a key or a click turns the switch on or off.
type KeyMap ¶
type KeyMap struct {
Flip keymap.Binding // turn the switch on if it is off, and off if it is on
Off keymap.Binding // turn the switch off
On keymap.Binding // turn the switch on
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns Space or Enter, Left and Right.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one switch's change from
// another's.
ID string
// Label is the text after the switch. An empty Label draws the switch
// alone.
Label string
// Disabled stops the switch being changed; it is drawn faint.
Disabled bool
Theme theme.Theme
// KeyMap holds the keys for each action. 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 switch or its label flips it. Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the switch.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one switch. The zero value is not ready to use; build one with New.
func (Model) Bindings ¶
Bindings returns the actions the switch honours, with descriptions, for help text. A disabled one has none.
func (*Model) Focus ¶
Focus gives the switch keyboard focus. It returns no Cmd; the result is there so a switch 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 switch as plain text for accessible output (see tui.Linearizer): "Overwrite, switch, on", with ", unavailable" when disabled.
func (*Model) SetOn ¶
SetOn turns the switch on or off without a key press: no ChangedMsg is delivered.
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.ComponentToggle, ...) and then by WithTokens.
func (Model) Update ¶
Update flips the switch on Space or Enter, turns it off on Left and on on Right, when it has focus, and, with Mouse on, flips it on a left press inside Bounds. A disabled switch ignores both.
func (Model) View ¶
View renders a track with a knob at its left end when off, "(○ )", and at its right end, filled, when on, "( ●)", followed by the label, so the state reads without colour in two ways. A switch that has focus is drawn with angle brackets, "< ●>", in the focus style; a disabled one is faint.