mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 20:26:42 +01:00
55 lines
2.1 KiB
Rust
55 lines
2.1 KiB
Rust
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<&str>, tail: &'a [EdnItem<&str>]
|
|
) -> Option<Self> {
|
|
use EdnItem::*;
|
|
Some(match (head, 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
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
});
|