TermUI.Widgets.ContextMenu (TermUI v0.2.0)

View Source

Context 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

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

action(id, label, opts \\ [])

@spec action(term(), String.t(), keyword()) :: map()

Creates an action menu item.

get_cursor(state)

@spec get_cursor(map()) :: term()

Gets the currently focused item ID.

hide(state)

@spec hide(map()) :: map()

Hides the context menu.

new(opts)

@spec new(keyword()) :: map()

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
  • :on_close - Callback when menu is closed
  • :item_style - Style for normal items
  • :selected_style - Style for focused item
  • :disabled_style - Style for disabled items

separator()

@spec separator() :: map()

Creates a separator.

set_position(state, position)

@spec set_position(
  map(),
  {non_neg_integer(), non_neg_integer()}
) :: map()

Updates the position of the context menu.

show(state)

@spec show(map()) :: map()

Shows the context menu.

visible?(state)

@spec visible?(map()) :: boolean()

Gets whether the context menu is visible.