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:
1 parent
a904cf4f36
commit
aeb60e50f5
16 files changed
+246
-238
No files matched your search
+110
-74
@@ -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
|
||||
|
||||
Reference in new issue
Block a user