fix keymap macros. rendering issue

This commit is contained in:
🪞👃🪞 2025-01-05 01:15:53 +01:00
parent 6f51872856
commit f3fd88a199
15 changed files with 303 additions and 180 deletions

View file

@ -14,13 +14,6 @@ pub trait Output<E: Engine> {
#[inline] fn h (&self) -> E::Unit { self.area().h() }
#[inline] fn wh (&self) -> E::Size { self.area().wh().into() }
}
pub struct Thunk<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync>(F, PhantomData<E>);
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
pub fn new (thunk: F) -> Self { Self(thunk, Default::default()) }
}
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Content<E> { (self.0)() }
}
pub trait Render<E: Engine>: Send + Sync {
fn layout (&self, area: E::Area) -> E::Area;
fn render (&self, output: &mut E::Output);
@ -34,21 +27,19 @@ impl<E: Engine, C: Content<E>> Render<E> for C {
fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) }
fn render (&self, output: &mut E::Output) { Content::render(self, output) }
}
impl<E: Engine> Content<E> for Box<dyn Render<E>> {
impl<'a, E: Engine> Content<E> for Box<dyn Render<E> + Send + Sync + 'a> {
fn content (&self) -> impl Render<E> { self }
}
impl<E: Engine> Content<E> for &dyn Render<E> {
impl<E: Engine> Content<E> for &(dyn Render<E> + '_) {
fn content (&self) -> impl Render<E> { self }
}
/// The platonic ideal unit of [Content]: total emptiness at dead center.
impl<E: Engine> Content<E> for () {
fn layout (&self, area: E::Area) -> E::Area {
let [x, y] = area.center();
[x, y, 0.into(), 0.into()].into()
}
fn render (&self, _: &mut E::Output) {}
pub struct Thunk<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync>(F, PhantomData<E>);
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
pub fn new (thunk: F) -> Self { Self(thunk, Default::default()) }
}
impl<E: Engine, T: Content<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Render<E> { (self.0)() }
}
impl<E: Engine, T: Content<E>> Content<E> for &T {
fn content (&self) -> impl Render<E> {
(*self).content()
@ -60,7 +51,14 @@ impl<E: Engine, T: Content<E>> Content<E> for &T {
(*self).render(output)
}
}
/// The platonic ideal unit of [Content]: total emptiness at dead center.
impl<E: Engine> Content<E> for () {
fn layout (&self, area: E::Area) -> E::Area {
let [x, y] = area.center();
[x, y, 0.into(), 0.into()].into()
}
fn render (&self, _: &mut E::Output) {}
}
impl<E: Engine, T: Content<E>> Content<E> for Option<T> {
fn content (&self) -> impl Render<E> {
self.as_ref()
@ -76,11 +74,10 @@ impl<E: Engine, T: Content<E>> Content<E> for Option<T> {
}
}
#[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Engine> Content<E> for $Struct {
fn content (&$self) -> impl Content<E> { Some($content) }
fn content (&$self) -> impl Render<E> { Some($content) }
}
};
(|$self:ident:$Struct:ident $(<
@ -98,7 +95,7 @@ impl<E: Engine, T: Content<E>> Content<E> for Option<T> {
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Content<$Engine> { $content }
fn content (&$self) -> impl Render<$Engine> { $content }
}
};