use nativeview_engine::LayoutRect;
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::Scroll;
use nativeview_tables::transform::{Transform2D, affine_map_aabb};
use crate::{MAX_TREE_DEPTH, StoreLayoutEngine};
use super::super::LaidOutScene;
impl<L: StoreLayoutEngine> LaidOutScene<L> {
#[allow(clippy::too_many_arguments)]
pub fn scroll_into_view_box_with_transform(
&mut self,
store: &NodeStore,
box_node: NodeId,
box_rect: LayoutRect,
target_rect: LayoutRect,
resolve_transform: impl Fn(NodeId, Option<Transform2D>) -> Option<Transform2D>,
mut apply_scroll: impl FnMut(&mut Self, NodeId, f32, f32) -> (f32, f32),
mut scrolled_containers: Option<&mut Vec<NodeId>>,
) -> bool {
let mut changed = false;
let (own_stx, own_sty) = self.compute_sticky_delta(store, box_node);
let mut cur_x0 = target_rect.x + own_stx;
let mut cur_y0 = target_rect.y + own_sty;
let mut cur_x1 = cur_x0 + target_rect.width;
let mut cur_y1 = cur_y0 + target_rect.height;
if let Some(tf) = resolve_transform(box_node, self.effective_transform(store, box_node))
&& !tf.is_identity()
{
let m = tf.to_surface_matrix_2d(
box_rect.x + own_stx,
box_rect.y + own_sty,
box_rect.width,
box_rect.height,
);
(cur_x0, cur_y0, cur_x1, cur_y1) = affine_map_aabb(&m, cur_x0, cur_y0, cur_x1, cur_y1);
}
let mut ancestor = store.parent(box_node);
let mut depth = 0usize;
while let Some(pid) = ancestor {
if depth > MAX_TREE_DEPTH {
break;
}
depth += 1;
if let Some(scroll) = store.try_row::<Scroll>(pid)
&& let Some(view) = self.layout_of(pid)
{
let spacer = scroll
.virtual_top_spacer()
.filter(|&v| v.is_finite() && v > 0.0)
.unwrap_or(0.0);
let (sx, sy) = self.scroll_offset(pid);
let (shift_x, shift_y) = self.visual_scroll_shift(store, pid);
let (vis_x0, vis_x1) = (cur_x0 - shift_x, cur_x1 - shift_x);
let (vis_y0, vis_y1) = (cur_y0 - shift_y, cur_y1 - shift_y);
let dx = if vis_x0 < view.x {
vis_x0 - view.x
} else if vis_x1 > view.x + view.width {
(vis_x1 - (view.x + view.width)).min(vis_x0 - view.x)
} else {
0.0
};
let dy = if vis_y0 < view.y {
vis_y0 - view.y
} else if vis_y1 > view.y + view.height {
(vis_y1 - (view.y + view.height)).min(vis_y0 - view.y)
} else {
0.0
};
if dx.abs() > 1e-3 || dy.abs() > 1e-3 {
let target_x = (sx + dx).max(0.0);
let target_y = if dy > 1e-3 {
if spacer > 0.0 {
spacer + shift_y + dy
} else {
(shift_y + dy).max(0.0)
}
} else if dy < -1e-3 {
(sy + dy).max(0.0)
} else {
sy
};
let next = apply_scroll(self, pid, target_x, target_y);
if (sx - next.0).abs() > 1e-4 || (sy - next.1).abs() > 1e-4 {
changed = true;
if let Some(out) = scrolled_containers.as_deref_mut() {
out.push(pid);
}
}
}
let (new_shift_x, new_shift_y) = self.visual_scroll_shift(store, pid);
cur_x0 -= new_shift_x;
cur_x1 -= new_shift_x;
cur_y0 -= new_shift_y;
cur_y1 -= new_shift_y;
}
let (stx, sty) = self.compute_sticky_delta(store, pid);
cur_x0 += stx;
cur_x1 += stx;
cur_y0 += sty;
cur_y1 += sty;
if let Some(tf) = resolve_transform(pid, self.effective_transform(store, pid))
&& !tf.is_identity()
&& let Some(p_rect) = self.layout_of(pid)
{
let m = tf.to_surface_matrix_2d(
p_rect.x + stx,
p_rect.y + sty,
p_rect.width,
p_rect.height,
);
(cur_x0, cur_y0, cur_x1, cur_y1) =
affine_map_aabb(&m, cur_x0, cur_y0, cur_x1, cur_y1);
}
ancestor = store.parent(pid);
}
changed
}
}