Skip to main content

Module command

Module command 

Source
Available on crate feature command only.
Expand description

A searchable list of actions: a search field over grouped results.

State holds the groups of Items, the query and the highlighted row. Typing ranks the items with score, a case-insensitive fuzzy match that prefers prefixes and word starts, and checks each item’s keywords too. State::update returns an Output when an item is activated or the query changes.

While the search field has focus, the list handles its own keys through a Keymap of Actions, before anything around it sees them; see default_keymap. Inside a dialog or popover, Escape first clears the query, and only then returns Output::Closed for the app to close whatever holds the list. Rows use the shared menu look.

Results can also arrive from background work. subscription owns a bounded channel and emits Event::Ready with its Sender first. When the query changes, update returns Output::Search; hand the query and a sender clone to a producer, which sends Results tagged with that query. Results for an older query are dropped.

use iced::{Element, Subscription};
use iced_cube::lucide;
use iced_cube::navigation::command::{self, Output, command, group, item};

#[derive(Debug, Clone)]
enum Message {
    Command(command::Event<&'static str>),
}

struct App {
    command: command::State<&'static str>,
}

impl App {
    fn new() -> Self {
        let command = command::State::new([group(
            "Suggestions",
            [
                item("calendar", "Calendar").icon(lucide!(Calendar)),
                item("settings", "Settings").shortcut("Ctrl+S"),
            ],
        )]);
        Self { command }
    }

    fn update(&mut self, message: Message) {
        let Message::Command(event) = message;
        if let Some(Output::Activated(id)) = self.command.update(event) {
            println!("Run {id}");
        }
    }

    fn view(&self) -> Element<'_, Message> {
        command(&self.command).on_event(Message::Command).into()
    }
}

Structs§

Command
A command list builder. Convert it into an Element to render.
Group
Items under a label. An empty label shows no heading.
Item
One action in a Group.
Results
Items a producer found for a query, shown under group after the built-in groups. They are shown in the order they arrive, unranked.
State
The groups, the query, the ranked results and the highlighted row.

Enums§

Action
What a command keyboard shortcut does.
Event
Everything that changes a command list.
Output
What the app may need to act on after an Event.

Constants§

BATCH_SIZE
The most result batches delivered in one Event::Received.
CHANNEL_CAPACITY
How many result batches a producer can send before it has to wait.
DEFAULT_EMPTY
Shown when nothing matches the query, unless the builder sets its own.
SEARCHING
Shown while Command::loading is set.
VISIBLE_ROWS
How many rows show at once unless the state sets its own number.

Functions§

batches
Groups whatever is already waiting in the channel into one batch.
command
Renders the search field and results of state.
default_keymap
The default command shortcuts:
divider_style
The line under the search field and between groups.
group
item
Creates an enabled item.
results
score
How well query matches text, ignoring case, or None when it does not match. Higher is better, and an empty query matches everything with 0.
search_style
The search field: borderless and transparent, inside the surface.
stream
The stream behind subscription, for driving it without a runtime.
subscription
Owns the results channel. Emits Event::Ready first, then batches.

Type Aliases§

Sender
The sending half of the results channel. Clone it for each producer.