From a93fe92a596b8a9bcec898e228fca909c0e2e9f4 Mon Sep 17 00:00:00 2001 From: i do not exist Date: Mon, 13 Apr 2026 17:30:43 +0300 Subject: [PATCH] fix Size, begin fixing View --- src/draw.rs | 30 +++++++++++++++++------------- src/space.rs | 21 +++++++++++++++++++++ src/text.rs | 5 ----- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/draw.rs b/src/draw.rs index 9b8ebdb..d1b476b 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -1,5 +1,17 @@ use crate::{*, lang::*, color::*, space::*}; +/// Emit a [Draw]able. +/// +/// Speculative. How to avoid conflicts with [Draw] proper? +pub trait View { + fn view (&self) -> impl Draw; +} +impl> Draw for &V { + fn draw (self, to: &mut T) -> Usually> { + self.view().draw(to) + } +} + /// Drawable that supports dynamic dispatch. /// /// Drawables are composable, e.g. the [when] and [either] conditionals @@ -7,6 +19,10 @@ use crate::{*, lang::*, color::*, space::*}; /// /// 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::*; @@ -26,13 +42,12 @@ use crate::{*, lang::*, color::*, space::*}; pub trait Draw { fn draw (self, to: &mut T) -> Usually>; } - +/// Empty draw impl Draw for () { fn draw (self, __: &mut T) -> Usually> { Ok(Default::default()) } } - impl> Draw for Option { fn draw (self, to: &mut T) -> Usually> { Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default()) @@ -111,14 +126,3 @@ pub trait Screen: Space + Send + Sync + Sized { unimplemented!() } } - -/// Emit a [Draw]able. -pub trait View { - fn view (&self) -> impl Draw; -} - -impl> Draw for &V { - fn draw (self, to: &mut T) -> Usually> { - self.view().draw(to) - } -} diff --git a/src/space.rs b/src/space.rs index b942f74..e0aa498 100644 --- a/src/space.rs +++ b/src/space.rs @@ -491,3 +491,24 @@ pub fn iter_west < > (_items: V, _cb: F) -> impl Draw { thunk(move|_to: &mut T|{ todo!() }) } + +#[derive(Default, Debug)] +pub struct Size(AtomicUsize, AtomicUsize); +impl X for Size { + fn x (&self) -> u16 { 0 } + fn w (&self) -> u16 { self.0.load(Relaxed) as u16 } +} +impl Y for Size { + fn y (&self) -> u16 { 0 } + fn h (&self) -> u16 { self.1.load(Relaxed) as u16 } +} +impl Size { + pub const fn of (&self, of: impl Draw) -> impl Draw { + thunk(move|to: &mut T|{ + let area = of.draw(to)?; + self.0.store(area.w().into(), Relaxed); + self.1.store(area.h().into(), Relaxed); + Ok(area) + }) + } +} diff --git a/src/text.rs b/src/text.rs index 9fe1dd4..c9752a4 100644 --- a/src/text.rs +++ b/src/text.rs @@ -17,11 +17,6 @@ pub(crate) use ::unicode_width::*; self.as_str().draw(to) } } - impl Draw for std::sync::Arc { - fn draw (self, to: &mut Tui) -> Usually> { - self.as_ref().draw(to) - } - } impl Draw for &std::sync::Arc { fn draw (self, to: &mut Tui) -> Usually> { self.as_ref().draw(to)