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
mutating method, so the same ListState can be reused across lists that
change size (menus, reward pools, deck views, …) without going stale.
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.
Implementations§
Source§impl ListState
impl ListState
Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
The current scroll offset (index of the first visible item/line).
Sourcepub const fn wrap(&self) -> SelectionWrap
pub const fn wrap(&self) -> SelectionWrap
How select_next/select_previous behave at the ends of the list. Defaults to
SelectionWrap::Clamp.
Sourcepub const fn set_wrap(&mut self, wrap: SelectionWrap)
pub const fn set_wrap(&mut self, wrap: SelectionWrap)
Sets how select_next/select_previous behave at the ends of the list.
Sourcepub const fn select(&mut self, index: Option<usize>)
pub const fn select(&mut self, index: Option<usize>)
Select an explicit index (or clear the selection with None).
Sourcepub const fn set_offset(&mut self, offset: usize)
pub const fn set_offset(&mut self, offset: usize)
Set the scroll offset directly.
Sourcepub const fn reset(&mut self)
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.
Sourcepub const fn ensure_visible(&mut self, visible_height: usize)
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.
Sourcepub fn scroll_by(&mut self, delta: i32)
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.
Sourcepub fn select_next(&mut self, len: usize)
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.
Sourcepub fn select_previous(&mut self, len: usize)
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.
Sourcepub fn select_first(&mut self, len: usize)
pub fn select_first(&mut self, len: usize)
Select the first item, or clear the selection if len is zero.
Sourcepub fn select_last(&mut self, len: usize)
pub fn select_last(&mut self, len: usize)
Select the last item, or clear the selection if len is zero.