//! Aligns things to the container. Comes with caveats. //! ``` //! let four = ||Fixed::::xy(4, 4, unit); //! test(area, &Align::nw(four()), [10, 10, 4, 4]); //! test(area, &Align::n(four()), [18, 10, 4, 4]); //! test(area, &Align::ne(four()), [26, 10, 4, 4]); //! test(area, &Align::e(four()), [26, 18, 4, 4]); //! test(area, &Align::se(four()), [26, 26, 4, 4]); //! test(area, &Align::s(four()), [18, 26, 4, 4]); //! test(area, &Align::sw(four()), [10, 26, 4, 4]); //! test(area, &Align::w(four()), [10, 18, 4, 4]); //! let two_by_four = ||Fixed::::xy(4, 2, unit); //! test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]); //! test(area, &Align::n(two_by_four()), [18, 10, 4, 2]); //! test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]); //! test(area, &Align::e(two_by_four()), [26, 19, 4, 2]); //! test(area, &Align::se(two_by_four()), [26, 28, 4, 2]); //! test(area, &Align::s(two_by_four()), [18, 28, 4, 2]); //! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]); //! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]); //! ``` 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, A); try_from_expr!(<'a, E>: Align>: |state, iter| if let Some(Token { value: Value::Key(key), .. }) = iter.peek() { match key { "align/c"|"align/x"|"align/y"| "align/n"|"align/s"|"align/e"|"align/w"| "align/nw"|"align/sw"|"align/ne"|"align/se" => { let _ = iter.next().unwrap(); let c = iter.next().expect("no content specified"); let c = state.get_content(&c.value).expect("no content provided"); return Some(match key { "align/c" => Self::c(c), "align/x" => Self::x(c), "align/y" => Self::y(c), "align/n" => Self::n(c), "align/s" => Self::s(c), "align/e" => Self::e(c), "align/w" => Self::w(c), "align/nw" => Self::nw(c), "align/ne" => Self::ne(c), "align/sw" => Self::sw(c), "align/se" => Self::se(c), _ => unreachable!() }) }, _ => return None } }); impl Align { pub fn c (a: A) -> Self { Self(Alignment::Center, a) } pub fn x (a: A) -> Self { Self(Alignment::X, a) } pub fn y (a: A) -> Self { Self(Alignment::Y, a) } pub fn n (a: A) -> Self { Self(Alignment::N, a) } pub fn s (a: A) -> Self { Self(Alignment::S, a) } pub fn e (a: A) -> Self { Self(Alignment::E, a) } pub fn w (a: A) -> Self { Self(Alignment::W, a) } pub fn nw (a: A) -> Self { Self(Alignment::NW, a) } pub fn sw (a: A) -> Self { Self(Alignment::SW, a) } pub fn ne (a: A) -> Self { Self(Alignment::NE, a) } pub fn se (a: A) -> 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()) } }