tek/output/src/align.rs

51 lines
1.8 KiB
Rust

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<T>(Alignment, T);
impl<T> Align<T> {
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<E: Output, T: Content<E>> Content<E> for Align<T> {
fn content (&self) -> impl Render<E> {
&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())
}
}