Class MenuBuilder
A utility class to build a Menu programmatically.
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: solution.dll
Syntax
public class MenuBuilder : AnchorPopup<MenuBuilder>
Constructors
MenuBuilder(VisualElement, VisualElement, VisualElement)
Constructor.
Declaration
public MenuBuilder(VisualElement parentView, VisualElement view, VisualElement contentView)
Parameters
Type | Name | Description |
---|---|---|
VisualElement | parentView | The parent view of the Menu. |
VisualElement | view | The Menu visual element (used by the popup system). |
VisualElement | contentView | The Menu's content visual element. |
Properties
closeOnSelection
Close the menu automatically when an item is selected.
Declaration
public bool closeOnSelection { get; }
Property Value
Type | Description |
---|---|
bool |
currentMenu
The last menu in the stack.
Declaration
public Menu currentMenu { get; }
Property Value
Type | Description |
---|---|
Menu |
Methods
AddAction(int, Action<MenuItem>)
Add an Action menu item to the current menu.
Declaration
public MenuBuilder AddAction(int actionId, Action<MenuItem> bindItemFunc)
Parameters
Type | Name | Description |
---|---|---|
int | actionId | A unique identifier for the action. |
Action<MenuItem> | bindItemFunc | A callback to bind the action. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
AddAction(int, string, string, string, EventCallback<ClickEvent>)
Add an Action menu item to the current menu.
Declaration
public MenuBuilder AddAction(int actionId, string labelStr, string iconName, string shortcut, EventCallback<ClickEvent> callback)
Parameters
Type | Name | Description |
---|---|---|
int | actionId | A unique identifier for the action. |
string | labelStr | The raw label of the menu item (will be localized). |
string | iconName | The icon of the menu item. |
string | shortcut | The shortcut of the menu item. |
EventCallback<ClickEvent> | callback | The callback to invoke when the menu item is clicked. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
AddAction(int, string, string, EventCallback<ClickEvent>)
Add an Action menu item to the current menu.
Declaration
public MenuBuilder AddAction(int actionId, string labelStr, string iconName, EventCallback<ClickEvent> callback)
Parameters
Type | Name | Description |
---|---|---|
int | actionId | A unique identifier for the action. |
string | labelStr | The raw label of the menu item (will be localized). |
string | iconName | The icon of the menu item. |
EventCallback<ClickEvent> | callback | The callback to invoke when the menu item is clicked. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
AddDivider(Action<MenuDivider>)
Add a Menu Divider to the current menu.
Declaration
public MenuBuilder AddDivider(Action<MenuDivider> bindDividerFunc = null)
Parameters
Type | Name | Description |
---|---|---|
Action<MenuDivider> | bindDividerFunc | A callback to bind the divider. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
AddSection(Action<MenuSection>)
Add a Section to the current menu.
Declaration
public MenuBuilder AddSection(Action<MenuSection> bindSectionFunc = null)
Parameters
Type | Name | Description |
---|---|---|
Action<MenuSection> | bindSectionFunc | A callback to bind the section. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
Build(VisualElement, Menu)
Create a MenuBuilder instance.
Declaration
public static MenuBuilder Build(VisualElement referenceView, Menu menu = null)
Parameters
Type | Name | Description |
---|---|---|
VisualElement | referenceView | The reference view to position the menu. |
Menu | menu | The menu to display. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
GetFocusableElement()
Returns the element that will be focused when the view will become visible.
The default value is `null`.
Declaration
protected override VisualElement GetFocusableElement()
Returns
Type | Description |
---|---|
VisualElement | The element that will be focused when the view will become visible. |
Overrides
GetMovableElement()
Method which must return the visual element that needs to be moved, based on the anchor position and size.
Declaration
protected override VisualElement GetMovableElement()
Returns
Type | Description |
---|---|
VisualElement | The visual element which will be moved. The default value is view. |
Overrides
InvokeDismissedEventHandlers(DismissType)
Called when the popup has been dismissed. This method will invoke any handlers attached to the dismissed event.
Declaration
protected override void InvokeDismissedEventHandlers(DismissType reason)
Parameters
Type | Name | Description |
---|---|---|
DismissType | reason |
Overrides
Pop()
Go back to the parent menu and make it the current menu.
Declaration
public MenuBuilder Pop()
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
PushSubMenu(Action<MenuItem>)
Create an action menu item, add a sub-menu to the current menu, and make it the current menu.
Declaration
public MenuBuilder PushSubMenu(Action<MenuItem> bindItemFunc)
Parameters
Type | Name | Description |
---|---|---|
Action<MenuItem> | bindItemFunc | A callback to bind the action. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
Remarks
When the binding callback is invoked, the sub-menu is already pushed on the stack and set on the menu item.
PushSubMenu(int, string, string)
Create an action menu item, add a sub-menu to the current menu, and make it the current menu.
Declaration
public MenuBuilder PushSubMenu(int actionId, string labelStr, string iconName)
Parameters
Type | Name | Description |
---|---|---|
int | actionId | A unique identifier for the action. |
string | labelStr | The label of the menu item. |
string | iconName | The icon of the menu item. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
PushSubMenu(int, string, string, Action)
Create an action menu item, add a sub-menu to the current menu, and make it the current menu.
Declaration
public MenuBuilder PushSubMenu(int actionId, string labelStr, string iconName, Action subMenuOpenedCallback)
Parameters
Type | Name | Description |
---|---|---|
int | actionId | A unique identifier for the action. |
string | labelStr | The label of the menu item. |
string | iconName | The icon of the menu item. |
Action | subMenuOpenedCallback | The callback to invoke when the sub-menu is opened. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
SetCloseOnSelection(bool)
Close the menu automatically when an item is selected.
Declaration
public MenuBuilder SetCloseOnSelection(bool value)
Parameters
Type | Name | Description |
---|---|---|
bool | value | True to close the menu automatically when an item is selected. |
Returns
Type | Description |
---|---|
MenuBuilder | The MenuBuilder instance. |
ShouldAnimate()
Implement this method to know if the popup should call AnimateViewIn() and AnimateViewOut(DismissType) methods or not.
Declaration
protected override bool ShouldAnimate()
Returns
Type | Description |
---|---|
bool |
|