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, TextBreaks,
GlyphPlacements, GlyphPlacements,
OutsidePinnedLen, OutsidePinnedLen,
OutsideFrame, OutsideRelBase,
OutsideRegion, OutsideRegion,
} }
@@ -89,7 +89,7 @@ impl Counter {
"text line breaks", "text line breaks",
"glyph placements", "glyph placements",
"reuse outside: the length it was pinned to", "reuse outside: the length it was pinned to",
"reuse outside: a frame length", "reuse outside: a rel base",
"reuse outside: a region length", "reuse outside: a region length",
]; ];
} }
+6 -6
View File
@@ -13,10 +13,10 @@ pub struct ActiveData {
pub placement: UiRegion, pub placement: UiRegion,
/// What a fraction declared or reported under this widget is a fraction /// What a fraction declared or reported under this widget is a fraction
/// of, as a length of the window. /// of, as a length of the window.
pub frame: UiVec2, pub rel_base: UiVec2,
/// A frame its parent decided for it on each axis -- a row's slot, or /// A rel base 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` /// padding's rel base less its pixels -- as a length of the window. `None`
/// forwards the parent's frame. What it declared is kept separately in /// 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. /// `declared` and is a fraction of whichever of the two reached it.
pub narrow: [Option<Len>; 2], pub narrow: [Option<Len>; 2],
/// Where its drawing was put, and where it was asked, each as a part of /// 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. /// What the widget reported, in window-unit lengths.
pub size: Size, pub size: Size,
/// The window and region reads that this drawing holds for, and the /// 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 holds: LayoutHolds,
pub drawn: bool, pub drawn: bool,
pub parent: Option<WidgetId>, pub parent: Option<WidgetId>,
@@ -61,7 +61,7 @@ pub struct ActiveData {
/// The movable region its primitives are positioned through: its own when /// The movable region its primitives are positioned through: its own when
/// opted in, otherwise the nearest ancestor's. /// opted in, otherwise the nearest ancestor's.
pub move_idx: MoveIdx, 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, /// A change to one moves a box this widget cannot fix by drawing again,
/// and comparing them is what says so. /// and comparing them is what says so.
pub declared: [Option<LayoutLen>; 2], 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 /// 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 /// 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 /// 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 /// 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 /// 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 /// parent's own; anywhere else the parent chose that length itself, and a
/// widget pinned this way is checked when it is re-placed. /// 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, /// A rel base pin says the answer or the drawing is a fraction of the rel base,
/// which is a different length wherever the frame is a different one -- at /// 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 /// 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 /// of the rel base that is only pixels is not one: it is that many pixels
/// whatever the frame turns out to be. /// whatever the rel base turns out to be.
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub struct LayoutHolds { pub struct LayoutHolds {
pub window: [Holds; 2], pub window: [Holds; 2],
pub frame_len: [Option<Len>; 2], pub rel_base: [Option<Len>; 2],
pub region: [Holds; 2], pub region: [Holds; 2],
pub region_len: [Option<Len>; 2], pub region_len: [Option<Len>; 2],
} }
@@ -29,7 +29,7 @@ pub struct LayoutHolds {
impl LayoutHolds { impl LayoutHolds {
pub const ANY: Self = Self { pub const ANY: Self = Self {
window: [Holds::ANY; 2], window: [Holds::ANY; 2],
frame_len: [None; 2], rel_base: [None; 2],
region: [Holds::ANY; 2], region: [Holds::ANY; 2],
region_len: [None; 2], region_len: [None; 2],
}; };
@@ -45,12 +45,12 @@ impl LayoutHolds {
|| self.region_len[n] == other.region_len[n] || self.region_len[n] == other.region_len[n]
); );
debug_assert!( debug_assert!(
self.frame_len[n].is_none() self.rel_base[n].is_none()
|| other.frame_len[n].is_none() || other.rel_base[n].is_none()
|| self.frame_len[n] == other.frame_len[n] || self.rel_base[n] == other.rel_base[n]
); );
result.region_len[n] = self.region_len[n].or(other.region_len[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 result
} }
@@ -62,16 +62,16 @@ impl LayoutHolds {
&& self.region[n].lo <= other.region[n].lo && self.region[n].lo <= other.region[n].lo
&& self.region[n].hi >= other.region[n].hi && self.region[n].hi >= other.region[n].hi
&& self.region_len[n].is_none_or(|len| other.region_len[n] == Some(len)) && 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| { AXES.into_iter().all(|axis| {
let n = axis as usize; let n = axis as usize;
let len = region.axis(axis).len(); let len = region.axis(axis).len();
self.window[n].contains(window.axis(axis)) 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[n].contains(len.to_px(window.axis(axis)))
&& self.region_len[n].is_none_or(|pinned| pinned == len) && 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) state: &'a mut UiRenderState,
pub(super) rsc: &'a mut dyn UiRsc, 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 /// 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 /// of. A length rather than a box, so padding can take from both the
/// frame and the box without either becoming the other. /// rel base and the box without either becoming the other.
pub(super) frame: UiVec2, pub(super) rel_base: UiVec2,
/// The box this widget was asked in, in its region node's coordinates: /// 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. /// what it draws in, and what its children's places are parts of.
pub(super) region: UiRegion, pub(super) region: UiRegion,
@@ -41,7 +41,7 @@ pub struct Painter<'a> {
pub(super) size_deps: Vec<WidgetId>, pub(super) size_deps: Vec<WidgetId>,
/// What this draw itself reads, as against what its children's drawings /// 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 /// 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 /// region length it read symbolically, each of which makes the drawing
/// hold for that length alone. /// hold for that length alone.
pub(super) own: LayoutHolds, 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 /// forwarded unchanged: what a container that is only a wrapper around
/// one child wants, and what every transparent container passes for the /// 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> { 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. /// 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 /// `narrow` is the child's rel base, per axis, as a length of the
/// axis, as a length of this widget's own frame: a resolved share, or a /// window: a resolved share, or a box a sibling's answer decided. `None`
/// box a sibling's answer decided. `None` forwards this widget's frame, /// forwards this widget's own, which is what a container that only
/// which is what a container that only divides room passes, so a /// divides room passes, so a fraction under it means the same wherever
/// fraction under it means the same wherever it sits and however deeply /// it sits and however deeply it is nested. It only ever narrows -- a
/// it is nested. A declared length narrows the frame here whatever the /// length the child declares narrows it again here whatever the caller
/// caller says. A narrowed frame is placed in the part by the child's /// says -- and what comes of it is also the box the child is asked in,
/// alignment and is 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 /// `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 /// 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 region_node = self.rsc.widgets().is_region_node(id.id());
let declared = self.declared_lens(id); let declared = self.declared_lens(id);
let align = self.rsc.widgets().alignment(id.id()); let align = self.rsc.widgets().alignment(id.id());
let (frame, region) = let (rel_base, region) =
frame_and_region(self.region, self.frame, place, narrow, declared, align); rel_base_and_region(self.region, self.rel_base, place, narrow, declared, align);
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
if region_node { if region_node {
diag::bump(Counter::RegionNodeDraws); diag::bump(Counter::RegionNodeDraws);
@@ -189,7 +209,7 @@ impl<'a> Painter<'a> {
if !re_asked { if !re_asked {
self.children.push(id.id()); 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( let (size, answer_holds, holds) = self.state.draw_inner(
id.id(), id.id(),
DrawInfo { DrawInfo {
@@ -199,7 +219,7 @@ impl<'a> Painter<'a> {
parent_move: self.move_idx, parent_move: self.move_idx,
region_node, region_node,
mask: self.mask, mask: self.mask,
frame, rel_base,
region, region,
placed: place, placed: place,
asked: place, asked: place,
@@ -233,18 +253,36 @@ impl<'a> Painter<'a> {
self.state.undraw_rec(id.id(), self.rsc); self.state.undraw_rec(id.id(), self.rsc);
} }
/// Puts a child asked about in this draw somewhere else in this /// Puts a child in `place` of this widget's box, where that box is the
/// widget's box: its answer, placed in this part instead. The drawing /// answer the child already gave: the drawing is re-expressed there
/// is re-expressed there rather than made again -- what a row does once /// rather than made again -- what a row does once it knows every slot,
/// it knows every slot, having measured each child from its cursor. /// having measured each child from its cursor.
pub fn place_at<W: ?Sized>(&mut self, id: &StrongWidget<W>, place: [Place; 2]) { ///
debug_assert!( /// A child this draw has not asked about, and one whose rel base this
self.children.contains(&id.id()), /// narrows, is asked here instead: there is no answer to re-express, or
"'{}' placed a child it did not ask about in this draw", /// the question has changed. So a container that places every child the
self.label() /// 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(); let at = self.placing();
self.state.place_in(id.id(), &at, place, self.rsc); 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. /// This widget as the thing its children are placed within.
@@ -252,7 +290,7 @@ impl<'a> Painter<'a> {
Placing { Placing {
id: self.id, id: self.id,
region: self.region, region: self.region,
frame: self.frame, rel_base: self.rel_base,
window: self.window, window: self.window,
depth: self.depth, depth: self.depth,
move_idx: self.move_idx, 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 /// 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 /// that comes of them is kept on the child, and `redraw` compares it
/// there. /// there.
fn declared_lens<W: ?Sized>(&self, id: &StrongWidget<W>) -> [Option<LayoutLen>; 2] { 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, /// 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 /// 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. /// nothing narrowed gets. Asking counts as reading its size.
pub fn size_hint<W: ?Sized>(&mut self, id: &StrongWidget<W>, axis: Axis) -> Option<LayoutLen> { pub fn size_hint<W: ?Sized>(&mut self, id: &StrongWidget<W>, axis: Axis) -> Option<LayoutLen> {
let widgets = self.rsc.widgets(); let widgets = self.rsc.widgets();
@@ -281,8 +319,8 @@ impl<'a> Painter<'a> {
.get_dyn(id.id()) .get_dyn(id.id())
.and_then(|widget| widget.size_hint(axis)) .and_then(|widget| widget.size_hint(axis))
}); });
let frame = self.frame.axis(axis); let rel_base = self.rel_base.axis(axis);
let resolved = hint.map(|hint| hint.within_len(frame)); let resolved = hint.map(|hint| hint.within_len(rel_base));
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
{ {
diag::hint_read(id.id(), self.id, axis, resolved); diag::hint_read(id.id(), self.id, axis, resolved);
@@ -293,12 +331,12 @@ impl<'a> Painter<'a> {
} }
if let Some(hint) = hint { if let Some(hint) = hint {
self.depend_on(id); self.depend_on(id);
// Resolving a fraction against this frame makes this draw a // Resolving a fraction against this rel base makes this draw a
// function of the frame's length. The fraction to ask about is // function of the rel base's length. The fraction to ask about is
// the child's own: resolved against a frame of pixels, none is // the child's own: resolved against a rel base of pixels, none is
// left to see it by. // left to see it by.
if hint.rel != Rel::ZERO { 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 resolved
@@ -322,7 +360,7 @@ impl<'a> Painter<'a> {
ui.text.render(buffer, attrs, width) 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. // TODO: merge the text methods into the primitive ones.
pub fn glyphs(&mut self, text: &RenderedText, origin: UiRegion) { pub fn glyphs(&mut self, text: &RenderedText, origin: UiRegion) {
// Glyph offsets and sizes are pixels, which compose additively. // 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 /// 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 /// the drawing to that length -- and to nothing about where the box
/// starts, which is what lets a container move without being drawn /// starts, which is what lets a container move without being drawn
/// again. One axis at a time, because a container that divides one axis /// again. One axis at a time, because a container that divides one axis
@@ -369,14 +407,14 @@ impl<'a> Painter<'a> {
len len
} }
/// The symbolic length of this widget's frame along one axis: what a /// This widget's rel base along one axis: what a fraction it or anything
/// fraction it or anything under it declares is a fraction of. A /// under it declares or reports is a fraction of. A container reads it
/// container reads it to hand a length of it down -- padding, which /// to hand a length of it down -- padding, which takes its pixels off.
/// takes its pixels off. Reading it pins the drawing to that frame, the /// Reading it pins the drawing to that rel base, the way
/// way [`Self::region_len`] pins it to the box. /// [`Self::region_len`] pins it to the box.
pub fn frame_len(&mut self, axis: Axis) -> Len { pub fn rel_base(&mut self, axis: Axis) -> Len {
let len = self.frame.axis(axis); let len = self.rel_base.axis(axis);
self.own.frame_len[axis as usize] = Some(len); self.own.rel_base[axis as usize] = Some(len);
len len
} }
@@ -559,14 +597,14 @@ impl PrimitiveLike for &TextureHandle {
/// only what the child was asked with. /// only what the child was asked with.
impl Painter<'_> { impl Painter<'_> {
/// Window ranges are already about the one unit and combine directly. /// 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 /// 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, /// 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 /// 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 /// 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 /// what fractions under the child mean and leaves the box the part it
/// was given. /// was given.
fn in_parent( fn in_parent(
@@ -586,18 +624,8 @@ impl Painter<'_> {
let reaches = narrow[n].is_none() let reaches = narrow[n].is_none()
&& !matches!(place[n].part(), Part::Sized(_)) && !matches!(place[n].part(), Part::Sized(_))
&& declared[n].is_none_or(|len| len.rel != Rel::ZERO); && 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()) { 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 // 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 // own lengths, so what it holds for maps back through that
// part into a range on this widget's box. A length it pinned // 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 // 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 // widget's box reaches it, so what it holds for is a range
// on the window and none of it on that box. // 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 /// 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, /// share of what is left over is only a length to the widget dividing one,
/// so it passes up in the size instead. /// 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 /// it reported, on the side of the part its alignment says, and the whole
/// part wherever the answer fills it. /// 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. /// 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 /// 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, /// 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( pub(crate) fn placement(
region: UiRegion, region: UiRegion,
size: Size, size: Size,
@@ -689,27 +725,27 @@ pub(crate) fn placement(
placed 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. /// widget asking draws in.
/// ///
/// `own` is that widget's own box, and `place` what of it the child is /// `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 /// given. `narrow` is a rel base the container decided for the child -- a row's
/// slot, or padding's frame less its pixels -- and [`Part::Sized`] one a /// 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 /// 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 /// 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, /// 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 /// and is the only one of the three that also places the box: a box the
/// caller decided is what `place` names. /// caller decided is what `place` names.
pub(crate) fn frame_and_region( pub(crate) fn rel_base_and_region(
own: UiRegion, own: UiRegion,
parent_frame: UiVec2, parent_rel_base: UiVec2,
place: [Place; 2], place: [Place; 2],
narrow: [Option<Len>; 2], narrow: [Option<Len>; 2],
declared: [Option<LayoutLen>; 2], declared: [Option<LayoutLen>; 2],
align: RegionAlign, align: RegionAlign,
) -> (UiVec2, UiRegion) { ) -> (UiVec2, UiRegion) {
let given = region_of(own, place, align); let given = region_of(own, place, align);
let mut frame = parent_frame; let mut rel_base = parent_rel_base;
let mut region = given; let mut region = given;
for axis in AXES { for axis in AXES {
let n = axis as usize; let n = axis as usize;
@@ -719,18 +755,18 @@ pub(crate) fn frame_and_region(
}; };
let base = sized let base = sized
.or(narrow[n]) .or(narrow[n])
.unwrap_or_else(|| parent_frame.axis(axis)); .unwrap_or_else(|| parent_rel_base.axis(axis));
let len = declared[n] let len = declared[n]
.map(|len| Len::from_parts(len.rel, len.px).within_len(base)) .map(|len| Len::from_parts(len.rel, len.px).within_len(base))
.unwrap_or(base); .unwrap_or(base);
*frame.axis_mut(axis) = len; *rel_base.axis_mut(axis) = len;
if declared[n].is_some() { if declared[n].is_some() {
let slot = given.axis(axis); let slot = given.axis(axis);
let start = slot.start + (slot.len() - len).scale(align.axis(axis).rel()); let start = slot.start + (slot.len() - len).scale(align.axis(axis).rel());
*region.axis_mut(axis) = UiSpan::new(start, start + len); *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 /// 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. /// What of a widget's own box a child is given, along one axis.
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum Part { pub enum Part {
/// The whole of it.
All,
/// Window lengths from where the box starts, which is what a container /// 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 /// 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 /// 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 /// 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 /// 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), From(UiSpan),
/// A part of the box in its own coordinates, which is what a container /// 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 /// 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. /// then feeds back into the answer.
Of(UiSpan), Of(UiSpan),
/// A box of this length, wherever in the parent's box the child's own /// 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 /// 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 /// container's cursor -- what a stack's sizing child decides for the
/// rest. /// rest.
@@ -27,10 +25,13 @@ pub enum Part {
} }
impl 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. /// Where it lands in the coordinates `own` is in.
pub(crate) fn of(self, own: UiSpan, align: AxisAlign) -> UiSpan { pub(crate) fn of(self, own: UiSpan, align: AxisAlign) -> UiSpan {
match self { match self {
Self::All => own,
Self::From(span) => UiSpan::new(own.start + span.start, own.start + span.end), Self::From(span) => UiSpan::new(own.start + span.start, own.start + span.end),
Self::Of(span) => span.within(&own), Self::Of(span) => span.within(&own),
Self::Sized(len) => { Self::Sized(len) => {
+71 -67
View File
@@ -1,6 +1,6 @@
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter, ReuseOutcome, TimerKind}; 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::{ use crate::{
ActiveData, Axis, DrawLayers, IdLike, LayoutHolds, LayoutLen, Len, MaskIdx, MoveIdx, Moves, ActiveData, Axis, DrawLayers, IdLike, LayoutHolds, LayoutLen, Len, MaskIdx, MoveIdx, Moves,
Painter, Part, PixelRegion, Place, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan, Painter, Part, PixelRegion, Place, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan,
@@ -22,7 +22,7 @@ pub(super) struct DrawInfo {
pub mask: MaskIdx, pub mask: MaskIdx,
/// What a fraction declared or reported under this widget is a fraction /// What a fraction declared or reported under this widget is a fraction
/// of, as a length of the window. /// 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 /// The box the widget is asked in, in its parent region node's
/// coordinates. /// coordinates.
pub region: UiRegion, pub region: UiRegion,
@@ -31,12 +31,12 @@ pub(super) struct DrawInfo {
/// parent puts the answer somewhere else. /// parent puts the answer somewhere else.
pub placed: [Place; 2], pub placed: [Place; 2],
pub asked: [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. /// window, which the widget's own declaration is a fraction of.
pub narrow: [Option<Len>; 2], pub narrow: [Option<Len>; 2],
/// Whether the parent already asked about this widget in this draw. /// Whether the parent already asked about this widget in this draw.
pub re_asked: bool, 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, pub px: PxVec2,
} }
@@ -53,7 +53,7 @@ impl DrawInfo {
pub(super) struct Placing { pub(super) struct Placing {
pub id: WidgetId, pub id: WidgetId,
pub region: UiRegion, pub region: UiRegion,
pub frame: UiVec2, pub rel_base: UiVec2,
pub window: PxVec2, pub window: PxVec2,
pub depth: usize, pub depth: usize,
pub move_idx: MoveIdx, pub move_idx: MoveIdx,
@@ -120,13 +120,13 @@ impl UiRenderState {
let Some(root) = self.old_root else { return }; let Some(root) = self.old_root else { return };
let stands = self.active.get(&root).is_some_and(|active| { let stands = self.active.get(&root).is_some_and(|active| {
// Nothing above the root chose anything, so the box it was first // 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 // drawing have to stand in the new window, since nothing above
// it will ask either again. // it will ask either again.
let answer = active let answer = active
.answer .answer
.is_some_and(|(_, holds)| 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.frame, active.region) answer && active.holds.contains(size, active.rel_base, active.region)
}); });
if !stands { if !stands {
widgets.needs_redraw.insert(root); 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 /// The root is asked about in the output. Its own rules narrow both its
/// frame and box; nothing above it chose a different one. /// rel base and box; nothing above it chose a different one.
fn root_info(&self, frame: UiVec2, region: UiRegion) -> DrawInfo { fn root_info(&self, rel_base: UiVec2, region: UiRegion) -> DrawInfo {
let px = frame.to_px(self.output_size); let px = rel_base.to_px(self.output_size);
DrawInfo { DrawInfo {
layer: 0, layer: 0,
parent: None, parent: None,
@@ -144,10 +144,10 @@ impl UiRenderState {
parent_move: MoveIdx::NONE, parent_move: MoveIdx::NONE,
region_node: false, region_node: false,
mask: MaskIdx::NONE, mask: MaskIdx::NONE,
frame, rel_base,
region, region,
placed: [Place::Within(Part::All); 2], placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::All); 2], asked: [Place::Within(Part::WHOLE); 2],
narrow: [None; 2], narrow: [None; 2],
re_asked: false, re_asked: false,
px, px,
@@ -196,20 +196,20 @@ impl UiRenderState {
let _layout = diag::timer(TimerKind::FullLayout); let _layout = diag::timer(TimerKind::FullLayout);
self.clear(rsc); self.clear(rsc);
if let Some(id) = root { if let Some(id) = root {
let (frame, region) = Self::root_layout(id.id(), rsc.widgets()); let (rel_base, region) = Self::root_layout(id.id(), rsc.widgets());
let info = self.root_info(frame, region); let info = self.root_info(rel_base, region);
self.draw_inner(id.id(), info, None, rsc); 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 /// rules. Nothing above it narrowed anything or chose where it goes, so
/// its declaration is the whole of what decides either. /// its declaration is the whole of what decides either.
fn root_layout(id: WidgetId, widgets: &Widgets) -> (UiVec2, UiRegion) { fn root_layout(id: WidgetId, widgets: &Widgets) -> (UiVec2, UiRegion) {
frame_and_region( rel_base_and_region(
UiRegion::FULL, UiRegion::FULL,
UiVec2::FULL_SIZE, UiVec2::FULL_SIZE,
[Place::Within(Part::All); 2], [Place::Within(Part::WHOLE); 2],
[None; 2], [None; 2],
declared_lens(widgets, id), declared_lens(widgets, id),
widgets.alignment(id), widgets.alignment(id),
@@ -269,10 +269,10 @@ impl UiRenderState {
let drawing_holds = self.active[&id].holds; let drawing_holds = self.active[&id].holds;
let active = self.active.get_mut(&id).unwrap(); 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 // and what of its own box it asked in. A local redraw asks the same
// question again from these. // question again from these.
active.frame = info.frame; active.rel_base = info.rel_base;
active.narrow = info.narrow; active.narrow = info.narrow;
active.re_asked = info.re_asked; active.re_asked = info.re_asked;
active.answer = Some(answer); active.answer = Some(answer);
@@ -292,7 +292,7 @@ impl UiRenderState {
(answer.0, answer.1, drawing_holds) (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( fn draw_at(
&mut self, &mut self,
id: WidgetId, id: WidgetId,
@@ -301,7 +301,7 @@ impl UiRenderState {
old: Option<ActiveData>, old: Option<ActiveData>,
rsc: &mut dyn UiRsc, rsc: &mut dyn UiRsc,
) -> (Size, LayoutHolds) { ) -> (Size, LayoutHolds) {
let frame = info.frame; let rel_base = info.rel_base;
let (move_idx, region, retired_move) = match info.region_node { let (move_idx, region, retired_move) = match info.region_node {
// A node entry is only a translation. Its local box keeps the // A node entry is only a translation. Its local box keeps the
// same window-unit length as the box in its parent's node. // same window-unit length as the box in its parent's node.
@@ -324,7 +324,7 @@ impl UiRenderState {
let window = self.output_size; let window = self.output_size;
let mut painter = Painter { let mut painter = Painter {
state: self, state: self,
frame, rel_base,
region, region,
window, window,
mask: info.mask, mask: info.mask,
@@ -359,7 +359,7 @@ impl UiRenderState {
let Painter { let Painter {
state: _, state: _,
rsc: _, rsc: _,
frame: _, rel_base: _,
region: _, region: _,
window: _, window: _,
mask, mask,
@@ -387,18 +387,18 @@ impl UiRenderState {
// A rule wins on the axis it names, and the draw answers the rest. // 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 // 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 // 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 // rel base is the answer where the rule gave a length outright: it was
// resolved into the frame when the child was asked, and resolving it // resolved into the rel base when the child was asked, and resolving it
// again here would take the fraction of a fraction. // again here would take the fraction of a fraction.
let rules = rsc.widgets().size_rules(id); let rules = rsc.widgets().size_rules(id);
let ruled = |axis: Axis, reported: LayoutLen| match rules.axis(axis).exact() { let ruled = |axis: Axis, reported: LayoutLen| match rules.axis(axis).exact() {
None => reported, None => reported,
Some(len) if len.leftover == Weight::ZERO => LayoutLen { Some(len) if len.leftover == Weight::ZERO => LayoutLen {
rel: info.frame.axis(axis).rel, rel: info.rel_base.axis(axis).rel,
px: info.frame.axis(axis).px, px: info.rel_base.axis(axis).px,
leftover: Weight::ZERO, 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 { let size = Size {
x: ruled(Axis::X, size.x), x: ruled(Axis::X, size.x),
@@ -428,27 +428,27 @@ impl UiRenderState {
if let Some(idx) = retired_move { if let Some(idx) = retired_move {
self.moves.remove(idx); self.moves.remove(idx);
} }
// A rule that is a fraction of the frame is answered with the // A rule that is a fraction of the rel base is answered with the
// frame's own length, so the answer is that frame's and not just // 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 // that many pixels of this window -- the same pin a widget that read
// its frame took for its drawing. // its rel base took for its drawing.
let frame_len = AXES.map(|axis| { let rel_base = AXES.map(|axis| {
let fraction = rules let fraction = rules
.axis(axis) .axis(axis)
.exact() .exact()
.is_some_and(|len| len.rel != Rel::ZERO); .is_some_and(|len| len.rel != Rel::ZERO);
match fraction { match fraction {
true => Some(info.frame.axis(axis)), true => Some(info.rel_base.axis(axis)),
false => own.frame_len[axis as usize], 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 answer_holds = own_holds.and(answer_under);
let holds = under let holds = under
.into_iter() .into_iter()
.fold(answer_holds, |holds, (_, child)| holds.and(child)); .fold(answer_holds, |holds, (_, child)| holds.and(child));
debug_assert!( 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:?}", "'{}' ({id:?}) drew in {px:?}, outside the ranges it reported: {holds:?}",
rsc.widgets().label(id), rsc.widgets().label(id),
); );
@@ -466,10 +466,10 @@ impl UiRenderState {
parent_move: move_idx, parent_move: move_idx,
region_node: false, region_node: false,
mask, mask,
frame: UiVec2::FULL_SIZE, rel_base: UiVec2::FULL_SIZE,
region: UiRegion::FULL, region: UiRegion::FULL,
placed: [Place::Within(Part::All); 2], placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::All); 2], asked: [Place::Within(Part::WHOLE); 2],
narrow: [None; 2], narrow: [None; 2],
re_asked: false, re_asked: false,
px, px,
@@ -483,7 +483,7 @@ impl UiRenderState {
let active = ActiveData { let active = ActiveData {
id, id,
placement: region, placement: region,
frame: info.frame, rel_base: info.rel_base,
narrow: info.narrow, narrow: info.narrow,
placed: info.placed, placed: info.placed,
asked: info.asked, asked: info.asked,
@@ -555,7 +555,7 @@ impl UiRenderState {
let answer = active.answer?; let answer = active.answer?;
answer answer
.1 .1
.contains(self.output_size, info.frame, region) .contains(self.output_size, info.rel_base, region)
.then_some(answer) .then_some(answer)
} }
@@ -620,10 +620,13 @@ impl UiRenderState {
// In pixels, because the box is a fraction of the window and that // 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 // may be what changed -- an unchanged fraction of a window half the
// size is half the widget. // 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")] #[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 // than it should says where to look. They overlap: a drawing
// can be outside two of them at once. // can be outside two of them at once.
let holds = active.holds; let holds = active.holds;
@@ -633,9 +636,10 @@ impl UiRenderState {
diag::bump(Counter::OutsidePinnedLen); diag::bump(Counter::OutsidePinnedLen);
} }
if !holds.window[n].contains(self.output_size.axis(axis)) 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] if !holds.region[n]
.contains(region.axis(axis).len().to_px(self.output_size.axis(axis))) .contains(region.axis(axis).len().to_px(self.output_size.axis(axis)))
@@ -677,7 +681,7 @@ impl UiRenderState {
} }
self.redepth(id, info.depth); self.redepth(id, info.depth);
let active = self.active.get_mut(&id).unwrap(); let active = self.active.get_mut(&id).unwrap();
active.frame = info.frame; active.rel_base = info.rel_base;
active.placed = info.placed; active.placed = info.placed;
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
{ {
@@ -718,7 +722,7 @@ impl UiRenderState {
rsc: &mut dyn UiRsc, rsc: &mut dyn UiRsc,
) { ) {
let active = &self.active[&child]; 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( let placed = placement(
region, region,
active.measured().unwrap_or(active.size), active.measured().unwrap_or(active.size),
@@ -733,25 +737,25 @@ impl UiRenderState {
parent_move: at.move_idx, parent_move: at.move_idx,
region_node: active.move_idx != active.parent_move, region_node: active.move_idx != active.parent_move,
mask: at.mask, mask: at.mask,
frame, rel_base,
region, region,
placed: place, placed: place,
asked: active.asked, asked: active.asked,
narrow: active.narrow, narrow: active.narrow,
re_asked: active.re_asked, re_asked: active.re_asked,
px: frame.to_px(at.window), px: rel_base.to_px(at.window),
}; };
self.relocate(child, placed, info, rsc); self.relocate(child, placed, info, rsc);
} }
/// The frame and the box a widget already drawn is given at `place` of /// 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 frame and what /// 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 /// 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) { fn ask_again(active: &ActiveData, at: &Placing, place: [Place; 2]) -> (UiVec2, UiRegion) {
frame_and_region( rel_base_and_region(
at.region, at.region,
at.frame, at.rel_base,
place, place,
active.narrow, active.narrow,
active.declared, active.declared,
@@ -776,7 +780,7 @@ impl UiRenderState {
let at = Placing { let at = Placing {
id, id,
region: placed, region: placed,
frame: info.frame, rel_base: info.rel_base,
window: self.output_size, window: self.output_size,
depth: info.depth, depth: info.depth,
move_idx: active.move_idx, move_idx: active.move_idx,
@@ -881,10 +885,10 @@ impl UiRenderState {
ActiveData { ActiveData {
id, id,
placement: UiRegion::FULL, placement: UiRegion::FULL,
frame: UiVec2::FULL_SIZE, rel_base: UiVec2::FULL_SIZE,
narrow: [None; 2], narrow: [None; 2],
placed: [Place::Within(Part::All); 2], placed: [Place::Within(Part::WHOLE); 2],
asked: [Place::Within(Part::All); 2], asked: [Place::Within(Part::WHOLE); 2],
region: UiRegion::FULL, region: UiRegion::FULL,
answer: None, answer: None,
re_asked: false, re_asked: false,
@@ -1112,10 +1116,10 @@ impl UiRenderState {
// box is its own to work out again against the output. Every other // box is its own to work out again against the output. Every other
// widget was given one. // widget was given one.
let Some(parent) = active.parent else { 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 { let info = DrawInfo {
mask: active.parent_mask, mask: active.parent_mask,
..self.root_info(frame, region) ..self.root_info(rel_base, region)
}; };
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::LocalRedraws); diag::bump(Counter::LocalRedraws);
@@ -1130,7 +1134,7 @@ impl UiRenderState {
// parent's answer put its own drawing is not a question anybody // parent's answer put its own drawing is not a question anybody
// asked, and nothing is asked in it here either. // asked, and nothing is asked in it here either.
let parent_at = self.placing_of(parent, self.active[&parent].region); 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 { let info = DrawInfo {
layer: active.layer, layer: active.layer,
parent: active.parent, parent: active.parent,
@@ -1138,13 +1142,13 @@ impl UiRenderState {
parent_move: active.parent_move, parent_move: active.parent_move,
region_node: rsc.widgets().is_region_node(id), region_node: rsc.widgets().is_region_node(id),
mask: active.parent_mask, mask: active.parent_mask,
frame, rel_base,
region, region,
placed: active.asked, placed: active.asked,
asked: active.asked, asked: active.asked,
narrow: active.narrow, narrow: active.narrow,
re_asked: false, re_asked: false,
px: frame.to_px(self.output_size), px: rel_base.to_px(self.output_size),
}; };
#[cfg(feature = "layout-diagnostics")] #[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::LocalRedraws); diag::bump(Counter::LocalRedraws);
@@ -1161,7 +1165,7 @@ impl UiRenderState {
active.answer = was_answer; active.answer = was_answer;
} }
if active.holds.covers(was_holds) 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; active.holds = was_holds;
} }
@@ -1192,7 +1196,7 @@ impl UiRenderState {
Placing { Placing {
id, id,
region, region,
frame: active.frame, rel_base: active.rel_base,
window: self.output_size, window: self.output_size,
depth: active.depth, depth: active.depth,
move_idx: active.move_idx, move_idx: active.move_idx,
+2 -2
View File
@@ -120,7 +120,7 @@ impl Widget for Branch {
let cut = Len::from_parts(Rel::ZERO, Px::from_int(40)); let cut = Len::from_parts(Rel::ZERO, Px::from_int(40));
let top = Place::Within(Part::From(UiSpan::new(Len::ZERO, cut))); let top = Place::Within(Part::From(UiSpan::new(Len::ZERO, cut)));
let measured = painter let measured = painter
.widget_at(&self.probe, [None; 2], [Place::Within(Part::All), top]) .widget_at(&self.probe, [None; 2], [Place::Within(Part::WHOLE), top])
.len(Axis::X); .len(Axis::X);
let len = measured.apply_leftover(); let len = measured.apply_leftover();
let px = painter.to_px(len, Axis::X); let px = painter.to_px(len, Axis::X);
@@ -135,7 +135,7 @@ impl Widget for Branch {
painter.window_holds(Axis::X, holds.through(len)); painter.window_holds(Axis::X, holds.through(len));
let below = Place::Within(Part::From(UiSpan::new(cut, painter.region_len(Axis::Y)))); let below = Place::Within(Part::From(UiSpan::new(cut, painter.region_len(Axis::Y))));
let place = [Place::Within(Part::All), below]; let place = [Place::Within(Part::WHOLE), below];
match px > threshold { match px > threshold {
true => painter.widget_at(&self.wide, [None; 2], place), true => painter.widget_at(&self.wide, [None; 2], place),
false => painter.widget_at(&self.narrow, [None; 2], place), false => painter.widget_at(&self.narrow, [None; 2], place),
+3 -9
View File
@@ -7,14 +7,8 @@ pub struct Offset {
impl Widget for Offset { impl Widget for Offset {
fn draw(&mut self, painter: &mut Painter) -> Size { fn draw(&mut self, painter: &mut Painter) -> Size {
// The whole of this widget's box, moved: the frame passes through, so painter
// what the child declares or reports means the same as it would .widget_within(&self.inner, UiRegion::FULL.offset(self.amt))
// without the offset. .size()
let moved = |len: Len, amt: Len| Place::Within(Part::From(UiSpan::new(amt, len + amt)));
let place = [
moved(painter.region_len(Axis::X), self.amt.x),
moved(painter.region_len(Axis::Y), self.amt.y),
];
painter.widget_at(&self.inner, [None; 2], place).size()
} }
} }
+4 -21
View File
@@ -14,31 +14,14 @@ impl Widget for Pad {
// widget -- the slack is the inner's to sit in, and forcing the near // widget -- the slack is the inner's to sit in, and forcing the near
// edge pinned it to a corner it had not asked for. // edge pinned it to a corner it had not asked for.
// //
// Padding is an inset of both: it comes off the frame, so `rel(1)` // Padding is an inset of both: it comes off the rel base, so `rel(1)`
// under it fills this widget rather than overflowing it by the // under it fills this widget rather than overflowing it by the
// padding, and it comes off the box, so what is drawn sits inside. // padding, and it comes off the box, so what is drawn sits inside.
// The two stay distinct -- the box can be narrower still, where a row // The two stay distinct -- the box can be narrower still, where a row
// asked this widget in the room left, and a text wraps at that. // asked this widget in the room left, and a text wraps at that.
let inset = |lead: Px, trail: Px| { let inner = painter
Place::Within(Part::Of(UiSpan::new( .widget_within(&self.inner, self.padding.region())
Len::from_parts(Rel::ZERO, lead), .size();
Len::from_parts(Rel::ONE, -trail),
)))
};
let place = [
inset(self.padding.left, self.padding.right),
inset(self.padding.top, self.padding.bottom),
];
// Read from this widget's own frame rather than written as a
// fraction of it: a frame is a length of the window like everything
// else here, and taking the padding off is the whole of what this
// widget does to it.
let narrow = [
(Axis::X, self.padding.left + self.padding.right),
(Axis::Y, self.padding.top + self.padding.bottom),
]
.map(|(axis, pixels)| Some(painter.frame_len(axis) - Len::from_parts(Rel::ZERO, pixels)));
let inner = painter.widget_at(&self.inner, narrow, place).size();
Size { Size {
x: LayoutLen { x: LayoutLen {
px: inner.x.px + self.padding.left + self.padding.right, px: inner.x.px + self.padding.left + self.padding.right,
+6 -4
View File
@@ -14,7 +14,7 @@ impl Widget for Scroll {
let container_len = painter.px_len(self.axis); let container_len = painter.px_len(self.axis);
// Asked in the whole viewport, then put at the scrolled offset. // Asked in the whole viewport, then put at the scrolled offset.
let answer_len = painter let answer_len = painter
.widget_at(&self.inner, [None; 2], [Place::Fill(Part::All); 2]) .widget_at(&self.inner, [None; 2], [Place::Fill(Part::WHOLE); 2])
.len(self.axis); .len(self.axis);
let fixed = painter.to_px(Len::from_parts(answer_len.rel, answer_len.px), self.axis); let fixed = painter.to_px(Len::from_parts(answer_len.rel, answer_len.px), self.axis);
self.container_len = container_len; self.container_len = container_len;
@@ -56,15 +56,17 @@ impl Widget for Scroll {
let start = Len::from_parts(Rel::ZERO, anchor - self.amt); let start = Len::from_parts(Rel::ZERO, anchor - self.amt);
Part::From(UiSpan::new(start, start.offset(self.content_len))) Part::From(UiSpan::new(start, start.offset(self.content_len)))
} }
false => Part::All, false => Part::WHOLE,
}; };
// The viewport is the inner's frame, so a fraction it declares or // The viewport is the inner's rel base, so a fraction it declares or
// reports is a fraction of what is on screen rather than of the // reports is a fraction of what is on screen rather than of the
// content box its own answer decided. Where it goes is the content // content box its own answer decided. Where it goes is the content
// box, scrolled: its drawing moved there, not made again there. // box, scrolled: its drawing moved there, not made again there.
painter.place_at( painter.place_at(
&self.inner, &self.inner,
self.axis.pair(Place::Fill(content), Place::Fill(Part::All)), [None; 2],
self.axis
.pair(Place::Fill(content), Place::Fill(Part::WHOLE)),
); );
// What it occupies is its box, on both axes: it clips its content to // What it occupies is its box, on both axes: it clips its content to
// that box, so it can neither take less of one nor honestly ask for // that box, so it can neither take less of one nor honestly ask for
+10 -22
View File
@@ -10,7 +10,7 @@ pub struct Span {
impl Widget for Span { impl Widget for Span {
fn draw(&mut self, painter: &mut Painter) -> Size { fn draw(&mut self, painter: &mut Painter) -> Size {
let axis = self.dir.axis; let axis = self.dir.axis;
// The row: this span's own box, as a length of the frame its children // The row: this span's own box, as a length of the rel base its children
// are laid out against. Its start is nothing's business -- a slot is // are laid out against. Its start is nothing's business -- a slot is
// a length from it -- so what this reads is the length alone. // a length from it -- so what this reads is the length alone.
let far = painter.region_len(axis); let far = painter.region_len(axis);
@@ -21,29 +21,23 @@ impl Widget for Span {
// Across itself the child sits where its own alignment says, in the // Across itself the child sits where its own alignment says, in the
// whole of the row: a span is what contains its children there, and // whole of the row: a span is what contains its children there, and
// nothing divides that axis. // nothing divides that axis.
let across = Place::Within(Part::All); let across = Place::Within(Part::WHOLE);
// A length for every child before their final slots are chosen: from // A length for every child before their final slots are chosen: from
// a hint where one says, and from drawing otherwise. The frame passes // a hint where one says, and from drawing otherwise. The rel base passes
// through unchanged, so `rel(0.5)` is half the area this span was // through unchanged, so `rel(0.5)` is half the area this span was
// given whatever else is in it and wherever this child sits among // given whatever else is in it and wherever this child sits among
// them; what a drawn child is asked in is the room left from the // them; what a drawn child is asked in is the room left from the
// cursor, because a text has to wrap at the width actually there. // cursor, because a text has to wrap at the width actually there.
let mut cursor = Len::rel_min(); let mut cursor = Len::rel_min();
let mut lens = Vec::with_capacity(self.children.len()); let mut lens = Vec::with_capacity(self.children.len());
let mut drawn_across = Vec::with_capacity(self.children.len());
for child in &self.children { for child in &self.children {
let len = match painter.size_hint(child, axis) { let len = match painter.size_hint(child, axis) {
Some(len) => { Some(len) => len,
drawn_across.push(None);
len
}
None => { None => {
let room = Place::Within(Part::From(along(cursor, far))); let room = Place::Within(Part::From(along(cursor, far)));
let size = painter painter
.widget_at(child, [None; 2], axis.pair(room, across)) .widget_at(child, [None; 2], axis.pair(room, across))
.size(); .len(axis)
drawn_across.push(Some(size.axis(!axis)));
size.axis(axis)
} }
}; };
cursor.px += len.px + self.gap; cursor.px += len.px + self.gap;
@@ -63,7 +57,7 @@ impl Widget for Span {
); );
// What is left for the shares to divide: the row less everything // What is left for the shares to divide: the row less everything
// fixed, as a length of the frame rather than a number of pixels. // fixed, as a length of the rel base rather than a number of pixels.
let room = far - Len::from_parts(total.rel, total.px); let room = far - Len::from_parts(total.rel, total.px);
// Whether anything is left over is a question in pixels: `rel(0.5)` // Whether anything is left over is a question in pixels: `rel(0.5)`
// beside 300 px is full at 600 and overfull at 400. Asked of `room` // beside 300 px is full at 600 and overfull at 400. Asked of `room`
@@ -100,7 +94,7 @@ impl Widget for Span {
let mut taken = Weight::ZERO; let mut taken = Weight::ZERO;
let mut start = Len::rel_min(); let mut start = Len::rel_min();
let mut ortho = LayoutLen::ZERO; let mut ortho = LayoutLen::ZERO;
for ((child, &len), &across_len) in self.children.iter().zip(&lens).zip(&drawn_across) { for (child, &len) in self.children.iter().zip(&lens) {
// A child asking for nothing but a part of what is left over, // A child asking for nothing but a part of what is left over,
// when nothing is, is not drawn at all. One that also asked for // when nothing is, is not drawn at all. One that also asked for
// pixels or a fraction keeps those and overflows. // pixels or a fraction keeps those and overflows.
@@ -121,7 +115,7 @@ impl Widget for Span {
// Along the row the span says where the child goes, and that slot // Along the row the span says where the child goes, and that slot
// is the child's box outright rather than something to place an // is the child's box outright rather than something to place an
// answer inside again. A share is decided here and nowhere // answer inside again. A share is decided here and nowhere
// else: its slot narrows its frame, and the child is asked in // else: its slot narrows its rel base, and the child is asked in
// it, since a text wraps at the width it is actually given. A // it, since a text wraps at the width it is actually given. A
// fixed child's slot is its own answer, so a drawing made in the // fixed child's slot is its own answer, so a drawing made in the
// room is put there as it is, and one not made yet is made here. // room is put there as it is, and one not made yet is made here.
@@ -131,13 +125,7 @@ impl Widget for Span {
if len.leftover > Weight::ZERO && shares { if len.leftover > Weight::ZERO && shares {
narrow[axis as usize] = Some(slot.len()); narrow[axis as usize] = Some(slot.len());
} }
let used = match (across_len, narrow[axis as usize]) { let used = painter.place_at(child, narrow, place).len(!axis);
(Some(across_len), None) => {
painter.place_at(child, place);
across_len
}
_ => painter.widget_at(child, narrow, place).len(!axis),
};
if shrinks { if shrinks {
// Choosing between a fixed and a relative length from the // Choosing between a fixed and a relative length from the
// span's own eventual width admits multiple fixed points. // span's own eventual width admits multiple fixed points.
+2 -2
View File
@@ -23,7 +23,7 @@ impl Widget for Stack {
Some((i, child)) => { Some((i, child)) => {
painter.child_layer_at(i); painter.child_layer_at(i);
painter painter
.widget_at(child, [None; 2], [Place::Fill(Part::All); 2]) .widget_at(child, [None; 2], [Place::Fill(Part::WHOLE); 2])
.size() .size()
} }
None => Size::LEFTOVER, None => Size::LEFTOVER,
@@ -37,7 +37,7 @@ impl Widget for Stack {
let len = size.axis(axis); let len = size.axis(axis);
match len.leftover == Weight::ZERO { match len.leftover == Weight::ZERO {
true => Place::Fill(Part::Sized(Len::from_parts(len.rel, len.px))), true => Place::Fill(Part::Sized(Len::from_parts(len.rel, len.px))),
false => Place::Within(Part::All), false => Place::Within(Part::WHOLE),
} }
}); });
for (i, child) in self.children.iter().enumerate() { for (i, child) in self.children.iter().enumerate() {
+2 -2
View File
@@ -24,12 +24,12 @@ impl Widget for BranchesOnMeasurement {
let cut = Len::from_parts(Rel::ZERO, Px::from_int(40)); let cut = Len::from_parts(Rel::ZERO, Px::from_int(40));
let top = Place::Within(Part::From(UiSpan::new(Len::ZERO, cut))); let top = Place::Within(Part::From(UiSpan::new(Len::ZERO, cut)));
let measured = painter let measured = painter
.widget_at(&self.probe, [None; 2], [Place::Within(Part::All), top]) .widget_at(&self.probe, [None; 2], [Place::Within(Part::WHOLE), top])
.len(Axis::X); .len(Axis::X);
let px = painter.to_px(measured.apply_leftover(), Axis::X); let px = painter.to_px(measured.apply_leftover(), Axis::X);
let below = Place::Within(Part::From(UiSpan::new(cut, painter.region_len(Axis::Y)))); let below = Place::Within(Part::From(UiSpan::new(cut, painter.region_len(Axis::Y))));
let place = [Place::Within(Part::All), below]; let place = [Place::Within(Part::WHOLE), below];
match px > Px::from_f32(self.threshold) { match px > Px::from_f32(self.threshold) {
true => painter.widget_at(&self.wide, [None; 2], place), true => painter.widget_at(&self.wide, [None; 2], place),
false => painter.widget_at(&self.narrow, [None; 2], place), false => painter.widget_at(&self.narrow, [None; 2], place),
+4 -4
View File
@@ -108,7 +108,7 @@ const PARAGRAPH: &str = "Wrapping shapes one source into as many lines as the bo
/// the icon's width, while a wrapping text beside it is asked in the room /// the icon's width, while a wrapping text beside it is asked in the room
/// left, 900 - 24 - 32, and wraps there. /// left, 900 - 24 - 32, and wraps there.
#[test] #[test]
fn padding_keeps_the_frame_distinct_from_the_room_left_in_a_row() { fn padding_keeps_the_rel_base_distinct_from_the_room_left_in_a_row() {
let mut h = Harness::new((900, 200)); let mut h = Harness::new((900, 200));
let icon = rect(Color::RED).width(24).add(&mut h.rsc); let icon = rect(Color::RED).width(24).add(&mut h.rsc);
let fill = rect(Color::GREEN).width(rel(1.0)).add(&mut h.rsc); let fill = rect(Color::GREEN).width(rel(1.0)).add(&mut h.rsc);
@@ -125,7 +125,7 @@ fn padding_keeps_the_frame_distinct_from_the_room_left_in_a_row() {
let active = &h.render.active[&text.id()]; let active = &h.render.active[&text.id()];
let window = h.render.output_size().x; let window = h.render.output_size().x;
let asked = active.region.x.len().to_px(window); let asked = active.region.x.len().to_px(window);
assert_eq!(active.frame.x.to_px(window), Px::from_int(868)); assert_eq!(active.rel_base.x.to_px(window), Px::from_int(868));
assert_eq!(asked, Px::from_int(844)); assert_eq!(asked, Px::from_int(844));
} }
@@ -155,7 +155,7 @@ fn a_share_inside_padding_fills_the_slot_it_was_given() {
/// The same padding in a share instead: the slot is 450, so both the /// The same padding in a share instead: the slot is 450, so both the
/// fraction and the wrap are the slot less the padding, and the two agree. /// fraction and the wrap are the slot less the padding, and the two agree.
#[test] #[test]
fn padding_narrows_both_frame_and_box_inside_a_share() { fn padding_narrows_both_rel_base_and_box_inside_a_share() {
let mut h = Harness::new((900, 200)); let mut h = Harness::new((900, 200));
let fill = rect(Color::GREEN).width(rel(1.0)).add(&mut h.rsc); let fill = rect(Color::GREEN).width(rel(1.0)).add(&mut h.rsc);
let padded = fill.pad(16).width(leftover(1)).add(&mut h.rsc); let padded = fill.pad(16).width(leftover(1)).add(&mut h.rsc);
@@ -170,7 +170,7 @@ fn padding_narrows_both_frame_and_box_inside_a_share() {
h.set_root((padded, other).span(Dir::RIGHT).width(rel(1.0))); h.set_root((padded, other).span(Dir::RIGHT).width(rel(1.0)));
let active = &h.render.active[&text.id()]; let active = &h.render.active[&text.id()];
let window = h.render.output_size().x; let window = h.render.output_size().x;
assert_eq!(active.frame.x.to_px(window), Px::from_int(418)); assert_eq!(active.rel_base.x.to_px(window), Px::from_int(418));
assert_eq!(active.region.x.len().to_px(window), Px::from_int(418)); assert_eq!(active.region.x.len().to_px(window), Px::from_int(418));
} }
+1 -1
View File
@@ -217,7 +217,7 @@ impl Widget for FromHint {
painter.widget_at( painter.widget_at(
&self.inner, &self.inner,
[None; 2], [None; 2],
[Place::Within(Part::All), Place::Within(Part::From(top))], [Place::Within(Part::WHOLE), Place::Within(Part::From(top))],
); );
Size::LEFTOVER Size::LEFTOVER
} }
+2 -2
View File
@@ -406,8 +406,8 @@ fn describe_widget(id: WidgetId, h: &Harness) -> String {
fn record(id: WidgetId, h: &Harness) -> String { fn record(id: WidgetId, h: &Harness) -> String {
let active = &h.render.active[&id]; let active = &h.render.active[&id];
format!( format!(
"frame {} region {} placement {} size {}", "rel_base {} region {} placement {} size {}",
active.frame, active.region, active.placement, active.size, active.rel_base, active.region, active.placement, active.size,
) )
} }