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>)
-> 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>)
-> 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>)
-> 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>)
-> 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::*;
pub enum Layout<T: Screen, X: Into<Option<T::Unit>>, I: Draw<T>> {
__(PhantomData<T>),
MinW(X, I),
MinH(X, I),
MinWH(X, X, I),
MaxW(X, I),
MaxH(X, I),
MaxWH(X, X, I),
ExactW(X, I),
ExactH(X, I),
ExactWH(X, X, I),
ClipW(X, I),
ClipH(X, I),
ClipWH(X, X, I),
pub trait Layout<S: Screen>: Draw<S> + Sized {
fn full_w (self) -> impl Draw<S> {
Full::W(self)
}
fn full_h (self) -> impl Draw<S> {
Full::H(self)
}
fn full_wh (self) -> impl Draw<S> {
Full::WH(self)
}
impl_draw!(<
T: Screen,
X: Into<Option<T::Unit>>,
I: Draw<T>,
>|self: Layout<T, X, I>, to: T|{
use Layout::*;
match self {
__(_) => unreachable!(),
MinW(x, it) => {
let x = x.into();
it.draw(to)
},
MinH(y, it) => {
let y = y.into();
it.draw(to)
},
MinWH(x, y, it) => {
let x = x.into();
let y = y.into();
it.draw(to)
},
MaxW(x, it) => {
let x = x.into();
it.draw(to)
},
MaxH(y, it) => {
let y = y.into();
it.draw(to)
},
MaxWH(x, y, it) => {
let x = x.into();
let y = y.into();
it.draw(to)
},
ExactW(x, it) => {
let x = x.into();
it.draw(to)
},
ExactH(y, it) => {
let y = y.into();
it.draw(to)
},
ExactWH(x, y, it) => {
let x = x.into();
let y = y.into();
it.draw(to)
},
ClipW(x, it) => {
let x = x.into();
it.draw(to)
},
ClipH(y, it) => {
let y = y.into();
it.draw(to)
},
ClipWH(x, y, it) => {
let x = x.into();
let y = y.into();
it.draw(to)
},
fn exact_w <N: Into<Option<S::Unit>>> (self, x: N) -> impl Draw<S> {
Exact::W(self, x.into())
}
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<S: Screen, T: Draw<S>> Layout<S> for T {}
/// Use whole drawing area along one or both axes.
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|{
todo!()
});
/// Only draw content if area is above a certain size.
///
/// ```
/// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
/// ```
pub enum Push<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
X(I, X),
Y(I, X),
XY(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Push<T, I, X>, to: T|{
todo!()
});
/// Only draw content if area is above a certain size.
///
/// ```
/// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
/// ```
pub enum Pull<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>> {
__(PhantomData<T>),
X(I, X),
Y(I, X),
XY(I, X, X),
}
impl_draw!(<T: Screen, I: Draw<T>, X: Into<Option<T::Unit>>,>|self: Pull<T, I, X>, to: T|{
todo!()
});
/// Only draw content if area is above a certain size.
///
/// ```
/// let min = tengri::wh_min(3, 5, "Hello"); // 5x5
/// ```
pub enum Min<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: 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 {}
/// 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,
_ => panic!("{}: unsupported axis {variant:?}; try /x, /y, /xy", $name)
}));
match variant {
// XY variant (can be omitted)
Some("xy") | None => xy_push(
$state.namespace($arg0?)?.unwrap(),
$state.namespace($arg1?)?.unwrap(),
cb
Some("xy") | None => cb.push_xy(
$state.namespace($arg0?)?,
$state.namespace($arg1?)?,
).draw($output),
// X variant
Some("x") => x_push(
$state.namespace($arg0?)?.unwrap(),
cb
Some("x") => cb.push_x(
$state.namespace($arg0?)?,
).draw($output),
// Y variant
Some("y") => y_push(
$state.namespace($arg0?)?.unwrap(),
cb
Some("y") => cb.push_y(
$state.namespace($arg0?)?,
).draw($output),
// Other namespace members are invalid
frag => {
let name = $name;
@ -41,6 +43,7 @@ macro_rules! eval_xy ((
head.src()?.unwrap_or_default().split("/").next()
)
}
}
}});

View file

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

View file

@ -165,11 +165,11 @@ mod phat {
pub const HI: &'static str = "";
/// A phat line
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
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
/// ```
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 low = w_exact(1, self::phat::hi(bg, lo));
let top = self::phat::lo(bg, hi).exact_h(1);
let low = self::phat::hi(bg, lo).exact_h(1);
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> {