UI Toolbox logoUI Toolbox

Context menus

UIToolbox.Core.ContextMenu has a general popup menu. ToolBarMenu uses it for its drop-downs.

Type Members
ContextMenuComponent AddItem(MenuItem), AddItem(text, callback, shortcut), AddCheckableItem(text, toggled, isChecked), AddRadioItem(text, group, callback, isChecked), AddSubmenu(text), AddSeparator(), AddLabel(text), AttachTo(target, context), DetachFrom(target), ShowDropdown(trigger, side, align, context), ShowAt(anchor, worldPosition, context, keepOpenOn), Hide(), IsVisible, Items, Highlight(item), HighlightedItem, Activate(item), ParentMenu, OwnerItem, events ActionTriggered(string action, object? context) and Closed.
MenuItem Text, Action, Callback, Shortcut, Icon, Checkable, Checked, RadioGroup, Destructive, Enabled, EnabledPredicate, CheckedPredicate, Submenu, HasSubmenu, MatchesShortcut(key, modifiers), static TryParseShortcut(text, out key, out modifiers).
MenuSeparator A line between items.
MenuLabel A heading for the items after it. It takes no click and the arrows skip it.
MenuSide Below, Above, Right, Left: the side of the trigger where a dropdown shows.
MenuAlign Start, Center, End: how a dropdown lines up with its trigger.
using UIToolbox;
using UIToolbox.Core.ContextMenu;
using UIToolbox.Utils;
using UnityEngine.UIElements;

public static class WindowMenu
{
    public static void Attach(VisualElement target, WindowDescriptor window)
    {
        var menu = new ContextMenuComponent();
        menu.AddItem(new MenuItem { Text = "Maximize", Action = "maximize",
            EnabledPredicate = ctx => !((WindowDescriptor)ctx!).IsMaximized });
        menu.AddSeparator();
        menu.AddItem(new MenuItem { Text = "Close", Action = "close", Destructive = true });

        menu.ActionTriggered += (action, ctx) =>
        {
            var w = (WindowDescriptor)ctx!;
            if (action == "maximize") AsyncRunner.Run(() => w.MaximizeAsync(), "Menu maximize");
            if (action == "close") AsyncRunner.Run(() => w.CloseAsync(), "Menu close");
        };

        // Right click, the Menu key and a long press
        menu.AttachTo(target, window);
    }
}

A dropdown menu on a button, with a heading and radio items:

var sort = new ContextMenuComponent();
sort.AddLabel("Sort by");
sort.AddRadioItem("Name", "sort", () => SortBy("name"), isChecked: true);
sort.AddRadioItem("Date", "sort", () => SortBy("date"));

var button = new Button { text = "Sort" };
button.clicked += () => sort.ShowDropdown(button, MenuSide.Below, MenuAlign.End);

Styling

Class Element
tb-context-menu The menu.
tb-menu-item An item. Parts: __check, __icon, __label, __shortcut, __arrow.
tb-menu-item--checked A checked item: the check mark or the radio dot shows.
tb-menu-item--highlighted The item that Enter runs.
tb-menu-item--destructive An item with Destructive.
tb-menu-separator The line between items.
tb-menu-label A heading.

The tab strip uses TabContextMenu (close, close others, close to the right, pin, duplicate, move to a new window). You cannot add items to the tab menu today.