Skip to main content

repose_ui/
scroll.rs

1//! # Scroll model
2//!
3//! Repose separates visual scroll containers from scroll state.
4//!
5//! This file implements scroll composables (wrappers that attach scroll bindings
6//! as modifiers). The actual scroll state types live in `repose_core::scroll`.
7//!
8//! Velocities are expressed in px/sec and integrated with dt,
9//! so behavior is frame-rate independent.
10
11use crate::ViewExt;
12pub use repose_core::scroll::{HorizontalScrollState, ScrollPhysics, ScrollState, ScrollStateXY};
13use repose_core::*;
14use std::rc::Rc;
15
16/// Remembered ScrollState (requires unique key).
17pub fn remember_scroll_state(key: impl Into<String>) -> Rc<ScrollState> {
18    repose_core::remember_with_key(key.into(), ScrollState::new)
19}
20
21pub fn remember_horizontal_scroll_state(key: impl Into<String>) -> Rc<HorizontalScrollState> {
22    repose_core::remember_with_key(key.into(), HorizontalScrollState::new)
23}
24
25pub fn remember_scroll_state_xy(key: impl Into<String>) -> Rc<ScrollStateXY> {
26    repose_core::remember_with_key(key.into(), ScrollStateXY::new)
27}
28
29/// Scroll container with inertia, like verticalScroll.
30///
31/// Content is wrapped so it:
32/// - does not flex-shrink,
33/// - is at least as wide/tall as the viewport,
34/// - grows with content on the scroll axis.
35///
36/// Prefer a stable `remember_scroll_state(key)` and put `scope!` / `repaint_boundary`
37/// around expensive list bodies.
38pub fn ScrollArea(modifier: Modifier, state: Rc<ScrollState>, content: View) -> View {
39    let binding = state.to_binding();
40    let on_scroll_with_source = match &binding {
41        ScrollBinding::Vertical(b) => b.on_scroll_with_source.clone(),
42        _ => unreachable!(),
43    };
44    let modifier = modifier.vertical_scroll(match binding {
45        ScrollBinding::Vertical(b) => b,
46        _ => unreachable!(),
47    });
48    let modifier = match on_scroll_with_source {
49        Some(handler) => modifier.on_scroll_with_source(move |d, s| handler(d, s)),
50        None => modifier,
51    };
52    let content = crate::Box(
53        Modifier::new()
54            .flex_shrink(0.0)
55            // Fill viewport width so short content still spans the scroller;
56            // height stays auto so content grows vertically.
57            .fill_max_width(),
58    )
59    .child(content);
60
61    View::new(0, ViewKind::Box)
62        .modifier(modifier)
63        .with_children(vec![content])
64}
65
66/// Horizontal scroll container with inertia.
67///
68/// Content is wrapped so it does not flex-shrink, is at least as tall as the
69/// viewport, and grows with content horizontally.
70pub fn HorizontalScrollArea(
71    modifier: Modifier,
72    state: Rc<HorizontalScrollState>,
73    content: View,
74) -> View {
75    let binding = state.to_binding();
76    let on_scroll_with_source = match &binding {
77        ScrollBinding::Horizontal(b) => b.on_scroll_with_source.clone(),
78        _ => unreachable!(),
79    };
80    let modifier = modifier.horizontal_scroll(match binding {
81        ScrollBinding::Horizontal(b) => b,
82        _ => unreachable!(),
83    });
84    let modifier = match on_scroll_with_source {
85        Some(handler) => modifier.on_scroll_with_source(move |d, s| handler(d, s)),
86        None => modifier,
87    };
88    let content = crate::Box(
89        Modifier::new()
90            .flex_shrink(0.0)
91            // Fill viewport height so short content still spans the scroller;
92            // width stays auto so content grows horizontally.
93            .fill_max_height(),
94    )
95    .child(content);
96
97    View::new(0, ViewKind::Box)
98        .modifier(modifier)
99        .with_children(vec![content])
100}
101
102/// Two-axis scroll container with inertia.
103///
104/// Content is wrapped so it does not flex-shrink and grows with content on
105/// both axes.
106pub fn ScrollAreaXY(modifier: Modifier, state: Rc<ScrollStateXY>, content: View) -> View {
107    let binding = state.to_binding();
108    let on_scroll_with_source = match &binding {
109        ScrollBinding::Both(b) => b.on_scroll_with_source.clone(),
110        _ => unreachable!(),
111    };
112    let modifier = modifier.scrollable(match binding {
113        ScrollBinding::Both(b) => b,
114        _ => unreachable!(),
115    });
116    let modifier = match on_scroll_with_source {
117        Some(handler) => modifier.on_scroll_with_source(move |d, s| handler(d, s)),
118        None => modifier,
119    };
120    let content = crate::Box(Modifier::new().flex_shrink(0.0)).child(content);
121
122    View::new(0, ViewKind::Box)
123        .modifier(modifier)
124        .with_children(vec![content])
125}