use crate::prelude::*; pub struct Pad { pub padding: Padding, pub inner: WidgetId, } impl Widget for Pad { fn draw(&mut self, painter: &mut Painter) { painter.widget_within(&self.inner, self.padding.region()); } fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { let mut size = ctx.size(&self.inner); if size.x.rest == 0.0 { size.x.abs += self.padding.left + self.padding.right; } if size.y.rest == 0.0 { size.y.abs += self.padding.top + self.padding.bottom; } size } } pub struct Padding { pub left: f32, pub right: f32, pub top: f32, pub bottom: f32, } impl Padding { 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.top_left.abs.x += self.left; region.top_left.abs.y += self.top; region.bot_right.abs.x -= self.right; region.bot_right.abs.y -= 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, } } } impl From for Padding { fn from(amt: T) -> Self { Self::uniform(amt.to_f32()) } }