Lay out in a frame that passes through and a box placed in it

A widget is asked in two boxes rather than one. Its frame is what a fraction
it declares or reports is a fraction of, and it passes through a span, a
stack and a scroll unchanged, so `rel(0.5)` is half the same area however
many containers sit between: a frame is narrowed only by what is decided
above the widget -- a declared length, the root. Its extent is where the
drawing goes, given as a `Place` per axis: a part of the parent's own box,
measured in frame lengths from where that box starts, which the child either
fills or has its answer placed inside.

What that buys is that nothing under a container depends on where the
container sits. A container reads `extent_len` for the length it divides and
nothing about the start, so moving it re-places its children by re-adding
that start and draws nobody again; and a fraction is resolved once, against
the frame, rather than once per box it is composed through -- a stack sized
by a child that reports `rel(0.5)` no longer takes half of half.

`Place` replaces `DrawRegion`, `ExtentPlacement`, `widget_within`,
`measure_len`, `region()`, `placement()` and `box_of`. Primitives and masks
are written in the widget's own box's coordinates alone, so the drawing has
one reference rather than two. The placement pin goes with them: reading the
extent's length pins that length symbolically, and pins compose only where a
child's box is its parent's own.

Placing an answer waits for the end of the parent's draw or for the next ask
of that child in it, so a span child is one drawing and one move rather than
two moves.

`Pad` is transparent: its padding goes around what it pads and its child
keeps the outer frame, which is where `Outset` was going anyway. A fraction
under a pad is now a fraction of the frame rather than of the inset box.

Checked: fmt, clippy with -D warnings, 109 suite tests and 20 core tests in
debug, the 11 generated cases, and the shrinker at 400 trees of depth 5 over
all fifteen cases -- which still finds seed 108 under `reorder`, where a
wrapping text measured in one box and drawn in another settles differently
warm than cold. `redraw` therefore keeps the baseline's deferral for a box
that is not as long as the one the widget was measured in; the plan's step
6 is not done, and the next commit message or the handoff says why.
This commit is contained in:
iris-ai committed 2026-09-18 00:40:59 -04:00
1 parent 34cafb6edc
commit 1956be3f3d
19 files changed
+989 -943

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@ pub struct Masked {
impl Widget for Masked {
fn draw(&mut self, painter: &mut Painter) -> Size {
painter.set_mask(DrawRegion::Extent(UiRegion::FULL));
painter.set_mask(UiRegion::FULL);
painter.widget(&self.inner);
// What it occupies is its box, on both axes, for the reason `Scroll`
// reports the same: it clips what is inside to that box, so it can
+7 -2
View File
@@ -7,7 +7,12 @@ pub struct Offset {
impl Widget for Offset {
fn draw(&mut self, painter: &mut Painter) -> Size {
let region = UiRegion::FULL.offset(self.amt);
painter.widget_within(&self.inner, region).size()
// The whole of this widget's box, moved: the frame passes through, so
// what the child declares or reports means the same as it would
// without the offset.
let len = painter.extent_len();
let moved = |len: Len, amt: Len| Place::Within(Some(UiSpan::new(amt, len + amt)));
let place = [moved(len.x, self.amt.x), moved(len.y, self.amt.y)];
painter.widget_at(&self.inner, UiRegion::FULL, place).size()
}
}
+16 -2
View File
@@ -13,8 +13,22 @@ impl Widget for Pad {
// it; where the box is bigger -- a share of a row, a rule over this
// 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.
let inside = DrawRegion::Extent(self.padding.region());
let inner = painter.widget_within(&self.inner, inside).size();
//
// The padding goes around what it pads: the frame passes through, so
// the inner's fractions mean what they would without it, and only
// the box it draws in is moved in by the pixels.
let len = painter.extent_len();
let inset = |len: Len, lead: Px, trail: Px| {
Place::Within(Some(UiSpan::new(
Len::from_parts(Rel::ZERO, lead),
len - Len::from_parts(Rel::ZERO, trail),
)))
};
let place = [
inset(len.x, self.padding.left, self.padding.right),
inset(len.y, self.padding.top, self.padding.bottom),
];
let inner = painter.widget_at(&self.inner, UiRegion::FULL, place).size();
Size {
x: LayoutLen {
px: inner.x.px + self.padding.left + self.padding.right,
+16 -17
View File
@@ -12,12 +12,11 @@ pub struct Scroll {
impl Widget for Scroll {
fn draw(&mut self, painter: &mut Painter) -> Size {
let container_len = painter.px_len(self.axis);
// Draw in the whole container only when its scrolling-axis length is
// not already known, then draw it at the scrolled offset.
// Measured in the whole viewport, then drawn at the scrolled offset.
let whole = UiRegion::FULL;
let own = painter.placement();
let answer_len =
painter.measure_len(&self.inner, self.axis, whole, [Some(own.x), Some(own.y)]);
let answer_len = painter
.widget_at(&self.inner, whole, [Place::Fill(None); 2])
.len(self.axis);
let content = answer_len.apply_leftover();
self.container_len = container_len;
self.content_len = content.to_px(container_len);
@@ -46,7 +45,7 @@ impl Widget for Scroll {
// have placed the whole scroll in a box longer than it.
let slack = (self.container_len - self.content_len).max(Px::ZERO);
let anchor = slack.mul(align.rel());
let mut region = UiRegion::FULL;
let mut content = UiSpan::FULL;
// Content that fills the viewport and has not been scrolled is the
// viewport, and is handed back as it came. Writing the same box as
// its own length in pixels is the same box in another form, and the
@@ -55,19 +54,19 @@ impl Widget for Scroll {
// each part of it.
let moved = anchor != Px::ZERO || self.amt != Px::ZERO;
if moved || self.content_len != self.container_len {
let offset = UiVec2::from_axis(
self.axis,
Len::from_parts(Rel::ZERO, anchor - self.amt),
Len::ZERO,
);
region = region.offset(offset);
region.axis_mut(self.axis).end = region.axis(self.axis).start.offset(self.content_len);
let start = Len::from_parts(Rel::ZERO, anchor - self.amt);
content = UiSpan::new(start, start.offset(self.content_len));
}
// The viewport is the inner's region, so a fraction it declares or
// The viewport is the inner's frame, so a fraction it declares or
// reports is a fraction of what is on screen rather than of the
// content box its own answer decided. Where it is put is the content
// box, scrolled.
painter.widget_at(&self.inner, whole, [Some(region.x), Some(region.y)]);
// content box its own answer decided. Where it is drawn is the
// content box, scrolled.
painter.widget_at(
&self.inner,
whole,
self.axis
.pair(Place::Fill(Some(content)), Place::Fill(None)),
);
// 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
// more. The content's length is what it scrolls through, not what it
+27 -32
View File
@@ -10,34 +10,30 @@ pub struct Span {
impl Widget for Span {
fn draw(&mut self, painter: &mut Painter) -> Size {
let axis = self.dir.axis;
// The row: this span's own box, as a span of the region it was given.
// Its children are laid out along it, and what they declare or report
// is a fraction of the region -- the area this span was told it has,
// which it passes on unchanged.
let own = painter.placement();
let row = *own.axis(axis);
// Across itself the span's own box is the child's region: a span is
// what contains its children there, and nothing divides that axis.
// Along it the whole region is, so a fraction means the same thing
// for every child however much of the row is left when it is asked.
let region = UiRegion::from_axis(axis, UiSpan::FULL, *own.axis(!axis));
// The row: this span's own box, as a length of the frame its children
// 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.
let far = painter.extent_len().axis(axis);
let along = |from: Len, to: Len| match self.dir.sign {
Sign::Pos => UiSpan::new(row.start + from, row.start + to),
Sign::Neg => UiSpan::new(row.end - to, row.end - from),
Sign::Pos => UiSpan::new(from, to),
Sign::Neg => UiSpan::new(far - to, far - from),
};
let far = row.len();
// A length for every child before their final boxes are chosen: from
// a hint where one exists, and from drawing otherwise.
// 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
// nothing divides that axis.
let across = Place::Within(None);
// A length for every child before their final slots are chosen. The
// frame passes 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 them; what it is drawn in is the room left from the cursor,
// because a text has to wrap at the width actually there.
let mut cursor = Len::rel_min();
let mut lens = Vec::with_capacity(self.children.len());
for child in &self.children {
// The whole region is the child's, so `rel(0.5)` is half the area
// this span was given whatever else is in it and wherever this
// child sits among them. What it is placed in is the room left
// from the cursor, because a text has to wrap at the width
// actually there.
let room = axis.pair(Some(along(cursor, far)), None);
let len = painter.measure_len(child, axis, region, room);
let room = Place::Within(Some(along(cursor, far)));
let len = painter
.widget_at(child, UiRegion::FULL, axis.pair(room, across))
.len(axis);
cursor.px += len.px + self.gap;
cursor.rel += len.rel;
lens.push(len);
@@ -55,7 +51,7 @@ impl Widget for Span {
);
// What is left for the shares to divide: the row less everything
// fixed, as a length of the region rather than a number of pixels.
// fixed, as a length of the frame rather than a number of pixels.
let room = far - Len::from_parts(total.rel, total.px);
// 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`
@@ -68,12 +64,12 @@ impl Widget for Span {
// exist at all turns on this.
let mut shares = false;
if total.leftover > Weight::ZERO {
shares = room.to_px(painter.region_px_len(axis)) > Px::ZERO;
shares = room.to_px(painter.frame_px_len(axis)) > Px::ZERO;
let holds = match shares {
true => Holds::from(Px::STEP..=Px::MAX),
false => Holds::from(Px::MIN..=Px::ZERO),
};
painter.region_holds(axis, holds.through(room));
painter.frame_holds(axis, holds.through(room));
}
// Across itself a span is as long as its longest child -- unless a
@@ -109,12 +105,11 @@ impl Widget for Span {
fixed.px += len.px;
fixed.rel += len.rel;
start = shared(fixed, taken, total.leftover, room);
// Along the row the span says where the child goes; across it the
// child sits where its own alignment says. Its region is the
// whole of what this span was given either way, which is what its
// fractions are of.
let placed =
painter.widget_at(child, region, axis.pair(Some(along(from, start)), None));
// Along the row the span says where the child goes, and that slot
// is the drawing's box outright rather than something to place an
// answer inside again.
let slot = Place::Fill(Some(along(from, start)));
let placed = painter.widget_at(child, UiRegion::FULL, axis.pair(slot, across));
if shrinks {
let used = placed.len(!axis);
// Choosing between a fixed and a relative length from the
+9 -9
View File
@@ -13,17 +13,18 @@ impl Widget for Stack {
StackSize::Default => None,
StackSize::Child(i) => Some(i),
};
// Whichever child sizes the stack keeps the stack's whole region as
// its own -- the stack is the length that child asked for, so taking
// the fraction of the stack's box again would take it twice -- and is
// put where the stack itself is put.
// Whichever child sizes the stack is given the stack's whole box --
// the stack is the length that child asked for, so placing that
// answer inside the box it decided would apply it twice.
let size = match sizing.and_then(|i| self.children.get(i).map(|c| (i, c))) {
// On the layer that child ends up on, so the ask below is a reuse
// rather than a second drawing of it somewhere else: a retained
// drawing belongs to the layer it was made on.
Some((i, child)) => {
painter.child_layer_at(i);
painter.widget(child).size()
painter
.widget_at(child, UiRegion::FULL, [Place::Fill(None); 2])
.size()
}
None => Size::LEFTOVER,
};
@@ -32,10 +33,9 @@ impl Widget for Stack {
continue;
}
painter.child_layer_at(i);
// Every other child has the stack's own box for its region, since
// the stack is what contains it, and where it sits in one bigger
// than itself is its own business.
painter.widget_within(child, DrawRegion::Extent(UiRegion::FULL));
// Every other child is drawn in the stack's own box, and where it
// sits in one bigger than itself is its own business.
painter.widget(child);
}
size
}
+1 -1
View File
@@ -80,7 +80,7 @@ impl TextView {
// hair under that line, and the break made in it is not the break a
// cold layout makes there.
let size = Size::from_px(PxVec2::ceil_from_f32(tex.size));
painter.glyphs(tex, DrawRegion::Extent(region));
painter.glyphs(tex, region);
(region, size)
}