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

@ -22,6 +22,7 @@ pub struct Tui {
impl Engine for Tui {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
type HandleInput = Self;
type Handled = bool;
@ -45,24 +46,11 @@ impl Engine for Tui {
self.backend.show_cursor()?;
disable_raw_mode().map_err(Into::into)
}
// FIXME
fn area (&self) -> Self::Area {
#[inline] fn area (&self) -> Self::Area {
self.area
}
#[inline]
fn with_area (&mut self, x: u16, y: u16, w: u16, h: u16) -> &mut Self {
self.with_rect([x, y, w, h]);
self
}
#[inline]
fn render_in (
&mut self, area: [u16;4], widget: &impl Widget<Engine = Self>
) -> Perhaps<[u16;4]> {
let last = self.area;
self.area = area;
let next = widget.render(self)?;
self.area = last;
Ok(next)
#[inline] fn area_mut (&mut self) -> &mut Self::Area {
&mut self.area
}
}
impl Tui {
@ -183,13 +171,6 @@ impl Tui {
Ok(Some([x, y, text.len() as u16, 1]))
}
#[inline]
pub fn alter_area (
&mut self, alter: impl Fn([u16;4])->[u16;4]
) -> &mut Self {
let [x, y, w, h] = alter(self.area.xywh());
self.with_area(x, y, w, h)
}
#[inline]
pub fn with_rect (&mut self, area: [u16;4]) -> &mut Self {
self.area = area;
self
@ -425,41 +406,6 @@ where
}
}
impl<T: Widget<Engine = Tui>> Content for Inset<u16, T> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> {
match self {
Self::X(x, inner) => Plus::X(
*x, Shrink::X(*x + *x, Align::X(inner as &dyn Widget<Engine = Tui>))
),
Self::Y(y, inner) => Plus::Y(
*y, Shrink::X(*y + *y, Align::Y(inner as &dyn Widget<Engine = Tui>))
),
Self::XY(x, y, inner) => Plus::XY(
*x, *y, Shrink::XY(*x, *y, Align::Center(inner as &dyn Widget<Engine = Tui>))
),
}
}
}
impl<T: Widget<Engine = Tui>> Widget for Outset<u16, T> {
type Engine = Tui;
fn layout (&self, to: [u16;4]) -> Perhaps<[u16;4]> {
match *self {
Self::X(x, ref inner) => Grow::X(x + x, inner as &dyn Widget<Engine = Tui>),
Self::Y(y, ref inner) => Grow::Y(y + y, inner as &dyn Widget<Engine = Tui>),
Self::XY(x, y, ref inner) => Grow::XY(x + x, y + y, inner as &dyn Widget<Engine = Tui>),
}.layout(to)
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
match *self {
Self::X(x, ref inner) => Plus::X(x, inner as &dyn Widget<Engine = Tui>),
Self::Y(y, ref inner) => Plus::Y(y, inner as &dyn Widget<Engine = Tui>),
Self::XY(x, y, ref inner) => Plus::XY(x, y, inner as &dyn Widget<Engine = Tui>),
}.render(to)
}
}
pub struct Border<S: BorderStyle>(pub S);
impl<S: BorderStyle> Widget for Border<S> {