Decide reuse on the box a widget drew against, in pixels

Two holes the random trees found, both of which kept a wrapping text shaped
for a width it no longer had.

**A region is a fraction of a slot's box, so an unchanged region is not an
unchanged box.** `try_reuse` compared regions, and a child drawn at
`UiRegion::FULL` of a slot whose box had just halved compared equal to
itself and was reused without being descended into. `ActiveData` now keeps
the pixel size of the box it drew against and the comparison is against
that, which is the question that was being asked all along and is right
through a slot change and an output resize alike.

**A size the parent learnt by drawing the child is an answer for that box
only.** The walk looking for what cannot survive a length change skipped a
child whose own box was a fixed width -- correctly, its box does not change
-- but that width was what the child reported when the span drew it in the
span's box, and the span's box did change. So a child whose size the widget
read is redrawn unless it declares an exact `size_hint` for the changed
axis, which is the one case the parent did not have to draw it to know.

The cost is that a size-reading container gives up its reuse when its box
changes length, which is every span, so `OnResize::Scale` now earns its
keep on moves and on subtrees whose sizes nobody read rather than on every
stretch. Correct first; `replace_cost` still measures the case the chain was
built for.

`tests/generated.rs` is what found both and what says they are fixed: 90 of
90 warm trees now land where a cold build does, against 83 before this
commit and 83 on `db1751f`. The ignored sweep agrees over 300 checks on 100
seeds.

`a_fixed_length_child_is_not_redrawn_when_the_box_around_it_grows` became
`a_declared_length_...`: the child now says its width, since a width the
span measured is not one it may keep.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iris-aiandClaude Opus 5 committed 2026-09-14 13:23:29 -04:00
1 parent 86a7e8dfc3
commit b0f9f046da
4 files changed
+65 -32

No files matched your search

+47 -21
View File
@@ -132,6 +132,7 @@ impl UiRenderState {
(parent_move, region)
}
};
let px = self.px_of(move_idx, local);
rsc.widgets_mut().needs_redraw.remove(&id);
self.draw_started.insert(id);
@@ -180,6 +181,7 @@ impl UiRenderState {
id,
region,
size,
px,
parent,
textures,
primitives,
@@ -227,6 +229,14 @@ impl UiRenderState {
}
}
/// The pixel size of a region held in `slot`'s coordinates.
fn px_of(&self, slot: MoveIdx, region: UiRegion) -> Vec2 {
self.moves
.resolve(slot, region)
.size()
.to_abs(self.output_size)
}
/// The drawing a widget already has, kept for a new box if the box has not
/// changed in a way it depends on.
fn try_reuse(
@@ -245,8 +255,16 @@ impl UiRenderState {
if active.parent_move != parent_move {
return None;
}
let (size, old, slot) = (active.size, active.region, active.move_idx);
if old == region {
let (size, old, slot, was) = (active.size, active.region, active.move_idx, active.px);
// In pixels, because `region` is a fraction of a slot's box and that
// box may be what changed -- an unchanged fraction of a box half the
// size is half the widget.
let px = self.px_of(parent_move, region);
let mut changed = [false; 2];
for (axis, c) in AXES.into_iter().zip(changed.iter_mut()) {
*c = px.axis(axis) != was.axis(axis);
}
if !changed.iter().any(|&c| c) && old == region {
return Some(size);
}
// Only a placed widget can be given a different box without drawing
@@ -255,28 +273,23 @@ impl UiRenderState {
if slot == parent_move {
return None;
}
let mut changed = [false; 2];
for (axis, c) in AXES.into_iter().zip(changed.iter_mut()) {
*c = region.axis(axis).len() != old.axis(axis).len();
}
if changed.iter().any(|&c| c) {
let widget = rsc.widgets().get_dyn(id)?;
let redraws = AXES
.into_iter()
.zip(changed)
.any(|(axis, c)| c && widget.on_resize(axis) != OnResize::Scale);
if redraws {
// Anything under it that has to be drawn again is drawn by drawing
// this, because whatever reads that widget's size sits in between
// and has to lay out around what it comes to.
if redraws || self.redraws_under(id, changed, rsc) {
return None;
}
}
// Anything under it that has to be drawn again is drawn by drawing
// this, because whatever reads that widget's size sits in between and
// has to lay out around whatever it comes to.
if changed.iter().any(|&c| c) && self.redraws_under(id, changed, rsc) {
return None;
}
self.moves.set(slot, region);
self.active.get_mut(&id).unwrap().region = region;
let active = self.active.get_mut(&id).unwrap();
active.region = region;
active.px = px;
Some(size)
}
@@ -293,10 +306,26 @@ impl UiRenderState {
let Some(active) = self.active.get(&id) else {
return false;
};
let size_deps = &active.size_deps;
active.children.iter().any(|&child| {
let Some(data) = self.active.get(&child) else {
return false;
};
let Some(widget) = rsc.widgets().get_dyn(child) else {
return true;
};
// What it drew to learn this child's size was the child in *this*
// box, so a different box is a different answer -- unless the
// child gave an exact one without being drawn at all.
if size_deps.contains(&child) {
let measured = AXES
.into_iter()
.zip(changed)
.any(|(axis, c)| c && widget.size_hint(axis).is_none());
if measured {
return true;
}
}
let mut own = changed;
for (axis, c) in AXES.into_iter().zip(own.iter_mut()) {
*c &= data.region.axis(axis).len().rel != 0.0;
@@ -304,13 +333,10 @@ impl UiRenderState {
if !own.iter().any(|&c| c) {
return false;
}
let redraws = match rsc.widgets().get_dyn(child) {
Some(widget) => AXES
.into_iter()
.zip(own)
.any(|(axis, c)| c && widget.on_resize(axis) != OnResize::Scale),
None => true,
};
let redraws = AXES
.into_iter()
.zip(own)
.any(|(axis, c)| c && widget.on_resize(axis) != OnResize::Scale);
redraws || self.redraws_under(child, own, rsc)
})
}