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
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.
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.
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.
Trait Implementations§
impl Copy for ListState
impl Eq for ListState
impl StructuralPartialEq for ListState
Auto Trait Implementations§
impl Freeze for ListState
impl RefUnwindSafe for ListState
impl Send for ListState
impl Sync for ListState
impl Unpin for ListState
impl UnsafeUnpin for ListState
impl UnwindSafe for ListState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<C> WithAlpha for Cwhere
C: Copy,
impl<C> WithAlpha for Cwhere
C: Copy,
§fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
self with alpha, storing alpha before the color in memory
(see [AlphaFirst]).§fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
self with alpha, storing alpha after the color in memory
(see [AlphaLast]).