Box::deref makes the EDN rendering examples really work!

This commit is contained in:
🪞👃🪞 2025-01-05 16:37:06 +01:00
parent ea8ba031c3
commit 62a0e8c17c
9 changed files with 103 additions and 92 deletions

View file

@ -1,13 +1,6 @@
use crate::*;
use std::sync::{Mutex, Arc, RwLock};
/// Handle input
pub trait Handle<E: Engine>: Send + Sync {
fn handle (&mut self, _input: &E::Input) -> Perhaps<E::Handled> {
Ok(None)
}
}
#[macro_export] macro_rules! handle {
(<$E:ty>|$self:ident:$Struct:ty,$input:ident|$handler:expr) => {
impl Handle<$E> for $Struct {
@ -18,12 +11,17 @@ pub trait Handle<E: Engine>: Send + Sync {
}
}
/// Handle input
pub trait Handle<E: Engine>: Send + Sync {
fn handle (&mut self, _input: &E::Input) -> Perhaps<E::Handled> {
Ok(None)
}
}
impl<E: Engine, H: Handle<E>> Handle<E> for &mut H {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
(*self).handle(context)
}
}
impl<E: Engine, H: Handle<E>> Handle<E> for Option<H> {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
if let Some(ref mut handle) = self {
@ -33,25 +31,21 @@ impl<E: Engine, H: Handle<E>> Handle<E> for Option<H> {
}
}
}
impl<H, E: Engine> Handle<E> for Mutex<H> where H: Handle<E> {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
self.get_mut().unwrap().handle(context)
}
}
impl<H, E: Engine> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
self.lock().unwrap().handle(context)
}
}
impl<H, E: Engine> Handle<E> for RwLock<H> where H: Handle<E> {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
self.write().unwrap().handle(context)
}
}
impl<H, E: Engine> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
self.write().unwrap().handle(context)

View file

@ -22,74 +22,3 @@ pub trait Output<E: Engine> {
#[inline] fn h (&self) -> E::Unit { self.area().h() }
#[inline] fn wh (&self) -> E::Size { self.area().wh().into() }
}
impl<E: Engine, T: Content<E>> Content<E> for &T {
fn content (&self) -> impl Render<E> {
(*self).content()
}
fn layout (&self, area: E::Area) -> E::Area {
(*self).layout(area)
}
fn render (&self, output: &mut E::Output) {
(*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()
}
fn layout (&self, area: E::Area) -> E::Area {
self.as_ref()
.map(|content|content.layout(area))
.unwrap_or([0.into(), 0.into(), 0.into(), 0.into(),].into())
}
fn render (&self, output: &mut E::Output) {
self.as_ref()
.map(|content|content.render(output));
}
}
#[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Engine> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> { Some($content) }
}
};
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Engine, $($T$(:$Trait)?),*> Content<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E::Output) { $render }
}
};
($Engine:ty:
($self:ident:$Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?) => $content:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Render<$Engine> { $content }
}
};
($Engine:ty:
|$self:ident : $Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?, $to:ident| $render:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut <$Engine as Engine>::Output) { $render }
}
};
}

View file

@ -64,9 +64,17 @@ pub trait Area<N: Coordinate>: From<[N;4]> + Debug + Copy {
#[inline] fn centered (&self) -> [N;2] {
[self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())]
}
fn zero () -> [N;4] {
#[inline] fn zero () -> [N;4] {
[N::zero(), N::zero(), N::zero(), N::zero()]
}
#[inline] fn from_position (pos: impl Size<N>) -> [N;4] {
let [x, y] = pos.wh();
[x, y, 0.into(), 0.into()]
}
#[inline] fn from_size (size: impl Size<N>) -> [N;4] {
let [w, h] = size.wh();
[0.into(), 0.into(), w, h]
}
}
impl<N: Coordinate> Area<N> for (N, N, N, N) {

View file

@ -6,7 +6,28 @@ pub trait Content<E: Engine>: Send + Sync + Sized {
fn layout (&self, area: E::Area) -> E::Area { self.content().layout(area) }
fn render (&self, output: &mut E::Output) { self.content().render(output) }
}
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, C: Content<E>> Content<E> for &C {
fn content (&self) -> impl Render<E> { (*self).content() }
fn layout (&self, area: E::Area) -> E::Area { (*self).layout(area) }
fn render (&self, output: &mut E::Output) { (*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 { area.center().to_area_pos().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()
}
fn layout (&self, area: E::Area) -> E::Area {
self.as_ref()
.map(|content|content.layout(area))
.unwrap_or([0.into(), 0.into(), 0.into(), 0.into(),].into())
}
fn render (&self, output: &mut E::Output) {
self.as_ref()
.map(|content|content.render(output));
}
}

View file

@ -1,4 +1,5 @@
use crate::*;
use std::ops::Deref;
/// Custom layout and rendering.
pub trait Render<E: Engine>: Send + Sync {
@ -8,11 +9,57 @@ pub trait Render<E: Engine>: Send + Sync {
Box::new(self) as RenderBox<'a, E>
}
}
pub type RenderDyn<'a, Engine> = dyn Render<Engine> + 'a;
impl<'a, E: Engine> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> { self }
fn content (&self) -> impl Render<E> { *self }
fn layout (&self, area: E::Area) -> E::Area { Render::layout(self.deref(), area) }
fn render (&self, output: &mut E::Output) { Render::render(self.deref(), output) }
}
pub type RenderBox<'a, E: Engine> = Box<RenderDyn<'a, E>>;
impl<'a, E: Engine> Content<E> for RenderBox<'a, E> {
fn content (&self) -> impl Render<E> { self }
fn content (&self) -> impl Render<E> { self.deref() }
}
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) }
}
#[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Engine> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> { Some($content) }
}
};
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Engine, $($T$(:$Trait)?),*> Content<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E::Output) { $render }
}
};
($Engine:ty:
($self:ident:$Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?) => $content:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Render<$Engine> { $content }
}
};
($Engine:ty:
|$self:ident : $Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?, $to:ident| $render:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Engine>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut <$Engine as Engine>::Output) { $render }
}
};
}

View file

@ -19,6 +19,14 @@ pub trait Size<N: Coordinate>: From<[N;2]> + Debug + Copy {
#[inline] fn zero () -> [N;2] {
[N::zero(), N::zero()]
}
#[inline] fn to_area_pos (&self) -> [N;4] {
let [x, y] = self.wh();
[x, y, 0.into(), 0.into()]
}
#[inline] fn to_area_size (&self) -> [N;4] {
let [w, h] = self.wh();
[0.into(), 0.into(), w, h]
}
}
impl<N: Coordinate> Size<N> for (N, N) {