generalize EdnItem.

maybe should rename it to Atom? ~90 instances of it
This commit is contained in:
🪞👃🪞 2025-01-17 19:47:35 +01:00
parent 1b9da07280
commit 143cd24e09
20 changed files with 314 additions and 286 deletions

179
output/src/op_transform.rs Normal file
View file

@ -0,0 +1,179 @@
use crate::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
///
/// The `_Unused` variant wraps the `Output` type
/// using `PhantomData` to permit the double generic.
macro_rules! transform_xy {
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
pub enum $Enum<E, T> { _Unused(PhantomData<E>), X(T), Y(T), XY(T) }
impl<E, T> $Enum<E, T> {
pub fn x (item: T) -> Self { Self::X(item) }
pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) }
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E, T> {
fn content (&self) -> impl Render<E> {
match self {
Self::X(item) => item,
Self::Y(item) => item,
Self::XY(item) => item,
_ => unreachable!(),
}
}
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
use $Enum::*;
$area
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
Some(match (head.to_ref(), tail) {
(Key($x), [a]) => Self::x(state.get_content(a).expect("no content")),
(Key($y), [a]) => Self::y(state.get_content(a).expect("no content")),
(Key($xy), [a]) => Self::xy(state.get_content(a).expect("no content")),
_ => return None
})
}
}
}
}
/// 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<E, U, T> { _Unused(E), X(U, T), Y(U, T), XY(U, U, T), }
impl<E, U, T> $Enum<E, U, T> {
pub fn x (x: U, item: T) -> Self { Self::X(x, item) }
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
impl<E, U: Copy + Coordinate, T> $Enum<E, U, T> {
pub fn dx (&self) -> U {
match self {
Self::X(x, _) => *x,
Self::Y(_, _) => 0.into(),
Self::XY(x, _, _) => *x,
_ => unreachable!(),
}
}
pub fn dy (&self) -> U {
match self {
Self::X(_, _) => 0.into(),
Self::Y(y, _) => *y,
Self::XY(_, y, _) => *y,
_ => unreachable!(),
}
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E, E::Unit, T> {
fn content (&self) -> impl Render<E> {
Some(match self {
Self::X(_, content) => content,
Self::Y(_, content) => content,
Self::XY(_, _, content) => content,
_ => unreachable!(),
})
}
fn layout (&$self, $to: E::Area) -> E::Area {
$layout.into()
}
}
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for $Enum<E, E::Unit, RenderBox<'a, E>> {
fn try_from_edn (
state: &'a T, head: &EdnItem<impl AsRef<str>>, tail: &'a [EdnItem<impl AsRef<str>>]
) -> Option<Self> {
use EdnItem::*;
Some(match (head.to_ref(), tail) {
(Key($x), [x, a]) => Self::x(
state.get_unit(x).expect("no x"),
state.get_content(a).expect("no content"),
),
(Key($y), [y, a]) => Self::y(
state.get_unit(y).expect("no y"),
state.get_content(a).expect("no content"),
),
(Key($xy), [x, y, a]) => Self::xy(
state.get_unit(x).expect("no x"),
state.get_unit(y).expect("no y"),
state.get_content(a).expect("no content"),
),
_ => return None
})
}
}
}
}
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],
_ => unreachable!()
}.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],
_ => unreachable!(),
};
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],
_ => unreachable!(),
};
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)],
_ => unreachable!(),
}});
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],
_ => unreachable!(),
}.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() + self.dx(), area.h() + self.dy()
].into()));
transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{
let area = Render::layout(&self.content(), area);
[area.x() + self.dx(), area.y() + 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() + dy + dy, area.h() + dy + 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() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]
});