command-palette only.Expand description
The global searchable launcher: a command list in a dialog, opened from anywhere with Ctrl+K or Ctrl+Shift+P.
State owns whether the palette is open, its groups of commands, any
nested Pages and the recently run commands, which it lists first
while the query is empty. Choosing an item whose id names a page, such
as “Change theme…”, opens that page’s list instead of running it;
Backspace on an empty query, Escape or the Back button returns to the
page before.
The app routes key presses from keys::subscription through
State::key_event so the toggle chords open the palette anywhere.
While it is open, the palette resolves its own keys: the command
list’s first, then Back and the toggle chords, then the dialog’s. Async
results arrive through the command list’s channel: subscription
wraps command::subscription, and Output::Search asks the app to
search the current page for a query.
use iced::{Element, Subscription};
use iced::widget::text;
use iced_cube::command_palette::{self, Output, command_palette, page};
use iced_cube::keys;
use iced_cube::navigation::command::{group, item};
#[derive(Debug, Clone)]
enum Message {
Palette(command_palette::Event<&'static str>),
Key(keys::Event),
}
struct App {
palette: command_palette::State<&'static str>,
}
impl App {
fn new() -> Self {
let palette = command_palette::State::new([group(
"Actions",
[item("new", "New file"), item("theme", "Change theme...")],
)])
.with_page(page("theme", "Theme", [group("", [item("dark", "Dark"), item("light", "Light")])]));
Self { palette }
}
fn update(&mut self, message: Message) {
let event = match message {
Message::Palette(event) => Some(event),
Message::Key(key) => self
.palette
.key_event(&command_palette::default_keymap(), &key),
};
if let Some(Output::Activated(id)) = event.and_then(|event| self.palette.update(event)) {
println!("Run {id}");
}
}
fn view(&self) -> Element<'_, Message> {
command_palette(&self.palette, text("Press Ctrl+K"))
.on_event(Message::Palette)
.into()
}
fn subscription(&self) -> Subscription<Message> {
keys::subscription().map(Message::Key)
}
}Structs§
- Command
Palette - A command palette builder. Convert it into an
Elementto render. - Page
- A nested list, opened by choosing the item with the page’s id.
- State
- Whether the palette is open, its pages, the recent commands and the command list on show.
Enums§
- Action
- What a command palette keyboard shortcut does.
- Event
- Changes to a command palette.
- Output
- What the app may need to act on after an
Event.
Constants§
- DEFAULT_
TITLE - The dialog title on the first page unless the builder sets its own.
- DIALOG_
ID - The widget id of the dialog surface, which scopes its focus.
- RECENT_
LABEL - The heading over the recent commands.
- RECENT_
LIMIT - How many recent commands are kept unless the state sets its own number.
- SEARCH_
ID - The widget id of the search field, to focus it or find it in tests.
- VISIBLE_
ROWS - How many rows show at once unless the state sets its own number.
Functions§
- command_
palette - Wraps
base, the app the palette opens over. Withouton_eventthe list renders disabled. - default_
keymap - The default command palette shortcuts:
- page
- A page listing
groups, opened by the item whose id isid. - subscription
- Owns the command list’s results channel. Emits
command::Event::Readyfirst, then batches, wrapped inEvent::Command.