Avoid speculative layout when retained answers suffice

This commit is contained in:
iris-ai committed 2026-09-14 15:33:39 -04:00
1 parent cdec29351a
commit a640c6cce2
5 files changed
+116 -27

No files matched your search

+7 -3
View File
@@ -13,9 +13,13 @@ impl Widget for Scroll {
fn draw(&mut self, painter: &mut Painter) -> Size {
let output_len = painter.output_size().axis(self.axis);
let container_len = UiScalar::abs(painter.px_size().axis(self.axis));
// Drawn in the whole container to learn its length, then placed at
// the scrolled offset.
let child = painter.place(&self.inner, UiRegion::FULL).size();
// Its last measured size stays valid while neither the child nor this
// container's constraints changed. Otherwise draw it in the whole
// container to learn its length, then place it at the scrolled offset.
let child = match painter.retained_size(&self.inner) {
Some(size) => size,
None => painter.place(&self.inner, UiRegion::FULL).size(),
};
let content_len = child
.axis(self.axis)
.apply_rest()
+17 -7
View File
@@ -12,14 +12,24 @@ impl Widget for Span {
let axis = self.dir.axis;
// A length for every child before any is placed: from its own hint
// where it has one, and from drawing it where it does not.
let lens: Vec<Len> = self
.children
.iter()
.map(|child| match painter.size_hint(child, axis) {
let mut cursor = UiScalar::rel_min();
let mut lens = Vec::with_capacity(self.children.len());
for child in &self.children {
let len = match painter.size_hint(child, axis) {
Some(len) => len,
None => painter.place(child, UiRegion::FULL).len(axis),
})
.collect();
None => {
let mut span = UiSpan::new(cursor, UiScalar::rel_max());
if self.dir.sign == Sign::Neg {
span.flip();
}
let region = UiRegion::from_axis(axis, span, UiSpan::FULL);
painter.place(child, region).len(axis)
}
};
cursor.abs += len.abs + self.gap;
cursor.rel += len.rel;
lens.push(len);
}
let gap = self.gap * self.children.len().saturating_sub(1) as f32;
let total = lens.iter().fold(Len::abs(gap), |sum, len| sum + *len);