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

+2 -2
View File
@@ -53,7 +53,7 @@ pub(crate) enum Counter {
TextBreaks,
GlyphPlacements,
OutsidePinnedLen,
OutsideFrame,
OutsideRelBase,
OutsideRegion,
}
@@ -89,7 +89,7 @@ impl Counter {
"text line breaks",
"glyph placements",
"reuse outside: the length it was pinned to",
"reuse outside: a frame length",
"reuse outside: a rel base",
"reuse outside: a region length",
];
}
+6 -6
View File
@@ -13,10 +13,10 @@ pub struct ActiveData {
pub placement: UiRegion,
/// What a fraction declared or reported under this widget is a fraction
/// of, as a length of the window.
pub frame: UiVec2,
/// A frame its parent decided for it on each axis -- a row's slot, or
/// padding's frame less its pixels -- as a length of the window. `None`
/// forwards the parent's frame. What it declared is kept separately in
pub rel_base: UiVec2,
/// A rel base its parent decided for it on each axis -- a row's slot, or
/// padding's rel base less its pixels -- as a length of the window. `None`
/// forwards the parent's rel base. What it declared is kept separately in
/// `declared` and is a fraction of whichever of the two reached it.
pub narrow: [Option<Len>; 2],
/// Where its drawing was put, and where it was asked, each as a part of
@@ -41,7 +41,7 @@ pub struct ActiveData {
/// What the widget reported, in window-unit lengths.
pub size: Size,
/// The window and region reads that this drawing holds for, and the
/// frame and region it pinned.
/// rel base and region it pinned.
pub holds: LayoutHolds,
pub drawn: bool,
pub parent: Option<WidgetId>,
@@ -61,7 +61,7 @@ pub struct ActiveData {
/// The movable region its primitives are positioned through: its own when
/// opted in, otherwise the nearest ancestor's.
pub move_idx: MoveIdx,
/// The declared lengths whoever drew this widget resolved into its frame.
/// The declared lengths whoever drew this widget resolved into its rel base.
/// A change to one moves a box this widget cannot fix by drawing again,
/// and comparing them is what says so.
pub declared: [Option<LayoutLen>; 2],
+15 -15
View File
@@ -4,24 +4,24 @@ const AXES: [Axis; 2] = [Axis::X, Axis::Y];
/// What one evaluation of a widget depends on: the window lengths its reads
/// hold for, the pixel lengths of its own box, and the symbolic lengths of
/// that box and of its frame where either one is what it was expressed in.
/// that box and of its rel base where either one is what it was expressed in.
///
/// The symbolic lengths are pins rather than ranges: a container places its
/// children as lengths of its frame measured from where its own box starts,
/// children as lengths of its rel base measured from where its own box starts,
/// so what it draws turns on that box's length and on nothing about where it
/// is. A box pin reaches the parent only where the box it pinned is the
/// parent's own; anywhere else the parent chose that length itself, and a
/// widget pinned this way is checked when it is re-placed.
///
/// A frame pin says the answer or the drawing is a fraction of the frame,
/// which is a different length wherever the frame is a different one -- at
/// A rel base pin says the answer or the drawing is a fraction of the rel base,
/// which is a different length wherever the rel base is a different one -- at
/// the same window size, so no range of window pixels can say it. A length
/// of the frame that is only pixels is not one: it is that many pixels
/// whatever the frame turns out to be.
/// of the rel base that is only pixels is not one: it is that many pixels
/// whatever the rel base turns out to be.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LayoutHolds {
pub window: [Holds; 2],
pub frame_len: [Option<Len>; 2],
pub rel_base: [Option<Len>; 2],
pub region: [Holds; 2],
pub region_len: [Option<Len>; 2],
}
@@ -29,7 +29,7 @@ pub struct LayoutHolds {
impl LayoutHolds {
pub const ANY: Self = Self {
window: [Holds::ANY; 2],
frame_len: [None; 2],
rel_base: [None; 2],
region: [Holds::ANY; 2],
region_len: [None; 2],
};
@@ -45,12 +45,12 @@ impl LayoutHolds {
|| self.region_len[n] == other.region_len[n]
);
debug_assert!(
self.frame_len[n].is_none()
|| other.frame_len[n].is_none()
|| self.frame_len[n] == other.frame_len[n]
self.rel_base[n].is_none()
|| other.rel_base[n].is_none()
|| self.rel_base[n] == other.rel_base[n]
);
result.region_len[n] = self.region_len[n].or(other.region_len[n]);
result.frame_len[n] = self.frame_len[n].or(other.frame_len[n]);
result.rel_base[n] = self.rel_base[n].or(other.rel_base[n]);
}
result
}
@@ -62,16 +62,16 @@ impl LayoutHolds {
&& self.region[n].lo <= other.region[n].lo
&& self.region[n].hi >= other.region[n].hi
&& self.region_len[n].is_none_or(|len| other.region_len[n] == Some(len))
&& self.frame_len[n].is_none_or(|len| other.frame_len[n] == Some(len))
&& self.rel_base[n].is_none_or(|len| other.rel_base[n] == Some(len))
})
}
pub fn contains(self, window: PxVec2, frame: UiVec2, region: UiRegion) -> bool {
pub fn contains(self, window: PxVec2, rel_base: UiVec2, region: UiRegion) -> bool {
AXES.into_iter().all(|axis| {
let n = axis as usize;
let len = region.axis(axis).len();
self.window[n].contains(window.axis(axis))
&& self.frame_len[n].is_none_or(|pinned| pinned == frame.axis(axis))
&& self.rel_base[n].is_none_or(|pinned| pinned == rel_base.axis(axis))
&& self.region[n].contains(len.to_px(window.axis(axis)))
&& self.region_len[n].is_none_or(|pinned| pinned == len)
})
+110 -74
View File
@@ -17,11 +17,11 @@ pub struct Painter<'a> {
pub(super) state: &'a mut UiRenderState,
pub(super) rsc: &'a mut dyn UiRsc,
/// This widget's frame, per axis: a length of the window, and what a
/// This widget's rel base, per axis: a length of the window, and what a
/// fraction it or anything under it declares or reports is a fraction
/// of. A length rather than a box, so padding can take from both the
/// frame and the box without either becoming the other.
pub(super) frame: UiVec2,
/// rel base and the box without either becoming the other.
pub(super) rel_base: UiVec2,
/// The box this widget was asked in, in its region node's coordinates:
/// what it draws in, and what its children's places are parts of.
pub(super) region: UiRegion,
@@ -41,7 +41,7 @@ pub struct Painter<'a> {
pub(super) size_deps: Vec<WidgetId>,
/// What this draw itself reads, as against what its children's drawings
/// hold for: every window and every length of its own region until it
/// reads one, then that one unless it says otherwise, and the frame or
/// reads one, then that one unless it says otherwise, and the rel base or
/// region length it read symbolically, each of which makes the drawing
/// hold for that length alone.
pub(super) own: LayoutHolds,
@@ -144,24 +144,44 @@ impl<'a> Painter<'a> {
};
}
/// Draws a widget in the whole of this widget's own box, with the frame
/// Draws a widget in the whole of this widget's own box, with the rel base
/// forwarded unchanged: what a container that is only a wrapper around
/// one child wants, and what every transparent container passes for the
/// frame.
/// rel base.
pub fn widget<'s, W: ?Sized>(&'s mut self, id: &'s StrongWidget<W>) -> DrawResult<'s, 'a, W> {
self.widget_at(id, [None; 2], [Place::Within(Part::All); 2])
self.widget_within(id, UiRegion::FULL)
}
/// Draws a widget in `region` of this widget's own box, in that box's own
/// coordinates -- an inset, or an offset.
///
/// The child's rel base is this widget's narrowed the same way the box is,
/// so padding takes its pixels off both and an offset, which changes the
/// box's length by nothing, changes neither. An axis the region leaves
/// whole is not read at all, so a wrapper that only moves its child does
/// not pin the drawing to a rel base.
pub fn widget_within<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
region: UiRegion,
) -> DrawResult<'s, 'a, W> {
let narrow = AXES.map(|axis| {
let len = region.axis(axis).len();
(len != Len::FULL).then(|| len.within_len(self.rel_base(axis)))
});
self.widget_at(id, narrow, region_places(region))
}
/// Asks a child, saying what its fractions are of and where it is asked.
///
/// `narrow` is a length this widget decided for the child's frame, per
/// axis, as a length of this widget's own frame: a resolved share, or a
/// box a sibling's answer decided. `None` forwards this widget's frame,
/// which is what a container that only divides room passes, so a
/// fraction under it means the same wherever it sits and however deeply
/// it is nested. A declared length narrows the frame here whatever the
/// caller says. A narrowed frame is placed in the part by the child's
/// alignment and is the box the child is asked in.
/// `narrow` is the child's rel base, per axis, as a length of the
/// window: a resolved share, or a box a sibling's answer decided. `None`
/// forwards this widget's own, which is what a container that only
/// divides room passes, so a fraction under it means the same wherever
/// it sits and however deeply it is nested. It only ever narrows -- a
/// length the child declares narrows it again here whatever the caller
/// says -- and what comes of it is also the box the child is asked in,
/// placed in the part by the child's alignment.
///
/// `place` is where the child is asked, per axis, as a part of this
/// widget's box: see [`Place`]. The child draws once, in that box, and
@@ -177,8 +197,8 @@ impl<'a> Painter<'a> {
let region_node = self.rsc.widgets().is_region_node(id.id());
let declared = self.declared_lens(id);
let align = self.rsc.widgets().alignment(id.id());
let (frame, region) =
frame_and_region(self.region, self.frame, place, narrow, declared, align);
let (rel_base, region) =
rel_base_and_region(self.region, self.rel_base, place, narrow, declared, align);
#[cfg(feature = "layout-diagnostics")]
if region_node {
diag::bump(Counter::RegionNodeDraws);
@@ -189,7 +209,7 @@ impl<'a> Painter<'a> {
if !re_asked {
self.children.push(id.id());
}
let px = frame.to_px(self.window);
let px = rel_base.to_px(self.window);
let (size, answer_holds, holds) = self.state.draw_inner(
id.id(),
DrawInfo {
@@ -199,7 +219,7 @@ impl<'a> Painter<'a> {
parent_move: self.move_idx,
region_node,
mask: self.mask,
frame,
rel_base,
region,
placed: place,
asked: place,
@@ -233,18 +253,36 @@ impl<'a> Painter<'a> {
self.state.undraw_rec(id.id(), self.rsc);
}
/// Puts a child asked about in this draw somewhere else in this
/// widget's box: its answer, placed in this part instead. The drawing
/// is re-expressed there rather than made again -- what a row does once
/// it knows every slot, having measured each child from its cursor.
pub fn place_at<W: ?Sized>(&mut self, id: &StrongWidget<W>, place: [Place; 2]) {
debug_assert!(
self.children.contains(&id.id()),
"'{}' placed a child it did not ask about in this draw",
self.label()
);
/// Puts a child in `place` of this widget's box, where that box is the
/// answer the child already gave: the drawing is re-expressed there
/// rather than made again -- what a row does once it knows every slot,
/// having measured each child from its cursor.
///
/// A child this draw has not asked about, and one whose rel base this
/// narrows, is asked here instead: there is no answer to re-express, or
/// the question has changed. So a container that places every child the
/// same way says it once, and which of the two happens is this widget's
/// business rather than the caller's.
pub fn place_at<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
narrow: [Option<Len>; 2],
place: [Place; 2],
) -> DrawResult<'s, 'a, W> {
if narrow.iter().any(Option::is_some) || !self.children.contains(&id.id()) {
return self.widget_at(id, narrow, place);
}
let at = self.placing();
self.state.place_in(id.id(), &at, place, self.rsc);
let active = &self.state.active[&id.id()];
let size = active.measured().unwrap_or(active.size);
DrawResult {
child: id,
painter: self,
size,
// Read where it was asked; moving it is not a second answer.
answer_holds: LayoutHolds::ANY,
}
}
/// This widget as the thing its children are placed within.
@@ -252,7 +290,7 @@ impl<'a> Painter<'a> {
Placing {
id: self.id,
region: self.region,
frame: self.frame,
rel_base: self.rel_base,
window: self.window,
depth: self.depth,
move_idx: self.move_idx,
@@ -261,7 +299,7 @@ impl<'a> Painter<'a> {
}
/// What a widget's rules declare its lengths to be, which whoever draws
/// it resolves into its frame. Reading them depends on nothing -- the box
/// it resolves into its rel base. Reading them depends on nothing -- the box
/// that comes of them is kept on the child, and `redraw` compares it
/// there.
fn declared_lens<W: ?Sized>(&self, id: &StrongWidget<W>) -> [Option<LayoutLen>; 2] {
@@ -270,7 +308,7 @@ impl<'a> Painter<'a> {
/// What a child says its length is without being drawn, if it can say,
/// as the length its draw would report: a fraction in it is resolved
/// against this widget's frame, which is the frame a child asked with
/// against this widget's rel base, which is the rel base a child asked with
/// nothing narrowed gets. Asking counts as reading its size.
pub fn size_hint<W: ?Sized>(&mut self, id: &StrongWidget<W>, axis: Axis) -> Option<LayoutLen> {
let widgets = self.rsc.widgets();
@@ -281,8 +319,8 @@ impl<'a> Painter<'a> {
.get_dyn(id.id())
.and_then(|widget| widget.size_hint(axis))
});
let frame = self.frame.axis(axis);
let resolved = hint.map(|hint| hint.within_len(frame));
let rel_base = self.rel_base.axis(axis);
let resolved = hint.map(|hint| hint.within_len(rel_base));
#[cfg(feature = "layout-diagnostics")]
{
diag::hint_read(id.id(), self.id, axis, resolved);
@@ -293,12 +331,12 @@ impl<'a> Painter<'a> {
}
if let Some(hint) = hint {
self.depend_on(id);
// Resolving a fraction against this frame makes this draw a
// function of the frame's length. The fraction to ask about is
// the child's own: resolved against a frame of pixels, none is
// Resolving a fraction against this rel base makes this draw a
// function of the rel base's length. The fraction to ask about is
// the child's own: resolved against a rel base of pixels, none is
// left to see it by.
if hint.rel != Rel::ZERO {
self.own.frame_len[axis as usize] = Some(frame);
self.own.rel_base[axis as usize] = Some(rel_base);
}
}
resolved
@@ -322,7 +360,7 @@ impl<'a> Painter<'a> {
ui.text.render(buffer, attrs, width)
}
/// Writes glyphs in the selected frame or region coordinates.
/// Writes glyphs in the selected rel base or region coordinates.
// TODO: merge the text methods into the primitive ones.
pub fn glyphs(&mut self, text: &RenderedText, origin: UiRegion) {
// Glyph offsets and sizes are pixels, which compose additively.
@@ -358,7 +396,7 @@ impl<'a> Painter<'a> {
}
/// The symbolic length of this widget's own box along one axis, in the
/// lengths of its frame that it places its children in. Reading it pins
/// lengths of its rel base that it places its children in. Reading it pins
/// the drawing to that length -- and to nothing about where the box
/// starts, which is what lets a container move without being drawn
/// again. One axis at a time, because a container that divides one axis
@@ -369,14 +407,14 @@ impl<'a> Painter<'a> {
len
}
/// The symbolic length of this widget's frame along one axis: what a
/// fraction it or anything under it declares is a fraction of. A
/// container reads it to hand a length of it down -- padding, which
/// takes its pixels off. Reading it pins the drawing to that frame, the
/// way [`Self::region_len`] pins it to the box.
pub fn frame_len(&mut self, axis: Axis) -> Len {
let len = self.frame.axis(axis);
self.own.frame_len[axis as usize] = Some(len);
/// This widget's rel base along one axis: what a fraction it or anything
/// under it declares or reports is a fraction of. A container reads it
/// to hand a length of it down -- padding, which takes its pixels off.
/// Reading it pins the drawing to that rel base, the way
/// [`Self::region_len`] pins it to the box.
pub fn rel_base(&mut self, axis: Axis) -> Len {
let len = self.rel_base.axis(axis);
self.own.rel_base[axis as usize] = Some(len);
len
}
@@ -559,14 +597,14 @@ impl PrimitiveLike for &TextureHandle {
/// only what the child was asked with.
impl Painter<'_> {
/// Window ranges are already about the one unit and combine directly.
/// A frame pin becomes this widget's own frame wherever a length of it
/// A rel base pin becomes this widget's own rel base wherever a length of it
/// is what reached the child; where only pixels did, no length of this
/// frame can change the child's and the pin stops here.
/// rel base can change the child's and the pin stops here.
///
/// A child's validity maps back through the part of this widget's box,
/// where the box the child was asked in is that part; a declared length
/// places the box inside the part instead, and then only that length
/// reaches the child. A narrowed frame is not one of these: it decides
/// reaches the child. A narrowed rel base is not one of these: it decides
/// what fractions under the child mean and leaves the box the part it
/// was given.
fn in_parent(
@@ -586,18 +624,8 @@ impl Painter<'_> {
let reaches = narrow[n].is_none()
&& !matches!(place[n].part(), Part::Sized(_))
&& declared[n].is_none_or(|len| len.rel != Rel::ZERO);
result.frame_len[n] = holds.frame_len[n].and(reaches.then(|| self.frame.axis(axis)));
result.rel_base[n] = holds.rel_base[n].and(reaches.then(|| self.rel_base.axis(axis)));
match (place[n].part(), declared[n].is_some()) {
// Its box is this widget's own, or a part of it in that
// box's own lengths: so what it holds for is a range on this
// widget's own box, which is what lets that box move without
// a redraw. A length it pinned is this widget's length
// wherever the part is the whole of it, and pins the same
// way.
(Part::All, false) => {
result.region[n] = holds.region[n];
result.region_len[n] = holds.region_len[n];
}
// Its box is a part of this widget's own box, in that box's
// own lengths, so what it holds for maps back through that
// part into a range on this widget's box. A length it pinned
@@ -614,7 +642,7 @@ impl Painter<'_> {
});
}
// Its box is a length this widget decided, from its own
// frame or from a sibling's answer: no length of this
// rel base or from a sibling's answer: no length of this
// widget's box reaches it, so what it holds for is a range
// on the window and none of it on that box.
_ => {
@@ -627,6 +655,14 @@ impl Painter<'_> {
}
}
/// A box of a widget's own, per axis, with the answer placed inside it.
fn region_places(region: UiRegion) -> [Place; 2] {
[
Place::Within(Part::Of(region.x)),
Place::Within(Part::Of(region.y)),
]
}
/// What a widget declares a length of its box to be. `leftover` is not one: a
/// share of what is left over is only a length to the widget dividing one,
/// so it passes up in the size instead.
@@ -662,11 +698,11 @@ pub(crate) fn fills(reported: LayoutLen, declared: Option<LayoutLen>, decided: b
/// it reported, on the side of the part its alignment says, and the whole
/// part wherever the answer fills it.
///
/// The length it reported is a length of its frame, and the part is one too,
/// The length it reported is a length of its rel base, and the part is one too,
/// so this takes one from the other rather than composing it into the part.
/// That is what makes a fraction the same fraction wherever the part it is
/// placed in sits and however long it is -- the fraction is resolved once,
/// here, against the frame it was reported of.
/// here, against the rel base it was reported of.
pub(crate) fn placement(
region: UiRegion,
size: Size,
@@ -689,27 +725,27 @@ pub(crate) fn placement(
placed
}
/// The frame length and the box a child is asked in, in the coordinates the
/// The rel base length and the box a child is asked in, in the coordinates the
/// widget asking draws in.
///
/// `own` is that widget's own box, and `place` what of it the child is
/// given. `narrow` is a frame the container decided for the child -- a row's
/// slot, or padding's frame less its pixels -- and [`Part::Sized`] one a
/// given. `narrow` is a rel base the container decided for the child -- a row's
/// slot, or padding's rel base less its pixels -- and [`Part::Sized`] one a
/// sibling's answer decided; both are window lengths, like every other
/// length here, since a slot of a row is not a fraction of anything the row
/// can name. The child's declaration is a fraction of whichever reached it,
/// and is the only one of the three that also places the box: a box the
/// caller decided is what `place` names.
pub(crate) fn frame_and_region(
pub(crate) fn rel_base_and_region(
own: UiRegion,
parent_frame: UiVec2,
parent_rel_base: UiVec2,
place: [Place; 2],
narrow: [Option<Len>; 2],
declared: [Option<LayoutLen>; 2],
align: RegionAlign,
) -> (UiVec2, UiRegion) {
let given = region_of(own, place, align);
let mut frame = parent_frame;
let mut rel_base = parent_rel_base;
let mut region = given;
for axis in AXES {
let n = axis as usize;
@@ -719,18 +755,18 @@ pub(crate) fn frame_and_region(
};
let base = sized
.or(narrow[n])
.unwrap_or_else(|| parent_frame.axis(axis));
.unwrap_or_else(|| parent_rel_base.axis(axis));
let len = declared[n]
.map(|len| Len::from_parts(len.rel, len.px).within_len(base))
.unwrap_or(base);
*frame.axis_mut(axis) = len;
*rel_base.axis_mut(axis) = len;
if declared[n].is_some() {
let slot = given.axis(axis);
let start = slot.start + (slot.len() - len).scale(align.axis(axis).rel());
*region.axis_mut(axis) = UiSpan::new(start, start + len);
}
}
(frame, region)
(rel_base, region)
}
/// The part of a widget's own box a `place` names, in the coordinates that
+6 -5
View File
@@ -3,14 +3,12 @@ use crate::{AxisAlign, Len, PrimitiveHandle, UiRegion, UiSpan};
/// What of a widget's own box a child is given, along one axis.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Part {
/// The whole of it.
All,
/// Window lengths from where the box starts, which is what a container
/// dividing room among its children speaks: a child's report is a window
/// length, so the cursor that sums those reports is one too. A moved box
/// re-places every child by re-adding its start, exactly. A fraction
/// here is a fraction of the window and not of the box -- the whole of a
/// box is [`Self::All`], not a `rel(1.0)` span.
/// box is `Of(UiSpan::FULL)`, not a `rel(1.0)` span.
From(UiSpan),
/// A part of the box in its own coordinates, which is what a container
/// that insets one speaks: taking eleven pixels off the end needs no
@@ -19,7 +17,7 @@ pub enum Part {
/// then feeds back into the answer.
Of(UiSpan),
/// A box of this length, wherever in the parent's box the child's own
/// alignment puts it, and that same length as its frame. Unlike `From`,
/// alignment puts it, and that same length as its rel base. Unlike `From`,
/// it is a length decided from above rather than a place along a
/// container's cursor -- what a stack's sizing child decides for the
/// rest.
@@ -27,10 +25,13 @@ pub enum Part {
}
impl Part {
/// The whole of the box. Not a variant of its own: it composes and
/// inverts through the same expressions every other `Of` does.
pub const WHOLE: Self = Self::Of(UiSpan::FULL);
/// Where it lands in the coordinates `own` is in.
pub(crate) fn of(self, own: UiSpan, align: AxisAlign) -> UiSpan {
match self {
Self::All => own,
Self::From(span) => UiSpan::new(own.start + span.start, own.start + span.end),
Self::Of(span) => span.within(&own),
Self::Sized(len) => {
+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,