Files
iris/src/widget/position/pad.rs
T
iris-aiandClaude Opus 5 e6ba570d07 Give a child a part of the container's extent rather than its raw box
`Pad` and `Stack` read `Painter::placement` to put their children inside
their own drawing, and reading it is what says the drawing holds for that
placement alone. So a pad or a stack anywhere in a row was drawn again --
with its whole subtree -- the moment an earlier sibling changed length,
however little else had moved.

`widget_within` now takes a `DrawRegion`, and `DrawRegion::Extent(part)`
gives the child a part of the extent without reading it. What is retained
is the part rather than the box it resolved to, so moving the extent
re-places the child through the same rule instead of redrawing the parent:
`inherited_children` becomes `extent_children`, carrying `Inherit` for the
wrapper case `Painter::widget` already had and `Within(part)` for the new
one.

The dependency that goes up is a range on the container's extent rather
than on its frame, since only the part's *length* reaches the child and
where the part sits is re-placed. A declared length is unchanged: it is a
length of the frame wherever the box it sits in came from. What still pins
the placement is a report with a fraction in it -- the same fraction of a
different extent is a different length -- and that pin is on the answer,
which `extent_frames_keep_fractional_reports_and_numeric_dependencies_valid`
fails without.

Three tests from the first attempt at this come with it, and the
diagnostics rig now says which of the three contracts refused a reuse,
which is what found the above.

Measured, seed 1 at depth 8, median frame: `many` 0.667 -> 0.613 ms and
`resize` 48 -> 32 us; seed 13's `many` 6.35 -> 5.15 ms. Green: fmt, clippy,
109 suite and 20 core tests, the oracle at 100 seeds, the shrinker at 400
trees of depth 5, 1000 seeds at depth 6, and 2000 seeds at depth 4 over all
fifteen cases. The five reference renders are byte-identical to `0e107f0`
on Venus, as are `tabs` resized to 900x1200 and `random` to 1280x800
against cold renders there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 19:15:17 -04:00

134 lines
3.3 KiB
Rust

use crate::prelude::*;
pub struct Pad {
pub padding: Padding,
pub inner: StrongWidget,
}
impl Widget for Pad {
fn draw(&mut self, painter: &mut Painter) -> Size {
// The inner's own alignment, not the near edge. This reports the
// inner's size plus the padding, so where the box is that answer the
// inset box is exactly the inner and alignment has no room to move
// 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();
Size {
x: LayoutLen {
px: inner.x.px + self.padding.left + self.padding.right,
..inner.x
},
y: LayoutLen {
px: inner.y.px + self.padding.top + self.padding.bottom,
..inner.y
},
}
}
}
pub struct Padding {
pub left: Px,
pub right: Px,
pub top: Px,
pub bottom: Px,
}
impl Padding {
pub const ZERO: Self = Self {
left: Px::ZERO,
right: Px::ZERO,
top: Px::ZERO,
bottom: Px::ZERO,
};
pub fn uniform(amt: impl UiNum) -> Self {
let amt = Px::from_num(amt);
Self {
left: amt,
right: amt,
top: amt,
bottom: amt,
}
}
/// `region` less this padding on each side.
pub fn region_of(&self, mut region: UiRegion) -> UiRegion {
region.x.start.px += self.left;
region.y.start.px += self.top;
region.x.end.px -= self.right;
region.y.end.px -= self.bottom;
region
}
pub fn region(&self) -> UiRegion {
self.region_of(UiRegion::FULL)
}
pub fn x(amt: impl UiNum) -> Self {
let amt = Px::from_num(amt);
Self {
left: amt,
right: amt,
..Self::ZERO
}
}
pub fn y(amt: impl UiNum) -> Self {
let amt = Px::from_num(amt);
Self {
top: amt,
bottom: amt,
..Self::ZERO
}
}
pub fn top(amt: impl UiNum) -> Self {
let mut s = Self::ZERO;
s.top = Px::from_num(amt);
s
}
pub fn bottom(amt: impl UiNum) -> Self {
let mut s = Self::ZERO;
s.bottom = Px::from_num(amt);
s
}
pub fn left(amt: impl UiNum) -> Self {
let mut s = Self::ZERO;
s.left = Px::from_num(amt);
s
}
pub fn right(amt: impl UiNum) -> Self {
let mut s = Self::ZERO;
s.right = Px::from_num(amt);
s
}
pub fn with_top(mut self, amt: impl UiNum) -> Self {
self.top = Px::from_num(amt);
self
}
pub fn with_bottom(mut self, amt: impl UiNum) -> Self {
self.bottom = Px::from_num(amt);
self
}
pub fn with_left(mut self, amt: impl UiNum) -> Self {
self.left = Px::from_num(amt);
self
}
pub fn with_right(mut self, amt: impl UiNum) -> Self {
self.right = Px::from_num(amt);
self
}
}
impl<T: UiNum> From<T> for Padding {
fn from(amt: T) -> Self {
Self::uniform(amt)
}
}