wip: migrating to Widget trait

This commit is contained in:
🪞👃🪞 2024-09-09 14:57:14 +03:00
parent b944dd5f9e
commit c4a5ee7b6e
4 changed files with 130 additions and 161 deletions

View file

@ -96,16 +96,12 @@ pub trait Content {
//()
//}
//}
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>
{
impl<E: Engine, W> Widget for W where W: Content<Engine = E> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
self.content().layout(to)
}
fn render (&self, to: &mut <Self as Content>::Engine)
-> Perhaps<<<Self as Content>::Engine as Engine>::Area>
{
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
self.content().render(to)
}
}

View file

@ -46,17 +46,34 @@ impl<E: Engine, T: Layout<E>> Layout<E> for Option<T> {
/// Override X and Y coordinates, aligning to corner, side, or center of area
pub enum Align<L> { Center(L), NW(L), N(L), NE(L), W(L), E(L), SW(L), S(L), SE(L) }
/// Enforce minimum size of drawing area
pub enum Min<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
pub enum Min<U: Number, T> { W(U, T), H(U, T), WH(U, U, T), }
/// Enforce maximum size of drawing area
pub enum Max<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
pub enum Max<U: Number, T> { W(U, T), H(U, T), WH(U, U, T), }
/// Expand drawing area
pub enum Outset<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
pub enum Outset<U: Number, T> { W(U, T), H(U, T), WH(U, U, T), }
/// Shrink drawing area
pub enum Inset<U: Number, L> { W(U, L), H(U, L), WH(U, U, L), }
pub enum Inset<U: Number, T> { W(U, T), H(U, T), WH(U, U, T), }
/// Move origin point of drawing area
pub enum Offset<U: Number, L> { X(U, L), Y(U, L), XY(U, U, L), }
pub enum Offset<U: Number, T> { X(U, T), Y(U, T), XY(U, U, T), }
impl<E: Engine, L: Layout<E>> Layout<E> for Min<E:: Unit, L> {
impl<T> Align<T> {
pub fn inner (&self) -> &T {
match self {
Self::Center(inner) => inner,
Self::NW(inner) => inner,
Self::N(inner) => inner,
Self::NE(inner) => inner,
Self::W(inner) => inner,
Self::E(inner) => inner,
Self::SW(inner) => inner,
Self::S(inner) => inner,
Self::SE(inner) => inner,
}
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
type Engine = E;
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
match self {
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
@ -72,9 +89,22 @@ impl<E: Engine, L: Layout<E>> Layout<E> for Min<E:: Unit, L> {
}
}
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
// 🡘 🡙 ←🡙→
self.layout(to.area())?
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
.map(|to|match self {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, L: Layout<E>> Layout<E> for Max<E:: Unit, L> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
type Engine = E;
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
match self {
Self::W(w, item) => {
@ -90,9 +120,21 @@ impl<E: Engine, L: Layout<E>> Layout<E> for Max<E:: Unit, L> {
}
}
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
self.layout(to.area())?
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
.map(|to|match self {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, L: Layout<E>> Layout<E> for Outset<E::Unit, L> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
type Engine = E;
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
match self {
Self::W(w, item) => if area.x() < *w { Ok(None) } else {
@ -106,9 +148,21 @@ impl<E: Engine, L: Layout<E>> Layout<E> for Outset<E::Unit, L> {
}
}
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
self.layout(to.area())?
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
.map(|to|match self {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, L: Layout<E>> Layout<E> for Inset<E::Unit, L> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
type Engine = E;
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
match self {
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
@ -122,9 +176,21 @@ impl<E: Engine, L: Layout<E>> Layout<E> for Inset<E::Unit, L> {
}
}
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
self.layout(to.area())?
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
.map(|to|match self {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, L: Layout<E>> Layout<E> for Offset<E::Unit, L> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
type Engine = E;
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
match self {
Self::X(x, item) => if area.w() < *x { Ok(None) } else {
@ -138,67 +204,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 {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, R: Render<E> + Layout<E>> Render<E> for Max<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 {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, R: Render<E> + Layout<E>> Render<E> for Inset<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 {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, R: Render<E> + Layout<E>> Render<E> for Outset<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 {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}.render(to))
.transpose()
.map(|x|x.flatten())
}
}
impl<E: Engine, R: Render<E> + Layout<E>> Render<E> for Offset<E::Unit, R> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
self.layout(to.area())?
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
.map(|to|match self {