Skip to content

Command palette

A command palette: it fuzzy-filters commands (ui.PaletteItem(label, on_press, keys=)) by query, highlights the matched letters and shows each shortcut. You own the query and the selected index; Enter calls on_submit.

ui.command_palette · Category: Overlays · Full reference →

Command paletteCommand palette
  • To make every action reachable from the keyboard (usually with Ctrl+Shift+P).
import pyzed as pz
from pyzed import ui
class App(pz.App):
def __init__(self):
self.open, self.query, self.selected = False, "", 0
def commands(self):
return [ui.PaletteItem("editor: toggle soft wrap", self.toggle_wrap, keys="alt-z"),
ui.PaletteItem("workspace: toggle left dock", self.toggle_left, keys="ctrl-b")]
def on_key(self, combo):
if combo == "ctrl-shift-p":
self.open, self.query, self.selected = True, "", 0
return pz.Command.focus("command-palette")
if self.open and combo in ("up", "down"):
self.selected += 1 if combo == "down" else -1
elif combo == "escape":
self.open = False
def run_selected(self):
matches = ui.fuzzy_filter(self.query, self.commands(), key=lambda c: c.label)
if matches:
self.open = False
return matches[self.selected % len(matches)][0].on_press()
def view(self):
palette = ui.command_palette(self.query, lambda q: setattr(self, "query", q), self.commands(),
selected=self.selected, on_submit=self.run_selected)
return ui.layer(self.main_view(), palette if self.open else None,
on_dismiss=lambda: setattr(self, "open", False))
Command palette: EmptyCommand palette: Empty
Empty
Code
ui.command_palette("", on_query, [
ui.PaletteItem("file: new", on_new, keys="ctrl-n"),
ui.PaletteItem("file: open", on_open, keys="ctrl-o"),
ui.PaletteItem("theme selector: toggle", on_theme),
], width=420)
Command palette: query="op"Command palette: query="op"
query="op"
Code
ui.command_palette("op", on_query, [
ui.PaletteItem("file: new", on_new, keys="ctrl-n"),
ui.PaletteItem("file: open", on_open, keys="ctrl-o"),
ui.PaletteItem("workspace: open recent", on_recent, keys="ctrl-r"),
], width=420)
Command palette: selected=1Command palette: selected=1
selected=1
Code
ui.command_palette("", on_query, [
ui.PaletteItem("file: new", on_new, keys="ctrl-n"),
ui.PaletteItem("file: open", on_open, keys="ctrl-o"),
ui.PaletteItem("file: save", on_save, keys="ctrl-s"),
], selected=1, width=420)

Command palette has 3 events of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

Event Of Handler gets When
on_query ui.command_palette (text) Callable that receives the new text.
on_submit ui.command_palette () Message or callable sent when Enter is pressed.
on_press ui.PaletteItem () Message or callable sent on press. None disables the control.

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class App(pz.App):
def __init__(self):
self.open, self.query, self.wrap = True, "", False
def toggle_wrap(self):
self.wrap, self.open = not self.wrap, False
def commands(self): # each item's on_press runs the command
return [ui.PaletteItem("editor: toggle soft wrap", self.toggle_wrap, keys="alt-z")]
def run_first(self): # on_submit: Enter runs the first match
matches = ui.fuzzy_filter(self.query, self.commands(), key=lambda c: c.label)
return matches[0][0].on_press() if matches else None
def view(self):
palette = ui.command_palette(self.query, lambda q: setattr(self, "query", q), # on_query
self.commands(), on_submit=self.run_first)
return ui.layer(main_view(), palette if self.open else None,
on_dismiss=lambda: setattr(self, "open", False))
command_palette(
query,
on_query,
commands,
*,
selected=0,
on_submit=None,
placeholder='Execute a command…',
input_id='command-palette',
theme=None,
**kwargs,
)

A command palette: fuzzy-filters commands by query and shows their shortcuts.

Property Default Description
query required Search text.
commands required List of PaletteItem.
selected 0 Highlighted index (for arrow-key navigation).
placeholder "Execute a command…" Hint text shown when empty.
input_id "command-palette" Field id (for Command.focus).
theme None Theme to use instead of the current one (pyzed.theme.current()).
**kwargs — Same as picker.

Events: on_query, on_submit (events).

Full reference →

@dataclass
class PaletteItem(label, on_press=None, keys=None, icon=None, detail=None)

An entry of command_palette or picker.

Property Default Description
label required Label text.
keys None Shortcut shown at the end, written as a string: "ctrl-s", "cmd-k cmd-s".
icon None Name of a Lucide icon (pyzed icons lists them).
detail None Secondary text (for example, a file path).

Events: on_press (events).

Full reference →