Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added render_stateful_widget method to ScrollView #65

Merged
merged 2 commits into from
Mar 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tui-scrollview/src/scroll_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@
pub fn render_widget<W: Widget>(&mut self, widget: W, area: Rect) {
widget.render(area, &mut self.buf);
}

/// Render a stateful widget into the scroll buffer
///
/// This is the equivalent of `Frame::render_stateful_widget`, but renders the stateful widget
/// into the scroll buffer rather than the main buffer. The stateful 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.

Check warning on line 199 in tui-scrollview/src/scroll_view.rs

View workflow job for this annotation

GitHub Actions / check / stable / clippy

warning: empty line after doc comment --> tui-scrollview/src/scroll_view.rs:198:5 | 198 | / /// ScrollView into the main buffer. 199 | | | |_^ 200 | / pub fn render_stateful_widget<W: StatefulWidget>( 201 | | &mut self, 202 | | widget: W, 203 | | area: Rect, 204 | | state: &mut W::State, 205 | | ) { | |_____- the comment documents this method | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default = help: if the empty line is unintentional remove it

Check warning on line 199 in tui-scrollview/src/scroll_view.rs

View workflow job for this annotation

GitHub Actions / check / beta / clippy

warning: empty line after doc comment --> tui-scrollview/src/scroll_view.rs:198:5 | 198 | / /// ScrollView into the main buffer. 199 | | | |_^ 200 | / pub fn render_stateful_widget<W: StatefulWidget>( 201 | | &mut self, 202 | | widget: W, 203 | | area: Rect, 204 | | state: &mut W::State, 205 | | ) { | |_____- the comment documents this method | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#empty_line_after_doc_comments = note: `#[warn(clippy::empty_line_after_doc_comments)]` on by default = help: if the empty line is unintentional remove it
pub fn render_stateful_widget<W: StatefulWidget>(
&mut self,
widget: W,
area: Rect,
state: &mut W::State,
) {
widget.render(area, &mut self.buf, state);
}
}

impl StatefulWidget for ScrollView {
Expand Down Expand Up @@ -762,4 +780,28 @@
])
)
}

#[rstest]
#[rustfmt::skip]
fn render_stateful_widget(mut scroll_view: ScrollView) {
use ratatui::widgets::{List, ListState};
scroll_view = scroll_view.horizontal_scrollbar_visibility(ScrollbarVisibility::Never);
let mut buf = Buffer::empty(Rect::new(0, 0, 7, 5));
let mut state = ScrollViewState::default();
let mut list_state = ListState::default();
let items: Vec<String> = (1..=10).map(|i| format!("Item {}", i)).collect();
let list = List::new(items);
scroll_view.render_stateful_widget(list, scroll_view.area(), &mut list_state);
scroll_view.render(buf.area, &mut buf, &mut state);
assert_eq!(
buf,
Buffer::with_lines(vec![
"Item 1▲",
"Item 2█",
"Item 3█",
"Item 4║",
"Item 5▼",
])
)
}
}
Loading