Skip to main content

Module command_palette

Module command_palette 

Source
Available on crate feature 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§

CommandPalette
A command palette builder. Convert it into an Element to 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. Without on_event the list renders disabled.
default_keymap
The default command palette shortcuts:
page
A page listing groups, opened by the item whose id is id.
subscription
Owns the command list’s results channel. Emits command::Event::Ready first, then batches, wrapped in Event::Command.