more space/ and draw/ modules

- 26 errors and 16 doctest fails
- getting there, perpetually
This commit is contained in:
facile pop culture reference 2026-04-24 02:06:04 +03:00
parent 42a1807c2b
commit 145047b7ff
15 changed files with 269 additions and 232 deletions

60
src/draw/draw.rs Normal file
View file

@ -0,0 +1,60 @@
use super::*;
/// Implement the [Draw] trait for a particular drawable and [Screen].
///
/// ```
/// use tengri::{*, draw::*, term::*};
/// struct MyDrawable;
/// impl_draw!(|self: MyDrawable, to: Tui|{
/// todo!()
/// });
/// ```
#[macro_export] macro_rules! impl_draw ((|
$self:ident:$Self:ty, $to:ident:$To:ty
|$draw:block)=>{ impl Draw<$To> for $Self {
fn draw ($self, $to: &mut $To) -> Usually<XYWH<u16>> $draw
} });
/// Drawable that supports dynamic dispatch.
///
/// Drawables are composable, e.g. the [when] and [either] conditionals
/// or the layout constraints.
///
/// Drawables are consumable, i.e. the [Draw::draw] method receives an
/// owned `self` and does not return it, consuming the drawable.
///
/// To draw a thing multiple times, instead of explicitly constructing it
/// every time, implement the [View] trait instead, which will construct
/// a [Draw]able.
///
/// ```
/// use tengri::{*, draw::*};
/// struct MyScreen(bool);
/// impl Screen for MyScreen { type Unit = u16; }
/// impl X<u16> for MyScreen { fn x (&self) -> u16 { 0 } }
/// impl Y<u16> for MyScreen { fn y (&self) -> u16 { 0 } }
/// struct MyWidget(bool);
/// impl Draw<MyScreen> for MyWidget {
/// fn draw (self, screen: &mut MyScreen) -> Usually<XYWH<u16>> {
/// screen.0 |= self.0;
/// }
/// }
/// let mut screen = MyScreen(false);
/// MyWidget(false).draw(&mut screen);
/// MyWidget(true).draw(&mut screen);
/// MyWidget(false).draw(&mut screen);
/// ```
pub trait Draw<T: Screen> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
}
impl<T: Screen, D: Draw<T>> Draw<T> for &D {
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
todo!()
}
}
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
todo!()
}
}