//! [Content] items that modify the inherent //! dimensions of their inner [Render]ables. //! //! Transform may also react to the [Area] provided. //! ``` //! use ::tengri::{output::*, tui::*}; //! let area: [u16;4] = [10, 10, 20, 20]; //! fn test (area: [u16;4], item: &impl Content, expected: [u16;4]) { //! assert_eq!(Content::layout(item, area), expected); //! assert_eq!(Render::layout(item, area), expected); //! }; //! test(area, &(), [20, 20, 0, 0]); //! //! test(area, &Fill::xy(()), area); //! test(area, &Fill::x(()), [10, 20, 20, 0]); //! test(area, &Fill::y(()), [20, 10, 0, 20]); //! //! //FIXME:test(area, &Fixed::x(4, ()), [18, 20, 4, 0]); //! //FIXME:test(area, &Fixed::y(4, ()), [20, 18, 0, 4]); //! //FIXME:test(area, &Fixed::xy(4, 4, unit), [18, 18, 4, 4]); //! ``` use crate::*; /// Defines an enum that transforms its content /// along either the X axis, the Y axis, or both. macro_rules! transform_xy { ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => { pub enum $Enum { X(T), Y(T), XY(T) } impl $Enum { #[inline] pub const fn x (item: T) -> Self { Self::X(item) } #[inline] pub const fn y (item: T) -> Self { Self::Y(item) } #[inline] pub const fn xy (item: T) -> Self { Self::XY(item) } } #[cfg(feature = "dsl")] impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromDsl<'a, T> for $Enum> { fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option { let mut iter = iter.clone(); if let Some(Token { value: Value::Key(k), .. }) = iter.peek() { if k == $x || k == $y || k == $xy { let _ = iter.next().unwrap(); let token = iter.next().expect("no content specified"); let content = get_content!(state => token); return Some(match k { $x => Self::x(content), $y => Self::y(content), $xy => Self::xy(content), _ => unreachable!() }) } } None } } impl> Content for $Enum { fn content (&self) -> impl Render { match self { Self::X(item) => item, Self::Y(item) => item, Self::XY(item) => item, } } fn layout (&$self, $to: ::Area) -> ::Area { use $Enum::*; $area } } } } /// Defines an enum that parametrically transforms its content /// along either the X axis, the Y axis, or both. macro_rules! transform_xy_unit { ($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => { pub enum $Enum { X(U, T), Y(U, T), XY(U, U, T), } impl $Enum { #[inline] pub const fn x (x: U, item: T) -> Self { Self::X(x, item) } #[inline] pub const fn y (y: U, item: T) -> Self { Self::Y(y, item) } #[inline] pub const fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) } } #[cfg(feature = "dsl")] impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromDsl<'a, T> for $Enum> { fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option { let mut iter = iter.clone(); if let Some(Token { value: Value::Key(k), .. }) = iter.peek() { if k == $x || k == $y { let _ = iter.next().unwrap(); let u = iter.next().expect("no unit specified"); let u = get_value!(state => u); let c = iter.next().expect("no content specified"); let c = get_content!(state => c); return Some(match k { $x => Self::x(u, c), $y => Self::y(u, c), _ => unreachable!(), }) } else if k == $xy { let _ = iter.next().unwrap(); let u = get_value!(state => iter.next().expect("no unit specified")); let v = get_value!(state => iter.next().expect("no unit specified")); let c = get_content!(state => iter.next().expect("no content specified")); return Some(Self::xy(u, v, c)) } } None } } impl> Content for $Enum { fn content (&self) -> impl Render { Some(match self { Self::X(_, content) => content, Self::Y(_, content) => content, Self::XY(_, _, content) => content, }) } fn layout (&$self, $to: E::Area) -> E::Area { $layout.into() } } impl $Enum { #[inline] pub fn dx (&self) -> U { match self { Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x, } } #[inline] pub fn dy (&self) -> U { match self { Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::XY(_, y, _) => *y, } } } } } transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{ let [x0, y0, wmax, hmax] = to.xywh(); let [x, y, w, h] = self.content().layout(to).xywh(); match self { X(_) => [x0, y, wmax, h], Y(_) => [x, y0, w, hmax], XY(_) => [x0, y0, wmax, hmax], }.into() }); transform_xy_unit!("fixed/x" "fixed/y" "fixed/xy"|self: Fixed, area|{ let [x, y, w, h] = area.xywh(); let fixed_area = match self { Self::X(fw, _) => [x, y, *fw, h], Self::Y(fh, _) => [x, y, w, *fh], Self::XY(fw, fh, _) => [x, y, *fw, *fh], }; let [x, y, w, h] = Render::layout(&self.content(), fixed_area.into()).xywh(); let fixed_area = match self { Self::X(fw, _) => [x, y, *fw, h], Self::Y(fh, _) => [x, y, w, *fh], Self::XY(fw, fh, _) => [x, y, *fw, *fh], }; fixed_area }); transform_xy_unit!("min/x" "min/y" "min/xy"|self: Min, area|{ let area = Render::layout(&self.content(), area); match self { Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()], Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().max(*mh)], Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)], } }); transform_xy_unit!("max/x" "max/y" "max/xy"|self: Max, area|{ let [x, y, w, h] = area.xywh(); Render::layout(&self.content(), match self { Self::X(fw, _) => [x, y, *fw, h], Self::Y(fh, _) => [x, y, w, *fh], Self::XY(fw, fh, _) => [x, y, *fw, *fh], }.into()) }); transform_xy_unit!("shrink/x" "shrink/y" "shrink/xy"|self: Shrink, area|Render::layout( &self.content(), [area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())].into())); transform_xy_unit!("expand/x" "expand/y" "expand/xy"|self: Expand, area|Render::layout( &self.content(), [area.x(), area.y(), area.w().plus(self.dx()), area.h().plus(self.dy())].into())); transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{ let area = Render::layout(&self.content(), area); [area.x().plus(self.dx()), area.y().plus(self.dy()), area.w(), area.h()] }); transform_xy_unit!("pull/x" "pull/y" "pull/xy"|self: Pull, area|{ let area = Render::layout(&self.content(), area); [area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()] }); transform_xy_unit!("margin/x" "margin/y" "margin/xy"|self: Margin, area|{ let area = Render::layout(&self.content(), area); let dx = self.dx(); let dy = self.dy(); [area.x().minus(dx), area.y().minus(dy), area.w().plus(dy.plus(dy)), area.h().plus(dy.plus(dy))] }); transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{ let area = Render::layout(&self.content(), area); let dx = self.dx(); let dy = self.dy(); [area.x().plus(dx), area.y().plus(dy), area.w().minus(dy.plus(dy)), area.h().minus(dy.plus(dy))] });