mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-10 21:56:42 +01:00
wip: Content and Widget traits
This commit is contained in:
parent
5fc7da3aca
commit
b944dd5f9e
13 changed files with 209 additions and 36 deletions
|
|
@ -53,7 +53,7 @@ impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Layers<'a, E: Engine>(pub &'a [&'a dyn Render<E>]);
|
||||
pub struct Layers<'a, E: Engine>(pub &'a [&'a dyn Layout<E>]);
|
||||
|
||||
// this actually works, except for the type inference
|
||||
//pub struct Layers<'a, E: Engine + 'a, I: std::iter::IntoIterator<Item = &'a dyn Render<E>>>(
|
||||
|
|
|
|||
|
|
@ -15,3 +15,97 @@ pub trait ExitableComponent<E>: Exit + Component<E> where E: Engine {
|
|||
}
|
||||
|
||||
impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}
|
||||
|
||||
pub trait Widget {
|
||||
type Engine: Engine;
|
||||
fn layout (&self, to: <<Self as Widget>::Engine as Engine>::Area) ->
|
||||
Perhaps<<<Self as Widget>::Engine as Engine>::Area>;
|
||||
fn render (&self, to: &mut Self::Engine) ->
|
||||
Perhaps<<<Self as Widget>::Engine as Engine>::Area>;
|
||||
}
|
||||
impl<E: Engine> Widget for Box<dyn Widget<Engine = E>> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
(**self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine> Widget for &dyn Widget<Engine = E> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
(*self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
(*self).render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine> Widget for &mut dyn Widget<Engine = E> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
(**self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, W: Widget<Engine = E>> Widget for Arc<W> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
self.as_ref().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.as_ref().render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, W: Widget<Engine = E>> Widget for Mutex<W> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
self.lock().unwrap().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.lock().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, W: Widget<Engine = E>> Widget for RwLock<W> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
self.read().unwrap().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.read().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, W: Widget<Engine = E>> Widget for Option<W> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
Ok(self.as_ref().map(|widget|widget.layout(to)).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
Ok(self.as_ref().map(|widget|widget.render(to)).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Content {
|
||||
type Engine: Engine;
|
||||
fn content (&self) -> impl Widget<Engine = <Self as Content>::Engine>;
|
||||
}
|
||||
//impl<E> Content<E> for () where E: Engine {
|
||||
//fn content (&self) -> impl Widget<E> {
|
||||
//()
|
||||
//}
|
||||
//}
|
||||
impl<W> Widget for W where W: Content {
|
||||
type Engine = <Self as Content>::Engine;
|
||||
fn layout (&self, to: <<Self as Content>::Engine as Engine>::Area)
|
||||
-> Perhaps<<<Self as Content>::Engine as Engine>::Area>
|
||||
{
|
||||
self.content().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut <Self as Content>::Engine)
|
||||
-> Perhaps<<<Self as Content>::Engine as Engine>::Area>
|
||||
{
|
||||
self.content().render(to)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,26 @@ pub fn center_box (area: [u16;4], w: u16, h: u16) -> [u16;4] {
|
|||
pub trait Layout<E: Engine>: Render<E> {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area>;
|
||||
}
|
||||
impl<'a, E: Engine> Layout<E> for Box<dyn Layout<E> + 'a> {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
(**self).layout(area)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Engine> Layout<E> for Box<dyn Component<E> + 'a> {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
(**self).layout(area)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, T: Layout<E>> Layout<E> for &T {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
(*self).layout(area)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, T: Layout<E>> Layout<E> for &mut T {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
(**self).layout(area)
|
||||
}
|
||||
}
|
||||
impl<E: Engine, T: Layout<E>> Layout<E> for Option<T> {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
|
|
@ -127,6 +142,7 @@ impl<E: Engine, L: Layout<E>> Layout<E> for Offset<E::Unit, L> {
|
|||
|
||||
impl<E: Engine, R: Render<E> + Layout<E>> Render<E> for Min<E::Unit, R> {
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
|
||||
// 🡘 🡙 ←🡙→
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|match self {
|
||||
|
|
|
|||
|
|
@ -27,6 +27,16 @@ impl<'a, E: Engine> Render<E> for Box<dyn Render<E> + 'a> {
|
|||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Engine> Render<E> for Box<dyn Layout<E> + 'a> {
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Engine> Render<E> for Box<dyn Component<E> + 'a> {
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
|
||||
/// Immutable references can be rendered.
|
||||
impl<R, E: Engine> Render<E> for &R where R: Render<E> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue