use crate::*; #[derive(Debug, Copy, Clone, Default)] pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } pub struct Align(Alignment, T); impl Align { pub fn c (a: T) -> Self { Self(Alignment::Center, a) } pub fn x (a: T) -> Self { Self(Alignment::X, a) } pub fn y (a: T) -> Self { Self(Alignment::Y, a) } pub fn n (a: T) -> Self { Self(Alignment::N, a) } pub fn s (a: T) -> Self { Self(Alignment::S, a) } pub fn e (a: T) -> Self { Self(Alignment::E, a) } pub fn w (a: T) -> Self { Self(Alignment::W, a) } pub fn nw (a: T) -> Self { Self(Alignment::NW, a) } pub fn sw (a: T) -> Self { Self(Alignment::SW, a) } pub fn ne (a: T) -> Self { Self(Alignment::NE, a) } pub fn se (a: T) -> Self { Self(Alignment::SE, a) } } impl> Content for Align { fn content (&self) -> impl Render { &self.1 } fn layout (&self, on: E::Area) -> E::Area { use Alignment::*; let it = Render::layout(&self.content(), on).xywh(); let cx = on.x()+(on.w().minus(it.w())/2.into()); let cy = on.y()+(on.h().minus(it.h())/2.into()); let fx = (on.x()+on.w()).minus(it.w()); let fy = (on.y()+on.h()).minus(it.h()); let [x, y] = match self.0 { Center => [cx, cy], X => [cx, it.y()], Y => [it.x(), cy], NW => [on.x(), on.y()], N => [cx, on.y()], NE => [fx, on.y()], W => [on.x(), cy], E => [fx, cy], SW => [on.x(), fy], S => [cx, fy], SE => [fx, fy], }.into(); [x, y, it.w(), it.h()].into() } fn render (&self, to: &mut E) { to.place(Content::layout(self, to.area()), &self.content()) } }