Documentation
¶
Overview ¶
Package menu is a grouped, filterable list of things to open: the sections of an app, the screens a home page leads to. Each item is a label, a dimmed description beside it, and an optional group it is filed under; choosing one emits a ChosenMsg and the screen decides what opening it means — usually screen.Push.
It is a pane component like pkg/list, and sits beside other components the same way: it takes focus, filters with /, and answers the mouse from its own rect. What it adds over a list is the groups, drawn as headers the cursor never lands on, by the same rules pkg/action's menu draws them:
- headers only once the items name two or more groups;
- groups in the order first seen, header text as written;
- items without a group last, under "other";
- [ and ] jump to the previous and next group.
tab is deliberately left alone, because on a screen with more than one component it moves focus between them.
A menu is not pkg/action's Menu. That one is a modal over a screen's verbs; this one is a screen's content.
Index ¶
- type ChosenMsg
- type Item
- type Keys
- type Model
- func (m *Model) Blur()
- func (m *Model) BlurFilter()
- func (m Model) Chosen(msg tea.Msg) (Item, bool)
- func (m Model) Filtering() bool
- func (m *Model) Focus() tea.Cmd
- func (m *Model) FocusFilter() tea.Cmd
- func (m Model) FocusToken() focus.Token
- func (m Model) Focused() bool
- func (m Model) Help() []key.Binding
- func (m Model) HelpSections() []help.Section
- func (m Model) Init() tea.Cmd
- func (m Model) IsCapturingKeys() bool
- func (m Model) Items() []Item
- func (m Model) Rect() geom.Rect
- func (m *Model) Restore(s State)
- func (m *Model) Select(id string) bool
- func (m Model) Selected() (Item, bool)
- func (m *Model) SetItems(items []Item)
- func (m *Model) SetRect(r geom.Rect)
- func (m *Model) SetTitle(s string)
- func (m *Model) SetValue(s string)
- func (m Model) State() State
- func (m Model) Title() string
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) Value() string
- func (m Model) View() string
- type Options
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChosenMsg ¶
ChosenMsg is emitted when the user opens an item — enter, or a double click. Token says which menu sent it; Chosen matches both for you.
type Item ¶
type Item struct {
// ID identifies the item to the caller. Optional; when set, it is also
// what keeps the cursor on the same item across SetItems.
ID string
// Label is the text the user picks by. Required.
Label string
// Group files the item under a header. See the package doc for the rules.
Group string
// Description is drawn dimmed beside the label and cut off when the pane
// runs out of room. It is not searched by the filter.
Description string
// Data is the caller's payload, handed back in ChosenMsg. The menu never
// reads it.
Data any
}
Item is one entry in the menu.
type Keys ¶
type Keys struct {
Up, Down key.Binding
Top, Bottom key.Binding
HalfUp, HalfDown key.Binding
// PrevGroup and NextGroup jump to the first item of the previous or next
// group. They do nothing when the menu has no headers.
PrevGroup, NextGroup key.Binding
Filter key.Binding
Choose key.Binding
}
Keys is the menu's keymap. Vertical movement follows rule 25.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model is the menu. Embed as a value; mutate via the setters.
func (*Model) Blur ¶
func (m *Model) Blur()
Blur releases the keyboard from both the body and the filter.
func (*Model) BlurFilter ¶
func (m *Model) BlurFilter()
BlurFilter returns input from the filter to the body.
func (Model) Chosen ¶
Chosen reports whether msg is this menu's ChosenMsg, and the item in it.
if it, ok := s.menu.Chosen(msg); ok {
return s, screen.Push(it.Data.(func(theme.Theme) screen.Screen)(s.t))
}
func (*Model) Focus ¶
Focus gives the menu the keyboard. It never takes input from the filter: a click on the filter also asks the group for focus, and that grant lands after the click (rule 27).
func (*Model) FocusFilter ¶
FocusFilter moves input to the filter. The pane stays lit: the menu has focus, and the filter row shows where the keys go.
func (Model) FocusToken ¶
FocusToken returns the menu's stable focus identity. See focus.Identified.
func (Model) HelpSections ¶
HelpSections groups the bindings by what they do. The group keys are listed only when there are headers to jump between.
func (Model) IsCapturingKeys ¶
IsCapturingKeys satisfies focus.Capturer: true while the filter has input.
func (*Model) Restore ¶
Restore puts s back: items, then the filter over them, then the cursor. A State from a menu never built restores nothing.
func (*Model) Select ¶
Select moves the cursor to the item with this ID, reporting whether one is showing.
func (Model) Selected ¶
Selected returns the item under the cursor. ok is false when the filter keeps nothing.
func (*Model) SetItems ¶
SetItems replaces the items and re-applies the filter. The cursor stays on the item with the same ID when there is one, and otherwise on the same position.
func (*Model) SetRect ¶
SetRect places the menu. The filter, when there is one, is a row inside the pane, placed once the pane has measured its own edge.
func (Model) Update ¶
Update handles keys and mouse. While the filter has input every key goes to it; otherwise the menu's own keys move the cursor and enter chooses.
type Options ¶
type Options struct {
// Title sits on the pane's top border. Defaults to "Menu".
Title string
// Items is the menu's content, copied.
Items []Item
// Filterable embeds a filter as a header row inside the pane. / focuses
// it; it matches the label and the group, case-insensitively.
Filterable bool
LabelStyle lipgloss.Style
// DescStyle styles the description; theme.Menu() dims it.
DescStyle lipgloss.Style
// HeaderStyle styles the group headers. GroupStyle, when set, styles each
// header by its group instead.
HeaderStyle lipgloss.Style
GroupStyle func(group string) lipgloss.Style
// SelectedStyle is applied to the whole cursor row.
SelectedStyle lipgloss.Style
// SelectedBlurredStyle replaces SelectedStyle while the menu doesn't
// have focus. Used only when it sets a background.
SelectedBlurredStyle lipgloss.Style
// Pane pass-throughs. See pkg/pane.Options.
ActiveColor lipgloss.TerminalColor
InactiveColor lipgloss.TerminalColor
ActiveBorder lipgloss.Border
InactiveBorder lipgloss.Border
// Glyphs are the marks this component draws, plus the scrollbar thumb
// and track it hands to its pane. Empty fields fall back to
// glyph.Default.
Glyphs glyph.Set
SlotBrackets pane.SlotBracketStyle
// Surface is how the component's edge and body are drawn. A theme sets
// it from the app's chosen surface. See docs/surfaces.md.
Surface surface.Resolved
// Filter configures the embedded filter. Ignored when Filterable=false.
Filter filter.Options
Keys Keys
}
Options configures a menu. Build it from theme.Menu() and set Title, Items and Filterable before passing to New.