pub struct ScrollState { /* private fields */ }Expand description
Tracks scroll position for virtual scrolling in TUI components.
ScrollState is a composable building block that any component can
embed in its state to gain scroll offset tracking, visible range
calculation, and scrollbar rendering support.
It supports two usage patterns:
- Offset mode: The scroll offset is controlled directly via
scroll_up,scroll_down, etc. Used by LogViewer, ChatView, ScrollableText. - Selection mode: The scroll offset follows a selected index via
ensure_visible, keeping it within the viewport. Used by SelectableList, Table, DataGrid, etc.
§Example
use envision::scroll::ScrollState;
let mut scroll = ScrollState::new(500);
scroll.set_viewport_height(20);
assert_eq!(scroll.offset(), 0);
assert!(scroll.can_scroll());
assert!(scroll.at_start());
scroll.scroll_down();
assert_eq!(scroll.offset(), 1);
scroll.scroll_to_end();
assert_eq!(scroll.offset(), 480); // 500 - 20Implementations§
Source§impl ScrollState
impl ScrollState
Sourcepub fn new(content_length: usize) -> Self
pub fn new(content_length: usize) -> Self
Creates a new ScrollState with the given content length.
The scroll offset starts at 0 and viewport height defaults to 0
(set it in view() once the render area is known).
§Example
use envision::scroll::ScrollState;
let scroll = ScrollState::new(100);
assert_eq!(scroll.content_length(), 100);
assert_eq!(scroll.offset(), 0);Sourcepub fn set_content_length(&mut self, len: usize)
pub fn set_content_length(&mut self, len: usize)
Sets the total number of scrollable items or lines.
If the current offset exceeds the new maximum, it is clamped.
Sourcepub fn set_viewport_height(&mut self, height: usize)
pub fn set_viewport_height(&mut self, height: usize)
Sets the viewport height (number of visible items).
Typically called in view() after computing the inner render area.
If the current offset exceeds the new maximum, it is clamped.
Sourcepub fn set_offset(&mut self, offset: usize)
pub fn set_offset(&mut self, offset: usize)
Sets the scroll offset directly, clamped to the valid range.
Sourcepub fn scroll_up(&mut self) -> bool
pub fn scroll_up(&mut self) -> bool
Scrolls up by one item. Returns true if the offset changed.
Sourcepub fn scroll_down(&mut self) -> bool
pub fn scroll_down(&mut self) -> bool
Scrolls down by one item. Returns true if the offset changed.
Sourcepub fn scroll_up_by(&mut self, n: usize) -> bool
pub fn scroll_up_by(&mut self, n: usize) -> bool
Scrolls up by n items. Returns true if the offset changed.
Sourcepub fn scroll_down_by(&mut self, n: usize) -> bool
pub fn scroll_down_by(&mut self, n: usize) -> bool
Scrolls down by n items. Returns true if the offset changed.
Sourcepub fn page_up(&mut self, page_size: usize) -> bool
pub fn page_up(&mut self, page_size: usize) -> bool
Scrolls up by one page (viewport height). Returns true if the offset changed.
Sourcepub fn page_down(&mut self, page_size: usize) -> bool
pub fn page_down(&mut self, page_size: usize) -> bool
Scrolls down by one page (viewport height). Returns true if the offset changed.
Sourcepub fn scroll_to_start(&mut self) -> bool
pub fn scroll_to_start(&mut self) -> bool
Scrolls to the first item. Returns true if the offset changed.
Sourcepub fn scroll_to_end(&mut self) -> bool
pub fn scroll_to_end(&mut self) -> bool
Scrolls to the last page. Returns true if the offset changed.
Sourcepub fn ensure_visible(&mut self, index: usize) -> bool
pub fn ensure_visible(&mut self, index: usize) -> bool
Adjusts the scroll offset to ensure the given index is visible.
If the index is above the viewport, scrolls up. If below, scrolls
down. If already visible, does nothing. Returns true if the
offset changed.
This is the key method for selection-based components: after changing the selected index, call this to keep the selection in view.
§Example
use envision::scroll::ScrollState;
let mut scroll = ScrollState::new(100);
scroll.set_viewport_height(10);
// Selection at item 25 — scrolls viewport to show it
assert!(scroll.ensure_visible(25));
assert_eq!(scroll.offset(), 16); // 25 - 10 + 1
// Selection at item 20 — already visible, no change
assert!(!scroll.ensure_visible(20));Sourcepub fn offset(&self) -> usize
pub fn offset(&self) -> usize
Returns the current scroll offset (index of the first visible item).
Sourcepub fn content_length(&self) -> usize
pub fn content_length(&self) -> usize
Returns the total content length.
Sourcepub fn viewport_height(&self) -> usize
pub fn viewport_height(&self) -> usize
Returns the viewport height.
Sourcepub fn visible_range(&self) -> Range<usize>
pub fn visible_range(&self) -> Range<usize>
Returns the range of visible item indices.
The range is offset..min(offset + viewport_height, content_length).
§Example
use envision::scroll::ScrollState;
let mut scroll = ScrollState::new(50);
scroll.set_viewport_height(10);
assert_eq!(scroll.visible_range(), 0..10);
scroll.scroll_to_end();
assert_eq!(scroll.visible_range(), 40..50);Sourcepub fn max_offset(&self) -> usize
pub fn max_offset(&self) -> usize
Returns the maximum valid scroll offset.
This is content_length.saturating_sub(viewport_height).
Sourcepub fn can_scroll(&self) -> bool
pub fn can_scroll(&self) -> bool
Returns true if scrolling is possible (content exceeds viewport).
Sourcepub fn scrollbar_state(&self) -> ScrollbarState
pub fn scrollbar_state(&self) -> ScrollbarState
Creates a ratatui ScrollbarState configured for this scroll state.
Use this with ratatui’s Scrollbar widget for visual scroll
indicators.
Trait Implementations§
Source§impl Clone for ScrollState
impl Clone for ScrollState
Source§fn clone(&self) -> ScrollState
fn clone(&self) -> ScrollState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ScrollState
impl Debug for ScrollState
Source§impl Default for ScrollState
impl Default for ScrollState
Source§fn default() -> ScrollState
fn default() -> ScrollState
Source§impl<'de> Deserialize<'de> for ScrollState
impl<'de> Deserialize<'de> for ScrollState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for ScrollState
impl PartialEq for ScrollState
Source§impl Serialize for ScrollState
impl Serialize for ScrollState
impl Eq for ScrollState
impl StructuralPartialEq for ScrollState
Auto Trait Implementations§
impl Freeze for ScrollState
impl RefUnwindSafe for ScrollState
impl Send for ScrollState
impl Sync for ScrollState
impl Unpin for ScrollState
impl UnsafeUnpin for ScrollState
impl UnwindSafe for ScrollState
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more