5 releases

new 0.11.2 Jun 15, 2024
0.11.1 Jun 4, 2024
0.11.0 May 30, 2024
0.10.1 May 29, 2024
0.10.0 May 18, 2024

#377 in Command-line interface

Download history 164/week @ 2024-05-13 41/week @ 2024-05-20 272/week @ 2024-05-27 157/week @ 2024-06-03

634 downloads per month
Used in 3 crates (via rat-widget)

MIT/Apache

63KB
1K SLoC

Scrolled and Viewport widgets

Requirements for Scrolled

There are two traits for the widget that should be scrolled.

For the widget struct:

pub trait ScrollingWidget<State> {
    /// Widget wants a (horizontal, vertical) scrollbar.
    fn need_scroll(&self, area: Rect, state: &mut State) -> (bool, bool);
}

For the widget state:

pub trait ScrollingState {
    /// Maximum offset that is accessible with scrolling.
    ///
    /// This is shorter than the length of the content by whatever fills the last page.
    /// This is the base for the scrollbar content_length.
    fn vertical_max_offset(&self) -> usize;
    /// Current vertical offset.
    fn vertical_offset(&self) -> usize;
    /// Vertical page-size at the current offset.
    fn vertical_page(&self) -> usize;

    /// Maximum offset that is accessible with scrolling.
    ///
    /// This is shorter than the length of the content by whatever fills the last page.
    /// This is the base for the scrollbar content_length.
    fn horizontal_max_offset(&self) -> usize;
    /// Current horizontal offset.
    fn horizontal_offset(&self) -> usize;
    /// Horizontal page-size at the current offset.
    fn horizontal_page(&self) -> usize;

    /// Change the vertical offset.
    ///
    /// Due to overscroll it's possible that this is an invalid offset for the widget.
    /// The widget must deal with this situation.
    ///
    /// The widget returns true if the offset changed at all.
    fn set_vertical_offset(&mut self, offset: usize) -> bool;

    /// Change the horizontal offset.
    ///
    /// Due to overscroll it's possible that this is an invalid offset for the widget.
    /// The widget must deal with this situation.
    ///
    /// The widget returns true if the offset changed at all.
    fn set_horizontal_offset(&mut self, offset: usize) -> bool;
}

Widget Scrolled

Does the scrolling and display of the ScrollBars.

Widget View and Viewport

Create a separate buffer, render the widget to this temp buffer. Then apply a col/row based offset and copy the temp buffer to the actual one.

The reason for two of those is: I can't impl the StatefulWidget trait in a way that it works with both Widget and StatefulWidget. So View works for Widget and Viewport for StatefulWidget.

There are convenience methods in Scrolled to add a View/Viewport.

Dependencies

~6–12MB
~118K SLoC