Separate measured-answer dependencies from retained drawing validity

This commit is contained in:
iris-ai committed 2026-09-17 17:10:55 -04:00
1 parent c44bd198ee
commit f860f716e6
7 files changed
+188 -356

No files matched your search

+49 -80
View File
@@ -1,9 +1,9 @@
#[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter};
use crate::{
Axis, DrawRegion, ExtentPlacement, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign,
RenderedText, RetainedPrimitive, Size, StrongWidget, TextAttrs, TextBuffer, TextData,
TextureHandle, UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2, Weight, WidgetId, Widgets,
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,
render::{
GlyphPrimitive, Mask, MaskIdx, MoveIdx, Primitive, PrimitiveInst, PrimitiveKind,
TexturePrimitive,
@@ -40,9 +40,10 @@ pub struct Painter<'a> {
pub(super) textures: Vec<TextureHandle>,
pub(super) primitives: Vec<RetainedPrimitive>,
pub(super) mask_region: Option<DrawRegion>,
pub(super) extent_children: Vec<(WidgetId, ExtentPlacement)>,
pub(super) inherited_children: Vec<WidgetId>,
pub(super) extent_own: [Holds; 2],
pub(super) extent_under: [Holds; 2],
/// Only children whose answers were read constrain this widget's answer.
pub(super) answer_under: LayoutHolds,
pub(super) children: Vec<WidgetId>,
/// The children asked about so far, so the first box each was asked in
/// is the one recorded as its offer.
@@ -59,8 +60,8 @@ pub struct Painter<'a> {
/// What this draw itself read of its box in pixels, per axis: every
/// length until it reads one, then that one, unless it says otherwise.
pub(super) own: [Holds; 2],
/// What the children it asked about and drew keep it to.
pub(super) under: [Holds; 2],
/// Dependencies of every child drawing, including unmeasured overlays.
pub(super) under: LayoutHolds,
/// The movable region this widget's primitives are positioned through:
/// its own when opted in, otherwise the nearest ancestor's.
pub(super) move_idx: MoveIdx,
@@ -154,13 +155,7 @@ 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)],
Some(ExtentPlacement::Inherit),
false,
)
self.widget_at_inner(id, UiRegion::FULL, [Some(own.x), Some(own.y)], true, false)
}
/// What a widget's rules declare its lengths to be, which whoever draws
@@ -176,29 +171,19 @@ 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.extent_children.retain(|(child, _)| *child != id.id());
self.inherited_children.retain(|child| *child != id.id());
self.state.undraw_rec(id.id(), self.rsc);
}
/// Draws a child in a frame relative to this widget's frame or extent.
/// A plain `UiRegion` is frame-relative. `DrawRegion::Extent` keeps the
/// child's frame attached to the extent without reading `placement()`.
/// The child places its answer within that frame by its own alignment.
/// 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.
pub fn widget_within<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
region: impl Into<DrawRegion>,
region: UiRegion,
) -> DrawResult<'s, 'a, W> {
match region.into() {
DrawRegion::Frame(region) => self.widget_at(id, region, [None; 2]),
DrawRegion::Extent(local) => self.widget_at_inner(
id,
local.within(&self.placement),
[None; 2],
Some(ExtentPlacement::Within(local)),
false,
),
}
self.widget_at(id, region, [None; 2])
}
/// Draws a widget in `region`, saying where in it the drawing goes.
@@ -221,7 +206,7 @@ impl<'a> Painter<'a> {
region: UiRegion,
placement: [Option<UiSpan>; 2],
) -> DrawResult<'s, 'a, W> {
self.widget_at_inner(id, region, placement, None, false)
self.widget_at_inner(id, region, placement, false, false)
}
fn widget_at_inner<'s, W: ?Sized>(
@@ -229,12 +214,15 @@ impl<'a> Painter<'a> {
id: &'s StrongWidget<W>,
region: UiRegion,
placement: [Option<UiSpan>; 2],
extent: Option<ExtentPlacement>,
inherited: bool,
measuring: bool,
) -> DrawResult<'s, 'a, W> {
self.extent_children.retain(|(child, _)| *child != id.id());
if let Some(extent) = extent {
self.extent_children.push((id.id(), extent));
if inherited {
if !self.inherited_children.contains(&id.id()) {
self.inherited_children.push(id.id());
}
} else {
self.inherited_children.retain(|child| *child != id.id());
}
let region_node = self.rsc.widgets().is_region_node(id.id());
let declared = self.declared_lens(id);
@@ -276,7 +264,7 @@ impl<'a> Painter<'a> {
// The answer and what it holds for, both about the box asked in. The
// child's record may say something else once its drawing has been
// placed: a drawing made again in its placed box holds for that box.
let (size, holds) = self.state.draw_inner(
let (size, answer_holds, holds) = self.state.draw_inner(
id.id(),
within,
DrawInfo {
@@ -297,53 +285,34 @@ impl<'a> Painter<'a> {
measuring,
self.rsc,
);
for axis in AXES {
let n = axis as usize;
match extent {
Some(ExtentPlacement::Inherit) if declared[n].is_none() => {
self.under[n] =
self.under[n].and(holds.frame[n].through(local.axis(axis).len()));
self.extent_under[n] = self.extent_under[n].and(holds.extent[n]);
self.reads_placement |= holds.placement.is_some();
}
Some(ExtentPlacement::Within(part))
if declared[n].is_none()
&& part.axis(axis).start.rel == crate::Rel::ZERO
&& part.axis(axis).end.rel == crate::Rel::ONE =>
{
let dependent = holds.frame[n]
.and(holds.extent[n])
.through(part.axis(axis).len());
self.extent_under[n] = self.extent_under[n].and(dependent);
}
_ => {
let chosen = placement[n].unwrap_or(UiSpan::FULL).len();
self.under[n] = self.under[n]
.and(holds.frame[n].through(local.axis(axis).len()))
.and(
holds.extent[n]
.through(chosen)
.through(local.axis(axis).len()),
);
// Fractional endpoints compose before pixel evaluation. Their
// difference cannot be inverted through the extent length alone.
if matches!(extent, Some(ExtentPlacement::Within(_))) && declared[n].is_none() {
self.reads_placement = true;
let in_parent = |holds: LayoutHolds| {
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);
}
} 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()),
);
}
}
}
// A fractional report is composed into the parent frame, so its
// value can change with the extent even when the drawing holds.
let reads_placement = matches!(extent, Some(ExtentPlacement::Within(_)))
&& AXES.into_iter().any(|axis| {
declared[axis as usize].is_none() && size.axis(axis).rel != crate::Rel::ZERO
});
result
};
self.under = self.under.and(in_parent(holds));
let answer_holds = in_parent(answer_holds);
DrawResult {
child: id,
painter: self,
size: in_parent_frame(size, local.size(), declared),
reads_placement,
answer_holds,
}
}
@@ -400,7 +369,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, None, true)
.widget_at_inner(child, region, offered, false, true)
.len(axis);
};
#[cfg(feature = "layout-diagnostics")]
@@ -417,7 +386,7 @@ impl<'a> Painter<'a> {
y: placement[1].unwrap_or(UiSpan::FULL),
};
let holds = holds.in_frame(placement);
for (axis, under) in AXES.into_iter().zip(self.under.iter_mut()) {
for (axis, under) in AXES.into_iter().zip(self.answer_under.frame.iter_mut()) {
*under = under.and(holds[axis as usize].through(local.axis(axis).len()));
}
in_parent_frame(size, local.size(), declared).axis(axis)
@@ -641,7 +610,7 @@ pub struct DrawResult<'p, 'a, W: ?Sized> {
painter: &'p mut Painter<'a>,
child: &'p StrongWidget<W>,
size: Size,
reads_placement: bool,
answer_holds: LayoutHolds,
}
impl<W: ?Sized> DrawResult<'_, '_, W> {
@@ -652,7 +621,7 @@ impl<W: ?Sized> DrawResult<'_, '_, W> {
diag::size_read(self.child.id(), self.painter.id, self.size);
}
self.painter.depend_on(self.child);
self.painter.reads_placement |= self.reads_placement;
self.painter.answer_under = self.painter.answer_under.and(self.answer_holds);
self.size
}