Skip to main content

ListState

Struct ListState 

Source
pub struct ListState { /* private fields */ }
Expand description

Selection index and scroll offset for a selectable, scrollable list.

Holds no reference to the list’s actual items: len is passed in to each selection-movement method, so select_next/select_previous/select_first/select_last and the scroll offset stay valid across lists that change size (menus, reward pools, deck views, …). A selection set directly with select is the exception: it is stored unchecked and can outlive a shrunk list. See that method.

Selection movement clamps at len’s ends by default; see SelectionWrap (set via ListState::set_wrap) to switch to wraparound instead. Scrolling is a separate, unbounded-above counter (clamped only at zero) since only the caller knows the content length and viewport height needed to clamp it from above.

offset is always a whole row: there’s no momentum, velocity, or sub-row position here. For continuous/pixel-ish scrolling with momentum and rubber-banding (a smoothly-scrolled log or panel, not a discrete item list), reach for crate::ScrollState instead, and drive this type’s offset from crate::ScrollState::integer_offset if a list needs both a selection cursor and smooth scrolling together.

Implementations§

Source§

impl ListState

Source

pub const fn new() -> Self

An empty state: nothing selected, no scroll.

Source

pub const fn selected(&self) -> Option<usize>

The currently selected index, if any.

Source

pub const fn offset(&self) -> usize

The current scroll offset (index of the first visible item/line).

Source

pub const fn wrap(&self) -> SelectionWrap

How select_next/select_previous behave at the ends of the list. Defaults to SelectionWrap::Clamp.

Source

pub const fn set_wrap(&mut self, wrap: SelectionWrap)

Sets how select_next/select_previous behave at the ends of the list.

Source

pub const fn select(&mut self, index: Option<usize>)

Select an explicit index, or clear the selection with None.

The index is stored verbatim and is not bounds-checked against any list length: unlike select_next/select_previous, this does not clamp. If the list later shrinks below a stored index, selected() keeps returning that now-out-of-range index (ensure_visible will not fix it, since it never sees len). Callers indexing their items by selected() must bound-check it against the current length first, or re-anchor with select_first/select_last after the list changes size.

Source

pub const fn set_offset(&mut self, offset: usize)

Set the scroll offset directly.

Source

pub const fn reset(&mut self)

Clear both the selection and the scroll offset, e.g. after the underlying list has been replaced with different content.

Source

pub const fn ensure_visible(&mut self, visible_height: usize)

Nudge the scroll offset by the minimum amount needed to bring selected into the visible_height-row window starting at offset.

A no-op if nothing is selected, visible_height is zero, or the selection is already visible. Call this once per frame before rendering (with the actual, current viewport height, since that can change on terminal resize) rather than only after moving the selection: it’s cheap and idempotent, so redoing it every frame costs nothing and needs no special-casing for resize.

Source

pub fn scroll_by(&mut self, delta: i32)

Move the scroll offset by delta, clamped at zero. There is no upper clamp here: only the caller knows the content length and viewport height needed to bound it from above.

Source

pub fn select_next(&mut self, len: usize)

Select the next item. Past the last item, clamps (stays on the last item) or wraps to the first, per wrap(). Selects index 0 if nothing was selected yet. No-op (clears the selection) if len is zero.

Source

pub fn select_previous(&mut self, len: usize)

Select the previous item. Before the first item, clamps (stays on the first item) or wraps to the last, per wrap(). Selects the last item if nothing was selected yet. No-op (clears the selection) if len is zero.

Source

pub fn select_first(&mut self, len: usize)

Select the first item, or clear the selection if len is zero.

Source

pub fn select_last(&mut self, len: usize)

Select the last item, or clear the selection if len is zero.

Trait Implementations§

Source§

impl Clone for ListState

Source§

fn clone(&self) -> ListState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ListState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ListState

Source§

fn default() -> ListState

Returns the “default value” for a type. Read more
Source§

impl PartialEq for ListState

Source§

fn eq(&self, other: &ListState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for ListState

Source§

impl Eq for ListState

Source§

impl StructuralPartialEq for ListState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<C> WithAlpha for C
where C: Copy,

§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see [AlphaFirst]).
§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see [AlphaLast]).