mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
generic Align; tui-specific Inset/Outset
This commit is contained in:
parent
4b413cfb60
commit
45ce37baa1
3 changed files with 57 additions and 49 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -61,7 +61,8 @@ pub trait Number: Send + Sync + Copy
|
|||
+ Mul<Self, Output=Self>
|
||||
+ Div<Self, Output=Self>
|
||||
+ Ord + PartialEq + Eq
|
||||
+ Debug + Display + Default {}
|
||||
+ Debug + Display + Default
|
||||
+ From<u16> {}
|
||||
|
||||
impl<T> Number for T where
|
||||
T: Send + Sync + Copy
|
||||
|
|
@ -71,4 +72,5 @@ impl<T> Number for T where
|
|||
+ Div<Self, Output=Self>
|
||||
+ Ord + PartialEq + Eq
|
||||
+ Debug + Display + Default
|
||||
+ From<u16>
|
||||
{}
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue