Skip to main content

ansiq_widgets/
scroll_view.rs

1use ansiq_core::{Element, ElementKind, Layout, Length, ScrollHandler, ScrollViewProps};
2
3pub struct ScrollView<Message = ()> {
4    child: Option<Element<Message>>,
5    follow_bottom: bool,
6    offset: Option<usize>,
7    on_scroll: Option<ScrollHandler<Message>>,
8    layout: Layout,
9    focusable: bool,
10}
11
12impl<Message> ScrollView<Message> {
13    pub fn new() -> Self {
14        Self {
15            child: None,
16            follow_bottom: false,
17            offset: None,
18            on_scroll: None,
19            layout: Layout {
20                width: Length::Fill,
21                height: Length::Fill,
22            },
23            focusable: false,
24        }
25    }
26    pub fn follow_bottom(mut self, follow_bottom: bool) -> Self {
27        self.follow_bottom = follow_bottom;
28        self
29    }
30
31    pub fn offset(mut self, offset: usize) -> Self {
32        self.offset = Some(offset);
33        self
34    }
35
36    pub fn on_scroll<F>(mut self, handler: F) -> Self
37    where
38        F: FnMut(usize) -> Option<Message> + 'static,
39    {
40        self.on_scroll = Some(std::boxed::Box::new(handler));
41        self.focusable = true;
42        self
43    }
44
45    pub fn child(mut self, child: Element<Message>) -> Self {
46        self.child = Some(child);
47        self
48    }
49
50    pub fn layout(mut self, layout: Layout) -> Self {
51        self.layout = layout;
52        self
53    }
54
55    pub fn build(self) -> Element<Message> {
56        Element::new(ElementKind::ScrollView(ScrollViewProps {
57            follow_bottom: self.follow_bottom,
58            offset: self.offset,
59            on_scroll: self.on_scroll,
60        }))
61        .with_layout(self.layout)
62        .with_focusable(self.focusable)
63        .with_children(self.child.into_iter().collect())
64    }
65}