Skip to main content

CommandPalette

Wisej.NET 4.2.0+

The CommandPalette opened with Ctrl+K: the query, matches grouped by category, a shortcut hint and the keyboard help.

A CommandPalette is the search box that pops up over the application when the user presses Ctrl+K: type a few letters, pick a command with the arrow keys, press Enter. It gives keyboard users one place to reach every screen and action without hunting through a MenuBar, and it can just as well search records — orders, customers, tickets — so that "jump to order 10482" is a keystroke away.

It is a component, not a control. It takes no space on the page and is not added to Controls: you drop it on a page or form in the designer, where it lands in the component tray like a Timer, or create it in code with the page's components container. The popup appears only when the user presses its shortcut or when your code calls Show().

info

For a complete list of properties, methods, and events, see the API documentation.

Features​

Commands​

The palette lists PaletteCommand objects from its Commands collection. A command has a Text, a secondary Description, an Icon (an image URL or a theme image name), a Category that groups the results under a heading, and Keywords: extra search terms, separated by spaces, that match without being displayed. Shortcut is a display-only hint shown on the right of the result; the palette does not register it, so wire the key yourself if it should work outside the palette too. Enabled keeps a command visible but not executable.

Tag holds your data and, like everything else you do not display, never leaves the server. Text and descriptions are displayed literally, never as HTML. Changing a command that is already in the collection updates the palette.

Opening and Closing​

Shortcut is the key combination that opens the palette, Keys.Control | Keys.K by default; Keys.None unregisters it. The shortcut is registered for as long as the component is alive, so dispose it with the page that owns it, which the components container does for you.

From code, Show() opens the palette with an empty query and Show(query) opens it with a query already typed; calling it while the palette is open replaces the query. Hide() closes it, IsOpen tells whether it is open, and Enabled set to false closes it and blocks it from opening. Opened and Closed are raised once per opening.

The user moves through the results with the arrow keys, runs one with Enter, jumps between categories with Tab and closes the palette with Esc. Width sets the popup width in pixels, capped by the browser window.

Client and Server Filtering​

FilterMode decides where the search happens:

  • CommandFilterMode.Client, the default, filters the registered Commands in the browser. A command matches when every word of the query appears in its text, description, category or keywords. No request is made while the user types.
  • CommandFilterMode.Server raises QueryChanged for every accepted change of the query. The handler sets e.Results to the commands to display, synchronously. Results do not have to be in Commands, which is what lets the palette search a database. Leaving Results null falls back to filtering Commands; an empty sequence shows "No matching commands".

MinQueryLength sets how many characters must be typed before anything is searched; shorter queries show a prompt instead, and do not raise QueryChanged.

Recent Commands​

With ShowRecent set to true, the default, the commands run most recently are listed first while the query is empty, up to MaxRecent. The history belongs to this component and this session.

How To​

Register Navigation Commands​

In the designer, set the properties of commandPalette1 and handle CommandExecuted; in code it looks like this:

this.commandPalette1 = new CommandPalette(this.components);

this.commandPalette1.Commands.Add(new PaletteCommand { Text = "Open orders", Category = "Sales", Shortcut = "Ctrl+O", Tag = typeof(OrdersPage) });
this.commandPalette1.Commands.Add(new PaletteCommand { Text = "Back orders", Category = "Sales", Keywords = "backlog stock", Tag = typeof(BackOrdersPage) });
this.commandPalette1.Commands.Add(new PaletteCommand { Text = "Shipments", Category = "Logistics", Description = "Carriers and tracking", Tag = typeof(ShipmentsPage) });
this.commandPalette1.Commands.Add(new PaletteCommand { Text = "New sales order", Category = "Actions", Tag = "new-order" });

Run the Selected Command​

CommandExecuted is raised after the user runs a command and the popup has closed. e.Command is the original PaletteCommand, Tag included, and e.Query is what the user typed.

private void commandPalette1_CommandExecuted(object sender, CommandEventArgs e)
{
if (e.Command.Tag is Type pageType)
((Page)Activator.CreateInstance(pageType)).Show();
else if ((string)e.Command.Tag == "new-order")
new SalesOrderForm().ShowDialog();
}

Search Records on the Server​

this.commandPalette1.FilterMode = CommandFilterMode.Server;
this.commandPalette1.MinQueryLength = 2;
this.commandPalette1.PlaceholderText = "Search orders and customers…";

this.commandPalette1.QueryChanged += (s, e) =>
{
e.Results = this.db.Orders
.Where(o => o.Number.Contains(e.Query) || o.CustomerName.Contains(e.Query))
.Take(20)
.AsEnumerable()
.Select(o => new PaletteCommand { Text = $"Order {o.Number}", Description = o.CustomerName, Category = "Orders", Tag = o.Id })
.ToList();
};

Open It from a Button​

The shortcut is not the only way in. A toolbar button or a menu item can open the palette with or without a query:

this.searchButton.Click += (s, e) => this.commandPalette1.Show();
this.findOrderMenuItem.Click += (s, e) => this.commandPalette1.Show("Order ");

Advanced​

JavaScript Widget​

ItemDescription
Class name"wisej.web.CommandPalette"
Theme appearance"commandpalette" for the popup, with "commandpalette-backdrop", "commandpalette-header", "commandpalette-input", "commandpalette-list", "commandpalette-category", "commandpalette-result" and "commandpalette-footer" among the appearances of its parts. See Themes.