generic Align; tui-specific Inset/Outset

This commit is contained in:
🪞👃🪞 2024-09-13 00:09:10 +03:00
parent 4b413cfb60
commit 45ce37baa1
3 changed files with 57 additions and 49 deletions

View file

@ -385,6 +385,36 @@ impl<T> Align<T> {
}
}
impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
type Engine = E;
fn layout (&self, outer_area: E::Area) -> Perhaps<E::Area> {
Ok(match self {
Self::Center(_) => self.inner().layout(outer_area)?.map(|inner_area|{
let [_, _, w, h] = inner_area.xywh();
let offset_x = (outer_area.w() / 2.into()) - (w / 2.into());
let offset_y = (outer_area.h() / 2.into()) - (h / 2.into());
let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h];
result.into()
}),
Self::NW(_) => { todo!() },
Self::N(_) => { todo!() },
Self::NE(_) => { todo!() },
Self::W(_) => { todo!() },
Self::E(_) => { todo!() },
Self::SW(_) => { todo!() },
Self::S(_) => { todo!() },
Self::SE(_) => { todo!() },
})
}
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
if let Some(area) = self.layout(to.area())? {
to.render_in(area, self.inner())
} else {
Ok(None)
}
}
}
/// Enforce fixed size of drawing area
pub enum Fixed<U: Number, T> {
/// Enforce fixed width
@ -531,22 +561,11 @@ pub enum Inset<N: Number, T> {
}
impl<N: Number, T: Widget> Inset<N, T> {
fn inner (&self) -> &T {
pub 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 Inset<E::Unit, T> {
type Engine = E;
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),
})
}
}
/// Grow on each side
pub enum Outset<N: Number, T> {
/// Increase width
@ -558,22 +577,11 @@ pub enum Outset<N: Number, T> {
}
impl<N: Number, T: Widget> Outset<N, T> {
fn inner (&self) -> &T {
pub 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),
})
}
}
/// Move origin point of drawing area
pub enum Offset<N: Number, T: Widget> {
/// Move origin to the right