Documentation
¶
Overview ¶
Package menubar is a horizontal menu bar of titled menus, each opening a dropdown of items below its title. Left and Right move between menus, Up and Down move within the dropdown, Enter activates, Esc closes, Alt plus a menu's accelerator letter opens it, and (with Mouse on) a click opens a menu or picks an item. The dropdown is a contextmenu.Model, so items share its separators, disabled entries, accelerators and shortcut hints.
The bar draws one row with View; the open dropdown is an overlay drawn by Render over the rest of the screen.
Example ¶
F10 opens the first menu, Right moves to the next, Enter activates.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/menubar"
)
func main() {
m := menubar.New(
menubar.Menu{Title: "File", Items: []menubar.Item{{Label: "Quit", Value: "quit"}}},
menubar.Menu{Title: "Help", Items: []menubar.Item{{Label: "About", Value: "about"}}},
)
m, _ = m.Update(tui.Key{Type: tui.KeyF10})
m, _ = m.Update(tui.Key{Type: tui.KeyRight})
_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
sel := cmd().(menubar.SelectedMsg)
fmt.Println(sel.Menu, sel.Item.Value)
}
Output: 1 about
Index ¶
- type ClosedMsg
- type Item
- type KeyMap
- type Menu
- type Model
- func (m Model) Active() int
- func (m Model) Bindings() []keymap.Binding
- func (m *Model) Hide()
- func (m Model) LayoutNode() layout.Node
- func (m Model) Linearize() string
- func (m Model) Open() bool
- func (m Model) Render(base string) string
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) Show(i int)
- func (m Model) Tokens() theme.Tokens
- func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)
- func (m Model) View() string
- func (m Model) WithTokens(tok theme.Tokens) Model
- type SelectedMsg
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClosedMsg ¶
type ClosedMsg struct{}
ClosedMsg is delivered (via the Cmd Update returns) when the open dropdown is closed without a selection.
type KeyMap ¶
type KeyMap struct {
// Focus opens the first menu while the bar is idle.
Focus keymap.Binding
// Left and Right move to the previous and next menu, wrapping around.
Left keymap.Binding
Right keymap.Binding
// Up, Down, Top and Bottom move within the open dropdown.
Up keymap.Binding
Down keymap.Binding
Top keymap.Binding
Bottom keymap.Binding
// Select activates the highlighted item.
Select keymap.Binding
// Close closes the open dropdown.
Close keymap.Binding
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default bindings: f10 focuses the bar, left/right switch menus, up/down and home/end move, enter selects, esc closes.
type Menu ¶
type Menu struct {
// Title is the text on the bar. Unless Model.Raw is set it is sanitised.
Title string
// Accel is the accelerator letter: Alt plus it opens the menu. Zero means
// none. The first occurrence of the letter in Title is underlined.
Accel rune
// Items are the dropdown rows.
Items []Item
}
Menu is one titled menu of the bar.
type Model ¶
type Model struct {
// Menus are the bar's menus, left to right.
Menus []Menu
// Theme styles the bar and its dropdown.
Theme theme.Theme
// Raw, when true, draws titles and labels unchanged instead of
// sanitising them.
Raw bool
// KeyMap holds the key bindings Update obeys. New fills it with
// DefaultKeyMap; a Model with no bindings set behaves as if it held
// DefaultKeyMap.
KeyMap KeyMap
// Mouse, when true, makes Update handle tui.MouseEvent: a left press on a
// title opens that menu (or closes it if it is open), hovering another
// title while a menu is open switches to it, and a left press on a
// dropdown item selects it. A press anywhere else closes the dropdown.
// Off (the default) ignores the mouse.
Mouse bool
// Bounds is the screen rectangle where the app draws the bar (its first
// row holds the titles); the dropdown opens under the title, on the row
// below Bounds.Y. The zero value is the top-left corner.
Bounds hittest.Rect
// ScreenW and ScreenH are the screen size the dropdown is kept inside.
// Zero means unbounded. Render uses the size of its base instead.
ScreenW, ScreenH int
// contains filtered or unexported fields
}
Model is a menu bar. Build it with New.
func (Model) Bindings ¶
Bindings returns the actions the bar currently honours, for help text. An idle bar honours only Focus.
func (Model) LayoutNode ¶
LayoutNode returns the bar as an ordinary layout.Node (a layout.CellNode, so it draws straight into a cell grid), cut to the size it is given. The first row holds the titles; while a dropdown is open it is drawn below them, inside the node's size. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the bar as plain text for accessible output (see tui.Linearizer): "Menu bar", then one line per menu such as "File, menu 1 of 3, expanded", and, when a dropdown is open, its items under the name "File menu". With no menus it reads "No menus".
func (Model) Render ¶
Render composites the open dropdown over base, kept inside base's size. An idle bar returns base unchanged.
func (Model) SetTheme ¶
SetTheme returns m with t applied to the bar and its open dropdown. 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.ComponentMenuBar, ...) and then by WithTokens.
func (Model) Update ¶
Update drives the bar. While idle it reacts to the Focus key and to Alt plus an accelerator; while a dropdown is open it switches menus on Left/Right and passes the other keys to the dropdown. With Mouse on it also handles clicks (see Model.Mouse). The returned Cmd delivers SelectedMsg or ClosedMsg. Any other Msg is a no-op.
type SelectedMsg ¶
type SelectedMsg struct {
// Menu is the index in Menus of the menu the item belongs to; Index is
// the item's index in that menu's Items.
Menu, Index int
Item Item
}
SelectedMsg is delivered (via the Cmd Update returns) when a dropdown item is activated. The bar closes first.