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

@ -578,33 +578,31 @@ where
}
}
impl<T: Widget<Engine = Tui>> Widget for Align<T> {
impl<T: Widget<Engine = Tui>> Widget for Inset<u16, T> {
type Engine = Tui;
fn layout (&self, outer_area: [u16;4]) -> Perhaps<[u16;4]> {
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).saturating_sub(w / 2);
let offset_y = (outer_area.h() / 2).saturating_sub(h / 2);
let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h];
result
}),
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 layout (&self, to: [u16;4]) -> Perhaps<[u16;4]> {
Align::Center(match self {
Self::X(x, inner) => Shrink::X(*x + *x, inner as &dyn Widget<Engine = Tui>),
Self::Y(y, inner) => Shrink::X(*y + *y, inner as &dyn Widget<Engine = Tui>),
Self::XY(x, y, inner) => Shrink::XY(*x, *y, inner as &dyn Widget<Engine = Tui>),
}).layout(to)
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
if let Some(area) = self.layout(to.area())? {
to.render_in(area, self.inner())
} else {
Ok(None)
}
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
}
}
impl<T: Widget<Engine = Tui>> Widget for Outset<u16, T> {
type Engine = Tui;
fn layout (&self, to: [u16;4]) -> Perhaps<[u16;4]> {
Align::Center(match self {
Self::X(x, inner) => Grow::X(*x + *x, inner as &dyn Widget<Engine = Tui>),
Self::Y(y, inner) => Grow::X(*y + *y, inner as &dyn Widget<Engine = Tui>),
Self::XY(x, y, inner) => Grow::XY(*x, *y, inner as &dyn Widget<Engine = Tui>),
}).layout(to)
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
}
}