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
impl ScrollView
Sourcepub fn new(size: Size) -> Self
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).
Sourcepub fn buf_mut(&mut self) -> &mut Buffer
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());
Sourcepub fn vertical_scrollbar_visibility(
self,
visibility: ScrollbarVisibility,
) -> Self
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);
Sourcepub fn horizontal_scrollbar_visibility(
self,
visibility: ScrollbarVisibility,
) -> Self
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);
Sourcepub fn scrollbars_visibility(self, visibility: ScrollbarVisibility) -> Self
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);
Sourcepub fn render_widget<W: Widget>(&mut self, widget: W, area: Rect)
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
impl Clone for ScrollView
Source§fn clone(&self) -> ScrollView
fn clone(&self) -> ScrollView
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for ScrollView
impl Debug for ScrollView
Source§impl Default for ScrollView
impl Default for ScrollView
Source§fn default() -> ScrollView
fn default() -> ScrollView
Source§impl Hash for ScrollView
impl Hash for ScrollView
Source§impl PartialEq for ScrollView
impl PartialEq for ScrollView
Source§impl StatefulWidget for ScrollView
impl StatefulWidget for ScrollView
impl Eq for ScrollView
impl StructuralPartialEq for ScrollView
Auto Trait Implementations§
impl Freeze for ScrollView
impl RefUnwindSafe for ScrollView
impl Send for ScrollView
impl Sync for ScrollView
impl Unpin for ScrollView
impl UnwindSafe for ScrollView
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