`Stack` and `Pad` forced the near edge on every child. That override exists so a container that reports a child's size and then hands it the box derived from that report does not place its content twice -- and it is owed only where the box really is the child's own answer. `Stack` gives every child the box its sizing child defines. That box is `box_of(child.size())`, so the sizing child has no room in it and needs the override; every other child is handed a box that owes nothing to it, and where it sits in one bigger than itself is its own business. With the override it could not be aligned at all. `Pad` reports its inner's size plus the padding, so where its box is that answer the inset box is exactly the inner and alignment has nowhere to move it. Where the box is bigger -- a share of a row, a rule over the pad -- the slack belongs to the inner, and the override pinned it to a corner. The `tabs` example is the visible case both ways: its counters asked for `Align::RIGHT` inside a stack and sat at the left, and `text`'s narrow panel filled a row it had asked to sit at the top of. Both match canonical `main` again. Neither was noticed when `d3b0ebf` made alignment a property, and the handoff's claim that `tabs` then "differs only in the widget count it prints about itself" was wrong -- it was checked at `8220a78` and not re-checked after the next commit. Checked: fmt, clippy, 81 suite tests, 17 core unit tests, the release oracle at 100 seeds and at 1000 seeds of depth 6, and all fifteen shrinker cases at 400 seeds of depth 5. `tabs`, `text` and `random` change exactly where a child now honours its own alignment; `view` and `minimal` are unchanged. `tabs` is still not `main`'s render: `.sized((100, 100)).center().width( leftover(2))` on one widget no longer means a square centred in a two-share box, because one widget carries one length per axis and `.width` overwrites what `.sized` set. That one is an API question, not a bug, and is open. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
131 lines
3.1 KiB
Rust
131 lines
3.1 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 inner = painter
|
|
.widget_within(&self.inner, self.padding.region())
|
|
.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,
|
|
}
|
|
}
|
|
pub fn region(&self) -> UiRegion {
|
|
let mut region = UiRegion::FULL;
|
|
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 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)
|
|
}
|
|
}
|