Skip to content

Commit

Permalink
Add scroll-view example (and fix small issues) (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-M authored Mar 10, 2024
1 parent ce9863f commit dd24a29
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
65 changes: 65 additions & 0 deletions examples/scroll_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use std::sync::Arc;

use anyhow::Result;
use ratatui::style::Color;
use trui::*;

fn main() -> Result<()> {
let entry = |num| {
weighted_h_stack((
format!("{num}").weight(0.05),
"Avatar"
.border(BorderKind::Straight)
.fill_max_height(1.0) // TODO not working
.weight(0.3),
v_stack((
"Description"
.fg(Color::Red)
.margin((2, Position::LEFT))
.margin((1, Position::VERTICAL)),
"Lorem ipsum dolor sit amet,\n\
consectetur adipiscing elit,\n\
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n\
Ut enim ad minim veniam,\n\
quis nostrud exercitation ullamco laboris nisi ut aliquip \
ex ea commodo consequat.\n\
Duis aute irure dolor in reprehenderit in voluptate velit esse \
cillum dolore eu fugiat nulla pariatur.\n\
Excepteur sint occaecat cupidatat non proident,\n\
sunt in culpa qui officia deserunt mollit anim id est laborum."
.fg(Color::Blue),
)),
))
.border(BorderKind::Rounded)
// .margin(1)
};

fn list_with_every_nth_red_otherwise_blue<T, A>(
n: usize,
views: impl Iterator<Item = impl View<T, A>>,
) -> Vec<impl View<T, A>> {
views
.enumerate()
.map(|(i, v)| {
if i % n == 0 {
OneOf2::A(
v.border(Borders::VERTICAL)
.fg(Color::Red)
.on_hover_bg(Color::Magenta),
)
} else {
OneOf2::B(
v.border(Borders::VERTICAL)
.fg(Color::Blue)
.on_hover_bg(Color::Yellow),
)
}
})
.collect()
}
let list = list_with_every_nth_red_otherwise_blue(2, (0..10).map(entry));
let scroll_view = Arc::new(scroll_view(v_stack(list)));

// let scroll_view = Arc::new(scroll_view());
App::new((), move |()| scroll_view.clone()).run()
}
1 change: 1 addition & 0 deletions src/view/weighted_linear_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use std::{any::Any, marker::PhantomData};
use xilem_core::{Id, MessageResult, VecSplice};

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct WeightedLinearLayout<T, A, VT> {
children: VT,
axis: Axis,
Expand Down
2 changes: 1 addition & 1 deletion src/widget/scroll_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Widget for ScrollView {
// TODO: scroll wheel + click-drag on scroll bars
let child_event = if let Event::Mouse(mouse_event) = event {
let mut mouse_event = *mouse_event;
mouse_event.column += self.offset.round() as i16;
mouse_event.row += self.offset.round() as i16;
Event::Mouse(mouse_event)
} else {
event.clone()
Expand Down

0 comments on commit dd24a29

Please sign in to comment.