menubar

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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 Item

type Item = contextmenu.Item

Item is one row of a dropdown; it is contextmenu.Item.

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 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 New

func New(menus ...Menu) Model

New returns an idle Model of menus with the default theme and keys.

func (Model) Active

func (m Model) Active() int

Active returns the index of the open (or last opened) menu.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the bar currently honours, for help text. An idle bar honours only Focus.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the dropdown without sending any message.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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) Open

func (m Model) Open() bool

Open reports whether a dropdown is open.

func (Model) Render

func (m Model) Render(base string) string

Render composites the open dropdown over base, kept inside base's size. An idle bar returns base unchanged.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

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) Show

func (m *Model) Show(i int)

Show opens menu i, clamped to a valid index. It does nothing without menus.

func (Model) Tokens

func (m Model) Tokens() theme.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

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

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.

func (Model) View

func (m Model) View() string

View renders the bar's single row. The open menu's title is bracketed and bold, so it shows without colour; the others are padded with spaces.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL