add origin and align methods to Layout trait

This commit is contained in:
facile pop culture reference 2026-07-10 18:24:44 +03:00
parent 09463649c6
commit 13c886d9e0
18 changed files with 333 additions and 248 deletions

View file

@ -3,32 +3,34 @@ use crate::*;
/// Output target.
///
/// ```
/// use tengri::{*, draw::*};
///
/// struct TestOut<T: Screen<Unit = u16>>(T);
///
/// impl<T: Screen<Unit = u16>> Screen for TestOut {
/// use tengri::*;
/// struct TestOut { w: u16, h: u16 };
/// impl Wide<u16> for TestOut {}
/// impl Tall<u16> for TestOut {}
/// impl Xy<u16> for TestOut { fn x (&self) -> u16 { 0 } fn y (&self) -> u16 { 0 } }
/// impl Screen for TestOut {
/// type Unit = u16;
/// fn show <D: Draw<Self> + ?Sized> (&mut self, _: D) {
/// fn show <D: Draw<Self>> (&mut self, _: D) -> Drawn<u16> {
/// println!("placed");
/// ()
/// Ok(None)
/// }
/// }
///
/// impl_draw!(|self: String, to: TestOut<u16>|{
/// to.area_mut().set_w(self.len() as u16);
/// impl_draw!(|self: String, to: TestOut|{
/// to.w = self.len() as u16;
/// Ok(None)
/// });
/// ```
pub trait Screen: Xy<Self::Unit> + Wh<Self::Unit> + Send + Sync + Sized {
type Unit: Coord;
/// Render drawable in subarea specified by `area`
fn show <'t, T: Draw<Self>> (&mut self, content: T) -> Perhaps<XYWH<Self::Unit>>;
fn show <T: Draw<Self>> (&mut self, content: T) -> Perhaps<XYWH<Self::Unit>>;
}
/// Implement the [Draw] trait for a particular drawable and [Screen].
///
/// ```
/// use tengri::{*, draw::*, term::*};
/// use tengri::*;
/// struct MyDrawable;
/// impl_draw!(|self: MyDrawable, to: Tui|{
/// todo!("your draw logic")
@ -60,7 +62,7 @@ pub trait Screen: Xy<Self::Unit> + Wh<Self::Unit> + Send + Sync + Sized {
/// a [Draw]able.
///
/// ```
/// use tengri::{*, draw::*, term::*};
/// use tengri::*;
/// struct MyWidget(bool);
/// impl Draw<Tui> for MyWidget {
/// fn draw (self, to: &mut Tui) -> Perhaps<XYWH<u16>> {
@ -106,6 +108,12 @@ pub trait View<T: Screen> {
fn view (&self) -> impl Draw<T>;
}
impl<T: Screen> View<T> for () {
fn view (&self) -> impl Draw<T> {
()
}
}
impl<T: Screen, V: View<T>> Draw<T> for &V {
fn draw (self, to: &mut T) -> Perhaps<XYWH<T::Unit>> {
self.view().draw(to)
@ -114,15 +122,11 @@ impl<T: Screen, V: View<T>> Draw<T> for &V {
features! {
"draw": [
align,
area,
azimuth,
color,
coord,
iter,
layout,
lrtb,
origin,
sizer,
space,
split,