Skip to main content

Module virtual_list

Module virtual_list 

Source
Available on crate feature virtual-list only.
Expand description

A long list of equally tall rows that only builds the rows in view, so a hundred thousand items scroll as smoothly as ten.

State holds the items, the selection in the chosen Mode and the keyboard highlight. Its update handles presses, keyboard navigation and items arriving from the channel that subscription owns. The app draws each row with a closure that gets the item’s index and the item.

A list made with_follow sticks to its end as items arrive, like a feed or a log. Scrolling up stops following, and scrolling back to the end, or pressing End, starts again. A list made with_limit drops its oldest items once it holds that many, so a stream never grows without bound.

The list resolves its keys through a Keymap of Actions while it has focus, which it takes when pressed; see default_keymap.

use iced::widget::text;
use iced::{Element, Subscription};
use iced_cube::data::virtual_list::{self, Event, Output, State, virtual_list};

#[derive(Debug, Clone)]
enum Message {
    Feed(Event<String>),
}

struct App {
    feed: State<String>,
    sender: Option<virtual_list::Sender<String>>,
}

impl App {
    fn update(&mut self, message: Message) {
        let Message::Feed(event) = message;
        if let Some(Output::Ready(sender)) = self.feed.update(event) {
            // Hand clones of the sender to producers.
            self.sender = Some(sender);
        }
    }

    fn view(&self) -> Element<'_, Message> {
        virtual_list(&self.feed, |_, line: &String| text(line.as_str()).into())
            .height(320)
            .on_event(Message::Feed)
            .into()
    }

    fn subscription(&self) -> Subscription<Message> {
        virtual_list::subscription().map(Message::Feed)
    }
}

Structs§

RowStyle
The colours of a row.
State
The items, the selection, the highlight and whether the list follows its end.
Status
How a row is shown.
VirtualList
A virtual list builder. Convert it into an Element to render.

Enums§

Action
What a list keyboard shortcut does.
Event
Everything that changes a list.
Mode
How items are selected.
Output
What the app may need to act on after an Event.

Constants§

BATCH_SIZE
The most items delivered in one Event::Received.
CHANNEL_CAPACITY
How many items a producer can send before it has to wait.
DEFAULT_PAGE
Rows a page key moves until the list reports how many fit.
ROW_HEIGHT
The default height of every row.

Functions§

default_keymap
The default list shortcuts:
frame_style
The frame around the rows: a subtle border with rounded corners.
row_style
Resolves a row’s colours as a tree’s rows do: the accent behind a selected row, a lighter tint under the pointer and a ring around the keyboard highlight. Disabled rows are plain.
stream
The stream behind subscription, for driving it without a runtime.
subscription
Owns the channel items arrive on. Emits Event::Ready first, then batches of up to BATCH_SIZE items.
virtual_list
Renders state, drawing each row with row, which gets the item’s index and the item. Without on_event the rows render disabled.

Type Aliases§

RowId
Names an item for as long as it is in the list: its position counted from the first item the list ever held, so it stays the same when older items are dropped. Without a limit it equals the index.
Sender
The sending half of the channel items arrive on. Clone it for each producer.