wip: return () from render method

This commit is contained in:
🪞👃🪞 2024-09-15 16:54:18 +03:00
parent 5d00e9f284
commit e3fa292a3c
3 changed files with 167 additions and 140 deletions

View file

@ -35,26 +35,26 @@ pub trait Output<E: Engine> {
fn area_mut (&mut self)
-> &mut E::Area;
fn render_in (&mut self, area: E::Area, widget: &dyn Widget<Engine = E>)
-> Perhaps<E::Area>;
-> Usually<()>;
}
pub trait Widget: Send + Sync {
type Engine: Engine;
fn layout (
&self, to: <Self::Engine as Engine>::Size
fn layout (&self,
to: <Self::Engine as Engine>::Size
) -> Perhaps<<Self::Engine as Engine>::Size> {
Ok(Some(to))
}
fn render (
&self, to: &mut <Self::Engine as Engine>::Output
) -> Perhaps<<<Self as Widget>::Engine as Engine>::Area>;
fn render (&self,
to: &mut <Self::Engine as Engine>::Output
) -> Usually<()>;
}
impl<'a, E: Engine> Widget for Box<dyn Widget<Engine = E> + 'a> {
type Engine = E;
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
(**self).layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
(**self).render(to)
}
}
@ -63,7 +63,7 @@ impl<E: Engine> Widget for &dyn Widget<Engine = E> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
(*self).layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
(*self).render(to)
}
}
@ -72,7 +72,7 @@ impl<E: Engine> Widget for &mut dyn Widget<Engine = E> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
(**self).layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
(**self).render(to)
}
}
@ -81,7 +81,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Arc<W> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.as_ref().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.as_ref().render(to)
}
}
@ -90,7 +90,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Mutex<W> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.lock().unwrap().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.lock().unwrap().render(to)
}
}
@ -99,7 +99,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for RwLock<W> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.read().unwrap().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.read().unwrap().render(to)
}
}
@ -108,8 +108,8 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Option<W> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
Ok(self.as_ref().map(|widget|widget.layout(to)).transpose()?.flatten())
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
Ok(self.as_ref().map(|widget|widget.render(to)).transpose()?.flatten())
fn render (&self, to: &mut E::Output) -> Usually<()> {
self.as_ref().map(|widget|widget.render(to)).unwrap_or(Ok(()))
}
}
@ -127,10 +127,10 @@ impl<E: Engine, W: Content<Engine = E>> Widget for W {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.content().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
match self.layout(to.area().wh().into())? {
Some(wh) => to.render_in(to.area().clip(wh).into(), &self.content()),
None => Ok(None)
None => Ok(())
}
}
}
@ -428,17 +428,17 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
fn layout (&self, outer_area: E::Size) -> Perhaps<E::Size> {
self.inner().layout(outer_area)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
let outer_area = to.area();
Ok(if let Some(inner_size) = self.layout(outer_area.wh().into())? {
let inner_area = outer_area.clip(inner_size);
if let Some(aligned) = align(&self, outer_area.into(), inner_area.into()) {
to.render_in(aligned, self.inner())?
} else {
None
()
}
} else {
None
()
})
}
}
@ -483,10 +483,10 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
}.into()))
}
// TODO: 🡘 🡙 ←🡙→
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
Ok(self.layout(to.area().wh().into())?
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
.transpose()?.flatten())
.transpose()?.unwrap_or(()))
}
}
@ -515,10 +515,10 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
Self::XY(w, h, _) => [to.w().min(w), to.h().min(h)],
}.into()))
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
Ok(self.layout(to.area().wh().into())?
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
.transpose()?.flatten())
.transpose()?.unwrap_or(()))
}
}
@ -547,10 +547,10 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Grow<E::Unit, T> {
Self::XY(w, h, _) => [to.w() + w, to.h() + h],
}.into()))
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
Ok(self.layout(to.area().wh().into())?
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
.transpose()?.flatten())
.transpose()?.unwrap_or(()))
}
}
@ -579,10 +579,10 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Shrink<E::Unit, T> {
Self::XY(w, h, _) => [to.w() - w, to.h() - h]
}.into()))
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
Ok(self.layout(to.area().wh().into())?
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
.transpose()?.flatten())
.transpose()?.unwrap_or(()))
}
}
@ -627,7 +627,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
Self::XY(x, y, ref inner) => Shrink::XY(x + x, y + y, inner as &dyn Widget<Engine = E>),
}.layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
match *self {
Self::X(x, ref inner) => Plus::X(x, inner as &dyn Widget<Engine = E>),
Self::Y(y, ref inner) => Plus::Y(y, inner as &dyn Widget<Engine = E>),
@ -645,7 +645,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
Self::XY(x, y, ref inner) => Grow::XY(x + x, y + y, inner as &dyn Widget<Engine = E>),
}.layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
match *self {
Self::X(x, ref inner) => Plus::X(x, inner as &dyn Widget<Engine = E>),
Self::Y(y, ref inner) => Plus::Y(y, inner as &dyn Widget<Engine = E>),
@ -681,14 +681,14 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Plus<E::Unit, T> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.inner().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
let area = to.area();
Ok(self.layout(area.wh().into())?
.map(|size|to.render_in(match *self {
Self::X(x, _) => [area.x() + x, area.y(), size.w(), size.h()],
Self::Y(y, _) => [area.x(), area.y() + y, size.w(), size.h()],
Self::XY(x, y, _) => [area.x() + x, area.y() + y, size.w(), size.h()],
}.into(), self.inner())).transpose()?.flatten())
}.into(), self.inner())).transpose()?.unwrap_or(()))
}
}
@ -719,14 +719,14 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Minus<E::Unit, T> {
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
self.inner().layout(to)
}
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
fn render (&self, to: &mut E::Output) -> Usually<()> {
let area = to.area();
Ok(self.layout(area.wh().into())?
.map(|size|to.render_in(match *self {
Self::X(x, _) => [area.x().minus(x), area.y(), size.w(), size.h()],
Self::Y(y, _) => [area.x(), area.y().minus(y), size.w(), size.h()],
Self::XY(x, y, _) => [area.x().minus(x), area.y().minus(y), size.w(), size.h()],
}.into(), self.inner())).transpose()?.flatten())
}.into(), self.inner())).transpose()?.unwrap_or(()))
}
}