Class MenuBuilder
A utility class to build a Menu programmatically.
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
public class MenuBuilder : AnchorPopup<MenuBuilder>
Constructors
MenuBuilder(VisualElement, VisualElement, VisualElement)
Constructor.
Declaration
public MenuBuilder(VisualElement referenceView, VisualElement view, VisualElement contentView)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | The element used as context provider for 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, Action<EventBase>)
Add an Action menu item to the current menu.
Declaration
public MenuBuilder AddAction(int actionId, string labelStr, string iconName, Action<EventBase> 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. |
| Action<EventBase> | callback | The callback to invoke when the menu item is clicked. |
Returns
| Type | Description |
|---|---|
| MenuBuilder | The MenuBuilder instance. |
AddAction(int, string, string, string, Action<EventBase>)
Add an Action menu item to the current menu.
Declaration
public MenuBuilder AddAction(int actionId, string labelStr, string iconName, string shortcut, Action<EventBase> 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. |
| Action<EventBase> | 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. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |
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
public override VisualElement GetMovableElement()
Returns
| Type | Description |
|---|---|
| VisualElement | The visual element which will be moved. The default value is view. |
Overrides
HideView(DismissType)
Called when it is time to hide the popup.
Declaration
protected override void HideView(DismissType reason)
Parameters
| Type | Name | Description |
|---|---|---|
| DismissType | reason | The reason why the popup should be dismissed. |
Overrides
InvokeShownEventHandlers()
Called when the popup has become visible. This method will invoke any handlers attached to the shown event.
Declaration
protected override void InvokeShownEventHandlers()
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 |
|