Documentation
¶
Overview ¶
Package button is a pressable button: a label that takes focus and reports a press. Enter or Space presses it when focused, and so does a left click that goes down and comes up inside it.
A button draws on one row. Its states are told apart without colour: square brackets mark it as a button, angle brackets mark focus ("< Save >"), reverse video marks a press, and a disabled or loading button says so in its label. Angle brackets are how every control of this family shows focus: buttongroup, radiogroup, checkbox, toggle, slider and rating too.
With Toggle set it is a toggle button: each press turns it on or off, and a dot before the label shows that it is on.
Stability: experimental. Its API may change in any minor release.
Example ¶
A focused button reports a press on Enter.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/button"
)
func main() {
save := button.New("Save")
save.ID = "save"
save.Focus()
fmt.Println(ansi.StripANSI(save.View()))
_, cmd := save.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(button.PressedMsg).ID)
}
Output: < Save > save
Index ¶
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Down() bool
- func (m *Model) Focus() tui.Cmd
- func (m Model) Focused() bool
- func (m Model) Hovered() 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
- type PressedMsg
- type Size
- type Variant
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Model ¶
type Model struct {
// ID is carried by PressedMsg, to tell one button's press from another's.
ID string
// Label is the button's text.
Label string
Variant Variant
Size Size
// Disabled stops the button being pressed; it is drawn dimmed.
Disabled bool
// Loading stops the button being pressed while the action it started is
// still running, and adds an ellipsis to the label.
Loading bool
// Toggle makes this a toggle button: a press turns it on or off (see On)
// as well as delivering PressedMsg. One cell before the label is kept
// for the dot that marks it on, so turning it on does not change its
// width.
Toggle bool
Theme theme.Theme
// KeyMap holds the keys that press the button. 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 button
// that goes down and comes up inside Bounds presses the button, and
// pointer motion over it sets Hovered. Off (the default) ignores the
// mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the button.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is one button. The zero value is not ready to use; build one with New.
func (Model) Bindings ¶
Bindings returns the action the button honours, for help text. A button that cannot be pressed has none.
func (*Model) Blur ¶
func (m *Model) Blur()
Blur takes keyboard focus away, and lets go of a pointer press in progress.
func (Model) Down ¶
Down reports whether a pointer press on the button is in progress: the left button went down inside it and has not come up yet.
func (*Model) Focus ¶
Focus gives the button keyboard focus. It returns no Cmd; the result is there so a button can stand where any focusable widget does.
func (Model) Hovered ¶
Hovered reports whether the pointer is over the button. It is only ever true with Mouse on and the Program reporting all pointer motion.
func (Model) LayoutNode ¶
LayoutNode adapts the button to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the button as plain text for accessible output (see tui.Linearizer): the label, the word "button", and its state, e.g. "Save, button, focused" or "Delete, button, unavailable". A toggle button reads "Bold, toggle button, on".
func (Model) On ¶
On reports whether a toggle button is on. It is always false for a button that is not a toggle.
func (*Model) SetOn ¶
SetOn turns a toggle button on or off without a press: no PressedMsg is delivered. It does nothing to a button that is not a toggle.
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.ComponentButton, ...) and then by WithTokens.
func (Model) Update ¶
Update presses the button on Enter or Space when it is focused, and, with Mouse on, on a left click inside Bounds. A click is a press and a release that both land inside the button: a pointer dragged off it before the release presses nothing. A disabled or loading button ignores both.
type PressedMsg ¶
type PressedMsg struct {
// ID is the pressed button's ID.
ID string
// On is a toggle button's state after the press; false for a button
// that is not a toggle.
On bool
}
PressedMsg is delivered (via the Cmd Update returns) when the button is pressed, by key or by pointer.
type Variant ¶
type Variant int
Variant is how much weight a button carries on the screen.
const ( // VariantDefault is the main action: "[ Save ]" in the primary colour. VariantDefault Variant = iota // VariantSecondary is an action of less weight, in the text colour. VariantSecondary // VariantDestructive is an action that removes or cannot be undone, in // the error colour. VariantDestructive // VariantGhost has no brackets until it is focused or pressed. VariantGhost // VariantLink is drawn as underlined text with no brackets. VariantLink )