1use nativeview_store::{NodeId, NodeStore};
6use nativeview_tables::tables::{OverscrollBehavior, Scroll};
7
8use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
9
10use super::super::{LaidOutScene, cascade_of};
11use super::engine::{
12 ScrollAxis, ScrollBoundsLookup, ScrollContainerCtx, ScrollDeltaResult, ScrollInputKind,
13 ScrollTimestampMs, ScrollTouchPhase,
14};
15use super::extent::compute_scroll_content_size;
16use super::{ScrollChainOutcome, scroll_axes_allow};
17
18#[inline]
20pub fn push_unique_node(buf: &mut Vec<NodeId>, id: NodeId) {
21 if !buf.contains(&id) {
22 buf.push(id);
23 }
24}
25
26#[derive(Clone, Copy)]
27struct AutoOverscrollTarget {
28 node: NodeId,
29 viewport: (f32, f32),
30 offset: (f32, f32),
31 max_offset: (f32, f32),
32 allow_axes: (bool, bool),
33 overscroll_behavior: (OverscrollBehavior, OverscrollBehavior),
34 bounce_requested: bool,
35}
36
37impl AutoOverscrollTarget {
38 #[inline]
39 fn as_ctx<'a>(&self, store: &'a NodeStore) -> ScrollContainerCtx<'a> {
40 ScrollContainerCtx {
41 store,
42 node: self.node,
43 viewport: self.viewport,
44 offset: self.offset,
45 max_offset: self.max_offset,
46 allow_axes: self.allow_axes,
47 overscroll_behavior: self.overscroll_behavior,
48 bounce_requested: self.bounce_requested,
49 }
50 }
51}
52
53pub(crate) struct SceneScrollBounds<'a, L: StoreLayoutEngine> {
54 pub(crate) layout: &'a L,
55 pub(crate) interaction: &'a nativeview_engine::InteractionTracker,
56 pub(crate) overlays: &'a super::super::OverlayState,
57}
58
59impl<L: StoreLayoutEngine> ScrollBoundsLookup for SceneScrollBounds<'_, L> {
60 fn scroll_bounds(&self, store: &NodeStore, id: NodeId) -> Option<(f32, f32, f32, f32)> {
61 let rect = self.layout.layout_of(id)?;
62 let cascade = cascade_of(self.layout, self.interaction);
63 let (cw, ch) = compute_scroll_content_size(
64 store,
65 |n| self.layout.layout_of(n),
66 |n| cascade.display(store, n),
67 id,
68 )
69 .unwrap_or((rect.width, rect.height));
70 let (allow_x, allow_y) = scroll_axes_allow(store, id);
71 let max_x = if allow_x {
72 (cw - rect.width).max(0.0)
73 } else {
74 0.0
75 };
76 let max_y = if allow_y {
77 (ch - rect.height).max(0.0)
78 } else {
79 0.0
80 };
81 Some((rect.width.max(1.0), rect.height.max(1.0), max_x, max_y))
82 }
83
84 #[inline]
85 fn is_top_layer_boundary(&self, node: NodeId) -> bool {
86 self.overlays.get(node).is_some()
87 }
88}
89
90impl<L: StoreLayoutEngine> LaidOutScene<L> {
91 pub fn apply_scroll_chain(
96 &mut self,
97 store: &NodeStore,
98 start: NodeId,
99 dx: f32,
100 dy: f32,
101 kind: ScrollInputKind,
102 ) -> ScrollChainOutcome {
103 self.scroll.chain_changed_scratch.clear();
104 self.scroll.chain_clamped_scratch.clear();
105 let dx = if dx.is_finite() { dx } else { 0.0 };
106 let dy = if dy.is_finite() { dy } else { 0.0 };
107
108 let (is_smooth_px, touch_phase, now_ms) = match kind {
109 ScrollInputKind::SmoothPixels {
110 phase,
111 timestamp_ms,
112 } => (true, phase, ScrollTimestampMs::new(timestamp_ms)),
113 _ => (false, ScrollTouchPhase::None, ScrollTimestampMs::new(0)),
114 };
115
116 let mut effective_start = start;
117 if is_smooth_px {
118 match touch_phase {
119 ScrollTouchPhase::Started => {
120 let bounds = SceneScrollBounds {
121 layout: &self.layout,
122 interaction: &self.interaction,
123 overlays: &self.overlays,
124 };
125 self.scroll_physics
126 .on_gesture_start(store, &bounds, start, now_ms);
127 }
128 ScrollTouchPhase::Moved | ScrollTouchPhase::Ended | ScrollTouchPhase::Cancelled => {
129 if let Some(latched) = self.scroll_physics.latched_target()
130 && store.is_live(latched)
131 && store.has::<Scroll>(latched)
132 && !self.is_inert_under_modal(store, latched)
133 {
134 effective_start = latched;
135 }
136 }
137 ScrollTouchPhase::None => {}
138 }
139 }
140
141 let mut rem_x = dx;
142 let mut rem_y = dy;
143 let mut consumed_x = false;
144 let mut consumed_y = false;
145 let mut last_auto_x: Option<AutoOverscrollTarget> = None;
146 let mut last_auto_y: Option<AutoOverscrollTarget> = None;
147
148 let mut cur = Some(effective_start);
149 for _ in 0..MAX_TREE_DEPTH {
150 let Some(id) = cur else {
151 break;
152 };
153 if rem_x.abs() <= 1e-4 && rem_y.abs() <= 1e-4 {
154 break;
155 }
156 if store.is_live(id)
157 && store.has::<Scroll>(id)
158 && let Some((vw, vh, max_x, max_y)) = self.scroll_bounds(store, id)
159 && let Some(sc) = store.try_row::<Scroll>(id)
160 {
161 let allow_axes = scroll_axes_allow(store, id);
162 let overscroll_behavior = (sc.overscroll_behavior_x(), sc.overscroll_behavior_y());
163 let offset = self.scroll.get(id);
164 let ctx = ScrollContainerCtx {
165 store,
166 node: id,
167 viewport: (vw, vh),
168 offset,
169 max_offset: (max_x, max_y),
170 allow_axes,
171 overscroll_behavior,
172 bounce_requested: sc.bounce(),
173 };
174 let mut res = ScrollDeltaResult {
175 next_offset: offset,
176 remaining: (rem_x, rem_y),
177 ..Default::default()
178 };
179 self.scroll_physics
180 .on_scroll_delta(&ctx, rem_x, rem_y, kind, &mut res);
181
182 if res.clamped_changed {
183 self.scroll.set_raw(id, res.next_offset.0, res.next_offset.1);
184 push_unique_node(&mut self.scroll.chain_clamped_scratch, id);
185 }
186 if res.visual_changed {
187 push_unique_node(&mut self.scroll.chain_changed_scratch, id);
188 }
189 if res.auto_overscroll_x {
190 last_auto_x = Some(AutoOverscrollTarget {
191 node: id,
192 viewport: (vw, vh),
193 offset: res.next_offset,
194 max_offset: (max_x, max_y),
195 allow_axes,
196 overscroll_behavior,
197 bounce_requested: sc.bounce(),
198 });
199 }
200 if res.auto_overscroll_y {
201 last_auto_y = Some(AutoOverscrollTarget {
202 node: id,
203 viewport: (vw, vh),
204 offset: res.next_offset,
205 max_offset: (max_x, max_y),
206 allow_axes,
207 overscroll_behavior,
208 bounce_requested: sc.bounce(),
209 });
210 }
211 consumed_x |= res.consumed_x;
212 consumed_y |= res.consumed_y;
213 rem_x = res.remaining.0;
214 rem_y = res.remaining.1;
215 }
216 if self.overlays.get(id).is_some() {
219 break;
220 }
221 cur = store.parent(id).filter(|&p| p != id);
222 }
223
224 if is_smooth_px {
225 if rem_x.abs() > 1e-4
226 && let Some(target) = last_auto_x
227 && self.scroll_physics.on_outermost_overscroll(
228 &target.as_ctx(store),
229 ScrollAxis::X,
230 rem_x,
231 kind,
232 )
233 {
234 push_unique_node(&mut self.scroll.chain_changed_scratch, target.node);
235 }
236 if rem_y.abs() > 1e-4
237 && let Some(target) = last_auto_y
238 && self.scroll_physics.on_outermost_overscroll(
239 &target.as_ctx(store),
240 ScrollAxis::Y,
241 rem_y,
242 kind,
243 )
244 {
245 push_unique_node(&mut self.scroll.chain_changed_scratch, target.node);
246 }
247
248 match touch_phase {
249 ScrollTouchPhase::Ended | ScrollTouchPhase::Cancelled => {
250 let mut out_visual = std::mem::take(&mut self.scroll.chain_changed_scratch);
251 let mut out_clamped = std::mem::take(&mut self.scroll.chain_clamped_scratch);
252 self.scroll_physics.on_gesture_end(
253 store,
254 &mut self.scroll,
255 now_ms,
256 touch_phase == ScrollTouchPhase::Cancelled,
257 &mut out_visual,
258 &mut out_clamped,
259 );
260 self.scroll.chain_changed_scratch = out_visual;
261 self.scroll.chain_clamped_scratch = out_clamped;
262 }
263 ScrollTouchPhase::Moved => {
264 self.scroll_physics.on_gesture_move(now_ms);
265 }
266 ScrollTouchPhase::Started | ScrollTouchPhase::None => {}
267 }
268 }
269
270 ScrollChainOutcome {
271 consumed_x,
272 consumed_y,
273 any_visual_changed: !self.scroll.chain_changed_scratch.is_empty(),
274 any_clamped_changed: !self.scroll.chain_clamped_scratch.is_empty(),
275 active_physics: self.scroll_physics.has_active(),
276 }
277 }
278
279 pub fn step_scroll_physics(&mut self, store: &NodeStore, dt_s: f32) -> (bool, bool) {
282 self.scroll.step_visual_scratch.clear();
283 self.scroll.step_clamped_scratch.clear();
284 if dt_s <= 0.0 || !dt_s.is_finite() || !self.scroll_physics.has_active() {
285 return (self.scroll_physics.has_active(), false);
286 }
287 let mut out_visual = std::mem::take(&mut self.scroll.step_visual_scratch);
288 let mut out_clamped = std::mem::take(&mut self.scroll.step_clamped_scratch);
289 let bounds = SceneScrollBounds {
290 layout: &self.layout,
291 interaction: &self.interaction,
292 overlays: &self.overlays,
293 };
294 let still_active = self.scroll_physics.step(
295 store,
296 &bounds,
297 &mut self.scroll,
298 dt_s,
299 &mut out_visual,
300 &mut out_clamped,
301 );
302 let any_visual = !out_visual.is_empty();
303 self.scroll.step_visual_scratch = out_visual;
304 self.scroll.step_clamped_scratch = out_clamped;
305 (still_active, any_visual)
306 }
307}