TermUI.Widgets.ContextMenu (TermUI v1.0.0)
View SourceContext menu widget for displaying floating menus at cursor position.
Context menu appears at a specific position (usually on right-click) and displays a list of actions. It automatically closes on selection, escape, or clicking outside.
Usage
ContextMenu.new(
items: [
ContextMenu.action(:cut, "Cut", shortcut: "Ctrl+X"),
ContextMenu.action(:copy, "Copy", shortcut: "Ctrl+C"),
ContextMenu.action(:paste, "Paste", shortcut: "Ctrl+V"),
ContextMenu.separator(),
ContextMenu.action(:select_all, "Select All", shortcut: "Ctrl+A")
],
position: {x, y},
on_select: fn id -> handle_action(id) end,
on_close: fn -> handle_close() end
)Features
- Floating overlay at specified position
- Keyboard navigation (Up/Down/Enter/Escape)
- Closes on selection or escape
- Closes on click outside menu bounds
- Z-order above other content
Callback Error Handling
The on_select and on_close callbacks are executed synchronously by the
caller. Callback exceptions are rescued and logged by the shared behaviour;
they do not crash the widget state machine.
Best Practices:
- Callbacks should not raise exceptions
- Return quickly to avoid blocking the UI
- Dispatch long-running work to separate processes
Example:
on_select: fn id ->
try do
handle_menu_action(id)
rescue
e -> Logger.error("Menu action failed: #{inspect(e)}")
end
end
Summary
Functions
Creates an action menu item.
Gets the currently focused item ID.
Hides the context menu.
Creates new ContextMenu widget props.
Creates a separator.
Updates the position of the context menu.
Shows the context menu.
Gets whether the context menu is visible.
Functions
Creates an action menu item.
Gets the currently focused item ID.
Hides the context menu.
Creates new ContextMenu widget props.
Options
:items- List of menu items (required):position- {x, y} tuple for menu position (required):on_select- Callback when item is selected:fn item_id -> ... endCalled synchronously. Should not raise exceptions.:on_close- Callback when menu is closed:fn -> ... endCalled synchronously. Should not raise exceptions.:item_style- Style for normal items:selected_style- Style for focused item:disabled_style- Style for disabled items
@spec separator() :: map()
Creates a separator.
@spec set_position( map(), {non_neg_integer(), non_neg_integer()} ) :: map()
Updates the position of the context menu.
Shows the context menu.
Gets whether the context menu is visible.