Say rel base, and give containers back a box to hand over

`frame` named a length, not a rectangle, which was the one word in the
layout vocabulary that lied about its own shape. It is `rel_base`: what a
fraction a widget declares or reports is a fraction of.

Three API changes with it, all for containers that do one simple thing:

- `widget_within(id, region)` returns, taking a box in the widget's own
  coordinates and deriving the child's rel base from it. `Offset` and `Pad`
  are one call each again. `Offset` also stops reading `region_len`, which
  pinned its drawing to a box length it does not care about.
- `place_at` takes the rel base, returns the answer, and asks the child
  where there is no answer to re-express. Which of the two happens is the
  painter's to work out, so `Span`'s second pass is one call and its
  `drawn_across` bookkeeping is gone.
- `Part::All` is a `Part::WHOLE` constant rather than a variant, since it
  was exactly `Of(UiSpan::FULL)` and bought a separate arm in two matches.
  Measured at 0.07% of instructions retired against 0.04% run-to-run noise.

Cold layout is byte-identical to `84dad21` over 400 depth-5 trees.
This commit is contained in:
iris-ai committed 2026-09-19 16:33:49 -04:00
1 parent a904cf4f36
commit aeb60e50f5
16 files changed
+246 -238

No files matched your search

+71 -67
View File
@@ -1,6 +1,6 @@
#[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter, ReuseOutcome, TimerKind};
use crate::ui::painter::{declared_lens, frame_and_region, placement};
use crate::ui::painter::{declared_lens, placement, rel_base_and_region};
use crate::{
ActiveData, Axis, DrawLayers, IdLike, LayoutHolds, LayoutLen, Len, MaskIdx, MoveIdx, Moves,
Painter, Part, PixelRegion, Place, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan,
@@ -22,7 +22,7 @@ pub(super) struct DrawInfo {
pub mask: MaskIdx,
/// What a fraction declared or reported under this widget is a fraction
/// of, as a length of the window.
pub frame: UiVec2,
pub rel_base: UiVec2,
/// The box the widget is asked in, in its parent region node's
/// coordinates.
pub region: UiRegion,
@@ -31,12 +31,12 @@ pub(super) struct DrawInfo {
/// parent puts the answer somewhere else.
pub placed: [Place; 2],
pub asked: [Place; 2],
/// A frame the parent decided for it on each axis, as a length of the
/// A rel base the parent decided for it on each axis, as a length of the
/// window, which the widget's own declaration is a fraction of.
pub narrow: [Option<Len>; 2],
/// Whether the parent already asked about this widget in this draw.
pub re_asked: bool,
/// The frame in pixels, resolved once against the window.
/// The rel base in pixels, resolved once against the window.
pub px: PxVec2,
}
@@ -53,7 +53,7 @@ impl DrawInfo {
pub(super) struct Placing {
pub id: WidgetId,
pub region: UiRegion,
pub frame: UiVec2,
pub rel_base: UiVec2,
pub window: PxVec2,
pub depth: usize,
pub move_idx: MoveIdx,
@@ -120,13 +120,13 @@ impl UiRenderState {
let Some(root) = self.old_root else { return };
let stands = self.active.get(&root).is_some_and(|active| {
// Nothing above the root chose anything, so the box it was first
// asked about is the whole of its frame. Both its answer and its
// asked about is the whole of its rel base. Both its answer and its
// drawing have to stand in the new window, since nothing above
// it will ask either again.
let answer = active
.answer
.is_some_and(|(_, holds)| holds.contains(size, active.frame, active.region));
answer && active.holds.contains(size, active.frame, active.region)
.is_some_and(|(_, holds)| holds.contains(size, active.rel_base, active.region));
answer && active.holds.contains(size, active.rel_base, active.region)
});
if !stands {
widgets.needs_redraw.insert(root);
@@ -134,9 +134,9 @@ impl UiRenderState {
}
/// The root is asked about in the output. Its own rules narrow both its
/// frame and box; nothing above it chose a different one.
fn root_info(&self, frame: UiVec2, region: UiRegion) -> DrawInfo {
let px = frame.to_px(self.output_size);
/// rel base and box; nothing above it chose a different one.
fn root_info(&self, rel_base: UiVec2, region: UiRegion) -> DrawInfo {
let px = rel_base.to_px(self.output_size);
DrawInfo {
layer: 0,
parent: None,
@@ -144,10 +144,10 @@ impl UiRenderState {
parent_move: MoveIdx::NONE,
region_node: false,
mask: MaskIdx::NONE,
frame,
rel_base,
region,
placed: [Place::Within(Part::All); 2],
asked: [Place::Within(Part::All); 2],
placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::WHOLE); 2],
narrow: [None; 2],
re_asked: false,
px,
@@ -196,20 +196,20 @@ impl UiRenderState {
let _layout = diag::timer(TimerKind::FullLayout);
self.clear(rsc);
if let Some(id) = root {
let (frame, region) = Self::root_layout(id.id(), rsc.widgets());
let info = self.root_info(frame, region);
let (rel_base, region) = Self::root_layout(id.id(), rsc.widgets());
let info = self.root_info(rel_base, region);
self.draw_inner(id.id(), info, None, rsc);
}
}
/// The root's frame and box: the window, taken in by the root's own
/// The root's rel base and box: the window, taken in by the root's own
/// rules. Nothing above it narrowed anything or chose where it goes, so
/// its declaration is the whole of what decides either.
fn root_layout(id: WidgetId, widgets: &Widgets) -> (UiVec2, UiRegion) {
frame_and_region(
rel_base_and_region(
UiRegion::FULL,
UiVec2::FULL_SIZE,
[Place::Within(Part::All); 2],
[Place::Within(Part::WHOLE); 2],
[None; 2],
declared_lens(widgets, id),
widgets.alignment(id),
@@ -269,10 +269,10 @@ impl UiRenderState {
let drawing_holds = self.active[&id].holds;
let active = self.active.get_mut(&id).unwrap();
// Whoever asked owns how the boxes were reached: the frame it stated,
// Whoever asked owns how the boxes were reached: the rel base it stated,
// and what of its own box it asked in. A local redraw asks the same
// question again from these.
active.frame = info.frame;
active.rel_base = info.rel_base;
active.narrow = info.narrow;
active.re_asked = info.re_asked;
active.answer = Some(answer);
@@ -292,7 +292,7 @@ impl UiRenderState {
(answer.0, answer.1, drawing_holds)
}
/// Calls a widget's `draw` and keeps what it drew in `region` of `frame`.
/// Calls a widget's `draw` and keeps what it drew in `region`.
fn draw_at(
&mut self,
id: WidgetId,
@@ -301,7 +301,7 @@ impl UiRenderState {
old: Option<ActiveData>,
rsc: &mut dyn UiRsc,
) -> (Size, LayoutHolds) {
let frame = info.frame;
let rel_base = info.rel_base;
let (move_idx, region, retired_move) = match info.region_node {
// A node entry is only a translation. Its local box keeps the
// same window-unit length as the box in its parent's node.
@@ -324,7 +324,7 @@ impl UiRenderState {
let window = self.output_size;
let mut painter = Painter {
state: self,
frame,
rel_base,
region,
window,
mask: info.mask,
@@ -359,7 +359,7 @@ impl UiRenderState {
let Painter {
state: _,
rsc: _,
frame: _,
rel_base: _,
region: _,
window: _,
mask,
@@ -387,18 +387,18 @@ impl UiRenderState {
// A rule wins on the axis it names, and the draw answers the rest.
// Applied here so it is one place rather than every widget that could
// carry one, and so the widget under a rule never learns of it. The
// frame is the answer where the rule gave a length outright: it was
// resolved into the frame when the child was asked, and resolving it
// rel base is the answer where the rule gave a length outright: it was
// resolved into the rel base when the child was asked, and resolving it
// again here would take the fraction of a fraction.
let rules = rsc.widgets().size_rules(id);
let ruled = |axis: Axis, reported: LayoutLen| match rules.axis(axis).exact() {
None => reported,
Some(len) if len.leftover == Weight::ZERO => LayoutLen {
rel: info.frame.axis(axis).rel,
px: info.frame.axis(axis).px,
rel: info.rel_base.axis(axis).rel,
px: info.rel_base.axis(axis).px,
leftover: Weight::ZERO,
},
Some(len) => len.within_len(info.frame.axis(axis)),
Some(len) => len.within_len(info.rel_base.axis(axis)),
};
let size = Size {
x: ruled(Axis::X, size.x),
@@ -428,27 +428,27 @@ impl UiRenderState {
if let Some(idx) = retired_move {
self.moves.remove(idx);
}
// A rule that is a fraction of the frame is answered with the
// frame's own length, so the answer is that frame's and not just
// A rule that is a fraction of the rel base is answered with the
// rel base's own length, so the answer is that rel base's and not just
// that many pixels of this window -- the same pin a widget that read
// its frame took for its drawing.
let frame_len = AXES.map(|axis| {
// its rel base took for its drawing.
let rel_base = AXES.map(|axis| {
let fraction = rules
.axis(axis)
.exact()
.is_some_and(|len| len.rel != Rel::ZERO);
match fraction {
true => Some(info.frame.axis(axis)),
false => own.frame_len[axis as usize],
true => Some(info.rel_base.axis(axis)),
false => own.rel_base[axis as usize],
}
});
let own_holds = LayoutHolds { frame_len, ..own };
let own_holds = LayoutHolds { rel_base, ..own };
let answer_holds = own_holds.and(answer_under);
let holds = under
.into_iter()
.fold(answer_holds, |holds, (_, child)| holds.and(child));
debug_assert!(
holds.contains(self.output_size, info.frame, region),
holds.contains(self.output_size, info.rel_base, region),
"'{}' ({id:?}) drew in {px:?}, outside the ranges it reported: {holds:?}",
rsc.widgets().label(id),
);
@@ -466,10 +466,10 @@ impl UiRenderState {
parent_move: move_idx,
region_node: false,
mask,
frame: UiVec2::FULL_SIZE,
rel_base: UiVec2::FULL_SIZE,
region: UiRegion::FULL,
placed: [Place::Within(Part::All); 2],
asked: [Place::Within(Part::All); 2],
placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::WHOLE); 2],
narrow: [None; 2],
re_asked: false,
px,
@@ -483,7 +483,7 @@ impl UiRenderState {
let active = ActiveData {
id,
placement: region,
frame: info.frame,
rel_base: info.rel_base,
narrow: info.narrow,
placed: info.placed,
asked: info.asked,
@@ -555,7 +555,7 @@ impl UiRenderState {
let answer = active.answer?;
answer
.1
.contains(self.output_size, info.frame, region)
.contains(self.output_size, info.rel_base, region)
.then_some(answer)
}
@@ -620,10 +620,13 @@ impl UiRenderState {
// In pixels, because the box is a fraction of the window and that
// may be what changed -- an unchanged fraction of a window half the
// size is half the widget.
if !active.holds.contains(self.output_size, info.frame, region) {
if !active
.holds
.contains(self.output_size, info.rel_base, region)
{
#[cfg(feature = "layout-diagnostics")]
{
// Which of the three said no, so a frame that redraws more
// Which of the three said no, so a rel base that redraws more
// than it should says where to look. They overlap: a drawing
// can be outside two of them at once.
let holds = active.holds;
@@ -633,9 +636,10 @@ impl UiRenderState {
diag::bump(Counter::OutsidePinnedLen);
}
if !holds.window[n].contains(self.output_size.axis(axis))
|| holds.frame_len[n].is_some_and(|pinned| pinned != info.frame.axis(axis))
|| holds.rel_base[n]
.is_some_and(|pinned| pinned != info.rel_base.axis(axis))
{
diag::bump(Counter::OutsideFrame);
diag::bump(Counter::OutsideRelBase);
}
if !holds.region[n]
.contains(region.axis(axis).len().to_px(self.output_size.axis(axis)))
@@ -677,7 +681,7 @@ impl UiRenderState {
}
self.redepth(id, info.depth);
let active = self.active.get_mut(&id).unwrap();
active.frame = info.frame;
active.rel_base = info.rel_base;
active.placed = info.placed;
#[cfg(feature = "layout-diagnostics")]
{
@@ -718,7 +722,7 @@ impl UiRenderState {
rsc: &mut dyn UiRsc,
) {
let active = &self.active[&child];
let (frame, region) = Self::ask_again(active, at, place);
let (rel_base, region) = Self::ask_again(active, at, place);
let placed = placement(
region,
active.measured().unwrap_or(active.size),
@@ -733,25 +737,25 @@ impl UiRenderState {
parent_move: at.move_idx,
region_node: active.move_idx != active.parent_move,
mask: at.mask,
frame,
rel_base,
region,
placed: place,
asked: active.asked,
narrow: active.narrow,
re_asked: active.re_asked,
px: frame.to_px(at.window),
px: rel_base.to_px(at.window),
};
self.relocate(child, placed, info, rsc);
}
/// The frame and the box a widget already drawn is given at `place` of
/// the box its parent is being taken as. What narrowed its frame and what
/// The rel base and the box a widget already drawn is given at `place` of
/// the box its parent is being taken as. What narrowed its rel base and what
/// it declared are its own record's, so both are resolved against that
/// parent's frame again exactly as the first ask resolved them.
/// parent's rel base again exactly as the first ask resolved them.
fn ask_again(active: &ActiveData, at: &Placing, place: [Place; 2]) -> (UiVec2, UiRegion) {
frame_and_region(
rel_base_and_region(
at.region,
at.frame,
at.rel_base,
place,
active.narrow,
active.declared,
@@ -776,7 +780,7 @@ impl UiRenderState {
let at = Placing {
id,
region: placed,
frame: info.frame,
rel_base: info.rel_base,
window: self.output_size,
depth: info.depth,
move_idx: active.move_idx,
@@ -881,10 +885,10 @@ impl UiRenderState {
ActiveData {
id,
placement: UiRegion::FULL,
frame: UiVec2::FULL_SIZE,
rel_base: UiVec2::FULL_SIZE,
narrow: [None; 2],
placed: [Place::Within(Part::All); 2],
asked: [Place::Within(Part::All); 2],
placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::WHOLE); 2],
region: UiRegion::FULL,
answer: None,
re_asked: false,
@@ -1112,10 +1116,10 @@ impl UiRenderState {
// box is its own to work out again against the output. Every other
// widget was given one.
let Some(parent) = active.parent else {
let (frame, region) = Self::root_layout(id, rsc.widgets());
let (rel_base, region) = Self::root_layout(id, rsc.widgets());
let info = DrawInfo {
mask: active.parent_mask,
..self.root_info(frame, region)
..self.root_info(rel_base, region)
};
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::LocalRedraws);
@@ -1130,7 +1134,7 @@ impl UiRenderState {
// parent's answer put its own drawing is not a question anybody
// asked, and nothing is asked in it here either.
let parent_at = self.placing_of(parent, self.active[&parent].region);
let (frame, region) = Self::ask_again(active, &parent_at, active.asked);
let (rel_base, region) = Self::ask_again(active, &parent_at, active.asked);
let info = DrawInfo {
layer: active.layer,
parent: active.parent,
@@ -1138,13 +1142,13 @@ impl UiRenderState {
parent_move: active.parent_move,
region_node: rsc.widgets().is_region_node(id),
mask: active.parent_mask,
frame,
rel_base,
region,
placed: active.asked,
asked: active.asked,
narrow: active.narrow,
re_asked: false,
px: frame.to_px(self.output_size),
px: rel_base.to_px(self.output_size),
};
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::LocalRedraws);
@@ -1161,7 +1165,7 @@ impl UiRenderState {
active.answer = was_answer;
}
if active.holds.covers(was_holds)
&& was_holds.contains(self.output_size, active.frame, active.placement)
&& was_holds.contains(self.output_size, active.rel_base, active.placement)
{
active.holds = was_holds;
}
@@ -1192,7 +1196,7 @@ impl UiRenderState {
Placing {
id,
region,
frame: active.frame,
rel_base: active.rel_base,
window: self.output_size,
depth: active.depth,
move_idx: active.move_idx,