UI Toolbox logoUI Toolbox
Support

ToolBarMenu

ToolBarMenu in UIToolbox is an application menu bar. Each ToolBarMenuItem is a title with a drop-down ContextMenuComponent. The children of a title go in its menu, in UXML and in code.

A menu bar with its File menu open: shortcuts, a submenu, a separator and a check markA menu bar with its File menu open: shortcuts, a submenu, a separator and a check mark

<ui:UXML xmlns:tb="UIToolbox" xmlns:cm="UIToolbox.Core.ContextMenu">
    <tb:ToolBarMenu name="menuBar">
        <tb:ToolBarMenuItem text="&amp;File">
            <cm:MenuItem
                text="&amp;New"
                action="new"
                shortcut="Ctrl+N" />
            <cm:MenuItem
                text="Save"
                action="save"
                shortcut="Ctrl+S"
                enabled="false" />
            <cm:MenuSeparator />
            <cm:MenuItem text="Recent">
                <cm:MenuItem
                    text="notes.txt"
                    action="recent notes.txt" />
            </cm:MenuItem>
        </tb:ToolBarMenuItem>
        <tb:ToolBarMenuItem text="View">
            <cm:MenuItem
                text="Word Wrap"
                action="wrap"
                checkable="true"
                checked="true" />
            <cm:MenuSeparator />
            <cm:MenuLabel text="Zoom" />
            <cm:MenuItem
                text="100%"
                action="zoom 100"
                radio-group="zoom"
                checked="true" />
            <cm:MenuItem
                text="200%"
                action="zoom 200"
                radio-group="zoom" />
        </tb:ToolBarMenuItem>
        <tb:ToolBarMenuItem text="Edit">
            <cm:MenuItem
                text="Delete All"
                action="delete all"
                destructive="true" />
        </tb:ToolBarMenuItem>
    </tb:ToolBarMenu>
</ui:UXML>
var bar = new ToolBarMenu();
var file = bar.AddMenu("File");
file.AddItem("New", () => NewDocument(), "Ctrl+N");
file.AddSubmenu("Recent").AddItem("notes.txt", () => Open("notes.txt"));
bar.AddMenu("View")
    .AddCheckableItem("Word Wrap", on => SetWrap(on), isChecked: true);

// Items with an action, as UXML declares them
foreach (var title in bar.Menus)
    title.Menu.ActionTriggered += (action, _) =>
        Debug.Log($"{title.Text} > {action}");
Member Description
AddMenu(text) Appends a title and returns its menu.
Menus, GetMenu(text) The titles. ToolBarMenuItem has Text, AccessKey and Menu. GetMenu compares the text with its &.
OpenByAccessKey(key) Opens the menu of the first title with this access key and highlights its first enabled item, as Alt and the key do.
OverflowItem, OverflowMenus The "≡" title, and the titles that do not fit and are in its menu.
Open(item), Close(), OpenItem Shows the menu of a title under it, hides it, and the title whose menu is open.
RunShortcut(key, modifiers) Runs the first enabled item, in any menu or submenu, whose Shortcut matches. Returns false when none matches.
HandleShortcuts (handle-shortcuts) Runs the item shortcuts on key down anywhere in the panel. On by default.

The ToolBarMenuDemo scene shows the menu bar of a level editor: submenus, shortcuts and access keys, the Grid check and the radio items of the zoom and the brushes, titles added at runtime that move into the overflow menu, and the looks of the bar and its menus.