ContextMenu
Floating context menu container managing hierarchical items, action callbacks, and auto-dismiss behavior.
Overview
ContextMenu is a floating dropdown menu container that presents an ordered list of actionable options, submenus, and visual separators.
Key features:
- Text and rich-text menu items with associated Runnable click handlers.
- Nested hierarchical submenus.
- Smart auto-repositioning to remain fully visible within host screen bounds (hostBounds, arrangeInHost).
- Dismissal on outside clicks (closeOnOutsideClick).
Creating and Opening Context Menu
ContextMenu menu = new ContextMenu();
menu.item("Copy", () -> copy()).separator().item("Paste", () -> paste());
menu.openAt(mouseX, mouseY);
Related links
- OverlayLayer: Full-screen overlay root managing modal backdrops, dialogs, popups, and layer-based hit testing.
- Popup: Anchored floating popup widget that positions itself relative to target widgets or screen coordinates.
API
ContextMenu()
Constructs an empty ContextMenu.
ContextMenu::arrangeInHost(RectView)
Arranges the menu within host boundaries, flipping placement if screen edges are exceeded.
| Parameter | Type | Description |
|---|---|---|
hostBounds |
RectView |
Available host rectangular boundary. |
ContextMenu::close()
Closes the menu and any active submenus.
Returns ContextMenu: This widget instance for chaining..
ContextMenu::closeOnOutsideClick()
Returns true if clicking outside automatically dismisses the menu.
Returns boolean: True if closing on outside click is enabled..
ContextMenu::closeOnOutsideClick(boolean)
Sets whether clicking outside closes the menu.
| Parameter | Type | Description |
|---|---|---|
closeOnOutsideClick |
boolean |
Whether clicking outside automatically dismisses the widget. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::item(Button)
Adds a preconfigured Button widget as a menu item.
| Parameter | Type | Description |
|---|---|---|
button |
Button |
Custom button widget to attach. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::item(RichText)
Adds a menu item with RichText.
| Parameter | Type | Description |
|---|---|---|
text |
RichText |
Display text string. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::item(RichText, Runnable)
Adds a menu item with RichText and click callback.
| Parameter | Type | Description |
|---|---|---|
text |
RichText |
Display text string. |
action |
Runnable |
Callback action to execute. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::item(String)
Adds a menu item with plain text.
| Parameter | Type | Description |
|---|---|---|
text |
String |
Display text string. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::item(String, Runnable)
Adds a menu item with plain text and click callback.
| Parameter | Type | Description |
|---|---|---|
text |
String |
Display text string. |
action |
Runnable |
Callback action to execute. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::itemButton(int)
Returns the button widget for the item at the specified index.
| Parameter | Type | Description |
|---|---|---|
index |
int |
Zero-based item index. |
Returns Button: The button widget instance..
ContextMenu::itemCount()
Returns the total number of menu items.
Returns int: Total count of items..
ContextMenu::open()
Opens the menu at its last configured position.
Returns ContextMenu: This widget instance for chaining..
ContextMenu::open(boolean)
Sets the open state.
| Parameter | Type | Description |
|---|---|---|
open |
boolean |
Target open state flag. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::openAt(float, float)
Opens the menu positioned at the specified root coordinates.
| Parameter | Type | Description |
|---|---|---|
x |
float |
Horizontal X coordinate. |
y |
float |
Vertical Y coordinate. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::opened()
Returns true if this menu is open.
Returns boolean: True if currently open..
ContextMenu::padding(EdgeInsets)
Sets interior padding around the menu items.
| Parameter | Type | Description |
|---|---|---|
padding |
EdgeInsets |
Internal content padding. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::selectItem(int)
Selects the item at the specified index.
| Parameter | Type | Description |
|---|---|---|
index |
int |
Zero-based item index. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::selectedItemIndex()
Returns the index of the currently hovered or keyboard-selected item.
Returns int: Item index..
ContextMenu::separator()
Adds a visual divider separator line.
Returns ContextMenu: This widget instance for chaining..
ContextMenu::separator(Separator)
Adds a custom Separator widget.
| Parameter | Type | Description |
|---|---|---|
separator |
Separator |
Visual separator item. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::submenu(Button, ContextMenu)
Adds a cascading submenu item with a custom button.
| Parameter | Type | Description |
|---|---|---|
button |
Button |
Custom button widget to attach. |
submenu |
ContextMenu |
Nested submenu container. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::submenu(RichText, ContextMenu)
Adds a cascading submenu item with RichText.
| Parameter | Type | Description |
|---|---|---|
text |
RichText |
Display text string. |
submenu |
ContextMenu |
Nested submenu container. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::submenu(String, ContextMenu)
Adds a cascading submenu item with plain text.
| Parameter | Type | Description |
|---|---|---|
text |
String |
Display text string. |
submenu |
ContextMenu |
Nested submenu container. |
Returns ContextMenu: This widget instance for chaining..
ContextMenu::toggle(float, float)
Toggles open/closed state at the specified coordinates.
| Parameter | Type | Description |
|---|---|---|
x |
float |
Horizontal X coordinate. |
y |
float |
Vertical Y coordinate. |
Returns ContextMenu: This widget instance for chaining..