UI Toolbox logoUI Toolbox

CommandPalette

A CommandPalette is a search box over the workspace that finds and runs a command. The user presses Ctrl+K, types a part of the command name, and presses Enter. It gives a keyboard user fast access to every action of your UI.

The command palette of the WorkspaceDemo, with the typed text "lay" and the matching commands in their groups

How to add a command palette:

  1. Add one CommandPalette to the workspace, in UXML or in code. It stays hidden. The shortcut works while the palette is in a panel.
  2. Add the commands in code. Each command has a text and an action.
<tb:Workspace name="workspace">
    <!-- the windows -->
    <tb:CommandPalette name="palette" />
</tb:Workspace>
var palette = root.Q<CommandPalette>("palette");
palette.AddCommand("Save layout", SaveLayout, "Ctrl+S", "Layout");
palette.AddCommand("Reset layout", ResetLayout, group: "Layout");
var paste = palette.AddCommand("Paste", Paste, "Ctrl+V", "Edit");
paste.Enabled = false; // dim, and Enter passes over it
palette.Executed += command => Debug.Log($"Ran {command.Text}");
Member Description
Shortcut (shortcut) The keys that open and close the palette from any control of the panel, such as "Ctrl+K" (the default), "Ctrl+Shift+P" or "F1". Command counts as Control. Empty turns the shortcut off.
Placeholder (placeholder) The text of the empty search field. Without a value it is the ToolboxText.TYPE_A_COMMAND text.
FuzzySearch (fuzzy-search) On (the default): "svl" finds "Save layout". Off: only a command that contains the typed text.
ShowClearButton (show-clear-button) On (the default): the search field shows a "x" button while it has text, and a click clears the text. See Clear button.
AddCommand(text, run, detail, group, icon) Adds a command and returns its ChoiceItem. detail shows muted on the right, for a shortcut or a hint. group is the header that the command shows under.
AddCommand(item, run) Adds a command from a ChoiceItem. An item that is in the palette gets the new action.
RemoveCommand(item), ClearCommands() Remove commands.
Commands, Matches, Highlighted, Query All the commands, the ones that match the typed text, the one that Enter runs, and the typed text.
Open(anchor), Close(), Toggle(anchor) Show and hide the palette from code, for example from a toolbar button. A palette in the hierarchy needs no anchor.
Complete() Puts the text of the highlighted command in the search, with the caret at its end. Tab calls it. False when no enabled command is highlighted.
Run(command) Closes the palette, runs the action and raises Executed. It does nothing for a disabled command.
IsOpen, Opened, Closed, Executed The state and its events.

Tips:

foreach (var button in workspace.Query<WorkspaceToolbarButton>().ToList())
{
    var id = button.WindowId;
    palette.AddCommand($"Open {button.Title}", () => _ = workspace.OpenWindowAsync(id), group: "Windows");
}

Styling

Class Element
tb-command-palette The overlay. It fills the workspace, uses --tb-color-overlay, and puts the dialog 10% from the top.
tb-command-palette__dialog The dialog: 480 px wide (90% at most), with the surface color, a border and --tb-radius-md.
tb-command-palette__search The search TextField.
tb-command-palette__ghost The grey rest of the highlighted command, over the text of the search. It uses --tb-color-text-muted.
tb-command-palette__list The list of commands. It also has tb-choice-list, so the rows use the tb-choice-list__* classes of "SearchDropdown". In the dialog it has no border and position: relative.
tb-command-palette--hidden Opacity 0 and the dialog 12 px up: the start of the enter animation and the end of the exit.
tb-command-palette--anim-in, --anim-out The transitions of opacity and translate on the overlay and the dialog.
tb-modal Also on the overlay, only while the palette is open.