wip: big flat pt.4: extract layout crate

This commit is contained in:
🪞👃🪞 2024-12-30 19:07:43 +01:00
parent cb680ab096
commit 34e731f111
21 changed files with 2125 additions and 83 deletions

View file

@ -1,6 +1,38 @@
use crate::*;
use std::sync::{Arc, Mutex, RwLock};
/// Define custom content for a struct.
#[macro_export] macro_rules! render {
// Implement for all engines
(|$self:ident:$Struct:ident$(<$($L:lifetime),*E$(,$T:ident$(:$U:path)?)*$(,)?>)?|$cb:expr) => {
impl<E: Engine, $($($L),*$($T $(: $U)?),*)?> Content<E> for $Struct $(<$($L,)* E, $($T),*>)? {
fn content (&$self) -> Option<impl Render<$E>> {
Some($cb)
}
}
};
// Implement for a specific engine
(<$E:ty>|$self:ident:$Struct:ident$(<
$($($L:lifetime),+)?
$($($T:ident$(:$U:path)?),+)?
>)?|$cb:expr) => {
impl $(<
$($($L),+)?
$($($T$(:$U)?),+)?
>)? Content<$E> for $Struct $(<
$($($L),+)?
$($($T),+)?
>)? {
fn content (&$self) -> Option<impl Render<$E>> {
Some($cb)
}
}
}
}
/// Write content to output buffer.
pub trait Render<E: Engine>: Send + Sync {
/// Minimum size to use
@ -18,18 +50,18 @@ pub trait Content<E: Engine>: Send + Sync {
fn content (&self) -> Option<impl Render<E>>;
}
impl<E: Engine, C: Content<E>> Render<E> for C {
/// Minimum size to use
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
self.content().map(|content|content.min_size(to))
.unwrap_or(Ok(None))
}
/// Draw to output render target
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.content().map(|content|content.render(to))
.unwrap_or(Ok(()))
}
}
//impl<E: Engine, C: Content<E>> Render<E> for C {
///// Minimum size to use
//fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
//self.content().map(|content|content.min_size(to))
//.unwrap_or(Ok(None))
//}
///// Draw to output render target
//fn render (&self, to: &mut E::Output) -> Usually<()> {
//self.content().map(|content|content.render(to))
//.unwrap_or(Ok(()))
//}
//}
/// Rendering target
pub trait Output<E: Engine> {
@ -44,9 +76,9 @@ pub trait Output<E: Engine> {
//impl<E: Engine> Render<E> for &dyn Render<E> {}
//impl<E: Engine> Render<E> for &mut dyn Render<E> {}
//impl<E: Engine> Render<E> for Box<dyn Render<E>> {}
//impl<E: Engine, R: Render<E>> Render<E> for &R {}
impl<E: Engine, R: Render<E>> Render<E> for &R {}
//impl<E: Engine, R: Render<E>> Render<E> for &mut R {}
//impl<E: Engine, R: Render<E>> Render<E> for Option<R> {}
impl<E: Engine, R: Render<E>> Render<E> for Option<R> {}
//impl<E: Engine, R: Render<E>> Render<E> for Arc<R> {}
//impl<E: Engine, R: Render<E>> Render<E> for Mutex<R> {}
//impl<E: Engine, R: Render<E>> Render<E> for RwLock<R> {}
@ -101,38 +133,6 @@ impl<E: Engine, C: Content<E>> Render<E> for C {
//}
/// Define custom content for a struct.
#[macro_export] macro_rules! render {
// Implement for all engines
(|$self:ident:$Struct:ident$(<$($L:lifetime),*E$(,$T:ident$(:$U:path)?)*$(,)?>)?|$cb:expr) => {
impl<E: Engine, $($($L),*$($T $(: $U)?),*)?> Content<E> for $Struct $(<$($L,)* E, $($T),*>)? {
fn content (&$self) -> Option<impl Render<$E>> {
Some($cb)
}
}
};
// Implement for a specific engine
(<$E:ty>|$self:ident:$Struct:ident$(<
$($($L:lifetime),+)?
$($($T:ident$(:$U:path)?),+)?
>)?|$cb:expr) => {
impl $(<
$($($L),+)?
$($($T$(:$U)?),+)?
>)? Content<$E> for $Struct $(<
$($($L),+)?
$($($T),+)?
>)? {
fn content (&$self) -> Option<impl Render<$E>> {
Some($cb)
}
}
}
}
impl<E: Engine, R: Render<E>> Render<E> for &R {
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
(*self).min_size(to)