Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Selector

Selector is a simple container widget that allows selecting an item from a fixed set of items. Selector widget shows the currently selected item at the center and two buttons on the sides that allows selecting either the previous or the next item.

Example

The following examples creates a new selector with three items and selects the middle one as active. The items can be of any type, even mixed types are allowed.

#![allow(unused)]
fn main() {
fn create_selector(ctx: &mut BuildContext) -> Handle<Selector> {
    SelectorBuilder::new(WidgetBuilder::new())
        .with_items(
            vec![
                TextBuilder::new(WidgetBuilder::new())
                    .with_text("Item1")
                    .build(ctx),
                TextBuilder::new(WidgetBuilder::new())
                    .with_text("Item2")
                    .build(ctx),
                TextBuilder::new(WidgetBuilder::new())
                    .with_text("Item3")
                    .build(ctx),
            ]
            .to_base(),
        )
        .with_current_item(1)
        .build(ctx)
}
}

Selection

The newly selected item index can be received from a selector by listening to [SelectorMessage::Current] message. To select a new item from code, send the same message with the desired index:

#![allow(unused)]
fn main() {
fn on_ui_message(selector: Handle<Selector>, message: &UiMessage, ui: &UserInterface) {
    if let Some(SelectorMessage::Current(Some(index))) = message.data_from(selector) {
        println!("The new selection is {index}!");

        if *index != 0 {
            // The selection can be changed by sending the same message to the widget:
            ui.send(selector, SelectorMessage::Current(Some(0)));
        }
    }
}
}