use crate::component::ComponentId;
use crate::geometry::PointF;
use crate::ui::Ui;
const MARGIN: f32 = 8.0;
impl Ui {
pub(crate) fn form_scroll_max(&self, root: ComponentId) -> f32 {
let Some(cb) = self.cb(root) else { return 0.0 };
let visible = cb.bounds().height - cb.safe_area.bottom;
let content = cb.content_size().height;
if content > visible { content + MARGIN - visible } else { 0.0 }
}
pub fn reveal(&mut self, id: ComponentId) {
self.reveal_with(id, false);
}
pub fn reveal_top(&mut self, id: ComponentId) {
self.reveal_with(id, true);
}
fn reveal_with(&mut self, id: ComponentId, at_top: bool) {
self.update_layout();
let root = self.root_of(id);
let mut current = self.parent(id);
while let Some(a) = current {
let scrolls = a == root || self.component(a).is_some_and(|c| c.scroll_axes().1);
if scrolls {
self.reveal_in(a, id, root, at_top);
}
if a == root {
break;
}
current = self.parent(a);
}
}
fn reveal_in(&mut self, container: ComponentId, id: ComponentId, root: ComponentId, at_top: bool) {
let (Some(r), Some(area)) = (self.absolute_rect(id), self.absolute_rect(container)) else { return };
let Some(form) = self.cb(root) else { return };
let (safe, form_height) = (form.safe_area, form.bounds().height);
let Some(cb) = self.cb(container) else { return };
let top = area.y.max(safe.top) + MARGIN;
let bottom = area.bottom().min(form_height - safe.bottom) - MARGIN;
let max = if container == root { self.form_scroll_max(root) } else { (cb.content_size().height - cb.bounds().height).max(0.0) };
let delta = if at_top || r.height > bottom - top || r.y < top {
r.y - top } else if r.bottom() > bottom {
r.bottom() - bottom
} else {
return;
};
let offset = cb.scroll_offset();
let y = (offset.y + delta).clamp(0.0, max);
if (y - offset.y).abs() < 0.5 {
return;
}
if let Some(cb) = self.cb_mut(container) {
cb.set_scroll_offset(PointF::new(offset.x, y));
}
self.invalidate();
self.update_layout();
self.request_repaint();
}
pub(crate) fn scroll_form(&mut self, root: ComponentId, delta: f32) -> bool {
let max = self.form_scroll_max(root);
let Some(cb) = self.cb(root) else { return false };
let offset = cb.scroll_offset();
let y = (offset.y - delta).clamp(0.0, max);
if (y - offset.y).abs() < 0.01 {
return false;
}
if let Some(cb) = self.cb_mut(root) {
cb.set_scroll_offset(PointF::new(offset.x, y));
}
self.invalidate();
self.request_repaint();
true
}
}