contextmenu

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: 10 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 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

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

func (it Item) Selectable() bool

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 New

func New(items ...Item) Model

New returns a closed Model of items with the default theme and keys.

func (Model) Bindings

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

Bindings returns the actions the menu currently honours, for help text. A closed menu honours only Open.

func (Model) Bounds

func (m Model) Bounds() hittest.Rect

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

func (m Model) Cursor() int

Cursor returns the index in Items of the highlighted item, or -1 when no item is selectable.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the menu without sending any message.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

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

func (m Model) Linearize() string

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

func (m Model) Open() bool

Open reports whether the menu is shown.

func (Model) Render

func (m Model) Render(base string) string

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

func (m *Model) SetCursor(i int)

SetCursor moves the cursor to item i. It does nothing unless i is a selectable item.

func (Model) SetTheme

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

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

func (m *Model) Show(x, y int)

Show opens the menu with its top-left corner at (x, y) and the cursor on the first selectable item.

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.ComponentContextMenu, ...) and then by WithTokens.

func (Model) Update

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

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.

func (Model) View

func (m Model) View() string

View renders the menu's box on its own, whether or not it is open, at its natural size.

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 {
	Index int
	Item  Item
}

SelectedMsg is delivered (via the Cmd Update returns) when an item is confirmed. The menu closes first.

Jump to

Keyboard shortcuts

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