wip: reenable dynamic dispatch

This commit is contained in:
🪞👃🪞 2025-01-04 10:44:20 +01:00
parent 600d0b3aca
commit ac3827b8f3
11 changed files with 997 additions and 194 deletions

View file

@ -9,7 +9,7 @@ pub trait Output<E: Engine> {
/// Mutable pointer to area
fn area_mut (&mut self) -> &mut E::Area;
/// Render widget in area
fn place (&mut self, area: E::Area, content: &impl Content<E>);
fn place (&mut self, area: E::Area, content: &impl Render<E>);
#[inline] fn x (&self) -> E::Unit { self.area().x() }
#[inline] fn y (&self) -> E::Unit { self.area().y() }
@ -17,19 +17,25 @@ 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 trait Content<E: Engine>: Send + Sync {
fn content (&self) -> impl Content<E> {
()
}
fn layout (&self, area: E::Area) -> E::Area {
self.content().layout(area)
}
fn render (&self, output: &mut E::Output) {
output.place(self.layout(output.area()), &self.content())
}
pub trait Render<E: Engine>: Send + Sync {
fn layout (&self, area: E::Area) -> E::Area { area }
fn render (&self, output: &mut E::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> Content<E> for Box<dyn Render<E>> {
fn content (&self) -> impl Content<E> { self }
}
impl<E: Engine> Content<E> for &dyn Render<E> {
fn content (&self) -> impl Content<E> { self }
}
pub trait Content<E: Engine>: Send + Sync + Sized {
fn content (&self) -> impl Content<E> { () }
fn layout (&self, area: E::Area) -> E::Area { area }
fn render (&self, output: &mut E::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 {

View file

@ -8,7 +8,7 @@ pub struct TuiOut {
impl Output<Tui> for TuiOut {
#[inline] fn area (&self) -> [u16;4] { self.area }
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
#[inline] fn place (&mut self, area: [u16;4], content: &impl Content<Tui>) {
#[inline] fn place (&mut self, area: [u16;4], content: &impl Render<Tui>) {
let last = self.area();
*self.area_mut() = area;
content.render(self);