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

@ -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)
}
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_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)
},
}
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!()
});