output: refactor Content and Render traits

This commit is contained in:
🪞👃🪞 2025-09-06 08:46:52 +03:00
parent 74b3af2212
commit 1c21a85f27
24 changed files with 346 additions and 613 deletions

View file

@ -1,88 +1,67 @@
use crate::*;
/// Composable renderable with static dispatch.
pub trait Content<E: Output> {
/// Return a [Render]able of a specific type.
fn content (&self) -> impl Render<E> + '_ {
()
}
/// Perform layout. By default, delegates to [Self::content].
fn layout (&self, area: E::Area) -> E::Area {
self.content().layout(area)
}
/// Draw to output. By default, delegates to [Self::content].
fn render (&self, output: &mut E) {
self.content().render(output)
}
}
/// Every pointer to [Content] is a [Content].
impl<E: Output, 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) { (*self).render(output) }
pub trait Content<E: Output>: Sized {
/// Return opaque [Render]able.
fn content (&self) -> Option<impl Render<E> + '_> { Option::<()>::None }
}
/// The platonic ideal unit of [Content]: total emptiness at dead center (e=1vg^sqrt(-1))
impl<E: Output> Content<E> for () {
fn layout (&self, area: E::Area) -> E::Area { area.center().to_area_pos().into() }
fn render (&self, _: &mut E) {}
impl<E: Output> Content<E> for () {}
impl<E: Output> Content<E> for fn(&mut E) {
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self)
}
}
impl<E: Output, T: Render<E>> Content<E> for fn()->T {
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self())
}
}
impl<E: Output, 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) {
self.as_ref()
.map(|content|content.render(output));
fn content (&self) -> Option<impl Render<E> + '_> {
if let Some(content) = self {
content.content()
} else {
None
}
}
}
/// You can render from a box.
impl<E: Output> Content<E> for RenderBox<E> {
fn content (&self) -> impl Render<E> + '_ { self.deref() }
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self.deref())
}
}
/// You can render from an opaque pointer.
impl<E: Output> Content<E> for &dyn Render<E> where Self: Sized {
fn content (&self) -> impl Render<E> + '_ {
fn content (&self) -> Option<impl Render<E> + '_> {
#[allow(suspicious_double_ref_op)]
self.deref()
}
fn layout (&self, area: E::Area) -> E::Area {
#[allow(suspicious_double_ref_op)]
Render::layout(self.deref(), area)
}
fn render (&self, output: &mut E) {
#[allow(suspicious_double_ref_op)]
Render::render(self.deref(), output)
Some(self.deref())
}
}
/// Implement [Content] with custom rendering for a struct.
#[macro_export] macro_rules! render {
(|$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 }
/// Implement composable content for a struct.
#[macro_export] macro_rules! content {
// Implement for all [Output]s.
(|$self:ident:$Struct:ty| $content:expr) => {
impl<E: Output> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> + '_ { Some($content) }
}
};
// Implement for specific [Output].
($Output:ty:|
$self:ident:
$Struct:ident $(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?, $to:ident
|$render:expr) => {
$Struct:ident$(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?
|$content:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut $Output) { $render }
fn content (&$self) -> impl Render<$Output> + '_ { $content }
}
};
}