add Layout trait

This commit is contained in:
facile pop culture reference 2026-07-07 16:35:29 +03:00
parent 1f60b43f61
commit 09463649c6
6 changed files with 208 additions and 222 deletions

View file

@ -73,23 +73,23 @@ pub fn iter_west <'a, S: Screen, D: 'a, I: Iterator<Item = D>, U: Draw<S>> (
pub fn row_south <S: Screen> (south: S::Unit, height: S::Unit, content: impl Draw<S>) pub fn row_south <S: Screen> (south: S::Unit, height: S::Unit, content: impl Draw<S>)
-> impl Draw<S> -> impl Draw<S>
{ {
y_push(south, h_exact(height, content)) content.exact_h(height).push_y(south)
} }
pub fn row_north <S: Screen> (north: S::Unit, height: S::Unit, content: impl Draw<S>) pub fn row_north <S: Screen> (north: S::Unit, height: S::Unit, content: impl Draw<S>)
-> impl Draw<S> -> impl Draw<S>
{ {
y_pull(north, h_exact(height, content)) content.exact_h(height).pull_y(north)
} }
pub fn col_east <S: Screen> (east: S::Unit, width: S::Unit, content: impl Draw<S>) pub fn col_east <S: Screen> (east: S::Unit, width: S::Unit, content: impl Draw<S>)
-> impl Draw<S> -> impl Draw<S>
{ {
x_push(east, h_exact(width, content)) content.exact_w(width).push_x(east)
} }
pub fn col_west <S: Screen> (west: S::Unit, width: S::Unit, content: impl Draw<S>) pub fn col_west <S: Screen> (west: S::Unit, width: S::Unit, content: impl Draw<S>)
-> impl Draw<S> -> impl Draw<S>
{ {
x_pull(west, h_exact(width, content)) content.exact_w(width).pull_x(west)
} }

View file

@ -1,80 +1,189 @@
use crate::*; use crate::*;
pub enum Layout<T: Screen, X: Into<Option<T::Unit>>, I: Draw<T>> { pub trait Layout<S: Screen>: Draw<S> + Sized {
__(PhantomData<T>), fn full_w (self) -> impl Draw<S> {
MinW(X, I), Full::W(self)
MinH(X, I), }
MinWH(X, X, I), fn full_h (self) -> impl Draw<S> {
MaxW(X, I), Full::H(self)
MaxH(X, I), }
MaxWH(X, X, I), fn full_wh (self) -> impl Draw<S> {
ExactW(X, I), Full::WH(self)
ExactH(X, I), }
ExactWH(X, X, I),
ClipW(X, I), fn exact_w <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
ClipH(X, I), Exact::W(self, x.into())
ClipWH(X, X, I), }
fn exact_h <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Exact::H(self, y.into())
}
fn exact_wh <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Exact::WH(self, x.into(), y.into())
}
fn min_w <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Min::W(self, x.into())
}
fn min_h <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Min::H(self, y.into())
}
fn min_wh <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Min::WH(self, x.into(), y.into())
}
fn max_w <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Max::W(self, x.into())
}
fn max_h <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Max::H(self, y.into())
}
fn max_wh <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Max::WH(self, x.into(), y.into())
}
fn pad_w <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Pad::W(self, x.into())
}
fn pad_h <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Pad::H(self, y.into())
}
fn pad_wh <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Pad::WH(self, x.into(), y.into())
}
fn pull_x <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Pull::X(self, x.into())
}
fn pull_y <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Pull::X(self, y.into())
}
fn pull_xy <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Pull::XY(self, x.into(), y.into())
}
fn push_x <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Push::X(self, x.into())
}
fn push_y <N: Into<Option<S::Unit>>> (self, y: N) -> impl Draw<S> {
Push::X(self, y.into())
}
fn push_xy <N: Into<Option<S::Unit>>> (self, x: N, y: N) -> impl Draw<S> {
Push::XY(self, x.into(), y.into())
}
} }
impl_draw!(< impl<S: Screen, T: Draw<S>> Layout<S> for T {}
T: Screen,
X: Into<Option<T::Unit>>, /// Use whole drawing area along one or both axes.
I: Draw<T>, pub enum Full<T: Screen, I: Draw<T>> {
>|self: Layout<T, X, I>, to: T|{ __(PhantomData<T>),
use Layout::*; W(I),
match self { H(I),
__(_) => unreachable!(), WH(I),
MinW(x, it) => { }
let x = x.into();
it.draw(to) impl_draw!(<T: Screen, I: Draw<T>,>|self: Full<T, I>, to: T|{
}, todo!()
MinH(y, it) => { });
let y = y.into();
it.draw(to) /// Only draw content if area is above a certain size.
}, ///
MinWH(x, y, it) => { /// ```
let x = x.into(); /// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
let y = y.into(); /// ```
it.draw(to) pub enum Push<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
}, __(PhantomData<T>),
MaxW(x, it) => { X(I, X),
let x = x.into(); Y(I, X),
it.draw(to) XY(I, X, X),
}, }
MaxH(y, it) => {
let y = y.into(); impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Push<T, I, X>, to: T|{
it.draw(to) todo!()
}, });
MaxWH(x, y, it) => {
let x = x.into(); /// Only draw content if area is above a certain size.
let y = y.into(); ///
it.draw(to) /// ```
}, /// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
ExactW(x, it) => { /// ```
let x = x.into(); pub enum Pull<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
it.draw(to) __(PhantomData<T>),
}, X(I, X),
ExactH(y, it) => { Y(I, X),
let y = y.into(); XY(I, X, X),
it.draw(to) }
},
ExactWH(x, y, it) => { impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pull<T, I, X>, to: T|{
let x = x.into(); todo!()
let y = y.into(); });
it.draw(to)
}, /// Only draw content if area is above a certain size.
ClipW(x, it) => { ///
let x = x.into(); /// ```
it.draw(to) /// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
}, /// ```
ClipH(y, it) => { pub enum Min<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
let y = y.into(); __(PhantomData<T>),
it.draw(to) W(I, X),
}, H(I, X),
ClipWH(x, y, it) => { WH(I, X, X),
let x = x.into(); }
let y = y.into();
it.draw(to) impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Min<T, I, X>, to: T|{
}, todo!()
} });
/// Set maximum size of of drawing area.
///
/// ```
/// let max = tengri::w_max(3, "Hello");
/// let max = tengri::w_max(None, "Hello");
/// let max = tengri::w_max(Some(3), "Hello");
/// ```
pub enum Max<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
W(I, X),
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Max<T, I, X>, to: T|{
todo!()
});
/// Set size of of drawing area.
///
/// ```
/// let max = tengri::w_max(3, "Hello");
/// let max = tengri::w_max(None, "Hello");
/// let max = tengri::w_max(Some(3), "Hello");
/// ```
pub enum Exact<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
W(I, X),
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Exact<T, I, X>, to: T|{
todo!()
});
/// Define inner drawing area.
///
/// ```
/// let max = tengri::w_max(3, "Hello");
/// let max = tengri::w_max(None, "Hello");
/// let max = tengri::w_max(Some(3), "Hello");
/// ```
pub enum Pad<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
W(I, X),
H(I, X),
WH(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pad<T, I, X>, to: T|{
todo!()
}); });

View file

@ -113,129 +113,3 @@ impl<N: Coord, T: Wide<N> + Tall<N>> Wh<N> for T {
} }
impl<N: Coord, T: Xy<N> + Wh<N>> Xywh<N> for T {} impl<N: Coord, T: Xy<N> + Wh<N>> Xywh<N> for T {}
/// Shrink drawing area symmetrically.
///
/// ```
/// let padded = tengri::wh_pad("Hello");
/// ```
pub const fn wh_pad <T: Screen> (w: T::Unit, h: T::Unit, it: impl Draw<T>)
-> impl Draw<T>
{
thunk(move|to: &mut T|it.draw(todo!()))
}
/// Only draw content if area is above a certain size.
///
/// ```
/// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
/// ```
pub const fn wh_min <T: Screen> (w: Option<T::Unit>, h: Option<T::Unit>, it: impl Draw<T>)
-> impl Draw<T>
{
thunk(move|to: &mut T|it.draw(todo!()))
}
/// Set the maximum width and/or height of the content.
///
/// ```
/// let max = tengri::wh_max(Some(3), Some(5), "Hello");
/// ```
pub const fn wh_max <T: Screen> (w: Option<T::Unit>, h: Option<T::Unit>, it: impl Draw<T>)
-> impl Draw<T>
{
thunk(move|_to: &mut T|it.draw(todo!()))
}
/// Set the maximum width and/or height of the content.
///
/// ```
/// let exact = tengri::wh_exact(Some(3), Some(5), "Hello");
/// ```
pub const fn wh_exact <T: Screen> (w: Option<T::Unit>, h: Option<T::Unit>, it: impl Draw<T>)
-> impl Draw<T>
{
thunk(move|_to: &mut T|it.draw(todo!()))
}
/// Limit size of drawing area
/// ```
/// let clipped = tengri::wh_clip(Some(3), Some(5), "Hello");
/// ```
pub const fn wh_clip <T: Screen> (
w: Option<T::Unit>, h: Option<T::Unit>, it: impl Draw<T>
) -> impl Draw<T> {
thunk(move|_to: &mut T|it.draw(todo!()))
}
pub const fn wh_full <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
todo!();
a
}
pub const fn w_full <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
todo!();
a
}
pub const fn h_full <T: Screen> (a: impl Draw<T>) -> impl Draw<T> {
todo!();
a
}
pub const fn h_max <T: Screen> (h: Option<T::Unit>, a: impl Draw<T>) -> impl Draw<T> {
wh_max(None, h, a)
}
pub const fn h_min <T: Screen> (h: Option<T::Unit>, a: impl Draw<T>) -> impl Draw<T> {
wh_min(None, h, a)
}
pub const fn h_exact <T: Screen> (h: T::Unit, c: impl Draw<T>) -> impl Draw<T> {
wh_exact(None, Some(h), c)
}
pub const fn w_max <T: Screen> (w: Option<T::Unit>, a: impl Draw<T>) -> impl Draw<T> {
wh_max(w, None, a)
}
pub const fn w_min <T: Screen> (w: Option<T::Unit>, a: impl Draw<T>) -> impl Draw<T> {
wh_min(w, None, a)
}
pub const fn w_exact <T: Screen> (w: T::Unit, c: impl Draw<T>) -> impl Draw<T> {
wh_exact(Some(w), None, c)
}
/// Shrink drawing area symmetrically.
///
/// ```
/// let padded = tengri::space::w_pad(3, "Hello");
/// ```
pub const fn w_pad <T: Screen> (x: T::Unit, draw: impl Draw<T>) -> impl Draw<T> {
thunk(move|to: &mut T|draw.draw(todo!()))
}
/// Shrink drawing area symmetrically.
///
/// ```
/// let padded = tengri::space::w_pad(3, "Hello");
/// ```
pub const fn h_pad <T: Screen> (x: T::Unit, draw: impl Draw<T>) -> impl Draw<T> {
thunk(move|to: &mut T|draw.draw(todo!()))
}
pub const fn xy_push <T: Screen> (x: T::Unit, y: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}
pub const fn xy_pull <T: Screen> (x: T::Unit, y: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}
pub const fn x_push <T: Screen> (x: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}
pub const fn x_pull <T: Screen> (x: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}
pub const fn y_push <T: Screen> (x: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}
pub const fn y_pull <T: Screen> (x: T::Unit, a: impl Draw<T>) -> impl Draw<T> {
a
}

View file

@ -14,23 +14,25 @@ macro_rules! eval_xy ((
Some("xy") | None => $arg2, Some("xy") | None => $arg2,
_ => panic!("{}: unsupported axis {variant:?}; try /x, /y, /xy", $name) _ => panic!("{}: unsupported axis {variant:?}; try /x, /y, /xy", $name)
})); }));
match variant { match variant {
// XY variant (can be omitted) // XY variant (can be omitted)
Some("xy") | None => xy_push( Some("xy") | None => cb.push_xy(
$state.namespace($arg0?)?.unwrap(), $state.namespace($arg0?)?,
$state.namespace($arg1?)?.unwrap(), $state.namespace($arg1?)?,
cb
).draw($output), ).draw($output),
// X variant // X variant
Some("x") => x_push( Some("x") => cb.push_x(
$state.namespace($arg0?)?.unwrap(), $state.namespace($arg0?)?,
cb
).draw($output), ).draw($output),
// Y variant // Y variant
Some("y") => y_push( Some("y") => cb.push_y(
$state.namespace($arg0?)?.unwrap(), $state.namespace($arg0?)?,
cb
).draw($output), ).draw($output),
// Other namespace members are invalid // Other namespace members are invalid
frag => { frag => {
let name = $name; let name = $name;
@ -41,6 +43,7 @@ macro_rules! eval_xy ((
head.src()?.unwrap_or_default().split("/").next() head.src()?.unwrap_or_default().split("/").next()
) )
} }
} }
}}); }});

View file

@ -5,8 +5,8 @@ use super::*;
/// ``` /// ```
/// /// TODO /// /// TODO
/// ``` /// ```
pub const fn border (on: bool, style: impl BorderStyle, draw: impl Draw<Tui>) -> impl Draw<Tui> { pub fn border (on: bool, style: impl BorderStyle, draw: impl Draw<Tui>) -> impl Draw<Tui> {
let content = wh_pad(1, 1, draw); let content = draw.pad_wh(1, 1);
let outline = when(on, thunk(move|to: &mut Tui|{ let outline = when(on, thunk(move|to: &mut Tui|{
let XYWH(x, y, w, h) = to.1; let XYWH(x, y, w, h) = to.1;
if w > 0 && h > 0 { if w > 0 && h > 0 {

View file

@ -165,11 +165,11 @@ mod phat {
pub const HI: &'static str = ""; pub const HI: &'static str = "";
/// A phat line /// A phat line
pub fn lo (fg: Color, bg: Color) -> impl Draw<Tui> { pub fn lo (fg: Color, bg: Color) -> impl Draw<Tui> {
h_exact(1, fg_bg(fg, bg, x_repeat(self::phat::LO))) fg_bg(fg, bg, x_repeat(self::phat::LO)).exact_h(1)
} }
/// A phat line /// A phat line
pub fn hi (fg: Color, bg: Color) -> impl Draw<Tui> { pub fn hi (fg: Color, bg: Color) -> impl Draw<Tui> {
h_exact(1, fg_bg(fg, bg, x_repeat(self::phat::HI))) fg_bg(fg, bg, x_repeat(self::phat::HI)).exact_h(1)
} }
} }
@ -253,10 +253,10 @@ pub const fn button_3 <'a> (
/// /// TODO /// /// TODO
/// ``` /// ```
pub fn phat (w: u16, h: u16, [fg, bg, hi, lo]: [Color;4], draw: impl Draw<Tui>) -> impl Draw<Tui> { pub fn phat (w: u16, h: u16, [fg, bg, hi, lo]: [Color;4], draw: impl Draw<Tui>) -> impl Draw<Tui> {
let top = w_exact(1, self::phat::lo(bg, hi)); let top = self::phat::lo(bg, hi).exact_h(1);
let low = w_exact(1, self::phat::hi(bg, lo)); let low = self::phat::hi(bg, lo).exact_h(1);
let draw = fg_bg(fg, bg, draw); let draw = fg_bg(fg, bg, draw);
wh_min(Some(w), Some(h), south(top, north(low, draw))) south(top, north(low, draw)).min_wh(w, h)
} }
fn x_scroll () -> impl Draw<Tui> { fn x_scroll () -> impl Draw<Tui> {