WIP: a widget's region stays put and its placement moves in it

The protocol split: `region` is the box a parent gives a widget -- what a
fraction it declares or reports is a fraction of, and the coordinates
every region it writes composes within -- and it is the same box on the
ask that measures and the ask that places. `placement` is what of that
region the drawing takes, chosen by the parent per axis or by the
widget's own answer and alignment.

That is what stops a fraction being resolved twice: the placing ask no
longer hands the widget its own answer as its box, so nothing under it
re-resolves against a box that came from its own report. `reports_of`
and `decided` are gone, folded into the two regions; `box_of` is gone;
`declared_box` becomes `ask_box`, which gives a rule the region's length
and takes the position from the placement.

84 of 92 suite tests pass. Five text and region-node cases still diverge
warm against cold, and three count a second widget draw where a span's
measuring ask and its placing ask give different placements.
This commit is contained in:
iris-ai committed 2026-09-17 13:53:14 -04:00
1 parent e44dea34b4
commit 5fcace1bfa
11 files changed
+346 -206

No files matched your search

+11
View File
@@ -7,6 +7,17 @@ pub enum Axis {
Y,
}
impl Axis {
/// A per-axis pair with `aligned` on this axis and `ortho` on the other,
/// which is what `from_axis` does for a vector.
pub fn pair<T>(self, aligned: T, ortho: T) -> [T; 2] {
match self {
Self::X => [aligned, ortho],
Self::Y => [ortho, aligned],
}
}
}
impl std::ops::Not for Axis {
type Output = Self;
+9 -5
View File
@@ -9,12 +9,16 @@ use crate::{
#[derive(Debug)]
pub struct ActiveData {
pub id: WidgetId,
/// The box its drawing is in, in `parent_move`'s coordinates.
/// The box its parent gave it, in `parent_move`'s coordinates: what it
/// was asked about, and what a fraction under it is a fraction of. A
/// local redraw asks here.
pub region: UiRegion,
/// The box its parent gave it, in the same coordinates: what it was
/// asked about, before its own answer placed its drawing inside it.
/// `region` is that placement, and a local redraw asks here.
pub given: UiRegion,
/// Where its drawing sits inside that box, in the box's own coordinates.
pub placement: UiRegion,
/// Whether its drawing read that placement, which is what says whether
/// moving it within the region is a redraw or only a different claim on
/// the same drawing.
pub reads_placement: bool,
/// The same box as lengths of its parent's box, which is the one route
/// to a box in pixels: a draw threads these down a level at a time, and
/// [`crate::UiRenderState::redraw`] takes the same steps back up.
+156 -85
View File
@@ -2,8 +2,8 @@
use crate::layout_diagnostics::{self as diag, Counter};
use crate::{
Axis, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign, RenderedText, Size, StrongWidget,
TextAttrs, TextBuffer, TextData, TextureHandle, UiRegion, UiRenderState, UiRsc, UiVec2, Weight,
WidgetId, Widgets,
TextAttrs, TextBuffer, TextData, TextureHandle, UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2,
Weight, WidgetId, Widgets,
render::{
GlyphPrimitive, Mask, MaskIdx, MoveIdx, Primitive, PrimitiveHandle, PrimitiveInst,
PrimitiveKind, TexturePrimitive,
@@ -17,8 +17,20 @@ pub struct Painter<'a> {
pub(super) state: &'a mut UiRenderState,
pub(super) rsc: &'a mut dyn UiRsc,
/// This widget's box, in the coordinates of `move_idx`.
/// The box its parent gave it, in the coordinates of `move_idx`: what a
/// fraction of this widget's area is a fraction of, and what every region
/// it writes composes within. The same box on the ask that measures and
/// the ask that places, which is what keeps a fraction under it from
/// being resolved twice.
pub(super) region: UiRegion,
/// Where this widget's drawing sits inside that box, in the box's own
/// coordinates: `FULL` while its answer is not yet known, and the box
/// its answer or its parent chose once one of them has.
pub(super) placement: UiRegion,
/// Whether this draw read its placement, which makes the drawing one
/// that holds for that placement alone -- the way reading a length in
/// pixels makes it hold for that length.
pub(super) reads_placement: bool,
/// That box in pixels, which its children's are a length of: threaded
/// down from the box this widget was given rather than composed back up
/// the chain, so every length in layout is one multiply from its
@@ -88,28 +100,37 @@ impl<'a> Painter<'a> {
self.primitives.push(h);
}
/// Writes a primitive to be rendered
/// Writes a primitive over the whole of this widget's own box.
pub fn primitive(&mut self, primitive: impl PrimitiveLike) {
let at = self.placed();
let primitive = primitive.into_primitive(self);
self.primitive_at(primitive, self.region)
self.primitive_at(primitive, at)
}
/// Writes one somewhere in this widget's region. A widget that wants its
/// own box rather than the region it was given composes through
/// [`Self::placement`] first.
pub fn primitive_within(&mut self, primitive: impl PrimitiveLike, region: UiRegion) {
let primitive = primitive.into_primitive(self);
self.primitive_at(primitive, region.within(&self.region));
}
/// `region` is in this widget's own region, as every region it writes is.
pub fn set_mask(&mut self, region: UiRegion) {
assert!(self.mask == MaskIdx::NONE);
self.mask = self.rsc.ui_mut().masks.push(Mask {
region,
region: region.within(&self.region),
move_idx: self.move_idx,
});
}
/// Draws a widget within this widget's region.
/// Draws a widget in the whole of this widget's own box: it gets the
/// same region -- the same area for its fractions to be of -- and is put
/// where this widget was put. What a container that is only a wrapper
/// around one child wants, since its box is the child's.
pub fn widget<'s, W: ?Sized>(&'s mut self, id: &'s StrongWidget<W>) -> DrawResult<'s, 'a, W> {
self.widget_within(id, UiRegion::FULL)
let own = self.placement();
self.widget_at(id, UiRegion::FULL, [Some(own.x), Some(own.y)])
}
/// What a widget's rules declare its lengths to be, which whoever draws
@@ -129,51 +150,41 @@ impl<'a> Painter<'a> {
}
/// Draws a widget somewhere within this one. `region` is in this widget's
/// own coordinates, and the child's declared lengths are still to be
/// taken from it. Where the child's drawing sits inside what it is given
/// is the child's alignment, applied where the child is drawn, so a
/// container positions a child either by handing it a box of exactly its
/// length or by leaving it room and letting its alignment decide.
/// own region, and is the child's own region: what its declared lengths
/// and its report are fractions of. Where its drawing sits inside that is
/// its own answer placed by its alignment.
pub fn widget_within<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
region: UiRegion,
) -> DrawResult<'s, 'a, W> {
self.widget_at(id, region, region.size(), [false; 2])
self.widget_at(id, region, [None; 2])
}
/// Draws a widget in `region`, saying what the answer means.
/// Draws a widget in `region`, saying where in it the drawing goes.
///
/// `reports_of` is what a fraction the child reports is a fraction of, as
/// lengths of this widget's own box. It is the box the child was given
/// wherever that box is the child's whole area -- a pad's inset, a stack
/// child, a scroll's content -- and a span passes its own extent along
/// the row instead: it offers each child the room left from its cursor,
/// because a text has to wrap at the width actually there, while
/// `rel(0.5)` still means half the span wherever the child sits in it.
/// `region` is the child's own area: what a fraction it declares or
/// reports is a fraction of, and the coordinates the regions it writes
/// compose within. It is the same box on the ask that measures and the
/// ask that places, which is what stops a fraction under it being
/// resolved twice.
///
/// A `decided` axis is one where this box was chosen from the widget's
/// own answer. On those the answer is not placed inside the box again: it
/// already is the box, and a fraction taken of it a second time would
/// shrink it twice. A container uses that where it hands back exactly
/// what a child asked for -- a span placing a child at the length it
/// reported, a scroll giving its content the content's own length.
/// `placement` is what of that region the child's drawing takes, per
/// axis, wherever this widget is choosing. `None` leaves the axis to the
/// child's own answer and alignment, which is what
/// [`Self::widget_within`] passes. A span passes the whole row as the
/// region, so `rel(0.5)` is half the row wherever the child sits in it,
/// and places the child by passing the slot along its axis.
pub fn widget_at<'s, W: ?Sized>(
&'s mut self,
id: &'s StrongWidget<W>,
region: UiRegion,
reports_of: UiVec2,
decided: [bool; 2],
placement: [Option<UiSpan>; 2],
) -> DrawResult<'s, 'a, W> {
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());
// Composing `FULL` through a box is not quite the identity in f32,
// so a child with nothing declared keeps the box it would have had.
let local = match declared.iter().any(Option::is_some) {
true => declared_box(region, declared, align),
false => region,
};
let (local, placement) = ask_box(region, declared, align, placement);
let within = match local == UiRegion::FULL {
true => self.region,
false => local.within(&self.region),
@@ -219,7 +230,7 @@ impl<'a> Painter<'a> {
offer_len,
px,
offered_px,
decided,
placement,
},
None,
self.rsc,
@@ -235,7 +246,7 @@ impl<'a> Painter<'a> {
DrawResult {
child: id,
painter: self,
size: in_parent_frame(size, reports_of, declared),
size: in_parent_frame(size, local.size(), declared),
}
}
@@ -267,20 +278,18 @@ impl<'a> Painter<'a> {
}
}
/// A child's length in the box it is about to be offered, if it can be
/// had without drawing it: from its hint, or from a drawing it already
/// has that holds for that box. `reports_of` is what a fraction in the
/// answer is a fraction of, as it is for [`Self::widget_at`].
/// A child's length in the region it is about to be offered, if it can
/// be had without drawing it: from its hint, or from a drawing it already
/// has that holds for that box.
pub fn known_len<W: ?Sized>(
&mut self,
child: &StrongWidget<W>,
axis: Axis,
region: UiRegion,
reports_of: UiVec2,
) -> Option<LayoutLen> {
let declared = self.declared_lens(child);
let align = self.rsc.widgets().alignment(child.id());
let local = declared_box(region, declared, align);
let (local, _) = ask_box(region, declared, align, [None; 2]);
let first_ask = self.offer(child.id());
if first_ask && let Some(active) = self.state.active.get_mut(&child.id()) {
active.offer_len = local.size();
@@ -302,7 +311,7 @@ impl<'a> Painter<'a> {
for (axis, under) in AXES.into_iter().zip(self.under.iter_mut()) {
*under = under.and(holds[axis as usize].through(local.axis(axis).len()));
}
Some(in_parent_frame(size, reports_of, declared).axis(axis))
Some(in_parent_frame(size, local.size(), declared).axis(axis))
}
/// Whether this is the first box a child is asked about in during a draw
@@ -334,8 +343,10 @@ impl<'a> Painter<'a> {
ui.text.render(buffer, attrs, width)
}
/// `origin` is in this widget's own region, as every region it writes is.
// TODO: merge the text methods into the primitive ones.
pub fn glyphs(&mut self, text: &RenderedText, origin: UiRegion) {
let origin = origin.within(&self.region);
let kind = self.rsc.ui_mut().primitives.kind::<GlyphPrimitive>();
for glyph in text.glyphs.iter() {
let mut region = origin;
@@ -362,12 +373,35 @@ impl<'a> Painter<'a> {
}
}
/// This widget's box, in the coordinates its own primitives are written
/// in -- so a region composed `within` it may be drawn directly.
/// The box this widget's parent gave it, in the coordinates its own
/// primitives are written in -- so a region composed `within` it may be
/// drawn directly. Its own box is [`Self::placement`] of this one.
pub fn region(&self) -> UiRegion {
self.region
}
/// Where this widget's drawing goes inside the box it was given, in that
/// box's coordinates: what its own answer took of it, or what its parent
/// chose for it. `FULL` on the ask that measures, since nothing has been
/// placed yet.
///
/// Reading it is what says the drawing depends on it, so a widget that
/// positions its own content reads it and is drawn again once its box is
/// known, and one that fills whatever it is given never is.
pub fn placement(&mut self) -> UiRegion {
self.reads_placement = true;
self.placement
}
/// This widget's own box in the coordinates its primitives are written
/// in: its placement composed through the region it was given.
fn placed(&mut self) -> UiRegion {
match self.placement() == UiRegion::FULL {
true => self.region,
false => self.placement.within(&self.region),
}
}
/// Where this widget sits in a box longer than the length it takes. A
/// widget that positions its own content reads it to place that content
/// the way the box around it would have placed the widget.
@@ -401,20 +435,47 @@ impl<'a> Painter<'a> {
placed_box(UiRegion::FULL, lens, RegionAlign::NEAR)
}
/// This widget's box in pixels. Reading it makes the drawing one that
/// holds for this box only, until `holds` says how far it goes.
/// This widget's own box in pixels. Reading it makes the drawing one
/// that holds for this box only, until `holds` says how far it goes.
pub fn px_size(&mut self) -> PxVec2 {
for (own, len) in self.own.iter_mut().zip([self.px.x, self.px.y]) {
if *own == Holds::ANY {
*own = Holds::at(len);
}
}
self.px
PxVec2::new(self.px_len(Axis::X), self.px_len(Axis::Y))
}
/// One axis of this widget's box in pixels. Prefer this to
/// One axis of this widget's own box in pixels. Prefer this to
/// [`Self::px_size`] when the other axis cannot affect the drawing.
pub fn px_len(&mut self, axis: Axis) -> Px {
let part = self.placement().axis(axis).len();
let len = part.to_px(self.px.axis(axis));
let own = &mut self.own[axis as usize];
if *own == Holds::ANY {
*own = Holds::at(len).through(part);
}
len
}
/// The lengths of this widget's own box on `axis` that what it is drawing
/// holds for -- the same primitives, in the same fractions and offsets
/// of the box, and the same reported size. A widget that read its length
/// in pixels holds for that one alone until it says otherwise.
pub fn holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
let part = self.placement().axis(axis).len();
let holds = holds.into();
debug_assert!(
holds.contains(part.to_px(self.px.axis(axis))),
"'{}' ({:?}) says its drawing holds for lengths that leave out its own box",
self.label(),
self.id
);
// Kept as a range of the region's lengths, which is the one variable
// every range here is about: its own box is a part of that box, and
// `through` is the exact preimage of taking the part.
self.own[axis as usize] = holds.through(part);
}
/// One axis of the box this widget's parent gave it, in pixels -- what a
/// fraction of its area resolves against, and so what a container divides
/// among its children. Its own box is a part of this one.
pub fn region_px_len(&mut self, axis: Axis) -> Px {
let len = self.px.axis(axis);
let own = &mut self.own[axis as usize];
if *own == Holds::ANY {
@@ -423,15 +484,14 @@ impl<'a> Painter<'a> {
len
}
/// The lengths of this widget's box on `axis` that what it is drawing
/// holds for -- the same primitives, in the same fractions and offsets
/// of the box, and the same reported size. A widget that read its
/// length in pixels holds for that one alone until it says otherwise.
pub fn holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
/// [`Self::holds`] stated about the region rather than about this
/// widget's own box, for a container whose drawing turns on the box it
/// was given rather than on the part of it it took.
pub fn region_holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
let holds = holds.into();
debug_assert!(
holds.contains(self.px.axis(axis)),
"'{}' ({:?}) says its drawing holds for lengths that leave out its own box",
"'{}' ({:?}) says its drawing holds for lengths that leave out its region",
self.label(),
self.id
);
@@ -521,19 +581,16 @@ impl PrimitiveLike for &TextureHandle {
}
}
/// A child's answer as lengths of the parent's own box. A widget reports a
/// fraction, and `reports_of` is the length that fraction is of: the box the
/// child was given wherever that is the child's whole area, and the parent's
/// own extent wherever the box is a positional remainder, as a span's is
/// after an earlier child. Pixels come through untouched either way, being
/// that many pixels wherever they end up. A declared axis is already the
/// parent's: it resolved the rule in its own box, and the rule is what the
/// report says.
fn in_parent_frame(size: Size, reports_of: UiVec2, declared: [Option<LayoutLen>; 2]) -> Size {
/// A child's answer as lengths of the parent's own region. A widget reports
/// a fraction of its own region, and `of` is that region as a length of this
/// one. Pixels come through untouched, being that many pixels wherever they
/// end up. A declared axis is already the parent's: it resolved the rule in
/// its own region, and the rule is what the report says.
fn in_parent_frame(size: Size, of: UiVec2, declared: [Option<LayoutLen>; 2]) -> Size {
let mut size = size;
for (axis, declared) in AXES.into_iter().zip(declared) {
if declared.is_none() {
*size.axis_mut(axis) = size.axis(axis).within_len(reports_of.axis(axis));
*size.axis_mut(axis) = size.axis(axis).within_len(of.axis(axis));
}
}
size
@@ -562,9 +619,9 @@ pub(crate) fn declared_lens(widgets: &Widgets, id: WidgetId) -> [Option<LayoutLe
/// Whether what a widget reported along an axis is the whole of the box it
/// is in rather than a part to be placed inside it. A share fills, because a
/// share is a length only to whoever divides one, and whoever did is the one
/// that handed down this box. A declared axis does too: `declared_box`
/// already placed it, in the parent's box, and the rule's length is what the
/// widget reports there. And an axis the parent decided from the answer is
/// that handed down this box. A declared axis does too: the rule already gave
/// the region its length, and the rule's length is what the widget reports
/// there. And an axis the parent decided from the answer is
/// the answer already.
pub(crate) fn fills(reported: LayoutLen, declared: Option<LayoutLen>, decided: bool) -> bool {
reported.leftover != Weight::ZERO || declared.is_some() || decided
@@ -612,21 +669,35 @@ pub(crate) fn placed_box(region: UiRegion, lens: UiVec2, align: RegionAlign) ->
placed
}
/// Takes a widget's declared lengths in the box `region` is given in, since a
/// fraction of a length means a fraction of that one, and puts what is left
/// over on the side its alignment says. A caller that already reserved the
/// space hands back the same length, so this is the identity for it.
pub(crate) fn declared_box(
/// A child's own region and where in it its drawing goes, from the box it is
/// offered, the lengths its rules declare, and what its parent chose.
///
/// A rule gives the region its length outright -- that is what makes a rule
/// win, and it is why a widget under one never learns of it -- and the region
/// then sits where its parent placed it, or where its alignment says if its
/// parent left the axis open. With no rule the region is the whole of what
/// was offered, since that is the area a fraction under it is a fraction of,
/// and what the parent chose is where in it the drawing goes. So the two
/// coordinate spaces are the same one wherever a placement survives.
pub(crate) fn ask_box(
mut region: UiRegion,
declared: [Option<LayoutLen>; 2],
align: RegionAlign,
) -> UiRegion {
for (axis, len) in AXES.into_iter().zip(declared) {
let Some(len) = len else { continue };
placement: [Option<UiSpan>; 2],
) -> (UiRegion, [Option<UiSpan>; 2]) {
let mut placed = [None; 2];
for (axis, (len, chosen)) in AXES.into_iter().zip(declared.into_iter().zip(placement)) {
let Some(len) = len else {
placed[axis as usize] = chosen;
continue;
};
let span = region.axis_mut(axis);
let len = Len::from_parts(len.rel, len.px);
span.start += (span.len() - len).scale(align.axis(axis).rel());
span.start = match chosen {
Some(chosen) => chosen.start,
None => span.start + (span.len() - len).scale(align.axis(axis).rel()),
};
span.end = span.start + len;
}
region
(region, placed)
}
+88 -51
View File
@@ -1,6 +1,6 @@
#[cfg(feature = "layout-diagnostics")]
use crate::layout_diagnostics::{self as diag, Counter, ReuseOutcome, TimerKind};
use crate::ui::painter::{declared_box, declared_lens, placed_box, placed_lens};
use crate::ui::painter::{ask_box, declared_lens, placed_box, placed_lens};
use crate::{
ActiveData, Axis, DrawLayers, Holds, IdLike, LayoutLen, Len, MaskIdx, MoveIdx, Moves, Painter,
PixelRegion, Px, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan, UiVec2, Weight,
@@ -30,10 +30,27 @@ pub(super) struct DrawInfo {
/// parent's own, which is where every pixel length in layout comes from.
pub px: PxVec2,
pub offered_px: PxVec2,
/// The axes along which the parent chose this box from the widget's own
/// answer, so the answer is not placed inside it again. See
/// [`Painter::widget_at`].
pub decided: [bool; 2],
/// What of that region the parent chose to put the drawing in, per axis.
/// `None` leaves the axis to the widget's own answer and its alignment.
/// See [`Painter::widget_at`].
pub placement: [Option<UiSpan>; 2],
}
impl DrawInfo {
/// The axes the parent chose the placement on, which are the axes the
/// answer is not placed inside its region again.
fn decided(&self) -> [bool; 2] {
self.placement.map(|span| span.is_some())
}
/// The placement to draw in before the answer is known: what the parent
/// chose, and the whole region on any axis it left open.
fn offered_placement(&self) -> UiRegion {
UiRegion {
x: self.placement[0].unwrap_or(UiSpan::FULL),
y: self.placement[1].unwrap_or(UiSpan::FULL),
}
}
}
pub struct UiRenderState {
@@ -127,7 +144,7 @@ impl UiRenderState {
offer_len: UiVec2::FULL_SIZE,
px,
offered_px: px,
decided: [false; 2],
placement: [None; 2],
}
}
@@ -181,11 +198,13 @@ impl UiRenderState {
}
fn root_region(id: WidgetId, widgets: &Widgets) -> UiRegion {
declared_box(
ask_box(
UiRegion::FULL,
declared_lens(widgets, id),
widgets.alignment(id),
[None; 2],
)
.0
}
pub(super) fn draw_inner(
@@ -217,41 +236,41 @@ impl UiRenderState {
if old.is_none() {
old = self.remove(id, false, rsc);
}
self.draw_at(id, region, info, old.take(), rsc)
self.draw_at(id, region, info.offered_placement(), info, old.take(), rsc)
});
let declared = declared_lens(rsc.widgets(), id);
// The second, final ask is in a box chosen from the answer on both
// axes, which is also what makes it terminate.
let lens = placed_lens(answer.0, declared, info.decided);
let placed = placed_box(region, lens, align);
let placed_info = DrawInfo {
px: lens.to_px(info.px),
decided: [true; 2],
..info
// Where the drawing goes, in the region's own coordinates: what the
// parent chose, and on any axis it left open, what the answer took of
// the region placed by the widget's alignment. The region itself does
// not change, so nothing under it resolves a fraction a second time.
let lens = placed_lens(answer.0, declared, info.decided());
let own = placed_box(UiRegion::FULL, lens, align);
let placement = UiRegion {
x: info.placement[0].unwrap_or(own.x),
y: info.placement[1].unwrap_or(own.y),
};
self.place(id, placed, placed_info, rsc);
self.place(id, region, placement, info, rsc);
// The answer is only reusable while both parts of the operation are:
// what the widget reported in the box it was asked in, and what it
// drew in the box its report selected. Express the latter's contract
// back in terms of the box asked in before handing it to the parent.
// what the widget reported, and what it drew once placed. Both are
// ranges of the region's own lengths, since that is the box neither
// ask changes.
let drawing_holds = self.active[&id].holds;
let mut settled = answer;
for axis in AXES {
settled.1[axis as usize] =
settled.1[axis as usize].and(drawing_holds[axis as usize].through(lens.axis(axis)));
settled.1[axis as usize] = settled.1[axis as usize].and(drawing_holds[axis as usize]);
}
let active = self.active.get_mut(&id).unwrap();
// Whoever asked owns how the box was reached: the box it stated, and
// what of that box the answer then took. A local redraw asks the
// same question again from these.
active.given = region;
active.region = region;
active.given_len = info.given_len;
active.offer_len = info.offer_len;
active.answer = settled;
active.decided = info.decided;
active.decided = info.decided();
active.own_align = align;
// A subtree can be reused whole under a different parent -- same box,
// same layer, same region node -- and nothing in the drawing says it
@@ -268,17 +287,33 @@ impl UiRenderState {
settled
}
/// Draws a widget in the final box its answer chose, reusing the drawing
/// already there where its retained contract holds for that box. The
/// symbolic box can be unchanged while the box it sits in changed pixel
/// length, so what reuse checks is the box in pixels.
fn place(&mut self, id: WidgetId, placed: UiRegion, info: DrawInfo, rsc: &mut dyn UiRsc) {
if self.try_reuse(id, placed, info, rsc).is_none() {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::PlaceRedraws);
let old = self.remove(id, false, rsc);
self.draw_at(id, placed, info, old, rsc);
/// Puts the drawing where the answer says it goes. A drawing that never
/// read its placement is the same drawing wherever it is put, so all that
/// changes is what box the widget is recorded as occupying; one that read
/// it is drawn again, and only where the placement it read has moved.
fn place(
&mut self,
id: WidgetId,
region: UiRegion,
placement: UiRegion,
info: DrawInfo,
rsc: &mut dyn UiRsc,
) {
let active = &self.active[&id];
// A drawing that never read its placement is the same drawing
// wherever it is put, so only what the widget is recorded as
// occupying changes; one that read it stands only for the placement
// it read. Either way the drawing still has to be where the region
// now is, which is what a retained answer on its own does not do.
let stands = !active.reads_placement || active.placement == placement;
if stands && self.try_reuse(id, region, info, rsc).is_some() {
self.active.get_mut(&id).unwrap().placement = placement;
return;
}
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::PlaceRedraws);
let old = self.remove(id, false, rsc);
self.draw_at(id, region, placement, info, old, rsc);
}
/// Calls a widget's `draw` and keeps what it drew in `region`.
@@ -286,6 +321,7 @@ impl UiRenderState {
&mut self,
id: WidgetId,
region: UiRegion,
placement: UiRegion,
info: DrawInfo,
old: Option<ActiveData>,
rsc: &mut dyn UiRsc,
@@ -317,6 +353,8 @@ impl UiRenderState {
let mut painter = Painter {
state: self,
region: local,
placement,
reads_placement: false,
px,
mask: info.mask,
layer: info.layer,
@@ -351,6 +389,8 @@ impl UiRenderState {
state: _,
rsc: _,
region: _,
placement: _,
reads_placement,
px: _,
mask,
textures,
@@ -425,7 +465,7 @@ impl UiRenderState {
offer_len: UiVec2::FULL_SIZE,
px,
offered_px: px,
decided: [false; 2],
placement: [None; 2],
},
rsc,
);
@@ -436,10 +476,8 @@ impl UiRenderState {
let active = ActiveData {
id,
region,
// The box a placing ask draws in is a part of the one its parent
// gave, which `draw_inner` writes back over these once the
// placement is done.
given: region,
placement,
reads_placement,
given_len: info.given_len,
offer_len: info.offer_len,
// Whoever asked writes the answer, if this was the asking.
@@ -454,7 +492,7 @@ impl UiRenderState {
children,
size_deps,
declared: declared_lens(rsc.widgets(), id),
decided: info.decided,
decided: info.decided(),
own_align: rsc.widgets().alignment(id),
move_idx,
parent_move: info.parent_move,
@@ -540,11 +578,7 @@ impl UiRenderState {
// Nothing above the root: the window is where a fraction becomes
// pixels, which is also the whole of the box the root is given.
let (parent_px, parent_offer) = match active.parent.and_then(|p| self.active.get(&p)) {
Some(parent) => {
let (given, offer) = self.asked_px(parent.id);
let lens = placed_lens(parent.answer.0, parent.declared, parent.decided);
(lens.to_px(given), offer)
}
Some(parent) => self.asked_px(parent.id),
None => (self.output_size, self.output_size),
};
let px = active.given_len.to_px(parent_px);
@@ -639,7 +673,6 @@ impl UiRenderState {
self.redepth(id, info.depth);
let active = self.active.get_mut(&id).unwrap();
active.region = region;
active.given = region;
active.given_len = info.given_len;
active.offer_len = info.offer_len;
#[cfg(feature = "layout-diagnostics")]
@@ -694,7 +727,6 @@ impl UiRenderState {
rsc: &mut dyn UiRsc,
) {
let active = self.active.get_mut(&id).unwrap();
active.given = remap.apply(active.given);
if active.move_idx != parent_move {
let region = remap.apply(active.region);
active.region = region;
@@ -792,7 +824,8 @@ impl UiRenderState {
ActiveData {
id,
region: UiRegion::FULL,
given: UiRegion::FULL,
placement: UiRegion::FULL,
reads_placement: false,
given_len: UiVec2::FULL_SIZE,
offer_len: UiVec2::FULL_SIZE,
answer: (size, [Holds::ANY; 2]),
@@ -951,8 +984,9 @@ impl UiRenderState {
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
let active = self.active.get(&id.id())?;
active.drawn.then(|| {
let placed = active.placement.within(&active.region);
self.moves
.resolve(active.parent_move, active.region)
.resolve(active.parent_move, placed)
.to_px(self.output_size)
})
}
@@ -1031,9 +1065,12 @@ impl UiRenderState {
offer_len: active.offer_len,
px: given_px,
offered_px,
decided: active.decided,
// The same question its parent asked: the axes its parent chose
// the placement on, put back where they were.
placement: AXES
.map(|axis| active.decided[axis as usize].then(|| *active.placement.axis(axis))),
};
let (given, was_answer) = (active.given, active.answer);
let (given, was_answer) = (active.region, active.answer);
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::LocalRedraws);