tek/crates/tek_layout/src/inset.rs

48 lines
1.3 KiB
Rust

use crate::*;
impl<E: Engine, W: Render<E>> LayoutInset<E> for W {}
pub trait LayoutInset<E: Engine>: Render<E> + Sized {
fn inset_x (self, x: E::Unit) -> Inset<E, Self> {
Inset::X(x, self)
}
fn inset_y (self, y: E::Unit) -> Inset<E, Self> {
Inset::Y(y, self)
}
fn inset_xy (self, x: E::Unit, y: E::Unit) -> Inset<E, Self> {
Inset::XY(x, y, self)
}
}
/// Shrink from each side
pub enum Inset<E: Engine, T> {
_Unused(PhantomData<E>),
/// Decrease width
X(E::Unit, T),
/// Decrease height
Y(E::Unit, T),
/// Decrease width and height
XY(E::Unit, E::Unit, T),
}
impl<E: Engine, T: Render<E>> Inset<E, T> {
pub fn inner (&self) -> &T {
match self {
Self::X(_, i) => i,
Self::Y(_, i) => i,
Self::XY(_, _, i) => i,
_ => unreachable!(),
}
}
}
impl<E: Engine, T: Render<E>> Render<E> for Inset<E, T> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
match *self {
Self::X(x, ref inner) => (inner as &dyn Render<E>).shrink_x(x).push_x(x),
Self::Y(y, ref inner) => (inner as &dyn Render<E>).shrink_y(y).push_y(y),
Self::XY(x, y, ref inner) => (inner as &dyn Render<E>).shrink_xy(x, y).push_xy(x, y),
_ => unreachable!(),
}.render(to)
}
}