`Place` was a product written as a sum -- a `Part` and a fill flag -- and `Part` named three operations the geometry already had, under words that did not match them. `Of` was `UiSpan::within`, `From` was `UiSpan::shift`, and `Sized` was `placement`'s own body with the length given rather than reported. Both enums are gone. `PlaceDescAxis` says one axis, named after the operation it performs: `within`, `shifted`, `sized`, and `WHOLE`. What is optional is a builder -- `fills` and `rel_base` -- so a caller writes only what it decided, and the rel base it does not write follows the constructor: a span composed into the caller's box narrows it, a span along a cursor does not, a decided length is it. That was the one rule a caller could get wrong with nothing failing. `PlaceDesc` says both axes with named fields, so `axis`, `axis_mut` and `from_axis` work the way they do on every other pair here, and the joint work -- resolving a region, reading the fill flags -- is written once rather than per axis. `widget_at` and `place_at` take `impl Into<PlaceDesc>`, so a wrapper passes a `UiRegion` and says nothing else. `widget_within` and `ActiveData::narrow_rel_base` are deleted; `asked` and `placed` carry the rel base their ask stated. Cold layout is byte-identical to `84dad21`.
139 lines
3.6 KiB
Rust
139 lines
3.6 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.
|
|
//
|
|
// Padding is an inset of both: it comes off the rel base, so `rel(1)`
|
|
// under it fills this widget rather than overflowing it by the
|
|
// padding, and it comes off the box, so what is drawn sits inside.
|
|
// The two stay distinct -- the box can be narrower still, where a row
|
|
// asked this widget in the room left, and a text wraps at that.
|
|
let inner = painter.widget_at(&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,
|
|
}
|
|
}
|
|
/// `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)
|
|
}
|
|
}
|