mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
generalize EdnItem.
maybe should rename it to Atom? ~90 instances of it
This commit is contained in:
parent
1b9da07280
commit
143cd24e09
20 changed files with 314 additions and 286 deletions
|
|
@ -1,66 +0,0 @@
|
|||
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<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, A: Content<E>> Content<E> for Align<E, A> {
|
||||
fn content (&self) -> impl Render<E> {
|
||||
&self.2
|
||||
}
|
||||
fn layout (&self, on: E::Area) -> E::Area {
|
||||
use Alignment::*;
|
||||
let it = Render::layout(&self.content(), on).xywh();
|
||||
let cx = on.x()+(on.w().minus(it.w())/2.into());
|
||||
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.1 {
|
||||
Center => [cx, cy],
|
||||
X => [cx, it.y()],
|
||||
Y => [it.x(), cy],
|
||||
NW => [on.x(), on.y()],
|
||||
N => [cx, on.y()],
|
||||
NE => [fx, on.y()],
|
||||
W => [on.x(), cy],
|
||||
E => [fx, cy],
|
||||
SW => [on.x(), fy],
|
||||
S => [cx, fy],
|
||||
SE => [fx, fy],
|
||||
}.into();
|
||||
[x, y, it.w(), it.h()].into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Align<E, RenderBox<'a, E>> {
|
||||
fn try_from_edn (state: &'a T, head: &EdnItem, tail: &'a [EdnItem]) -> Option<Self> {
|
||||
use EdnItem::*;
|
||||
Some(match (head, tail) {
|
||||
(Key("align/c"), [a]) => Self::c(state.get_content(a).expect("no content")),
|
||||
(Key("align/x"), [a]) => Self::x(state.get_content(a).expect("no content")),
|
||||
(Key("align/y"), [a]) => Self::y(state.get_content(a).expect("no content")),
|
||||
(Key("align/n"), [a]) => Self::n(state.get_content(a).expect("no content")),
|
||||
(Key("align/s"), [a]) => Self::s(state.get_content(a).expect("no content")),
|
||||
(Key("align/e"), [a]) => Self::e(state.get_content(a).expect("no content")),
|
||||
(Key("align/w"), [a]) => Self::w(state.get_content(a).expect("no content")),
|
||||
(Key("align/nw"), [a]) => Self::nw(state.get_content(a).expect("no content")),
|
||||
(Key("align/ne"), [a]) => Self::ne(state.get_content(a).expect("no content")),
|
||||
(Key("align/sw"), [a]) => Self::sw(state.get_content(a).expect("no content")),
|
||||
(Key("align/se"), [a]) => Self::se(state.get_content(a).expect("no content")),
|
||||
_ => return None
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue