cleanup Engine API and generalize Inset/Outset

This commit is contained in:
🪞👃🪞 2024-09-13 21:27:40 +03:00
parent 4e0eb0c335
commit 0737769232
6 changed files with 118 additions and 91 deletions

View file

@ -14,19 +14,22 @@ pub trait Engine: Send + Sync + Sized {
/// Unit of distance.
type Unit: Number;
type Area: Area<Self::Unit> + From<[Self::Unit;4]> + Debug;
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug;
type HandleInput;
type Handled;
// FIXME
fn area (&self) -> Self::Area;
// FIXME
fn with_area (&mut self, x: Self::Unit, y: Self::Unit, w: Self::Unit, h: Self::Unit)
-> &mut Self;
// FIXME
fn render_in (
#[inline] fn area (&self) -> Self::Area;
#[inline] fn area_mut (&mut self) -> &mut Self::Area;
#[inline] fn render_in (
&mut self, area: Self::Area, widget: &impl Widget<Engine = Self>
) -> Perhaps<Self::Area>;
) -> Perhaps<Self::Area> {
let last = self.area();
*self.area_mut() = area;
let next = widget.render(self)?;
*self.area_mut() = last;
Ok(next)
}
}
pub trait Widget: Send + Sync {
@ -600,6 +603,42 @@ impl<N: Number, T: Widget> Outset<N, T> {
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
match *self {
Self::X(x, ref inner) => Shrink::X(x + x, inner as &dyn Widget<Engine = E>),
Self::Y(y, ref inner) => Shrink::Y(y + y, inner as &dyn Widget<Engine = E>),
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) -> Perhaps<E::Area> {
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>),
Self::XY(x, y, ref inner) => Plus::XY(x, y, inner as &dyn Widget<Engine = E>),
}.render(to)
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
match *self {
Self::X(x, ref inner) => Grow::X(x + x, inner as &dyn Widget<Engine = E>),
Self::Y(y, ref inner) => Grow::Y(y + y, inner as &dyn Widget<Engine = E>),
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) -> Perhaps<E::Area> {
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>),
Self::XY(x, y, ref inner) => Plus::XY(x, y, inner as &dyn Widget<Engine = E>),
}.render(to)
}
}
/// Move origin point of drawing area
pub enum Plus<N: Number, T: Widget> {
/// Move origin to the right