mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
210 lines
8.6 KiB
Rust
210 lines
8.6 KiB
Rust
//! [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<TuiOut>, 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<A> { X(A), Y(A), XY(A) }
|
|
impl<A> $Enum<A> {
|
|
#[inline] pub const fn x (item: A) -> Self { Self::X(item) }
|
|
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
|
|
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
|
|
}
|
|
#[cfg(feature = "dsl")]
|
|
impl<'n, A: 'n, T: Receive<A>> Provide<'n, T> for $Enum<A> {
|
|
fn provide <'source> (state: &T, token: &mut TokenIter<'source>)
|
|
-> Perhaps<Self>
|
|
{
|
|
if let Some(Token { value: Value::Key(k), .. }) = token.peek() {
|
|
let mut base = token.clone();
|
|
return Ok(Some(match token.next() {
|
|
Some(Token{value:Value::Key($x),..}) =>
|
|
Self::x(state.receive_or_fail(token, ||"x: no content")?),
|
|
Some(Token{value:Value::Key($y),..}) =>
|
|
Self::y(state.receive_or_fail(token, ||"y: no content")?),
|
|
Some(Token{value:Value::Key($xy),..}) =>
|
|
Self::xy(state.receive_or_fail(token, ||"xy: no content")?),
|
|
_ => unreachable!()
|
|
}))
|
|
}
|
|
Ok(None)
|
|
}
|
|
}
|
|
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
|
|
fn content (&self) -> impl Render<E> + '_ {
|
|
match self {
|
|
Self::X(item) => item,
|
|
Self::Y(item) => item,
|
|
Self::XY(item) => item,
|
|
}
|
|
}
|
|
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::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<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
|
|
impl<U, A> $Enum<U, A> {
|
|
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
|
|
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
|
|
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
|
|
}
|
|
#[cfg(feature = "dsl")]
|
|
impl<'n, A: 'n, U: Coordinate + 'n, T: Receive<A> + Receive<U>> Provide<'n, T> for $Enum<U, A> {
|
|
fn provide <'source> (
|
|
state: &T, token: &mut TokenIter<'source>
|
|
) -> Perhaps<Self> {
|
|
Ok(if let Some(Token { value: Value::Key($x|$y|$xy), .. }) = token.peek() {
|
|
let mut base = token.clone();
|
|
Some(match token.next() {
|
|
Some(Token { value: Value::Key($x), .. }) => Self::x(
|
|
state.receive_or_fail(token, ||"x: no unit")?,
|
|
state.receive_or_fail(token, ||"x: no content")?,
|
|
),
|
|
Some(Token { value: Value::Key($y), .. }) => Self::y(
|
|
state.receive_or_fail(token, ||"y: no unit")?,
|
|
state.receive_or_fail(token, ||"y: no content")?,
|
|
),
|
|
Some(Token { value: Value::Key($x), .. }) => Self::xy(
|
|
state.receive_or_fail(token, ||"xy: no unit x")?,
|
|
state.receive_or_fail(token, ||"xy: no unit y")?,
|
|
state.receive_or_fail(token, ||"xy: no content")?
|
|
),
|
|
_ => unreachable!(),
|
|
})
|
|
} else {
|
|
None
|
|
})
|
|
}
|
|
}
|
|
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
|
|
fn content (&self) -> impl Render<E> + '_ {
|
|
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<U: Copy + Coordinate, T> $Enum<U, T> {
|
|
#[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))]
|
|
});
|