Skip to main content

mitsuami_widgets/
scroll.rs

1//! Scroll views.
2
3use 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
10/// A native scroll container. Its children go into a content box that
11/// keeps its natural size, so it can be larger than the scroll view.
12///
13/// Give the scroll view a bounded size (a fixed height, or `grow` inside a
14/// sized parent); otherwise it grows with its content and never scrolls.
15/// As in CSS, its natural size is its content's, so in a flex container its
16/// siblings shrink along with it unless they have `.shrink(0.0)`.
17/// Also as in CSS, a flex item doesn't shrink below its content's width:
18/// a horizontal scroll view in a column that grows in a row needs
19/// `.min_width(0)` on that column, or both are as wide as the content, and
20/// there's nothing to scroll.
21pub 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    /// Scrolls vertically.
47    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    /// The axes it scrolls along. In `view!`, where there's no constructor
65    /// to pick them: `<ScrollView axes=ScrollAxes::Horizontal>`.
66    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        // The content stretches across the non-scrolling axis and keeps its
71        // natural size along the scrolling ones.
72        self.outer.style.flex_direction =
73            if axes == ScrollAxes::Horizontal { FlexDirection::Row } else { FlexDirection::Column };
74        // Horizontal content flows in a row; the other kinds in a column.
75        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    /// Whether it shows scroll bars, on by default. They're the platform's,
92    /// shown as it shows them (overlay bars, say, as the user set on
93    /// macOS). Without, it still scrolls by wheel, trackpad and touch, as a
94    /// strip of photos or a carousel does.
95    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    /// Raw platform settings, past the semantic ones: see [`Tweak`]. What
101    /// the platforms offer (elasticity and a border on AppKit, classic
102    /// scroll bars on GTK, overshoot on Qt, always-expanded scroll bars and
103    /// zoom on WinUI) is each one's own.
104    pub fn native(mut self, tweak: Tweak<ScrollView>) -> ScrollView {
105        tweak.apply(&mut self.outer);
106        self
107    }
108
109    /// Called with the new offset whenever the content scrolls.
110    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}