reorganize, add Azimuth

This commit is contained in:
facile pop culture reference 2026-07-06 20:40:52 +03:00
parent bf16288884
commit 1f60b43f61
26 changed files with 677 additions and 594 deletions

View file

@ -1,5 +1,4 @@
use crate::*;
pub use crate::space::*;
/// Output target.
///
@ -70,14 +69,16 @@ pub trait Screen: Xy<Self::Unit> + Wh<Self::Unit> + Send + Sync + Sized {
/// }
/// ```
pub trait Draw<S: Screen> {
fn draw (self, to: &mut S) -> Perhaps<XYWH<S::Unit>>;
fn layout (&self, area: XYWH<S::Unit>) -> Perhaps<XYWH<S::Unit>> {
fn draw (self, to: &mut S) -> Drawn<S::Unit>;
fn layout (&self, area: XYWH<S::Unit>) -> Drawn<S::Unit> {
Ok(Some(area))
}
}
pub type Drawn<U> = Perhaps<XYWH<U>>;
impl<S: Screen> Draw<S> for () {
fn draw (self, _: &mut S) -> Perhaps<XYWH<S::Unit>> {
fn draw (self, _: &mut S) -> Drawn<S::Unit> {
Ok(None)
}
}
@ -87,7 +88,7 @@ impl_draw!(<S: Screen, D: Draw<S>,>|self: Option<D>, to: S|{
});
//impl<S: Screen, D: Draw<S>> Draw<S> for RwLock<D> {
//fn draw (self, __: &mut S) -> Perhaps<XYWH<S::Unit>> {
//fn draw (self, __: &mut S) -> Drawn<S::Unit> {
//todo!()
//}
//}
@ -111,52 +112,21 @@ impl<T: Screen, V: View<T>> Draw<T> for &V {
}
}
/// Because we can't implement [Draw] for `F: FnOnce...` without conflicts.
pub struct Thunk<T: Screen, F: FnOnce(&mut T)->Perhaps<XYWH<T::Unit>>>(
pub F,
std::marker::PhantomData<T>
);
impl<T: Screen, F: FnOnce(&mut T)->Perhaps<XYWH<T::Unit>>> Draw<T> for Thunk<T, F> {
fn draw (self, to: &mut T) -> Perhaps<XYWH<T::Unit>> {
(self.0)(to)
}
}
/// Basic [Draw]able closure.
///
/// ```
/// # use tengri::{draw::*, term::*};
/// # fn test () -> impl tengri::draw::Draw<tengri::term::Tui> {
/// thunk(|to: &mut Tui|Ok(to.1))
/// # }
/// ```
pub const fn thunk <T: Screen, F: FnOnce(&mut T)->Perhaps<XYWH<T::Unit>>> (
draw: F
) -> Thunk<T, F> {
Thunk(draw, std::marker::PhantomData)
}
/// Only render when condition is true.
///
/// ```
/// # use tengri::{draw::*, term::*};
/// # fn test () -> impl tengri::draw::Draw<tengri::term::Tui> {
/// when(true, "Yes")
/// # }
/// ```
pub const fn when <T: Screen> (condition: bool, draw: impl Draw<T>) -> impl Draw<T> {
thunk(move|to: &mut T|if condition { draw.draw(to) } else { Ok(Default::default()) })
}
/// Render one thing if a condition is true and another false.
///
/// ```
/// # use tengri::{draw::*, term::*};
/// # fn test () -> impl tengri::draw::Draw<tengri::term::Tui> {
/// either(true, "Yes", "No")
/// # }
/// ```
pub const fn either <T: Screen> (condition: bool, a: impl Draw<T>, b: impl Draw<T>) -> impl Draw<T> {
thunk(move|to: &mut T|if condition { a.draw(to) } else { b.draw(to) })
features! {
"draw": [
align,
area,
azimuth,
color,
coord,
iter,
layout,
lrtb,
origin,
sizer,
space,
split,
thunk,
xywh
]
}