mitsuami_widgets/
scroll.rs1use crate::Container;
4use mitsuami_core::{
5 Align, Children, Element, ElementBuilder, FlexDirection, NodeId, Point, Prop, ScrollAxes, Tweak, Ui, UiEvent, View,
6 WidgetKind,
7};
8use mitsuami_reactive::IntoValue;
9
10pub struct ScrollView {
22 outer: Element,
23 content: Container,
24}
25
26impl ElementBuilder for ScrollView {
27 fn element(&mut self) -> &mut Element {
28 &mut self.outer
29 }
30}
31
32impl View for ScrollView {
33 fn build(mut self, ui: &Ui) -> NodeId {
34 self.outer.add_children(self.content);
35 self.outer.build(ui)
36 }
37}
38
39impl Default for ScrollView {
40 fn default() -> ScrollView {
41 ScrollView::new()
42 }
43}
44
45impl ScrollView {
46 pub fn new() -> ScrollView {
48 ScrollView::with_axes(ScrollAxes::Vertical)
49 }
50
51 pub fn horizontal() -> ScrollView {
52 ScrollView::with_axes(ScrollAxes::Horizontal)
53 }
54
55 pub fn both() -> ScrollView {
56 ScrollView::with_axes(ScrollAxes::Both)
57 }
58
59 fn with_axes(axes: ScrollAxes) -> ScrollView {
60 let outer = Element::new(WidgetKind::ScrollView);
61 ScrollView { outer, content: Container::new().shrink(0.0) }.axes(axes)
62 }
63
64 pub fn axes(mut self, axes: ScrollAxes) -> ScrollView {
67 self.outer.prop(axes.into_value(), Prop::ScrollAxes);
68 self.outer.style.scroll_x = axes.horizontal();
69 self.outer.style.scroll_y = axes.vertical();
70 self.outer.style.flex_direction =
73 if axes == ScrollAxes::Horizontal { FlexDirection::Row } else { FlexDirection::Column };
74 let content = &mut self.content.0.style;
76 content.flex_direction =
77 if axes == ScrollAxes::Horizontal { FlexDirection::Row } else { FlexDirection::Column };
78 content.align_self = (axes == ScrollAxes::Both).then_some(Align::Start);
79 self
80 }
81
82 pub fn children(mut self, children: impl Children) -> ScrollView {
83 self.content = self.content.children(children);
84 self
85 }
86
87 pub fn child(self, child: impl View) -> ScrollView {
88 self.children(child)
89 }
90
91 pub fn scroll_bars(mut self, show: impl IntoValue<bool>) -> ScrollView {
96 self.outer.prop(show.into_value(), Prop::ScrollBars);
97 self
98 }
99
100 pub fn native(mut self, tweak: Tweak<ScrollView>) -> ScrollView {
105 tweak.apply(&mut self.outer);
106 self
107 }
108
109 pub fn on_scroll(mut self, handler: impl Fn(Point) + 'static) -> ScrollView {
111 self.outer.on(move |event| {
112 if let UiEvent::Scrolled(offset) = event {
113 handler(*offset);
114 }
115 });
116 self
117 }
118}
119
120impl ScrollView {
121 #[doc(hidden)]
122 pub fn __tag() -> ScrollView {
123 ScrollView::new()
124 }
125
126 #[doc(hidden)]
127 pub fn __children<C: Children>(self, children: impl FnOnce() -> C) -> ScrollView {
128 self.children(children())
129 }
130}