mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
wip: hook up more builtins
This commit is contained in:
parent
fa70a42bad
commit
811e341cd5
4 changed files with 94 additions and 64 deletions
|
|
@ -1,27 +1,23 @@
|
|||
use crate::*;
|
||||
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
|
||||
|
||||
pub struct Align<T>(Alignment, T);
|
||||
|
||||
impl<T> Align<T> {
|
||||
pub fn c (a: T) -> Self { Self(Alignment::Center, a) }
|
||||
pub fn x (a: T) -> Self { Self(Alignment::X, a) }
|
||||
pub fn y (a: T) -> Self { Self(Alignment::Y, a) }
|
||||
pub fn n (a: T) -> Self { Self(Alignment::N, a) }
|
||||
pub fn s (a: T) -> Self { Self(Alignment::S, a) }
|
||||
pub fn e (a: T) -> Self { Self(Alignment::E, a) }
|
||||
pub fn w (a: T) -> Self { Self(Alignment::W, a) }
|
||||
pub fn nw (a: T) -> Self { Self(Alignment::NW, a) }
|
||||
pub fn sw (a: T) -> Self { Self(Alignment::SW, a) }
|
||||
pub fn ne (a: T) -> Self { Self(Alignment::NE, a) }
|
||||
pub fn se (a: T) -> Self { Self(Alignment::SE, a) }
|
||||
pub struct Align<E, A>(PhantomData<E>, Alignment, A);
|
||||
impl<E, A> Align<E, A> {
|
||||
pub fn c (a: A) -> Self { Self(Default::default(), Alignment::Center, a) }
|
||||
pub fn x (a: A) -> Self { Self(Default::default(), Alignment::X, a) }
|
||||
pub fn y (a: A) -> Self { Self(Default::default(), Alignment::Y, a) }
|
||||
pub fn n (a: A) -> Self { Self(Default::default(), Alignment::N, a) }
|
||||
pub fn s (a: A) -> Self { Self(Default::default(), Alignment::S, a) }
|
||||
pub fn e (a: A) -> Self { Self(Default::default(), Alignment::E, a) }
|
||||
pub fn w (a: A) -> Self { Self(Default::default(), Alignment::W, a) }
|
||||
pub fn nw (a: A) -> Self { Self(Default::default(), Alignment::NW, a) }
|
||||
pub fn sw (a: A) -> Self { Self(Default::default(), Alignment::SW, a) }
|
||||
pub fn ne (a: A) -> Self { Self(Default::default(), Alignment::NE, a) }
|
||||
pub fn se (a: A) -> Self { Self(Default::default(), Alignment::SE, a) }
|
||||
}
|
||||
|
||||
impl<E: Output, T: Content<E>> Content<E> for Align<T> {
|
||||
impl<E: Output, A: Content<E>> Content<E> for Align<E, A> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
&self.1
|
||||
&self.2
|
||||
}
|
||||
fn layout (&self, on: E::Area) -> E::Area {
|
||||
use Alignment::*;
|
||||
|
|
@ -30,7 +26,7 @@ impl<E: Output, T: Content<E>> Content<E> for Align<T> {
|
|||
let cy = on.y()+(on.h().minus(it.h())/2.into());
|
||||
let fx = (on.x()+on.w()).minus(it.w());
|
||||
let fy = (on.y()+on.h()).minus(it.h());
|
||||
let [x, y] = match self.0 {
|
||||
let [x, y] = match self.1 {
|
||||
Center => [cx, cy],
|
||||
X => [cx, it.y()],
|
||||
Y => [it.x(), cy],
|
||||
|
|
@ -49,3 +45,27 @@ impl<E: Output, T: Content<E>> Content<E> for Align<T> {
|
|||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
impl<'a, T, E, A> TryFromEdn<'a, T> for Align<E, A>
|
||||
where
|
||||
T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a,
|
||||
E: Output,
|
||||
A: Render<E> + 'a,
|
||||
{
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("align/c"), [a]) => Self::c(state.get(a).expect("no content")),
|
||||
(Key("align/x"), [a]) => Self::x(state.get(a).expect("no content")),
|
||||
(Key("align/y"), [a]) => Self::y(state.get(a).expect("no content")),
|
||||
(Key("align/n"), [a]) => Self::n(state.get(a).expect("no content")),
|
||||
(Key("align/s"), [a]) => Self::s(state.get(a).expect("no content")),
|
||||
(Key("align/e"), [a]) => Self::e(state.get(a).expect("no content")),
|
||||
(Key("align/w"), [a]) => Self::w(state.get(a).expect("no content")),
|
||||
(Key("align/nw"), [a]) => Self::nw(state.get(a).expect("no content")),
|
||||
(Key("align/ne"), [a]) => Self::ne(state.get(a).expect("no content")),
|
||||
(Key("align/sw"), [a]) => Self::sw(state.get(a).expect("no content")),
|
||||
(Key("align/se"), [a]) => Self::se(state.get(a).expect("no content")),
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue