implementing Fixed constraint

This commit is contained in:
🪞👃🪞 2024-09-12 20:19:07 +03:00
parent a57bb60ac9
commit 4b19abd98a
4 changed files with 66 additions and 10 deletions

View file

@ -385,6 +385,26 @@ impl<T> Align<T> {
}
}
/// Enforce fixed size of drawing area
pub enum Fixed<U: Number, T> {
/// Enforce fixed width
W(U, T),
/// Enforce fixed height
H(U, T),
/// Enforce fixed width and height
WH(U, U, T),
}
impl<N: Number, T> Fixed<N, T> {
pub fn inner (&self) -> &T {
match self {
Self::W(_, inner) => inner,
Self::H(_, inner) => inner,
Self::WH(_, _, inner) => inner,
}
}
}
/// Enforce minimum size of drawing area
pub enum Min<U: Number, T> {
/// Enforce minimum width

View file

@ -452,6 +452,31 @@ pub const NOT_DIM_BOLD: Style = Style {
sub_modifier: Modifier::DIM,
};
impl<T: Widget<Engine = Tui>> Widget for Fixed<u16, T> {
type Engine = Tui;
fn layout (&self, area: [u16;4]) -> Perhaps<[u16;4]> {
Ok(match self {
Self::W(w, item) => if area.w() < *w { None } else {
item.layout(area)?.map(|[x, y, _, h]|[x, y, *w, h])
},
Self::H(h, item) => if area.w() < *h { None } else {
item.layout(area)?.map(|[x, y, w, _]|[x, y, w, *h])
},
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { None } else {
item.layout(area)?.map(|[x, y, _, _]|[x, y, *w, *h])
}
})
}
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)
}
}
}
impl<F> Widget for Split<Tui, F>
where
F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget<Engine = Tui>)->Usually<()>)->Usually<()>