mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
refactor engine and layout into input and output
This commit is contained in:
parent
f052891473
commit
4d0f98acd2
40 changed files with 104 additions and 109 deletions
66
output/src/render.rs
Normal file
66
output/src/render.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use crate::*;
|
||||
use std::ops::Deref;
|
||||
|
||||
/// Custom layout and rendering.
|
||||
pub trait Render<E: Output>: Send + Sync {
|
||||
fn layout (&self, area: E::Area) -> E::Area;
|
||||
fn render (&self, output: &mut E);
|
||||
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Sized + 'a {
|
||||
Box::new(self) as RenderBox<'a, E>
|
||||
}
|
||||
}
|
||||
|
||||
pub type RenderDyn<'a, Output> = dyn Render<Output> + 'a;
|
||||
impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
|
||||
fn content (&self) -> impl Render<E> { self.deref() }
|
||||
fn layout (&self, area: E::Area) -> E::Area { Render::layout(self.deref(), area) }
|
||||
fn render (&self, output: &mut E) { Render::render(self.deref(), output) }
|
||||
}
|
||||
|
||||
pub type RenderBox<'a, E: Output> = Box<RenderDyn<'a, E>>;
|
||||
impl<'a, E: Output> Content<E> for RenderBox<'a, E> {
|
||||
fn content (&self) -> impl Render<E> { self.deref() }
|
||||
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
|
||||
}
|
||||
|
||||
impl<E: Output, 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) { Content::render(self, output) }
|
||||
}
|
||||
|
||||
#[macro_export] macro_rules! render {
|
||||
(($self:ident:$Struct:ty) => $content:expr) => {
|
||||
impl <E: Output> 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: Output, $($T$(:$Trait)?),*> Content<E>
|
||||
for $Struct $(<$($L),* $($T),*>>)? {
|
||||
fn render (&$self, $to: &mut E) { $render }
|
||||
}
|
||||
};
|
||||
($Output:ty:
|
||||
($self:ident:$Struct:ident $(<$(
|
||||
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
|
||||
),+>)?) => $content:expr
|
||||
) => {
|
||||
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
|
||||
for $Struct $(<$($($L)? $($T)?),+>)? {
|
||||
fn content (&$self) -> impl Render<$Output> { $content }
|
||||
}
|
||||
};
|
||||
|
||||
($Output:ty:
|
||||
|$self:ident : $Struct:ident $(<$(
|
||||
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
|
||||
),+>)?, $to:ident| $render:expr
|
||||
) => {
|
||||
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
|
||||
for $Struct $(<$($($L)? $($T)?),+>)? {
|
||||
fn render (&$self, $to: &mut $Output) { $render }
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue