wip: Outset=Center+2*Grow, Inset=Center+2*Shrink

This commit is contained in:
🪞👃🪞 2024-09-12 23:49:22 +03:00
parent c9b79e76fc
commit 4b413cfb60
2 changed files with 203 additions and 120 deletions

View file

@ -418,13 +418,11 @@ impl<N: Number, T> Min<N, T> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::X(w, _) => (to.w() < *w).then(||[to.x() + *w, to.y(), to.w() - *w, to.h()]),
Self::Y(h, _) => (to.h() < *h).then(||[to.x(), to.y() + *h, to.w(), to.h() - *h]),
Self::XY(w, h, _) => (to.w() < *w || to.h() < *h).then(||[
to.x() + *w, to.y() + *h, to.w() - *w, to.h() - *h
])
}.map(|stretched|self.inner().layout(stretched.into())).transpose()?.flatten())
Ok(self.inner().layout(to)?.map(|to|match *self {
Self::X(w, _) => [to.x(), to.y(), to.w().max(w), to.h()],
Self::Y(h, _) => [to.x(), to.y(), to.w(), to.h().max(h)],
Self::XY(w, h, _) => [to.x(), to.y(), to.w().max(w), to.h().max(h)],
}.into()))
}
// TODO: 🡘 🡙 ←🡙→
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
@ -451,13 +449,11 @@ impl<N: Number, T> Max<N, T> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::X(w, _) => (*w <= to.w()).then(||[to.x(), to.y(), to.w().min(*w), to.h()]),
Self::Y(h, _) => (*h <= to.h()).then(||[to.x(), to.y(), to.w(), to.h().min(*h)]),
Self::XY(w, h, _) => (*w <= to.w() || *h <= to.h()).then(||[
to.x(), to.y(), to.w().min(*w), to.h().min(*h)
])
}.map(|clamped|self.inner().layout(clamped.into())).transpose()?.flatten())
Ok(self.inner().layout(to)?.map(|to|match *self {
Self::X(w, _) => [to.x(), to.y(), to.w().min(w), to.h()],
Self::Y(h, _) => [to.x(), to.y(), to.w(), to.h().min(h)],
Self::XY(w, h, _) => [to.x(), to.y(), to.w().min(w), to.h().min(h)],
}.into()))
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
@ -465,7 +461,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
}
/// Expand drawing area
pub enum Outset<N: Number, T> {
pub enum Grow<N: Number, T> {
/// Increase width
X(N, T),
/// Increase height
@ -474,22 +470,20 @@ pub enum Outset<N: Number, T> {
XY(N, N, T)
}
impl<N: Number, T> Outset<N, T> {
impl<N: Number, T> Grow<N, T> {
fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Grow<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::X(w, _) => (*w <= to.w()).then(||[to.x() - *w, to.y(), to.w() + *w + *w, to.h()]),
Self::Y(h, _) => (*h <= to.h()).then(||[to.x(), to.y() - *h, to.w(), to.h() + *h + *h]),
Self::XY(w, h, _) => (*w <= to.w() || *h <= to.h()).then(||[
to.x()- *w, to.y() - *h, to.w() + *w + *w, to.h() + *h + *h
])
}.map(|grown|self.inner().layout(grown.into())).transpose()?.flatten())
Ok(self.inner().layout(to)?.map(|to|match *self {
Self::X(w, _) => [to.x(), to.y(), to.w() + w, to.h()],
Self::Y(h, _) => [to.x(), to.y(), to.w(), to.h() + h],
Self::XY(w, h, _) => [to.x(), to.y(), to.w() + w, to.h() + h],
}.into()))
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
@ -497,6 +491,36 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
}
/// Shrink drawing area
pub enum Shrink<N: Number, T> {
/// Decrease width
X(N, T),
/// Decrease height
Y(N, T),
/// Decrease width and height
XY(N, N, T),
}
impl<N: Number, T: Widget> Shrink<N, T> {
fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Shrink<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(self.inner().layout(to)?.map(|to|match *self {
Self::X(w, _) => [to.x(), to.y(), to.w() - w, to.h()],
Self::Y(h, _) => [to.x(), to.y(), to.w(), to.h() - h],
Self::XY(w, h, _) => [to.x(), to.y(), to.w() - w, to.h() - h]
}.into()))
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
}
}
/// Shrink from each side
pub enum Inset<N: Number, T> {
/// Decrease width
X(N, T),
@ -512,19 +536,41 @@ impl<N: Number, T: Widget> Inset<N, T> {
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
impl<E: Engine, T: Widget<Engine = E>> Content for Inset<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::X(w, _) => (*w <= to.w()).then(||[to.x() + *w, to.y(), to.w() - *w, to.h()]),
Self::Y(h, _) => (*h <= to.h()).then(||[to.x(), to.y() + *h, to.w(), to.h() - *h]),
Self::XY(w, h, _) => (*w <= to.w() || *h <= to.h()).then(||[
to.x() - *w, to.y() - *h, to.w() + *w, to.h() + *h
])
}.map(|shrunk|self.inner().layout(shrunk.into())).transpose()?.flatten())
fn content (&self) -> impl Widget<Engine = E> {
Align::Center(match *self {
Self::X(x, inner) => Shrink::X(x + x, inner),
Self::Y(y, inner) => Shrink::X(y + y, inner),
Self::XY(x, y, inner) => Shrink::XY(x, y, inner),
})
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
}
/// Grow on each side
pub enum Outset<N: Number, T> {
/// Increase width
X(N, T),
/// Increase height
Y(N, T),
/// Increase width and height
XY(N, N, T),
}
impl<N: Number, T: Widget> Outset<N, T> {
fn inner (&self) -> &T {
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
}
}
impl<E: Engine, T: Widget<Engine = E>> Content for Outset<E::Unit, T> {
type Engine = E;
fn content (&self) -> impl Widget<Engine = E> {
Align::Center(match self {
Self::X(x, inner) => Grow::X(*x + *x, inner),
Self::Y(y, inner) => Grow::X(*y + *y, inner),
Self::XY(x, y, inner) => Grow::XY(*x, *y, inner),
})
}
}
@ -553,13 +599,11 @@ impl<N: Number, T: Widget> Offset<N, T> {
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
type Engine = E;
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::X(w, _) => (*w <= to.w()).then(||[to.x() + *w, to.y(), to.w() - *w, to.h()]),
Self::Y(h, _) => (*h <= to.h()).then(||[to.x(), to.y() + *h, to.w(), to.h() - *h]),
Self::XY(w, h, _) => (*w <= to.w() || *h <= to.h()).then(||[
to.x() + *w, to.y() + *h, to.w() - *w, to.h() - *h
])
}.map(|shifted|self.inner().layout(shifted.into())).transpose()?.flatten())
Ok(self.inner().layout(to)?.map(|to|match *self {
Self::X(x, _) => [to.x() + x, to.y(), to.w(), to.h()],
Self::Y(y, _) => [to.x(), to.y() + y, to.w(), to.h()],
Self::XY(x, y, _) => [to.x() + x, to.y() + y, to.w(), to.h()]
}.into()))
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())