Give a child a part of the container's extent rather than its raw box

`Pad` and `Stack` read `Painter::placement` to put their children inside
their own drawing, and reading it is what says the drawing holds for that
placement alone. So a pad or a stack anywhere in a row was drawn again --
with its whole subtree -- the moment an earlier sibling changed length,
however little else had moved.

`widget_within` now takes a `DrawRegion`, and `DrawRegion::Extent(part)`
gives the child a part of the extent without reading it. What is retained
is the part rather than the box it resolved to, so moving the extent
re-places the child through the same rule instead of redrawing the parent:
`inherited_children` becomes `extent_children`, carrying `Inherit` for the
wrapper case `Painter::widget` already had and `Within(part)` for the new
one.

The dependency that goes up is a range on the container's extent rather
than on its frame, since only the part's *length* reaches the child and
where the part sits is re-placed. A declared length is unchanged: it is a
length of the frame wherever the box it sits in came from. What still pins
the placement is a report with a fraction in it -- the same fraction of a
different extent is a different length -- and that pin is on the answer,
which `extent_frames_keep_fractional_reports_and_numeric_dependencies_valid`
fails without.

Three tests from the first attempt at this come with it, and the
diagnostics rig now says which of the three contracts refused a reuse,
which is what found the above.

Measured, seed 1 at depth 8, median frame: `many` 0.667 -> 0.613 ms and
`resize` 48 -> 32 us; seed 13's `many` 6.35 -> 5.15 ms. Green: fmt, clippy,
109 suite and 20 core tests, the oracle at 100 seeds, the shrinker at 400
trees of depth 5, 1000 seeds at depth 6, and 2000 seeds at depth 4 over all
fifteen cases. The five reference renders are byte-identical to `0e107f0`
on Venus, as are `tabs` resized to 900x1200 and `random` to 1280x800
against cold renders there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
iris-aiandClaude Opus 5 committed 2026-09-17 19:15:17 -04:00
1 parent 0e107f0e89
commit e6ba570d07
7 files changed
+358 -57

No files matched your search

+84 -30
View File
@@ -1,9 +1,9 @@
#[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter};
use crate::{
Axis, DrawRegion, Holds, LayoutHolds, LayoutLen, Len, Px, PxVec2, RegionAlign, RenderedText,
RetainedPrimitive, Size, StrongWidget, TextAttrs, TextBuffer, TextData, TextureHandle,
UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2, Weight, WidgetId, Widgets,
Axis, DrawRegion, ExtentPlacement, Holds, LayoutHolds, LayoutLen, Len, Px, PxVec2, RegionAlign,
RenderedText, RetainedPrimitive, Size, StrongWidget, TextAttrs, TextBuffer, TextData,
TextureHandle, UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2, Weight, WidgetId, Widgets,
render::{
GlyphPrimitive, Mask, MaskIdx, MoveIdx, Primitive, PrimitiveInst, PrimitiveKind,
TexturePrimitive,
@@ -40,7 +40,7 @@ pub struct Painter<'a> {
pub(super) textures: Vec<TextureHandle>,
pub(super) primitives: Vec<RetainedPrimitive>,
pub(super) mask_region: Option<DrawRegion>,
pub(super) inherited_children: Vec<WidgetId>,
pub(super) extent_children: Vec<(WidgetId, ExtentPlacement)>,
pub(super) extent_own: [Holds; 2],
/// Only children whose answers were read constrain this widget's answer.
pub(super) answer_under: LayoutHolds,
@@ -155,7 +155,13 @@ impl<'a> Painter<'a> {
/// around one child wants, since its box is the child's.
pub fn widget<'s, W: ?Sized>(&'s mut self, id: &'s StrongWidget<W>) -> DrawResult<'s, 'a, W> {
let own = self.placement;
self.widget_at_inner(id, UiRegion::FULL, [Some(own.x), Some(own.y)], true, false)
self.widget_at_inner(
id,
UiRegion::FULL,
[Some(own.x), Some(own.y)],
Some(ExtentPlacement::Inherit),
false,
)
}
/// What a widget's rules declare its lengths to be, which whoever draws
@@ -171,19 +177,38 @@ impl<'a> Painter<'a> {
/// this frame; what it answered is still something this widget asked.
pub fn undraw<W: ?Sized>(&mut self, id: &StrongWidget<W>) {
self.children.retain(|child| *child != id.id());
self.inherited_children.retain(|child| *child != id.id());
self.extent_children.retain(|(child, _)| *child != id.id());
self.state.undraw_rec(id.id(), self.rsc);
}
/// Draws a child in `region`, relative to this widget's frame. The child
/// resolves declared lengths and reports against that frame, then places
/// its drawing by its own alignment.
///
/// `DrawRegion::Extent` gives a part of where this widget's own drawing
/// sits instead, which is what a container whose children belong inside
/// its drawing rather than inside the box it was offered wants. The
/// child's box then follows the extent without this widget's drawing
/// depending on where that extent is, so moving it re-places the child
/// rather than drawing this widget again.
pub fn widget_within<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
region: UiRegion,
region: impl Into<DrawRegion>,
) -> DrawResult<'s, 'a, W> {
self.widget_at(id, region, [None; 2])
match region.into() {
DrawRegion::Frame(region) => self.widget_at(id, region, [None; 2]),
DrawRegion::Extent(part) => {
let within = part.within(&self.placement);
self.widget_at_inner(
id,
within,
[None; 2],
Some(ExtentPlacement::Within(part)),
false,
)
}
}
}
/// Draws a widget in `region`, saying where in it the drawing goes.
@@ -206,7 +231,7 @@ impl<'a> Painter<'a> {
region: UiRegion,
placement: [Option<UiSpan>; 2],
) -> DrawResult<'s, 'a, W> {
self.widget_at_inner(id, region, placement, false, false)
self.widget_at_inner(id, region, placement, None, false)
}
fn widget_at_inner<'s, W: ?Sized>(
@@ -214,15 +239,12 @@ impl<'a> Painter<'a> {
id: &'s StrongWidget<W>,
region: UiRegion,
placement: [Option<UiSpan>; 2],
inherited: bool,
extent: Option<ExtentPlacement>,
measuring: bool,
) -> DrawResult<'s, 'a, W> {
if inherited {
if !self.inherited_children.contains(&id.id()) {
self.inherited_children.push(id.id());
}
} else {
self.inherited_children.retain(|child| *child != id.id());
self.extent_children.retain(|(child, _)| *child != id.id());
if let Some(extent) = extent {
self.extent_children.push((id.id(), extent));
}
let region_node = self.rsc.widgets().is_region_node(id.id());
let declared = self.declared_lens(id);
@@ -289,25 +311,57 @@ impl<'a> Painter<'a> {
let mut result = LayoutHolds::ANY;
for axis in AXES {
let n = axis as usize;
result.frame[n] = holds.frame[n].through(local.axis(axis).len());
if inherited && declared[n].is_none() {
result.extent[n] = holds.extent[n];
if holds.placement.is_some() {
result.placement = Some(self.placement);
let chosen = placement[n].unwrap_or(UiSpan::FULL).len();
match extent {
// Its box is this widget's own, so what its drawing holds
// for is what this widget's extent holds for.
Some(ExtentPlacement::Inherit) if declared[n].is_none() => {
result.frame[n] = holds.frame[n].through(local.axis(axis).len());
result.extent[n] = holds.extent[n];
if holds.placement.is_some() {
result.placement = Some(self.placement);
}
}
// Its box is a part of this widget's extent, so both what
// it was given and what it took of that are ranges on the
// extent and none of them a range on the frame. That is
// what lets this widget's drawing move without being made
// again: only the part's length reaches the child, and
// where the part sits is re-placed rather than redrawn.
Some(ExtentPlacement::Within(part)) if declared[n].is_none() => {
result.extent[n] = holds.frame[n]
.and(holds.extent[n].through(chosen))
.through(part.axis(axis).len());
}
// A declared length is a length of this widget's frame
// wherever the box it sits in came from, so what the child
// holds for is a range on the frame either way.
_ => {
result.frame[n] = holds.frame[n].through(local.axis(axis).len()).and(
holds.extent[n]
.through(chosen)
.through(local.axis(axis).len()),
);
}
} else {
let chosen = placement[n].unwrap_or(UiSpan::FULL).len();
result.frame[n] = result.frame[n].and(
holds.extent[n]
.through(chosen)
.through(local.axis(axis).len()),
);
}
}
result
};
self.under = self.under.and(in_parent(holds));
let answer_holds = in_parent(answer_holds);
let mut answer_holds = in_parent(answer_holds);
// What it reports is a fraction of the box it was given, and that box
// is a part of this widget's extent -- so the same fraction is a
// different length once the extent is. Only the report: where the
// extent moved without changing what it holds, the drawing under it
// is re-placed rather than made again, which is what the extent ask
// is for. Pixels come up unchanged and say nothing.
if matches!(extent, Some(ExtentPlacement::Within(_)))
&& AXES.into_iter().any(|axis| {
declared[axis as usize].is_none() && size.axis(axis).rel != crate::Rel::ZERO
})
{
answer_holds.placement = Some(self.placement);
}
DrawResult {
child: id,
painter: self,
@@ -369,7 +423,7 @@ impl<'a> Painter<'a> {
.retained_size(child.id(), px, placement, self.move_idx, self.rsc.widgets());
let Some((size, holds)) = retained else {
return self
.widget_at_inner(child, region, offered, false, true)
.widget_at_inner(child, region, offered, None, true)
.len(axis);
};
#[cfg(feature = "layout-diagnostics")]