Skip to main content

nativeview_layout/scene/scroll/
into_view.rs

1//! Minimal ancestor scroll-into-view geometry over [`super::LaidOutScene`] (accounting for
2//! `virtual_top_spacer`, `Position::Sticky`, and live `Transform2D` overrides).
3
4use nativeview_engine::LayoutRect;
5use nativeview_store::{NodeId, NodeStore};
6use nativeview_tables::tables::Scroll;
7use nativeview_tables::transform::{Transform2D, affine_map_aabb};
8
9use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
10
11use super::super::LaidOutScene;
12
13impl<L: StoreLayoutEngine> LaidOutScene<L> {
14    /// Computes minimal scroll target `(next_x, next_y)` for each enclosing `Scroll` ancestor of
15    /// `box_node` so `target_rect` becomes visible, invoking `apply_scroll(scroll_id, tx, ty)`
16    /// (which returns the clamped `(x, y)` actually applied) and `resolve_transform` for live
17    /// animated transforms. Returns `true` if any scroll offset moved.
18    #[allow(clippy::too_many_arguments)]
19    pub fn scroll_into_view_box_with_transform(
20        &mut self,
21        store: &NodeStore,
22        box_node: NodeId,
23        box_rect: LayoutRect,
24        target_rect: LayoutRect,
25        resolve_transform: impl Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>,
26        mut apply_scroll: impl FnMut(&mut Self, NodeId, f32, f32) -> (f32, f32),
27        mut scrolled_containers: Option<&mut Vec<NodeId>>,
28    ) -> bool {
29        let mut changed = false;
30        let (own_stx, own_sty) = self.compute_sticky_delta(store, box_node);
31        let mut cur_x0 = target_rect.x + own_stx;
32        let mut cur_y0 = target_rect.y + own_sty;
33        let mut cur_x1 = cur_x0 + target_rect.width;
34        let mut cur_y1 = cur_y0 + target_rect.height;
35        if let Some(tf) = resolve_transform(box_node, self.effective_transform(store, box_node))
36            && !tf.is_identity()
37        {
38            let m = tf.to_surface_matrix_2d(
39                box_rect.x + own_stx,
40                box_rect.y + own_sty,
41                box_rect.width,
42                box_rect.height,
43            );
44            (cur_x0, cur_y0, cur_x1, cur_y1) = affine_map_aabb(&m, cur_x0, cur_y0, cur_x1, cur_y1);
45        }
46
47        let mut ancestor = store.parent(box_node);
48        let mut depth = 0usize;
49        while let Some(pid) = ancestor {
50            if depth > MAX_TREE_DEPTH {
51                break;
52            }
53            depth += 1;
54            if let Some(scroll) = store.try_row::<Scroll>(pid)
55                && let Some(view) = self.layout_of(pid)
56            {
57                let spacer = scroll
58                    .virtual_top_spacer()
59                    .filter(|&v| v.is_finite() && v > 0.0)
60                    .unwrap_or(0.0);
61                let (sx, sy) = self.scroll_offset(pid);
62                let (shift_x, shift_y) = self.visual_scroll_shift(store, pid);
63                let (vis_x0, vis_x1) = (cur_x0 - shift_x, cur_x1 - shift_x);
64                let (vis_y0, vis_y1) = (cur_y0 - shift_y, cur_y1 - shift_y);
65
66                let dx = if vis_x0 < view.x {
67                    vis_x0 - view.x
68                } else if vis_x1 > view.x + view.width {
69                    (vis_x1 - (view.x + view.width)).min(vis_x0 - view.x)
70                } else {
71                    0.0
72                };
73                let dy = if vis_y0 < view.y {
74                    vis_y0 - view.y
75                } else if vis_y1 > view.y + view.height {
76                    (vis_y1 - (view.y + view.height)).min(vis_y0 - view.y)
77                } else {
78                    0.0
79                };
80
81                if dx.abs() > 1e-3 || dy.abs() > 1e-3 {
82                    let target_x = (sx + dx).max(0.0);
83                    let target_y = if dy > 1e-3 {
84                        if spacer > 0.0 {
85                            spacer + shift_y + dy
86                        } else {
87                            (shift_y + dy).max(0.0)
88                        }
89                    } else if dy < -1e-3 {
90                        (sy + dy).max(0.0)
91                    } else {
92                        sy
93                    };
94                    let next = apply_scroll(self, pid, target_x, target_y);
95                    if (sx - next.0).abs() > 1e-4 || (sy - next.1).abs() > 1e-4 {
96                        changed = true;
97                        if let Some(out) = scrolled_containers.as_deref_mut() {
98                            out.push(pid);
99                        }
100                    }
101                }
102                let (new_shift_x, new_shift_y) = self.visual_scroll_shift(store, pid);
103                cur_x0 -= new_shift_x;
104                cur_x1 -= new_shift_x;
105                cur_y0 -= new_shift_y;
106                cur_y1 -= new_shift_y;
107            }
108            let (stx, sty) = self.compute_sticky_delta(store, pid);
109            cur_x0 += stx;
110            cur_x1 += stx;
111            cur_y0 += sty;
112            cur_y1 += sty;
113            if let Some(tf) = resolve_transform(pid, self.effective_transform(store, pid))
114                && !tf.is_identity()
115                && let Some(p_rect) = self.layout_of(pid)
116            {
117                let m = tf.to_surface_matrix_2d(
118                    p_rect.x + stx,
119                    p_rect.y + sty,
120                    p_rect.width,
121                    p_rect.height,
122                );
123                (cur_x0, cur_y0, cur_x1, cur_y1) =
124                    affine_map_aabb(&m, cur_x0, cur_y0, cur_x1, cur_y1);
125            }
126            ancestor = store.parent(pid);
127        }
128        changed
129    }
130}