faststep 0.1.0

UIKit-inspired embedded UI framework built on embedded-graphics
Documentation
mod interaction;
mod render;
mod types;

pub use types::{
    ListActivity, ListDataSource, ListDelegate, ListEvent, ListItem, ListRow, ListRowState,
    ListSelection,
};

use crate::{ScrollView, ScrollViewState};

/// Scrollable data-driven list view.
pub struct ListView<DataSource, Delegate> {
    pub(super) scroll_view: ScrollView,
    pub(super) data_source: DataSource,
    pub(super) delegate: Delegate,
    pub(super) touch_started_inside: bool,
    pub(super) tap_candidate: bool,
    pub(super) highlighted_index: Option<usize>,
    pub(super) selected_index: Option<usize>,
    pub(super) allows_selection: bool,
}

impl<DataSource, Delegate> ListView<DataSource, Delegate> {
    /// Creates a list view from a data source and delegate.
    pub fn new(data_source: DataSource, delegate: Delegate) -> Self {
        Self {
            scroll_view: ScrollView::new(),
            data_source,
            delegate,
            touch_started_inside: false,
            tap_candidate: false,
            highlighted_index: None,
            selected_index: None,
            allows_selection: true,
        }
    }

    /// Returns the underlying scroll view.
    pub const fn scroll_view(&self) -> &ScrollView {
        &self.scroll_view
    }

    /// Returns the underlying scroll view mutably.
    pub fn scroll_view_mut(&mut self) -> &mut ScrollView {
        &mut self.scroll_view
    }

    /// Returns the underlying scroll state.
    pub const fn state(&self) -> &ScrollViewState {
        self.scroll_view.state()
    }

    /// Returns the underlying scroll state mutably.
    pub fn state_mut(&mut self) -> &mut ScrollViewState {
        self.scroll_view.state_mut()
    }

    /// Returns the list data source.
    pub const fn data_source(&self) -> &DataSource {
        &self.data_source
    }

    /// Returns the list data source mutably.
    pub fn data_source_mut(&mut self) -> &mut DataSource {
        &mut self.data_source
    }

    /// Returns the list delegate.
    pub const fn delegate(&self) -> &Delegate {
        &self.delegate
    }

    /// Returns the list delegate mutably.
    pub fn delegate_mut(&mut self) -> &mut Delegate {
        &mut self.delegate
    }

    /// Returns whether row selection is enabled.
    pub const fn allows_selection(&self) -> bool {
        self.allows_selection
    }

    /// Enables or disables row selection.
    pub fn set_allows_selection(&mut self, allows_selection: bool) {
        self.allows_selection = allows_selection;
        if !allows_selection {
            self.selected_index = None;
        }
    }
}