Struct ScrollView

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

A widget that can scroll its contents

Allows you to render a widget into a buffer larger than the area it is rendered into, and then scroll the contents of that buffer around.

Note that the origin of the buffer is always at (0, 0), and the buffer is always the size of the size passed to new. The ScrollView widget itself is responsible for rendering the visible area of the buffer into the main buffer.

§Examples

use ratatui::{prelude::*, layout::Size, widgets::*};
use tui_scrollview::{ScrollView, ScrollViewState};

let mut scroll_view = ScrollView::new(Size::new(20, 20));

// render a few widgets into the buffer at various positions
scroll_view.render_widget(Paragraph::new("Hello, world!"), Rect::new(0, 0, 20, 1));
scroll_view.render_widget(Paragraph::new("Hello, world!"), Rect::new(10, 10, 20, 1));
scroll_view.render_widget(Paragraph::new("Hello, world!"), Rect::new(15, 15, 20, 1));

// You can also render widgets into the buffer programmatically
Line::raw("Hello, world!").render(Rect::new(0, 0, 20, 1), scroll_view.buf_mut());

// usually you would store the state of the scroll view in a struct that implements
// StatefulWidget (or in your app state if you're using an `App` struct)
let mut state = ScrollViewState::default();

// you can also scroll the view programmatically
state.scroll_down();

// render the scroll view into the main buffer at the given position within a widget
let scroll_view_area = Rect::new(0, 0, 10, 10);
scroll_view.render(scroll_view_area, buf, &mut state);
// or if you're rendering in a terminal draw closure instead of from within another widget:
frame.render_stateful_widget(scroll_view, frame.size(), state);

Implementations§

Source§

impl ScrollView

Source

pub fn new(size: Size) -> Self

Create a new scroll view with a buffer of the given size

The buffer will be empty, with coordinates ranging from (0, 0) to (size.width, size.height).

Source

pub fn size(&self) -> Size

The content size of the scroll view

Source

pub fn area(&self) -> Rect

The area of the buffer that is available to be scrolled

Source

pub fn buf(&self) -> &Buffer

The buffer containing the contents of the scroll view

Source

pub fn buf_mut(&mut self) -> &mut Buffer

The mutable buffer containing the contents of the scroll view

This can be used to render widgets into the buffer programmatically

§Examples

let mut scroll_view = ScrollView::new(Size::new(20, 20));
Line::raw("Hello, world!").render(Rect::new(0, 0, 20, 1), scroll_view.buf_mut());
Source

pub fn vertical_scrollbar_visibility( self, visibility: ScrollbarVisibility, ) -> Self

Set the visibility of the vertical scrollbar

See ScrollbarVisibility for all the options.

This is a fluent setter method which must be chained or used as it consumes self

§Examples

let mut scroll_view = ScrollView::new(Size::new(20, 20)).vertical_scrollbar_visibility(ScrollbarVisibility::Always);
Source

pub fn horizontal_scrollbar_visibility( self, visibility: ScrollbarVisibility, ) -> Self

Set the visibility of the horizontal scrollbar

See ScrollbarVisibility for all the options.

This is a fluent setter method which must be chained or used as it consumes self

§Examples

let mut scroll_view = ScrollView::new(Size::new(20, 20)).horizontal_scrollbar_visibility(ScrollbarVisibility::Never);
Source

pub fn scrollbars_visibility(self, visibility: ScrollbarVisibility) -> Self

Set the visibility of both vertical and horizontal scrollbars

See ScrollbarVisibility for all the options.

This is a fluent setter method which must be chained or used as it consumes self

§Examples

let mut scroll_view = ScrollView::new(Size::new(20, 20)).scrollbars_visibility(ScrollbarVisibility::Automatic);
Source

pub fn render_widget<W: Widget>(&mut self, widget: W, area: Rect)

Render a widget into the scroll buffer

This is the equivalent of Frame::render_widget, but renders the widget into the scroll buffer rather than the main buffer. The widget will be rendered into the area of the buffer specified by the area parameter.

This should not be confused with the render method, which renders the visible area of the ScrollView into the main buffer.

Trait Implementations§

Source§

impl Clone for ScrollView

Source§

fn clone(&self) -> ScrollView

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 ScrollView

Source§

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

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

impl Default for ScrollView

Source§

fn default() -> ScrollView

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

impl Hash for ScrollView

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ScrollView

Source§

fn eq(&self, other: &ScrollView) -> 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 StatefulWidget for ScrollView

Source§

type State = ScrollViewState

State associated with the stateful widget. Read more
Source§

fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom stateful widget.
Source§

impl Eq for ScrollView

Source§

impl StructuralPartialEq for ScrollView

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> 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.