support multipass layout, barely

This commit is contained in:
facile pop culture reference 2026-08-04 07:59:20 +03:00
parent 083ce8ba76
commit 7f9b7091e2
20 changed files with 1581 additions and 1783 deletions

245
src/layout/axis.rs Normal file
View file

@ -0,0 +1,245 @@
use crate::*;
macro_rules! def_layout_modifier {
(
$Trait:ident ($fn_x:ident $fn_y:ident $fn_xy:ident),
$Struct:ident { $X:ident $Y:ident $XY:ident } $kw_name:ident $name:literal
|$self:ident, $to:ident| $body:block
) => {
impl<S: Screen, T: Draw<S>> $Trait<S> for T {}
pub trait $Trait<S: Screen>: Draw<S> + Sized {
fn $fn_x <N: Into<Option<S::Unit>> + Copy> (self, x: N)
-> $Struct<S, Self, N> { $Struct::$X(self, x) }
fn $fn_y <N: Into<Option<S::Unit>> + Copy> (self, y: N)
-> $Struct<S, Self, N> { $Struct::$Y(self, y) }
fn $fn_xy <N: Into<Option<S::Unit>> + Copy> (self, x: N, y: N)
-> $Struct<S, Self, N> { $Struct::$XY(self, x, y) }
}
pub enum $Struct<S: Screen, I: Draw<S>, X: Into<Option<S::Unit>>> {
__(PhantomData<S>), $X(I, X), $Y(I, X), $XY(I, X, X),
}
impl <S: Screen, I: Draw<S>, X: Into<Option<S::Unit>> + Copy> Draw<S> for $Struct<S, I, X> {
fn draw (&$self, $to: &mut S) -> Perhaps<XYWH<S::Unit>> {
$body
}
}
fn_kw_layout!($kw_name |state, output, expr| {
let head = expr.head()?;
let mut frags = head.src()?.unwrap_or_default().split("/");
Ok(matches!(expr.head()?, Some("exact")).then(||{
let args = expr.tail();
let arg0 = args.head();
let tail0 = args.tail();
let arg1 = tail0.head();
let tail1 = tail0.tail();
let arg2 = tail1.head();
eval_xy!(
$name => expr, head, output, state, frags.next(),
$fn_xy, $fn_x, $fn_y,
arg0, arg1, arg2,
)
}).transpose()?.flatten())
});
}
}
def_layout_modifier!(
CanExact (exact_w exact_h exact_wh),
Exact { W H WH } kw_exact "exact" |self, to| {
let XYWH(x, y, w0, h0) = to.area();
let (item, w, h) = match self {
Self::W(item, w) => (item, (*w).into().unwrap_or(w0), h0),
Self::H(item, h) => (item, w0, (*h).into().unwrap_or(h0)),
Self::WH(item, w, h) => (item, (*w).into().unwrap_or(h0), (*h).into().unwrap_or(h0)),
_ => unreachable!()
};
to.draw(XYWH(x, y, w0, h0), item)
}
);
def_layout_modifier!(
CanMin (min_w min_h min_wh),
Min { W H WH } kw_min "min" |self, to| {
match self {
Self::__(_) => unreachable!(),
Self::W(item, w1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
let w: S::Unit = (*w1).into().map(|w1|w.max(w1)).unwrap_or(w);
to.draw(XYWH(x, y, w, h), item)
},
Self::H(item, h1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
let h: S::Unit = (*h1).into().map(|h1|h.max(h1)).unwrap_or(h);
to.draw(XYWH(x, y, w, h), item)
},
Self::WH(item, w1, h1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
let w: S::Unit = (*w1).into().map(|w1|w.max(w1)).unwrap_or(w);
let h: S::Unit = (*h1).into().map(|h1|h.max(h1)).unwrap_or(h);
to.draw(XYWH(x, y, w, h), item)
},
_ => Ok(None)
}
}
);
def_layout_modifier!(
CanMax (max_w max_h max_wh),
Max { W H WH } kw_max "max" |self, to| {
let area: XYWH<S::Unit> = to.area();
let (item, area) = match self {
Self::W(item, max_w) => (item, XYWH(
area.0, area.1, (*max_w).into().map(|max|max.min(area.2)).unwrap_or(area.2),
area.3
)),
Self::H(item, max_h) => (item, XYWH(
area.0, area.1, area.2,
(*max_h).into().map(|max|max.min(area.3)).unwrap_or(area.3)
)),
Self::WH(item, max_w, max_h) => (item, XYWH(
area.0, area.1, (*max_w).into().map(|max|max.min(area.2)).unwrap_or(area.2),
(*max_h).into().map(|max|max.min(area.3)).unwrap_or(area.3),
)),
_ => return Ok(None)
};
to.draw(area, item)
}
);
def_layout_modifier!(
CanPad (pad_w pad_h pad_wh),
Pad { X Y XY } kw_pad "pad" |self, to| {
let area = to.area();
let (item, area) = match self {
Self::X(item, w1) => {
let w1: S::Unit = (*w1).into().unwrap_or_default();
(item, XYWH(area.0 + w1, area.1, area.2.minus(w1 + w1), area.3))
},
Self::Y(item, h1) => {
let h1: S::Unit = (*h1).into().unwrap_or_default();
(item, XYWH(area.0, area.1 + h1, area.2, area.3.minus(h1 + h1)))
},
Self::XY(item, w1, h1) => {
let w1: S::Unit = (*w1).into().unwrap_or_default();
let h1: S::Unit = (*h1).into().unwrap_or_default();
(item, XYWH(area.0 + w1, area.1 + h1, area.2.minus(w1 + w1), area.3.minus(h1 + h1)))
},
_ => return Ok(None)
};
item.draw(to)
}
);
def_layout_modifier!(
CanPush (push_x push_y push_xy),
Push { X Y XY } kw_push "push" |self, to| {
match self {
Self::__(_) => unreachable!(),
Self::X(item, x1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
to.draw(XYWH(x + (*x1).into().unwrap_or_default(), y, w, h), item)
},
Self::Y(item, y1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
to.draw(XYWH(x, y + (*y1).into().unwrap_or_default(), w, h), item)
},
Self::XY(item, x1, y1) if let Some(XYWH(x, y, w, h)) = to.size(None, item)? => {
to.draw(XYWH(x + (*x1).into().unwrap_or_default(), y + (*y1).into().unwrap_or_default(), w, h), item)
},
_ => Ok(None)
}
}
);
def_layout_modifier!(
CanPull (pull_x pull_y pull_xy),
Pull { X Y XY } kw_pull "pull" |self, to| {
todo!()
}
);
/// Use whole drawing area along one or both axes.
///
/// ```
/// # fn doctest_layout_full () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(0u16, 0, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".full_w().layout(area)?, Some(XYWH(0u16, 0, 80, 1)));
/// assert_eq!("1".full_h().layout(area)?, Some(XYWH(0u16, 0, 1, 25)));
/// assert_eq!("1".full_wh().layout(area)?, Some(XYWH(0u16, 0, 80, 25)));
/// # Ok(()) }
/// ```
pub enum Full<T: Screen, I: Draw<T>> {
__(PhantomData<T>),
W(I),
H(I),
WH(I),
}
impl_draw!(<T: Screen, I: Draw<T>,>|self: Full<T, I>, to: T|{
let XYWH(x0, y0, w0, h0) = to.area();
match self {
Self::W(item) => if let Some(XYWH(_, y, _, h)) = to.size(None, item)? {
to.draw(XYWH(x0, y, w0, h), item)
} else {
Ok(None)
},
Self::H(item) => if let Some(XYWH(x, _, w, _)) = to.size(None, item)? {
to.draw(XYWH(x, y0, w, h0), item)
} else {
Ok(None)
},
Self::WH(item) => if let Some(XYWH(..)) = to.size(None, item)? {
to.draw(XYWH(x0, y0, w0, h0), item)
} else {
Ok(None)
},
_ => unreachable!(),
}
});
/// Only draw content if area is above a certain size.
///
/// ```
/// # fn doctest_layout_min () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(1u16, 1, 80, 25);
/// assert_eq!("1".min_w(5).layout(area)?, Some(XYWH(1u16, 1, 5, 1)));
/// assert_eq!("1".min_h(5).layout(area)?, Some(XYWH(1u16, 1, 1, 5)));
/// assert_eq!("1".min_wh(5, 5).layout(area)?, Some(XYWH(1u16, 1, 5, 5)));
/// assert_eq!("123456".min_w(5).layout(area)?, Some(XYWH(1u16, 1, 6, 1)));
/// # Ok(()) }
/// ```
/// Move content in the negative direction of one or both axes.
///
/// ```
/// # fn doctest_layout_pull () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(1u16, 1, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".pull_x(1).layout(area)?, Some(XYWH(0u16, 1, 1, 1)));
/// assert_eq!("1".pull_y(1).layout(area)?, Some(XYWH(1u16, 0, 1, 1)));
/// assert_eq!("1".pull_xy(1, 1).layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// # Ok(()) }
/// ```
/// Move content in the positive direction of one or both axes.
///
/// ```
/// # fn doctest_layout_push () -> Result<(), Box<dyn std::error::Error>> {
/// use tengri::{Layout, Draw, XYWH};
/// let area = XYWH(0u16, 0, 80, 25);
/// assert_eq!("1".layout(area)?, Some(XYWH(0u16, 0, 1, 1)));
/// assert_eq!("1".push_x(1).layout(area)?, Some(XYWH(1u16, 0, 1, 1)));
/// assert_eq!("1".push_y(1).layout(area)?, Some(XYWH(0u16, 1, 1, 1)));
/// assert_eq!("1".push_xy(1, 1).layout(area)?, Some(XYWH(1u16, 1, 1, 1)));
/// # Ok(()) }
/// ```
#[cfg(test)] #[test] fn test_exact () -> Usually<()> {
let mut screen = Tui::Layout(XYWH(0, 0, 80, 25));
assert_eq!("FOOBAR\nKILROY".draw(&mut screen)?, Some(XYWH(0, 0, 6, 2)));
assert_eq!("FOOBAR\nKILROY".exact_w(3).draw(&mut screen)?, Some(XYWH(0, 0, 3, 2)));
assert_eq!("FOOBAR\nKILROY".exact_h(1).draw(&mut screen)?, Some(XYWH(0, 0, 6, 1)));
assert_eq!("FOOBAR\nKILROY".exact_wh(2, 1).draw(&mut screen)?, Some(XYWH(0, 0, 2, 1)));
Ok(())
}