TermUI.Widgets.ContextMenu.Factory (TermUI v1.0.0)
View SourceFactory for creating context menus with automatic mode selection.
This module provides a unified way to create context menus that automatically selects between positioned (mouse) and inline (keyboard) modes based on terminal capabilities and provided options.
Usage
# Auto-detect: uses positioned if position provided, inline otherwise
{:ok, {module, props}} = Factory.create(
items: [
ContextMenu.action(:copy, "Copy"),
ContextMenu.action(:paste, "Paste")
],
position: {10, 5}, # Optional - triggers positioned mode
on_select: fn id -> handle_action(id) end
)
# Force inline mode
{:ok, {module, props}} = Factory.create(
items: items,
mode: :inline,
on_select: on_select
)
# Force positioned mode (requires position)
{:ok, {module, props}} = Factory.create(
items: items,
mode: :positioned,
position: {x, y},
on_select: on_select
)Mode Selection
The factory selects a menu mode based on:
Explicit mode - If
:modeoption is provided::inline- Always useContextMenu.Inline:positioned- Always useContextMenu(requires:position):auto- Auto-detect based on position and capabilities (default)
Auto-detection (
:mode == :autoor not specified):- If
:positionis provided → use positionedContextMenu - If no position and mouse not supported → use
ContextMenu.Inline - If no position but mouse supported → returns error (caller should provide position)
- If
Return Value
Returns {:ok, {module, props}} where:
moduleis eitherTermUI.Widgets.ContextMenuorTermUI.Widgets.ContextMenu.Inlinepropsare the initialized props for that module
Or {:error, reason} if the configuration is invalid.
Summary
Functions
Creates a context menu with automatic mode selection.
Creates a context menu, raising on error.
Returns whether the terminal supports mouse tracking.
Types
@type mode() :: :auto | :positioned | :inline
@type option() :: {:items, [map()]} | {:position, {non_neg_integer(), non_neg_integer()}} | {:mode, mode()} | {:on_select, (term() -> any())} | {:on_close, (-> any())} | {:orientation, :horizontal | :vertical} | {:item_style, term()} | {:selected_style, term()} | {:disabled_style, term()} | {:number_style, term()}
Functions
Creates a context menu with automatic mode selection.
Options
:items- List of menu items (required). UseContextMenu.action/3andContextMenu.separator/0to create items.:position-{x, y}tuple for positioned mode. If provided and mode is:auto, positioned mode will be used.:mode- Explicit mode selection::auto- Auto-detect based on position and capabilities (default):positioned- Force positioned mode (requires:position):inline- Force inline mode
:on_select- Callback when item is selected:fn id -> ... end:on_close- Callback when menu is closed:fn -> ... end:orientation- For inline mode::horizontal(default) or:vertical:item_style- Style for normal items:selected_style- Style for focused item:disabled_style- Style for disabled items:number_style- For inline mode: style for[n]prefix
Returns
{:ok, {module, props}}- The module and props to use{:error, :missing_items}- Items not provided{:error, :missing_position}- Positioned mode requires position{:error, :position_required}- Auto mode with mouse support but no position
Creates a context menu, raising on error.
Same as create/1 but raises ArgumentError on invalid configuration.
@spec mouse_supported?() :: boolean()
Returns whether the terminal supports mouse tracking.
This is used for auto-detection when no position is provided.