Documentation
¶
Overview ¶
Package contextmenu is a popup menu opened at an anchor point, typically on a right-click or a key press. It lists Items (with separators, disabled entries, accelerator letters and optional shortcut hints), moves a cursor over the enabled ones, reports the confirmed item as SelectedMsg, and closes on Esc or a click outside. Render composites it over a base view, shifted or flipped so that it stays on screen.
Like popover.Model it is an overlay: a parent calls Render every frame and the closed menu leaves the base unchanged.
Example ¶
A right-click opens the menu at the pointer; Enter on an item reports it.
package main
import (
"fmt"
"github.com/ows4444/tui"
"github.com/ows4444/tui/contextmenu"
)
func main() {
m := contextmenu.New(
contextmenu.Item{Label: "Copy", Value: "copy"},
contextmenu.Item{Separator: true},
contextmenu.Item{Label: "Delete", Value: "delete"},
)
m.Mouse = true
m, _ = m.Update(tui.MouseEvent{X: 4, Y: 2, Button: tui.MouseButtonRight, Action: tui.MouseActionPress})
m, _ = m.Update(tui.Key{Type: tui.KeyDown})
_, cmd := m.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(cmd().(contextmenu.SelectedMsg).Item.Value)
}
Output: delete
Index ¶
- type ClosedMsg
- type Item
- type KeyMap
- type Model
- func (m Model) Bindings() []keymap.Binding
- func (m Model) Bounds() hittest.Rect
- func (m Model) Cursor() int
- 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) SetCursor(i int)
- func (m Model) SetTheme(t theme.Theme) Model
- func (m *Model) Show(x, y 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 menu is closed without a selection: by the Close key or by a click outside it.
type Item ¶
type Item struct {
// Label is the text shown. Unless Model.Raw is set it is sanitised
// (ansi.Clean) so untrusted text cannot carry terminal escapes.
Label string
// Value is returned in SelectedMsg and never altered.
Value string
// Shortcut is an optional hint drawn right-aligned (for example
// "ctrl+c"). It is display only.
Shortcut string
// Accel is the accelerator letter: while the menu is open, typing it
// (case-insensitively) activates the item. Zero means none. When Label
// contains the letter, its first occurrence is underlined.
Accel rune
// Disabled makes the item unselectable: the cursor skips it and it
// cannot be activated.
Disabled bool
// Separator makes the row a horizontal rule. Every other field is ignored.
Separator bool
}
Item is one row of a menu.
func (Item) Selectable ¶
Selectable reports whether the cursor may rest on the item.
type KeyMap ¶
type KeyMap struct {
// Open opens the menu at its current anchor while it is closed.
Open keymap.Binding
Up keymap.Binding
Down keymap.Binding
Top keymap.Binding
Bottom keymap.Binding
// Select confirms the item under the cursor.
Select keymap.Binding
// Close closes the menu without a selection.
Close keymap.Binding
}
KeyMap names the keys of each action of a Model.
func DefaultKeyMap ¶
func DefaultKeyMap() KeyMap
DefaultKeyMap returns the default bindings: shift+f10 or the menu key opens it, up/down and home/end move, enter selects, esc closes.
type Model ¶
type Model struct {
// Items are the rows, top to bottom.
Items []Item
// Theme styles the box, the cursor row and disabled items.
Theme theme.Theme
// Label is the accessible name of the menu; "" reads as "Context menu".
Label string
// Raw, when true, draws 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 right press
// opens the menu at the pointer; while open, hovering moves the cursor, a
// left press on an item selects it, a press elsewhere closes the menu and
// the wheel moves the cursor. Off (the default) ignores the mouse.
Mouse bool
// ScreenW and ScreenH are the screen size the menu is kept inside, for
// placement and mouse hit-testing. Zero means unbounded. Render uses the
// size of its base instead.
ScreenW, ScreenH int
// AnchorX and AnchorY are the screen cell the menu's top-left corner is
// placed at. Show sets them.
AnchorX, AnchorY int
// contains filtered or unexported fields
}
Model is a popup menu. The zero value is a closed, empty menu; use New.
func (Model) Bindings ¶
Bindings returns the actions the menu currently honours, for help text. A closed menu honours only Open.
func (Model) Bounds ¶
Bounds is the screen rectangle the open menu covers, clipped to the screen (ScreenW, ScreenH). A closed menu has an empty rectangle.
func (Model) Cursor ¶
Cursor returns the index in Items of the highlighted item, or -1 when no item is selectable.
func (Model) LayoutNode ¶
LayoutNode returns the menu's bordered box as an ordinary layout.Node (it is also a layout.CellNode, so it draws straight into a cell grid), cut to the size it is given. While the menu is closed it measures as nothing and renders blank. The Model is not changed.
func (Model) Linearize ¶
Linearize renders the menu as plain text for accessible output (see tui.Linearizer), or "" while it is closed: the menu's Label (default "Context menu"), then one line per non-separator item such as "Copy, item 1 of 3, shortcut ctrl+c, selected". Disabled items read ", disabled". An empty menu reads "No items".
func (Model) Render ¶
Render composites the open menu over base, placed at the anchor and kept inside base: shifted left when it would pass base's right edge, flipped above the anchor when it would pass the bottom, and cut off if it is still too big. A closed menu returns base unchanged.
func (*Model) SetCursor ¶
SetCursor moves the cursor to item i. It does nothing unless i is a selectable item.
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) Show ¶
Show opens the menu with its top-left corner at (x, y) and the cursor on the first selectable item.
func (Model) Tokens ¶
Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentContextMenu, ...) and then by WithTokens.
func (Model) Update ¶
Update handles keys and, with Mouse on, mouse events. While closed it only reacts to the Open key (at the current anchor) and a right press; any other Msg is a no-op. The returned Cmd delivers SelectedMsg or ClosedMsg.
type SelectedMsg ¶
SelectedMsg is delivered (via the Cmd Update returns) when an item is confirmed. The menu closes first.