1use std::rc::Rc;
2
3use crate::Vec2;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum NestedScrollSource {
8 UserInput,
9 SideEffect,
10}
11
12pub type PreScrollFn = Rc<dyn Fn(Vec2, NestedScrollSource) -> Vec2>;
13pub type PostScrollFn = Rc<dyn Fn(Vec2, Vec2, NestedScrollSource) -> Vec2>;
14
15#[derive(Clone, Default)]
16pub struct NestedScrollConnection {
17 pub on_pre_scroll: Option<PreScrollFn>,
18 pub on_post_scroll: Option<PostScrollFn>,
19 pub on_pre_fling: Option<PreScrollFn>,
20 pub on_post_fling: Option<PostScrollFn>,
21}
22
23impl std::fmt::Debug for NestedScrollConnection {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 f.debug_struct("NestedScrollConnection")
26 .field("has_pre_scroll", &self.on_pre_scroll.is_some())
27 .field("has_post_scroll", &self.on_post_scroll.is_some())
28 .field("has_pre_fling", &self.on_pre_fling.is_some())
29 .field("has_post_fling", &self.on_post_fling.is_some())
30 .finish()
31 }
32}
33
34impl NestedScrollConnection {
35 pub fn new() -> Self {
36 Self::default()
37 }
38
39 pub fn on_pre_scroll(mut self, f: impl Fn(Vec2, NestedScrollSource) -> Vec2 + 'static) -> Self {
40 self.on_pre_scroll = Some(Rc::new(f));
41 self
42 }
43
44 pub fn on_post_scroll(
45 mut self,
46 f: impl Fn(Vec2, Vec2, NestedScrollSource) -> Vec2 + 'static,
47 ) -> Self {
48 self.on_post_scroll = Some(Rc::new(f));
49 self
50 }
51
52 pub fn on_pre_fling(mut self, f: impl Fn(Vec2, NestedScrollSource) -> Vec2 + 'static) -> Self {
53 self.on_pre_fling = Some(Rc::new(f));
54 self
55 }
56
57 pub fn on_post_fling(
58 mut self,
59 f: impl Fn(Vec2, Vec2, NestedScrollSource) -> Vec2 + 'static,
60 ) -> Self {
61 self.on_post_fling = Some(Rc::new(f));
62 self
63 }
64
65 pub fn dispatch_pre_scroll(&self, available: Vec2, source: NestedScrollSource) -> Vec2 {
66 self.on_pre_scroll
67 .as_ref()
68 .map(|f| f(available, source))
69 .unwrap_or(Vec2::ZERO)
70 }
71
72 pub fn dispatch_post_scroll(
73 &self,
74 consumed: Vec2,
75 available: Vec2,
76 source: NestedScrollSource,
77 ) -> Vec2 {
78 self.on_post_scroll
79 .as_ref()
80 .map(|f| f(consumed, available, source))
81 .unwrap_or(Vec2::ZERO)
82 }
83
84 pub fn dispatch_pre_fling(&self, available: Vec2, source: NestedScrollSource) -> Vec2 {
87 self.on_pre_fling
88 .as_ref()
89 .map(|f| f(available, source))
90 .unwrap_or(Vec2::ZERO)
91 }
92
93 pub fn dispatch_post_fling(
110 &self,
111 consumed: Vec2,
112 available: Vec2,
113 source: NestedScrollSource,
114 ) -> Vec2 {
115 self.on_post_fling
116 .as_ref()
117 .map(|f| f(consumed, available, source))
118 .unwrap_or(Vec2::ZERO)
119 }
120
121 pub fn then(&self, further: &Self) -> Self {
122 let on_pre_scroll: Option<PreScrollFn> = match (&self.on_pre_scroll, &further.on_pre_scroll)
123 {
124 (Some(close), Some(far)) => {
125 let close = close.clone();
126 let far = far.clone();
127 Some(Rc::new(move |v, s| {
128 let fc = far(v, s);
129 let cc = close(v - fc, s);
130 fc + cc
131 }))
132 }
133 (Some(close), None) => Some(close.clone()),
134 (None, Some(far)) => Some(far.clone()),
135 (None, None) => None,
136 };
137 let on_post_scroll: Option<PostScrollFn> =
138 match (&self.on_post_scroll, &further.on_post_scroll) {
139 (Some(close), Some(far)) => {
140 let close = close.clone();
141 let far = far.clone();
142 Some(Rc::new(move |consumed, available, s| {
143 let cc = close(consumed, available, s);
144 let fc = far(consumed + cc, available - cc, s);
145 cc + fc
146 }))
147 }
148 (Some(close), None) => Some(close.clone()),
149 (None, Some(far)) => Some(far.clone()),
150 (None, None) => None,
151 };
152 let on_pre_fling: Option<PreScrollFn> = match (&self.on_pre_fling, &further.on_pre_fling) {
153 (Some(close), Some(far)) => {
154 let close = close.clone();
155 let far = far.clone();
156 Some(Rc::new(move |v, s| {
157 let fc = far(v, s);
158 let cc = close(v - fc, s);
159 fc + cc
160 }))
161 }
162 (Some(close), None) => Some(close.clone()),
163 (None, Some(far)) => Some(far.clone()),
164 (None, None) => None,
165 };
166 let on_post_fling: Option<PostScrollFn> =
167 match (&self.on_post_fling, &further.on_post_fling) {
168 (Some(close), Some(far)) => {
169 let close = close.clone();
170 let far = far.clone();
171 Some(Rc::new(move |consumed, available, s| {
172 let cc = close(consumed, available, s);
173 let fc = far(consumed + cc, available - cc, s);
174 cc + fc
175 }))
176 }
177 (Some(close), None) => Some(close.clone()),
178 (None, Some(far)) => Some(far.clone()),
179 (None, None) => None,
180 };
181 Self {
182 on_pre_scroll,
183 on_post_scroll,
184 on_pre_fling,
185 on_post_fling,
186 }
187 }
188}