diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index e8edf32f..ccf39fb3 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -385,6 +385,36 @@ impl Align { } } +impl> Widget for Align { + type Engine = E; + fn layout (&self, outer_area: E::Area) -> Perhaps { + 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 { + 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 { /// Enforce fixed width @@ -531,22 +561,11 @@ pub enum Inset { } impl Inset { - fn inner (&self) -> &T { + pub fn inner (&self) -> &T { match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } } } -impl> Content for Inset { - type Engine = E; - fn content (&self) -> impl Widget { - 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 { /// Increase width @@ -558,22 +577,11 @@ pub enum Outset { } impl Outset { - fn inner (&self) -> &T { + pub fn inner (&self) -> &T { match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, } } } -impl> Content for Outset { - type Engine = E; - fn content (&self) -> impl Widget { - 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 { /// Move origin to the right diff --git a/crates/tek_core/src/lib.rs b/crates/tek_core/src/lib.rs index b4eaba29..e26d00e2 100644 --- a/crates/tek_core/src/lib.rs +++ b/crates/tek_core/src/lib.rs @@ -61,7 +61,8 @@ pub trait Number: Send + Sync + Copy + Mul + Div + Ord + PartialEq + Eq - + Debug + Display + Default {} + + Debug + Display + Default + + From {} impl Number for T where T: Send + Sync + Copy @@ -71,4 +72,5 @@ impl Number for T where + Div + Ord + PartialEq + Eq + Debug + Display + Default + + From {} diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index 64621bb1..9f80aa97 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -578,33 +578,31 @@ where } } -impl> Widget for Align { +impl> Widget for Inset { 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), + Self::Y(y, inner) => Shrink::X(*y + *y, inner as &dyn Widget), + Self::XY(x, y, inner) => Shrink::XY(*x, *y, inner as &dyn Widget), + }).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> Widget for Outset { + 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), + Self::Y(y, inner) => Grow::X(*y + *y, inner as &dyn Widget), + Self::XY(x, y, inner) => Grow::XY(*x, *y, inner as &dyn Widget), + }).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()) } }