iced-cube 0.0.21

Themeable application components for the iced GUI library, with Lucide icons
Documentation
#![cfg(feature = "tabs")]

use iced::Element;
use iced::widget::column;
use iced_cube::navigation::tabs::{self, Event, State, Variant, tab};
use iced_test::simulator;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Page {
    Account,
    Password,
    Billing,
}

#[derive(Debug, Clone, PartialEq)]
enum Message {
    Tabs(Event<Page>),
}

fn state() -> State<Page> {
    State::new([
        tab(Page::Account, "Account"),
        tab(Page::Password, "Password"),
        tab(Page::Billing, "Billing").disabled(true),
    ])
}

fn view(state: &State<Page>, variant: Variant) -> Element<'_, Message> {
    tabs::tabs(state)
        .variant(variant)
        .on_event(Message::Tabs)
        .into()
}

#[test]
fn clicking_a_tab_emits_select() {
    for variant in Variant::ALL {
        let state = state();
        let mut ui = simulator(view(&state, variant));
        ui.click("Password").expect("Password tab is rendered");
        ui.click("Account").expect("Account tab is rendered");

        let messages: Vec<_> = ui.into_messages().collect();
        assert_eq!(
            messages,
            vec![
                Message::Tabs(Event::Select(Page::Password)),
                Message::Tabs(Event::Select(Page::Account)),
            ],
            "{variant:?}"
        );
    }
}

#[test]
fn disabled_tab_emits_nothing() {
    let state = state();
    let mut ui = simulator(view(&state, Variant::Underline));
    ui.click("Billing").expect("Billing tab is still rendered");
    assert_eq!(ui.into_messages().count(), 0);
}

#[test]
fn emitted_messages_drive_the_state() {
    let mut state = state();
    let messages: Vec<_> = {
        let mut ui = simulator(view(&state, Variant::Pills));
        ui.click("Password").expect("Password tab is rendered");
        ui.into_messages().collect()
    };
    for Message::Tabs(event) in messages {
        let _ = state.update(event);
    }
    assert_eq!(state.selected(), Some(Page::Password));
}

#[test]
fn triggers_stay_on_one_line_in_a_narrow_container() {
    let state = State::new([
        tab(Page::Account, "Account settings"),
        tab(Page::Password, "Change password"),
    ]);
    for variant in [Variant::Underline, Variant::Pills] {
        let element: Element<'_, Message> = column![view(&state, variant)].width(60).into();
        let mut ui = simulator(element);
        for label in ["Account settings", "Change password"] {
            let bounds = ui.find(label).expect("trigger is rendered").bounds();
            assert!(
                bounds.height < 24.0,
                "{variant:?} {label}: {}",
                bounds.height
            );
            assert!(bounds.width > 60.0, "{variant:?} {label}: {}", bounds.width);
        }
    }
}

#[test]
fn tabs_without_a_handler_are_disabled() {
    let state = state();
    let element: Element<'_, Message> = tabs::tabs(&state).into();
    let mut ui = simulator(element);
    ui.click("Password").expect("Password tab is rendered");
    assert_eq!(ui.into_messages().count(), 0);
}

mod support;

use iced::Point;
use iced::keyboard::{Key, key::Named};
use iced_cube::Chord;
use iced_cube::navigation::tabs::Action;

#[derive(Debug, Clone)]
enum Keyed {
    Tabs(Event<Page>),
    Typed,
}

/// The tab list with a text input after it, so Tab has somewhere to go.
fn keyed_view(state: &State<Page>) -> Element<'_, Keyed> {
    column![
        tabs::tabs(state).on_event(Keyed::Tabs),
        iced::widget::text_input("", "").on_input(|_| Keyed::Typed),
    ]
    .into()
}

fn keyed_update(state: &mut State<Page>, message: Keyed) {
    if let Keyed::Tabs(event) = message {
        let _ = state.update(event);
    }
}

/// Inside the first tab, "Account".
const FIRST_TAB: Point = Point::new(20.0, 15.0);

fn named(named: Named) -> Key {
    Key::Named(named)
}

#[test]
fn keys_act_only_after_the_tabs_are_pressed() {
    let mut ui = support::Harness::new(state(), keyed_view, keyed_update);
    assert!(!ui.tap(named(Named::ArrowRight)), "not pressed yet");
    assert_eq!(ui.state.selected(), Some(Page::Account));

    ui.click(FIRST_TAB);
    assert!(ui.tap(named(Named::ArrowRight)));
    assert_eq!(ui.state.selected(), Some(Page::Password));
    ui.tap(named(Named::Home));
    assert_eq!(ui.state.selected(), Some(Page::Account));

    // A press elsewhere hands the keys back to the rest of the app.
    ui.click(Point::new(20.0, 500.0));
    assert!(!ui.tap(named(Named::End)));
    assert_eq!(ui.state.selected(), Some(Page::Account));
}

#[test]
fn tab_focus_gives_the_tabs_their_keys_and_tab_away_takes_them() {
    let mut ui = support::Harness::new(state(), keyed_view, keyed_update);
    ui.focus_next();
    assert!(ui.tap(named(Named::End)));
    assert_eq!(ui.state.selected(), Some(Page::Password));

    ui.focus_next();
    ui.tap(named(Named::Home));
    assert_eq!(
        ui.state.selected(),
        Some(Page::Password),
        "the text input has the keys now"
    );
}

#[test]
fn tab_away_after_a_press_takes_the_keys_too() {
    let mut ui = support::Harness::new(state(), keyed_view, keyed_update);
    ui.click(FIRST_TAB);
    ui.focus_next();
    ui.focus_next();
    ui.tap(named(Named::ArrowRight));
    assert_eq!(ui.state.selected(), Some(Page::Account));
}

#[test]
fn ctrl_tab_also_needs_the_keys() {
    let mut ui = support::Harness::new(state(), keyed_view, keyed_update);
    let ctrl = iced::keyboard::Modifiers::CTRL;
    assert!(!ui.press(ctrl, named(Named::Tab)));
    assert_eq!(ui.state.selected(), Some(Page::Account));
    ui.click(FIRST_TAB);
    assert!(ui.press(ctrl, named(Named::Tab)));
    assert_eq!(ui.state.selected(), Some(Page::Password));
}

#[test]
fn a_changed_keymap_is_used() {
    fn view(state: &State<Page>) -> Element<'_, Keyed> {
        let keymap = tabs::default_keymap()
            .unbind(&Chord::named(Named::ArrowRight))
            .bind(Chord::character('l'), Action::Next);
        tabs::tabs(state)
            .keymap(keymap)
            .on_event(Keyed::Tabs)
            .into()
    }
    let mut ui = support::Harness::new(state(), view, keyed_update);
    ui.focus_next();
    assert!(!ui.tap(named(Named::ArrowRight)));
    assert_eq!(ui.state.selected(), Some(Page::Account));
    ui.tap(support::letter('l'));
    assert_eq!(ui.state.selected(), Some(Page::Password));
}

#[test]
fn tabs_without_a_handler_take_no_keys() {
    fn view(state: &State<Page>) -> Element<'_, Keyed> {
        tabs::tabs(state).into()
    }
    let mut ui = support::Harness::new(state(), view, keyed_update);
    ui.focus_next();
    ui.click(FIRST_TAB);
    assert!(!ui.tap(named(Named::ArrowRight)));
    assert_eq!(ui.state.selected(), Some(Page::Account));
}

#[cfg(feature = "icedwright")]
#[path = "support/semantic.rs"]
mod semantic;

#[cfg(feature = "icedwright")]
#[test]
fn tabs_report_a_tab_list_with_the_selection() {
    let state = state();
    for variant in Variant::ALL {
        assert_eq!(
            semantic::yaml(view(&state, variant)),
            "- tablist [focused=false] [focusable]:\n  - scrollview:\n\
             \x20   - tab \"Account\" [selected]:\n      - text: Account\n\
             \x20   - tab \"Password\" [selected=false]:\n      - text: Password\n\
             \x20   - tab \"Billing\" [disabled] [selected=false]:\n      - text: Billing\n",
            "{variant:?}"
        );
    }
}