Documentation
¶
Overview ¶
Package buttongroup is a row of buttons that take one place in the focus order: Left and Right move between them, and Enter or Space presses the one the cursor is on.
In ModeActions the buttons are plain and each press is an action. In ModeSingle and ModeMultiple they are toggle buttons and the group is a choice: one of them, or any number.
Stability: experimental. Its API may change in any minor release.
Example ¶
A single-choice group: Right moves the cursor, Enter chooses.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/ansi"
"github.com/ows4444/tui/buttongroup"
)
func main() {
align := buttongroup.New(buttongroup.ModeSingle, "Left", "Centre", "Right")
align.Focus()
align, _ = align.Update(tui.Key{Type: tui.KeyRight})
align, _ = align.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(ansi.StripANSI(align.View()))
fmt.Println(align.On())
}
Output: [ Left ] <●Centre > [ Right ] [Centre]
Index ¶
- type ChangedMsg
- type KeyMap
- type Mode
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Blur()
- func (m Model) Cursor() int
- 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() []string
- func (m *Model) SetCursor(i int)
- func (m *Model) SetOn(ids ...string)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) Width() int
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 group's ID.
ID string
// Pressed is the ID of the button whose press made the change.
Pressed string
// On holds the IDs of the buttons that are on, left to right.
On []string
}
ChangedMsg is delivered (via the Cmd Update returns) when a press changed which buttons of a ModeSingle or ModeMultiple group are on. It stands in place of the pressed button's own button.PressedMsg, so one press is one message. A ModeActions group delivers button.PressedMsg.
type KeyMap ¶
type KeyMap struct {
Prev keymap.Binding // move to the button before
Next keymap.Binding // move to the button after
First keymap.Binding // move to the first button
Last keymap.Binding // move to the last button
}
KeyMap names the keys that move a group's cursor.
type Model ¶
type Model struct {
// ID is carried by ChangedMsg, to tell one group's change from another's.
ID string
// Buttons are the group's buttons, left to right. Each keeps its own
// Label, ID, Variant, Size and Disabled; the group sets their focus,
// their toggle state in ModeSingle, and their Mouse and Bounds.
Buttons []button.Model
Mode Mode
// Required, in ModeSingle, stops the button that is on being turned off
// by pressing it again, so that once a choice is made one is always on.
Required bool
// Gap is the number of blank cells between two buttons.
Gap int
// KeyMap holds the keys that move the cursor. 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 click on a
// button presses it and moves the cursor to it. Off (the default)
// ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the group; its
// left edge is the first button's.
Bounds hittest.Rect
// contains filtered or unexported fields
}
Model is a row of buttons with a cursor. The zero value is an empty ModeActions group; build one with New.
func New ¶
New returns a group in the given mode with one button per label, a gap of one cell, and the cursor on the first button. Each button's ID is its label. In ModeSingle and ModeMultiple the buttons are toggle buttons.
func (Model) Bindings ¶
Bindings returns the keys the group honours, for help text: its own, then those of the button under the cursor.
func (*Model) Focus ¶
Focus gives the group keyboard focus: the button under the cursor shows it, in angle brackets. It returns no Cmd; the result is there so a group can stand where any focusable widget does.
func (Model) LayoutNode ¶
LayoutNode adapts the group to a layout.Node of its own width on one row. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the group as plain text for accessible output (see tui.Linearizer): one line per button, as the button reads itself, with its position, e.g. "Bold, toggle button, on, 1 of 3".
func (Model) On ¶
On returns the IDs of the buttons that are on, left to right. It is empty in ModeActions.
func (*Model) SetCursor ¶
SetCursor moves the cursor to button i. An index that is out of range, or names a disabled button, is ignored.
func (*Model) SetOn ¶
SetOn turns on the buttons with the given IDs and turns the others off, without a press: no message is delivered. In ModeSingle only the first of ids that names a button is turned on.
func (Model) SetTheme ¶
SetTheme returns m with t applied to every button. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme). The group draws nothing of its own but the gaps.
func (Model) Update ¶
Update moves the cursor on Left, Right, Home and End, skipping disabled buttons and wrapping at the ends, and passes every other message to the button under the cursor, so Enter or Space presses it. With Mouse on, a click on a button moves the cursor to it and presses it. Only a group that has focus acts on keys.