use crate::prelude::*; pub struct Pad { pub padding: Padding, pub inner: StrongWidget, } impl Widget for Pad { fn draw(&mut self, painter: &mut Painter) -> Size { let inner = painter .widget_within(&self.inner, self.padding.region()) .size(); Size { x: Len { px: inner.x.px + self.padding.left + self.padding.right, ..inner.x }, y: Len { px: inner.y.px + self.padding.top + self.padding.bottom, ..inner.y }, } } /// The padding is an offset from each edge, so a longer box pads the same /// amount and the child takes the rest. fn on_resize(&self, _: Axis) -> OnResize { OnResize::Scale } } pub struct Padding { pub left: f32, pub right: f32, pub top: f32, pub bottom: f32, } impl Padding { pub const ZERO: Self = Self { left: 0.0, right: 0.0, top: 0.0, bottom: 0.0, }; pub fn uniform(amt: impl UiNum) -> Self { let amt = amt.to_f32(); 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 = amt.to_f32(); Self { left: amt, right: amt, top: 0.0, bottom: 0.0, } } pub fn y(amt: impl UiNum) -> Self { let amt = amt.to_f32(); Self { left: 0.0, right: 0.0, top: amt, bottom: amt, } } pub fn top(amt: impl UiNum) -> Self { let mut s = Self::ZERO; s.top = amt.to_f32(); s } pub fn bottom(amt: impl UiNum) -> Self { let mut s = Self::ZERO; s.bottom = amt.to_f32(); s } pub fn left(amt: impl UiNum) -> Self { let mut s = Self::ZERO; s.left = amt.to_f32(); s } pub fn right(amt: impl UiNum) -> Self { let mut s = Self::ZERO; s.right = amt.to_f32(); s } pub fn with_top(mut self, amt: impl UiNum) -> Self { self.top = amt.to_f32(); self } pub fn with_bottom(mut self, amt: impl UiNum) -> Self { self.bottom = amt.to_f32(); self } pub fn with_left(mut self, amt: impl UiNum) -> Self { self.left = amt.to_f32(); self } pub fn with_right(mut self, amt: impl UiNum) -> Self { self.right = amt.to_f32(); self } } impl From for Padding { fn from(amt: T) -> Self { Self::uniform(amt.to_f32()) } }