Skip to main content

ScrollViewState

Struct ScrollViewState 

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

State for a ScrollView component.

Contains the scroll position, content dimensions, and display options.

§Example

use envision::component::ScrollViewState;

let state = ScrollViewState::new()
    .with_content_height(50)
    .with_title("Preview");
assert_eq!(state.content_height(), 50);
assert_eq!(state.title(), Some("Preview"));
assert_eq!(state.scroll_offset(), 0);

Implementations§

Source§

impl ScrollViewState

Source

pub fn new() -> Self

Creates a new scroll view state with default settings.

The content height starts at 0, with no title, scrollbar enabled, unfocused, and enabled.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new();
assert_eq!(state.content_height(), 0);
assert_eq!(state.scroll_offset(), 0);
assert!(state.show_scrollbar());
assert_eq!(state.title(), None);
Source

pub fn with_content_height(self, height: u16) -> Self

Sets the total content height (builder pattern).

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_content_height(100);
assert_eq!(state.content_height(), 100);
Source

pub fn with_title(self, title: impl Into<String>) -> Self

Sets the title (builder pattern).

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_title("Preview");
assert_eq!(state.title(), Some("Preview"));
Source

pub fn with_show_scrollbar(self, show: bool) -> Self

Sets whether the scrollbar is shown (builder pattern).

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_show_scrollbar(false);
assert!(!state.show_scrollbar());
Source

pub fn content_height(&self) -> u16

Returns the total content height.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_content_height(50);
assert_eq!(state.content_height(), 50);
Source

pub fn set_content_height(&mut self, height: u16)

Sets the total content height.

Updates the scroll state’s content length accordingly.

§Example
use envision::component::ScrollViewState;

let mut state = ScrollViewState::new();
state.set_content_height(75);
assert_eq!(state.content_height(), 75);
Source

pub fn title(&self) -> Option<&str>

Returns the title.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_title("Output");
assert_eq!(state.title(), Some("Output"));
Source

pub fn set_title(&mut self, title: Option<String>)

Sets the title.

§Example
use envision::component::ScrollViewState;

let mut state = ScrollViewState::new();
state.set_title(Some("Output".to_string()));
assert_eq!(state.title(), Some("Output"));
state.set_title(None);
assert_eq!(state.title(), None);
Source

pub fn show_scrollbar(&self) -> bool

Returns whether the scrollbar is shown.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new();
assert!(state.show_scrollbar());
Source

pub fn set_show_scrollbar(&mut self, show: bool)

Sets whether the scrollbar is shown.

§Example
use envision::component::ScrollViewState;

let mut state = ScrollViewState::new();
state.set_show_scrollbar(false);
assert!(!state.show_scrollbar());
Source

pub fn scroll_offset(&self) -> usize

Returns the current scroll offset.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new();
assert_eq!(state.scroll_offset(), 0);
Source

pub fn set_scroll_offset(&mut self, offset: usize)

Sets the scroll offset directly.

The offset is clamped to the valid range based on the content height and viewport height.

§Example
use envision::component::ScrollViewState;

let mut state = ScrollViewState::new().with_content_height(100);
state.set_scroll_offset(50);
assert_eq!(state.scroll_offset(), 50);
Source

pub fn scroll_state(&self) -> &ScrollState

Returns a reference to the internal scroll state.

§Example
use envision::component::ScrollViewState;

let state = ScrollViewState::new().with_content_height(50);
assert_eq!(state.scroll_state().content_length(), 50);
Source

pub fn content_area(&self, area: Rect) -> Rect

Returns the visible content Rect accounting for the scroll offset.

This is the area inside the border where the parent should render content. The returned Rect has its y coordinate adjusted by the scroll offset, so the parent can use it directly to position content.

The border uses 1 cell on each side (top, bottom, left, right), so the inner area is smaller than the provided area by 2 in each dimension.

§Example
use envision::component::ScrollViewState;
use ratatui::prelude::Rect;

let state = ScrollViewState::new().with_content_height(100);
let area = Rect::new(0, 0, 40, 20);

let content = state.content_area(area);
// Inner area: x+1, y+1, width-2, height-2 = (1, 1, 38, 18)
assert_eq!(content.x, 1);
assert_eq!(content.y, 1);
assert_eq!(content.width, 38);
assert_eq!(content.height, 18);
Source

pub fn viewport_height(&self, area: Rect) -> u16

Returns the viewport height for the given render area.

This is the number of visible lines inside the bordered area.

§Example
use envision::component::ScrollViewState;
use ratatui::prelude::Rect;

let state = ScrollViewState::new();
let area = Rect::new(0, 0, 40, 20);
assert_eq!(state.viewport_height(area), 18);
Source

pub fn update(&mut self, msg: ScrollViewMessage) -> Option<()>

Updates the state with a message, returning any output.

§Example
use envision::component::{ScrollViewMessage, ScrollViewState};

let mut state = ScrollViewState::new().with_content_height(100);
state.update(ScrollViewMessage::ScrollDown);
assert_eq!(state.scroll_offset(), 1);

Trait Implementations§

Source§

impl Clone for ScrollViewState

Source§

fn clone(&self) -> ScrollViewState

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ScrollViewState

Source§

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

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

impl Default for ScrollViewState

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for ScrollViewState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ScrollViewState

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Serialize for ScrollViewState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ScrollViewState

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,