fix Size, begin fixing View

This commit is contained in:
i do not exist 2026-04-13 17:30:43 +03:00
parent a06ea2ac13
commit a93fe92a59
3 changed files with 38 additions and 18 deletions

View file

@ -1,5 +1,17 @@
use crate::{*, lang::*, color::*, space::*}; use crate::{*, lang::*, color::*, space::*};
/// Emit a [Draw]able.
///
/// Speculative. How to avoid conflicts with [Draw] proper?
pub trait View<T: Screen> {
fn view (&self) -> impl Draw<T>;
}
impl<T: Screen, V: View<T>> Draw<T> for &V {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
self.view().draw(to)
}
}
/// Drawable that supports dynamic dispatch. /// Drawable that supports dynamic dispatch.
/// ///
/// Drawables are composable, e.g. the [when] and [either] conditionals /// 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 /// Drawables are consumable, i.e. the [Draw::draw] method receives an
/// owned `self` and does not return it, consuming the drawable. /// 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::*; /// use tengri::draw::*;
@ -26,13 +42,12 @@ use crate::{*, lang::*, color::*, space::*};
pub trait Draw<T: Screen> { pub trait Draw<T: Screen> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>; fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>>;
} }
/// Empty draw
impl<T: Screen> Draw<T> for () { impl<T: Screen> Draw<T> for () {
fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> { fn draw (self, __: &mut T) -> Usually<XYWH<T::Unit>> {
Ok(Default::default()) Ok(Default::default())
} }
} }
impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> { impl<T: Screen, D: Draw<T>> Draw<T> for Option<D> {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> { fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default()) Ok(self.map(|draw|draw.draw(to)).transpose()?.unwrap_or_default())
@ -111,14 +126,3 @@ pub trait Screen: Space<Self::Unit> + Send + Sync + Sized {
unimplemented!() unimplemented!()
} }
} }
/// Emit a [Draw]able.
pub trait View<T: Screen> {
fn view (&self) -> impl Draw<T>;
}
impl<T: Screen, V: View<T>> Draw<T> for &V {
fn draw (self, to: &mut T) -> Usually<XYWH<T::Unit>> {
self.view().draw(to)
}
}

View file

@ -491,3 +491,24 @@ pub fn iter_west <
> (_items: V, _cb: F) -> impl Draw<T> { > (_items: V, _cb: F) -> impl Draw<T> {
thunk(move|_to: &mut T|{ todo!() }) thunk(move|_to: &mut T|{ todo!() })
} }
#[derive(Default, Debug)]
pub struct Size(AtomicUsize, AtomicUsize);
impl X<u16> for Size {
fn x (&self) -> u16 { 0 }
fn w (&self) -> u16 { self.0.load(Relaxed) as u16 }
}
impl Y<u16> for Size {
fn y (&self) -> u16 { 0 }
fn h (&self) -> u16 { self.1.load(Relaxed) as u16 }
}
impl Size {
pub const fn of <T: Screen> (&self, of: impl Draw<T>) -> impl Draw<T> {
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)
})
}
}

View file

@ -17,11 +17,6 @@ pub(crate) use ::unicode_width::*;
self.as_str().draw(to) self.as_str().draw(to)
} }
} }
impl Draw<Tui> for std::sync::Arc<str> {
fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> {
self.as_ref().draw(to)
}
}
impl Draw<Tui> for &std::sync::Arc<str> { impl Draw<Tui> for &std::sync::Arc<str> {
fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> { fn draw (self, to: &mut Tui) -> Usually<XYWH<u16>> {
self.as_ref().draw(to) self.as_ref().draw(to)