output: more big refactors

This commit is contained in:
🪞👃🪞 2025-09-07 12:34:30 +03:00
parent 194f2f9874
commit 5e6338fad8
68 changed files with 1604 additions and 1016 deletions

2
Cargo.lock generated
View file

@ -1084,6 +1084,7 @@ dependencies = [
name = "tengri_output"
version = "0.14.0"
dependencies = [
"bumpalo",
"proptest",
"proptest-derive",
"tengri",
@ -1109,6 +1110,7 @@ version = "0.14.0"
dependencies = [
"atomic_float",
"better-panic",
"bumpalo",
"crossterm 0.29.0",
"konst",
"palette",

View file

@ -33,6 +33,7 @@ tengri_proc = { path = "./proc" }
anyhow = { version = "1.0" }
atomic_float = { version = "1" }
better-panic = { version = "0.3.0" }
bumpalo = { version = "3.19.0" }
const_panic = { version = "0.2.12", features = [ "derive" ] }
crossterm = { version = "0.29.0" }
heck = { version = "0.5" }

View file

@ -63,7 +63,7 @@ dsl_type!(DslExpr {
).into())
};
)*
Ok(Some($body as $Type))
Ok(Some($body/* as $Type*/))
};
($name, get)
}),* )? ];
@ -74,12 +74,14 @@ pub trait DslNsExprs<'a, T: 'a>: 'a {
const EXPRS: DslExprs<'a, Self, T> = &[];
/// Resolve an expression if known.
fn from_expr (&'a self, dsl: impl DslExpr + 'a) -> Perhaps<T> {
let head = dsl.head()?;
for (key, get) in Self::EXPRS.iter() {
if Some(*key) == head {
return get(self, dsl.tail()?.unwrap_or(""))
if let Some(dsl) = dsl.expr()? {
let head = dsl.head()?;
for (key, get) in Self::EXPRS.iter() {
if Some(*key) == head {
return get(self, dsl.tail()?.unwrap_or(""))
}
}
}
return Ok(None)
Ok(None)
}
}

View file

@ -16,3 +16,31 @@ pub trait DslNs<'a, T: 'a>: DslNsWords<'a, T> + DslNsExprs<'a, T> {
Ok(None)
}
}
#[macro_export] macro_rules! dsl_ns {
($State:ty: $Type:ty {
$(literal = |$dsl:ident| $literal:expr;)?
$(word = |$state_w:ident| {
$($word:literal => $body_w:expr),* $(,)?
};)?
$(expr = |$state_e:ident| {
$($head:literal $args:tt => $body_e:expr),* $(,)?
};)?
}) => {
impl<'a> DslNs<'a, $Type> for $State {
$(fn from_literal (&self, $dsl: impl Dsl) -> Perhaps<$Type> {
$literal
})?
}
impl<'a> DslNsWords<'a, $Type> for $State {
$(dsl_words! { 'a |$state_w| -> $Type {
$($word => $body_w),*
} })?
}
impl<'a> DslNsExprs<'a, $Type> for $State {
$(dsl_exprs! { 'a |$state_e| -> $Type {
$($head $args => $body_e),*
} })?
}
}
}

View file

@ -4,12 +4,17 @@ description = "UI metaframework, output layer."
version = { workspace = true }
edition = { workspace = true }
[lib]
path = "src/output.rs"
[features]
dsl = [ "tengri_dsl" ]
bumpalo = [ "dep:bumpalo" ]
dsl = [ "dep:tengri_dsl" ]
[dependencies]
tengri_core = { path = "../core" }
tengri_dsl = { optional = true, path = "../dsl" }
bumpalo = { optional = true, workspace = true }
[dev-dependencies]
tengri = { path = "../tengri", features = [ "dsl", "tui" ] }

View file

@ -4,10 +4,10 @@ it expresses the following notions:
* [**space:**](./src/space.rs) `Direction`, `Coordinate`, `Area`, `Size`, `Measure`
* [**output:**](./src/output.rs) `Output`, `Render`, `Content`
* the layout operators are generic over `Render` and/or `Content`
* the traits `Render` and `Content` are generic over `Output`
* implement `Output` to bring a layout to a new backend:
* [**output:**](./src/output.rs) `Out`, `Draw`, `Content`
* the layout operators are generic over `Draw` and/or `Content`
* the traits `Draw` and `Content` are generic over `Out`
* implement `Out` to bring a layout to a new backend:
[see `TuiOut` in `tengri_tui`](../tui/src/tui_engine/tui_output.rs)
* [**layout:**](./src/layout.rs)

40
output/src/content.rs Normal file
View file

@ -0,0 +1,40 @@
use crate::*;
/// Composable renderable with static dispatch.
pub trait Content<E: Out>: Sized {
/// Return opaque [Draw]able.
fn content (&self) -> impl Draw<E> + Layout<E> + '_ { () }
}
/// The platonic ideal unit of [Content]:
/// total emptiness at dead center (e=1vg^sqrt(-1))
impl<E: Out> Content<E> for () {}
impl<E: Out, T: Draw<E> + Layout<E>> Content<E> for fn()->T {
fn content (&self) -> impl Draw<E> + Layout<E> + '_ {
self()
}
}
/// Implement composable content for a struct.
#[macro_export] macro_rules! content {
// Implement for all [Out]s.
(|$self:ident:$Struct:ty| $content:expr) => {
impl<E: Out> Content<E> for $Struct {
fn content (&$self) -> impl Draw<E> + Layout<E> + '_ { Some($content) }
}
};
// Implement for specific [Out].
($Out:ty:|
$self:ident:
$Struct:ident$(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?
|$content:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Out>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Draw<$Out> + Layout<$Out> + '_ { $content }
}
};
}

94
output/src/draw.rs Normal file
View file

@ -0,0 +1,94 @@
use crate::*;
/// Drawable with dynamic dispatch.
pub trait Draw<O: Out> {
/// Write data to display.
fn draw (&self, to: &mut O);
}
impl<O: Out> Draw<O> for fn(&mut O) {
fn draw (&self, to: &mut O) {
self(to)
}
}
impl<O: Out> Draw<O> for () { fn draw (&self, _: &mut O) {} }
impl<O: Out, T: Draw<O>> Draw<O> for &T {
fn draw (&self, to: &mut O) { (*self).draw(to) }
}
impl<'x, O: Out> Draw<O> for &(dyn Draw<O> + 'x) {
fn draw (&self, to: &mut O) {
(*self).draw(to)
}
}
impl<'x, O: Out> Draw<O> for &mut (dyn Draw<O> + 'x) {
fn draw (&self, to: &mut O) {
(**self).draw(to)
}
}
impl<O: Out, T: Draw<O>> Draw<O> for RwLock<T> {
fn draw (&self, to: &mut O) { self.read().unwrap().draw(to) }
}
impl<O: Out, T: Draw<O>> Draw<O> for [T] {
fn draw (&self, to: &mut O) {
for draw in self.iter() {
draw.draw(to)
}
}
}
//impl<O: Out, T: Draw<O>> Draw<O> for &mut T {
//fn draw (&self, to: &mut O) {
//(**self).draw(to)
//}
//}
//impl<O: Out, T: Iterator<Item = U>, U: Draw<O>> Draw<O> for &mut T {
//fn draw (&self, to: &mut O) {
//for draw in *self {
//draw.draw(to)
//}
//}
//}
/// Implement custom drawing for a struct.
#[macro_export] macro_rules! draw {
// Implement for all [Out] backends.
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $draw:expr) => {
impl <$($($L),*)? O: Out, $($($T$(:$Trait)?),*)?> Draw<O>
for $Struct $(<$($L),* $($T),*>)? {
fn draw (&$self, $to: &mut O) { $draw }
}
};
// Implement for a specific [Out] backend.
($O:ty:|
$self:ident:
$Struct:ident $(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?, $to:ident
|$draw:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Draw<$O>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn draw (&$self, $to: &mut $O) { $draw }
}
};
}
draw!(|self: Arc<T: Draw<O>>, to|(**self).draw(to));
draw!(|self: Box<T: Draw<O>>, to|(**self).draw(to));
//draw!(|self: Option<T: Draw<O>>, to|if let Some(draw) = self { draw.draw(to) });
impl<O: Out, T: Draw<O>> Draw<O> for Option<T> {
fn draw (&self, to: &mut O) {
if let Some(draw) = self {
draw.draw(to)
}
}
}

12
output/src/group.rs Normal file
View file

@ -0,0 +1,12 @@
use crate::*;
pub struct Group<T>(T);
impl<T> Group<T> {
pub const fn new () -> Group<()> {
Group(())
}
pub const fn add <U> (self, value: U) -> Group<(T, U)> {
Group((self.0, value))
}
}

98
output/src/layout.rs Normal file
View file

@ -0,0 +1,98 @@
use crate::*;
pub trait Layout<O: Out> {
fn x (&self, area: O::Area) -> O::Unit {
area.x()
}
fn y (&self, area: O::Area) -> O::Unit {
area.y()
}
fn min_w (&self, _area: O::Area) -> O::Unit {
0.into()
}
fn max_w (&self, area: O::Area) -> O::Unit {
area.w()
}
fn min_h (&self, _area: O::Area) -> O::Unit {
0.into()
}
fn max_h (&self, area: O::Area) -> O::Unit {
area.h()
}
fn layout (&self, area: O::Area) -> O::Area {
O::Area::from([
self.x(area),
self.y(area),
area.w().max(self.min_w(area)).min(self.max_w(area)),
area.h().max(self.min_h(area)).min(self.max_h(area)),
])
}
}
#[macro_export] macro_rules! layout {
// Implement for all [Out] backends.
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident|$($method:ident = |$area:ident|$body:expr;)*) => {
impl <$($($L),*)? O: Out, $($($T$(:$Trait)?),*)?> Layout<O>
for $Struct $(<$($L),* $($T),*>)? {
$(fn $method (&$self, $area: O::Area) -> O::Area {
$body
})*
}
};
// Implement for a specific [Out] backend.
($O:ty:|
$self:ident:
$Struct:ident $(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?,
$to:ident
|$($method:ident = |$area:ident|$body:expr;)*) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Layout<$O>
for $Struct $(<$($($L)? $($T)?),+>)? {
$(fn $method (&$self, $area: <$O as Out>::Area) -> <$O as Out>::Area {
$body
})*
}
};
}
impl<O: Out> Layout<O> for () {}
impl<O: Out, L: Layout<O>> Layout<O> for &L { /*FIXME*/ }
impl<O: Out, L: Layout<O>> Layout<O> for RwLock<L> { /*FIXME*/ }
impl<O: Out, L: Layout<O>> Layout<O> for Option<L> { /*FIXME*/ }
//impl<O: Out> Layout<O> for fn(&mut O) {}
impl<O: Out, L: Layout<O>> Layout<O> for Arc<L> {
fn layout (&self, to: O::Area) -> O::Area {
(**self).layout(to)
}
}
impl<'x, O: Out> Layout<O> for &(dyn Draw<O> + 'x) {
fn layout (&self, to: O::Area) -> O::Area {
Fill::xy(self).layout(to)
}
}
mod layout_align; pub use self::layout_align::*;
mod layout_bsp; pub use self::layout_bsp::*;
mod layout_cond; pub use self::layout_cond::*;
mod layout_expand; pub use self::layout_expand::*;
mod layout_fill; pub use self::layout_fill::*;
mod layout_fixed; pub use self::layout_fixed::*;
mod layout_map; pub use self::layout_map::*;
mod layout_margin; pub use self::layout_margin::*;
mod layout_max; pub use self::layout_max::*;
mod layout_min; pub use self::layout_min::*;
mod layout_padding; pub use self::layout_padding::*;
mod layout_pull; pub use self::layout_pull::*;
mod layout_push; pub use self::layout_push::*;
mod layout_shrink; pub use self::layout_shrink::*;
mod layout_stack; pub use self::layout_stack::*;

View file

@ -1,9 +1,9 @@
//! ```
//! use ::tengri::{output::*, tui::*};
//! let area: [u16;4] = [10, 10, 20, 20];
//! fn test (area: [u16;4], item: &impl Render<TuiOut>, expected: [u16;4]) {
//! fn test (area: [u16;4], item: &impl Draw<TuiOut>, expected: [u16;4]) {
//! assert_eq!(Content::layout(item, area), expected);
//! assert_eq!(Render::layout(item, area), expected);
//! assert_eq!(Draw::layout(item, area), expected);
//! };
//!
//! let four = ||Fixed::xy(4, 4, "");
@ -32,33 +32,35 @@ use crate::*;
#[derive(Debug, Copy, Clone, Default)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
pub struct Align<T>(Alignment, T);
impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }
#[inline] pub const fn x (a: A) -> Self { Self(Alignment::X, a) }
#[inline] pub const fn y (a: A) -> Self { Self(Alignment::Y, a) }
#[inline] pub const fn n (a: A) -> Self { Self(Alignment::N, a) }
#[inline] pub const fn s (a: A) -> Self { Self(Alignment::S, a) }
#[inline] pub const fn e (a: A) -> Self { Self(Alignment::E, a) }
#[inline] pub const fn w (a: A) -> Self { Self(Alignment::W, a) }
#[inline] pub const fn nw (a: A) -> Self { Self(Alignment::NW, a) }
#[inline] pub const fn sw (a: A) -> Self { Self(Alignment::SW, a) }
#[inline] pub const fn ne (a: A) -> Self { Self(Alignment::NE, a) }
#[inline] pub const fn se (a: A) -> Self { Self(Alignment::SE, a) }
impl<T> Align<T> {
#[inline] pub const fn c (a: T) -> Self { Self(Alignment::Center, a) }
#[inline] pub const fn x (a: T) -> Self { Self(Alignment::X, a) }
#[inline] pub const fn y (a: T) -> Self { Self(Alignment::Y, a) }
#[inline] pub const fn n (a: T) -> Self { Self(Alignment::N, a) }
#[inline] pub const fn s (a: T) -> Self { Self(Alignment::S, a) }
#[inline] pub const fn e (a: T) -> Self { Self(Alignment::E, a) }
#[inline] pub const fn w (a: T) -> Self { Self(Alignment::W, a) }
#[inline] pub const fn nw (a: T) -> Self { Self(Alignment::NW, a) }
#[inline] pub const fn sw (a: T) -> Self { Self(Alignment::SW, a) }
#[inline] pub const fn ne (a: T) -> Self { Self(Alignment::NE, a) }
#[inline] pub const fn se (a: T) -> Self { Self(Alignment::SE, a) }
}
impl<E: Output, A: Render<E>> Render<E> for Align<A> {
impl<E: Out, T: Layout<E>> Layout<E> for Align<T> {
fn layout (&self, on: E::Area) -> E::Area {
self.0.align(on, &self.1)
}
fn render (&self, to: &mut E) {
to.place(self.layout(to.area()), &self.1)
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Align<T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.1)
}
}
impl Alignment {
fn align <E: Output> (&self, on: E::Area, content: impl Render<E>) -> E::Area {
fn align <E: Out> (&self, on: E::Area, content: &impl Layout<E>) -> E::Area {
use Alignment::*;
let it = content.layout(on).xywh();
let cx = on.x()+(on.w().minus(it.w())/2.into());

View file

@ -0,0 +1,105 @@
use crate::*;
use Direction::*;
/// A split or layer.
pub struct Bsp<Head, Tail>(
pub(crate) Direction,
/// First element.
pub(crate) Head,
/// Second element.
pub(crate) Tail,
);
impl<Head, Tail> Bsp<Head, Tail> {
#[inline] pub const fn n (a: Head, b: Tail) -> Self { Self(North, a, b) }
#[inline] pub const fn s (a: Head, b: Tail) -> Self { Self(South, a, b) }
#[inline] pub const fn e (a: Head, b: Tail) -> Self { Self(East, a, b) }
#[inline] pub const fn w (a: Head, b: Tail) -> Self { Self(West, a, b) }
#[inline] pub const fn a (a: Head, b: Tail) -> Self { Self(Above, a, b) }
#[inline] pub const fn b (a: Head, b: Tail) -> Self { Self(Below, a, b) }
}
impl<
O: Out,
Head: Draw<O> + Layout<O>,
Tail: Draw<O> + Layout<O>
> Draw<O> for Bsp<Head, Tail> {
fn draw (&self, to: &mut O) {
let [a, b, _] = bsp_areas(to.area(), self.0, &self.1, &self.2);
if self.0 == Below {
to.place_at(a, &self.1);
to.place_at(b, &self.2);
} else {
to.place_at(b, &self.2);
to.place_at(a, &self.1);
}
}
}
impl<O: Out, Head: Layout<O>, Tail: Layout<O>> Layout<O> for Bsp<Head, Tail> {
fn layout (&self, area: O::Area) -> O::Area {
bsp_areas(area, self.0, &self.1, &self.2)[2]
}
}
fn bsp_areas <O: Out> (
area: O::Area, direction: Direction, a: &impl Layout<O>, b: &impl Layout<O>,
) -> [O::Area;3] {
let [x, y, w, h] = area.xywh();
let [aw, ah] = a.layout(area).wh();
let [bw, bh] = b.layout(match direction {
Above | Below => area,
South => [x, y + ah, w, h.minus(ah)].into(),
North => [x, y, w, h.minus(ah)].into(),
East => [x + aw, y, w.minus(aw), h].into(),
West => [x, y, w.minus(aw), h].into(),
}).wh();
match direction {
Above | Below => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah.max(bh)]);
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
South => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
North => {
let [x, y, w, h] = area.center_xy([aw.max(bw), ah + bh]);
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
East => {
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
West => {
let [x, y, w, h] = area.center_xy([aw + bw, ah.max(bh)]);
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
}
}
/// Stack things on top of each other,
#[macro_export] macro_rules! lay (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }});
/// Stack southward.
#[macro_export] macro_rules! col (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }});
/// Stack northward.
#[macro_export] macro_rules! col_up (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }});
/// Stack eastward.
#[macro_export] macro_rules! row (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }});

View file

@ -6,7 +6,7 @@ impl<A> When<A> {
/// Create a binary condition.
pub const fn new (c: bool, a: A) -> Self { Self(c, a) }
}
impl<E: Output, A: Render<E>> Render<E> for When<A> {
impl<E: Out, A: Layout<E>> Layout<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, item) = self;
let mut area = E::Area::zero();
@ -19,26 +19,32 @@ impl<E: Output, A: Render<E>> Render<E> for When<A> {
}
area.into()
}
fn render (&self, to: &mut E) {
}
impl<E: Out, A: Draw<E>> Draw<E> for When<A> {
fn draw (&self, to: &mut E) {
let Self(cond, item) = self;
if *cond { item.render(to) }
if *cond { item.draw(to) }
}
}
/// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> {
pub struct Either<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>>(pub bool, pub A, pub B, pub PhantomData<E>);
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Either<E, A, B> {
/// Create a ternary view condition.
pub const fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) }
pub const fn new (c: bool, a: A, b: B) -> Self {
Self(c, a, b, PhantomData)
}
}
impl<E: Output, A: Render<E>, B: Render<E>> Render<E> for Either<A, B> {
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Layout<E> for Either<E, A, B> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, a, b) = self;
let Self(cond, a, b, ..) = self;
if *cond { a.layout(to) } else { b.layout(to) }
}
fn render (&self, to: &mut E) {
let Self(cond, a, b) = self;
if *cond { a.render(to) } else { b.render(to) }
}
impl<E: Out, A: Draw<E> + Layout<E>, B: Draw<E> + Layout<E>> Draw<E> for Either<E, A, B> {
fn draw (&self, to: &mut E) {
let Self(cond, a, b, ..) = self;
if *cond { a.draw(to) } else { b.draw(to) }
}
}

View file

@ -0,0 +1,39 @@
use crate::*;
pub enum Expand<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Expand<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
}
impl<O: Out, T: Draw<O> + Layout<O>> Content<O> for Expand<O::Unit, T> {
fn content (&self) -> impl Draw<O> + Layout<O> + '_ {
use Expand::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<O: Out, T: Draw<O> + Layout<O>> Draw<O> for Expand<O::Unit, T> {
fn draw (&self, to: &mut O) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Expand<U, T> {
#[inline] pub fn dx (&self) -> U {
use Expand::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Expand::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<O: Out, T: Layout<O>> Layout<O> for Expand<O::Unit, T> {
fn layout (&self, to: O::Area) -> O::Area {
[to.x(), to.y(), to.w().plus(self.dx()), to.h().plus(self.dy())].into()
}
}

View file

@ -0,0 +1,55 @@
use crate::*;
pub enum Fill<A> {
/// Use maximum width of area.
X(A),
/// Use maximum height of area.
Y(A),
/// Use maximum width and height of area.
XY(A)
}
impl<T> Fill<T> {
#[inline] pub const fn x (item: T) -> Self {
Self::X(item)
}
#[inline] pub const fn y (item: T) -> Self {
Self::Y(item)
}
#[inline] pub const fn xy (item: T) -> Self {
Self::XY(item)
}
#[inline] pub const fn has_x (&self) -> bool {
matches!(self, Self::X(_) | Self::XY(_))
}
#[inline] pub const fn has_y (&self) -> bool {
matches!(self, Self::Y(_) | Self::XY(_))
}
#[inline] pub const fn content (&self) -> &T {
use Fill::*;
match self { X(item) | Y(item) | XY(item) => item }
}
}
impl<O: Out, T: Draw<O>> Draw<O> for Fill<T> {
fn draw (&self, to: &mut O) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<O: Out, T: Draw<O>> Layout<O> for Fill<T> {
fn min_w (&self, area: O::Area) -> O::Unit {
if self.has_x() {
area.w()
} else {
0.into()
}
}
fn min_h (&self, area: O::Area) -> O::Unit {
if self.has_y() {
area.h()
} else {
0.into()
}
}
}

View file

@ -0,0 +1,47 @@
use crate::*;
pub enum Fixed<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Fixed<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Fixed::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<U: Coordinate, T> Fixed<U, T> {
#[inline] pub fn dx (&self) -> U {
use Fixed::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Fixed::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<O: Out, T: Draw<O>> Draw<O> for Fixed<O::Unit, T> {
fn draw (&self, to: &mut O) {
let area = Layout::<O>::layout(&self, to.area());
to.place_at(area, &self.content())
}
}
impl<O: Out, T> Layout<O> for Fixed<O::Unit, T> {
fn layout (&self, area: O::Area) -> O::Area {
[area.x(), area.y(), match self {
Fixed::X(w, _) | Fixed::XY(w, _, _) => *w, _ => area.w()
}, match self {
Fixed::Y(h, _) | Fixed::XY(_, h, _) => *h, _ => area.h()
}].into()
}
}

View file

View file

@ -1,19 +1,19 @@
use crate::*;
/// Renders items from an iterator.
pub struct Map<E, A, B, I, F, G>
/// Draws items from an iterator.
pub struct Map<O, A, B, I, F, G>
where
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync,
{
__: PhantomData<(E, B)>,
__: PhantomData<(O, B)>,
/// Function that returns iterator over stacked components
get_iter: F,
/// Function that returns each stacked component
get_item: G,
}
impl<'a, E, A, B, I, F, G> Map<E, A, B, I, F, G> where
impl<'a, O, A, B, I, F, G> Map<O, A, B, I, F, G> where
I: Iterator<Item = A> + Send + Sync + 'a,
F: Fn() -> I + Send + Sync + 'a,
{
@ -27,30 +27,30 @@ impl<'a, E, A, B, I, F, G> Map<E, A, B, I, F, G> where
}
macro_rules! impl_map_direction (($name:ident, $axis:ident, $align:ident)=>{
impl<'a, E, A, B, I, F> Map<
E, A, Push<E::Unit, Align<Fixed<E::Unit, Fill<B>>>>, I, F, fn(A, usize)->B
impl<'a, O, A, B, I, F> Map<
O, A, Push<O::Unit, Align<Fixed<O::Unit, Fill<B>>>>, I, F, fn(A, usize)->B
> where
E: Output,
B: Render<E>,
O: Out,
B: Draw<O> + Layout<O>,
I: Iterator<Item = A> + Send + Sync + 'a,
F: Fn() -> I + Send + Sync + 'a
{
pub const fn $name (
size: E::Unit,
size: O::Unit,
get_iter: F,
get_item: impl Fn(A, usize)->B + Send + Sync
) -> Map<
E, A,
Push<E::Unit, Align<Fixed<E::Unit, B>>>,
O, A,
Push<O::Unit, Align<Fixed<O::Unit, B>>>,
I, F,
impl Fn(A, usize)->Push<E::Unit, Align<Fixed<E::Unit, B>>> + Send + Sync
impl Fn(A, usize)->Push<O::Unit, Align<Fixed<O::Unit, B>>> + Send + Sync
> {
Map {
__: PhantomData,
get_iter,
get_item: move |item: A, index: usize|{
// FIXME: multiply
let mut push: E::Unit = E::Unit::from(0u16);
let mut push: O::Unit = O::Unit::from(0u16);
for _ in 0..index {
push = push + size;
}
@ -66,14 +66,14 @@ impl_map_direction!(south, y, n);
impl_map_direction!(west, x, e);
impl_map_direction!(north, y, s);
impl<'a, E, A, B, I, F, G> Render<E> for Map<E, A, B, I, F, G> where
E: Output,
B: Render<E>,
impl<'a, O, A, B, I, F, G> Layout<O> for Map<O, A, B, I, F, G> where
O: Out,
B: Draw<O> + Layout<O>,
I: Iterator<Item = A> + Send + Sync + 'a,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
fn layout (&self, area: E::Area) -> E::Area {
fn layout (&self, area: O::Area) -> O::Area {
let Self { get_iter, get_item, .. } = self;
let mut index = 0;
let [mut min_x, mut min_y] = area.center();
@ -91,40 +91,48 @@ impl<'a, E, A, B, I, F, G> Render<E> for Map<E, A, B, I, F, G> where
//[min_x.into(), min_y.into(), w.into(), h.into()].into()
area.center_xy([w.into(), h.into()].into()).into()
}
fn render (&self, to: &mut E) {
}
impl<'a, O, A, B, I, F, G> Draw<O> for Map<O, A, B, I, F, G> where
O: Out,
B: Draw<O> + Layout<O>,
I: Iterator<Item = A> + Send + Sync + 'a,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
fn draw (&self, to: &mut O) {
let Self { get_iter, get_item, .. } = self;
let mut index = 0;
let area = Render::layout(self, to.area());
let area = self.layout(to.area());
for item in get_iter() {
let item = get_item(item, index);
//to.place(area.into(), &item);
to.place(item.layout(area), &item);
//to.place_at(area.into(), &item);
to.place_at(item.layout(area), &item);
index += 1;
}
}
}
#[inline] pub fn map_south<O: Output>(
#[inline] pub fn map_south<O: Out>(
item_offset: O::Unit,
item_height: O::Unit,
item: impl Render<O>
) -> impl Render<O> {
item: impl Draw<O> + Layout<O>
) -> impl Draw<O> + Layout<O> {
Push::y(item_offset, Fixed::y(item_height, Fill::x(item)))
}
#[inline] pub fn map_south_west<O: Output>(
#[inline] pub fn map_south_west<O: Out>(
item_offset: O::Unit,
item_height: O::Unit,
item: impl Render<O>
) -> impl Render<O> {
item: impl Draw<O> + Layout<O>
) -> impl Draw<O> + Layout<O> {
Push::y(item_offset, Align::nw(Fixed::y(item_height, Fill::x(item))))
}
#[inline] pub fn map_east<O: Output>(
#[inline] pub fn map_east<O: Out>(
item_offset: O::Unit,
item_width: O::Unit,
item: impl Render<O>
) -> impl Render<O> {
item: impl Draw<O> + Layout<O>
) -> impl Draw<O> + Layout<O> {
Push::x(item_offset, Align::w(Fixed::x(item_width, Fill::y(item))))
}

View file

@ -0,0 +1,50 @@
use crate::*;
pub enum Margin<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Margin<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Margin::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Margin<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Margin<U, T> {
#[inline] pub fn dx (&self) -> U {
use Margin::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Margin::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Margin<E::Unit, T> {
fn layout (&self, area: E::Area) -> E::Area {
let area = self.content().layout(area);
let dx = self.dx();
let dy = self.dy();
[
area.x().minus(dx),
area.y().minus(dy),
area.w().plus(dy.plus(dy)),
area.h().plus(dy.plus(dy)),
].into()
}
}

View file

@ -0,0 +1,47 @@
use crate::*;
pub enum Max<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Max<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Max::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Max<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Max<U, T> {
#[inline] pub fn dx (&self) -> U {
use Max::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Max::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Max<E::Unit, T> {
fn layout (&self, area: E::Area) -> E::Area {
let [x, y, w, h] = self.content().layout(area).xywh();
match self {
Self::X(mw, _) => [x, y, w.min(*mw), h],
Self::Y(mh, _) => [x, y, w, h.min(*mh)],
Self::XY(mw, mh, _) => [x, y, w.min(*mw), h.min(*mh)],
}.into()
}
}

View file

@ -0,0 +1,47 @@
use crate::*;
pub enum Min<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Min<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Min::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Min<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Min<U, T> {
#[inline] pub fn dx (&self) -> U {
use Min::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Min::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Min<E::Unit, T> {
fn layout (&self, area: E::Area) -> E::Area {
let [x, y, w, h] = self.content().layout(area).xywh();
match self {
Self::X(mw, _) => [x, y, w.max(*mw), h],
Self::Y(mh, _) => [x, y, w, h.max(*mh)],
Self::XY(mw, mh, _) => [x, y, w.max(*mw), h.max(*mh)],
}.into()
}
}

View file

@ -0,0 +1,50 @@
use crate::*;
pub enum Padding<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Padding<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Padding::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Padding<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Padding<U, T> {
#[inline] pub fn dx (&self) -> U {
use Padding::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Padding::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Padding<E::Unit, T> {
fn layout (&self, area: E::Area) -> E::Area {
let area = self.content().layout(area);
let dx = self.dx();
let dy = self.dy();
[
area.x().plus(dx),
area.y().plus(dy),
area.w().minus(dy.plus(dy)),
area.h().minus(dy.plus(dy))
].into()
}
}

View file

@ -0,0 +1,48 @@
use crate::*;
pub enum Pull<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Pull<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Pull::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Pull<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Pull<U, T> {
#[inline] pub fn dx (&self) -> U {
use Pull::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Pull::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Pull<E::Unit, T> {
fn layout (&self, to: E::Area) -> E::Area {
let area = self.content().layout(to);
[
area.x().minus(self.dx()),
area.y().minus(self.dy()),
area.w(),
area.h()
].into()
}
}

View file

@ -0,0 +1,43 @@
use crate::*;
pub enum Push<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Push<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Push::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Push<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Push<U, T> {
#[inline] pub fn dx (&self) -> U {
use Push::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Push::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Push<E::Unit, T> {
fn layout (&self, area: E::Area) -> E::Area {
let area = self.content().layout(area);
[area.x().plus(self.dx()), area.y().plus(self.dy()), area.w(), area.h()].into()
}
}

View file

@ -0,0 +1,43 @@
use crate::*;
pub enum Shrink<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> Shrink<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self {
Self::X(x, item)
}
#[inline] pub const fn y (y: U, item: A) -> Self {
Self::Y(y, item)
}
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self {
Self::XY(x, y, item)
}
#[inline] pub const fn content (&self) -> &A {
use Shrink::*;
match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, }
}
}
impl<E: Out, T: Draw<E> + Layout<E>> Draw<E> for Shrink<E::Unit, T> {
fn draw (&self, to: &mut E) {
to.place_at(self.layout(to.area()), &self.content())
}
}
impl<U: Coordinate, T> Shrink<U, T> {
#[inline] pub fn dx (&self) -> U {
use Shrink::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use Shrink::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
impl<E: Out, T: Layout<E>> Layout<E> for Shrink<E::Unit, T> {
fn layout (&self, to: E::Area) -> E::Area {
let area = self.content().layout(to);
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())].into()
}
}

View file

@ -0,0 +1,170 @@
//use crate::*;
//use Direction::*;
//pub struct Stack<'x, E, F1> {
//__: PhantomData<&'x (E, F1)>,
//direction: Direction,
//callback: F1
//}
//impl<'x, E, F1> Stack<'x, E, F1> {
//pub fn new (direction: Direction, callback: F1) -> Self {
//Self { direction, callback, __: Default::default(), }
//}
//pub fn above (callback: F1) -> Self {
//Self::new(Above, callback)
//}
//pub fn below (callback: F1) -> Self {
//Self::new(Below, callback)
//}
//pub fn north (callback: F1) -> Self {
//Self::new(North, callback)
//}
//pub fn south (callback: F1) -> Self {
//Self::new(South, callback)
//}
//pub fn east (callback: F1) -> Self {
//Self::new(East, callback)
//}
//pub fn west (callback: F1) -> Self {
//Self::new(West, callback)
//}
//}
//impl<'x, E: Out, F1: Fn(&mut dyn FnMut(&dyn Layout<E>))> Layout<E> for Stack<'x, E, F1> {
//fn layout (&self, to: E::Area) -> E::Area {
//let state = StackLayoutState::<E>::new(self.direction, to);
//(self.callback)(&mut |component: &dyn Layout<E>|{
//let StackLayoutState { x, y, w_remaining, h_remaining, .. } = *state.borrow();
//let [_, _, w, h] = component.layout([x, y, w_remaining, h_remaining].into()).xywh();
//state.borrow_mut().grow(w, h);
//});
//let StackLayoutState { w_used, h_used, .. } = *state.borrow();
//match self.direction {
//North | West => { todo!() },
//South | East => { [to.x(), to.y(), w_used, h_used].into() },
//_ => unreachable!(),
//}
//}
//}
//impl<'x, E: Out, F1: Fn(&mut dyn FnMut(&dyn Draw<E>))> Draw<E> for Stack<'x, E, F1> {
//fn draw (&self, to: &mut E) {
//let state = StackLayoutState::<E>::new(self.direction, to.area());
//let to = Rc::new(RefCell::new(to));
//(self.callback)(&mut |component: &dyn Draw<E>|{
//let StackLayoutState { x, y, w_remaining, h_remaining, .. } = *state.borrow();
//let layout = component.layout([x, y, w_remaining, h_remaining].into());
//state.borrow_mut().grow(layout.w(), layout.h());
//to.borrow_mut().place_at(layout, component);
//});
//}
//}
//#[derive(Copy, Clone)]
//struct StackLayoutState<E: Out> {
//direction: Direction,
//x: E::Unit,
//y: E::Unit,
//w_used: E::Unit,
//h_used: E::Unit,
//w_remaining: E::Unit,
//h_remaining: E::Unit,
//}
//impl<E: Out> StackLayoutState<E> {
//fn new (direction: Direction, area: E::Area) -> std::rc::Rc<std::cell::RefCell<Self>> {
//let [x, y, w_remaining, h_remaining] = area.xywh();
//std::rc::Rc::new(std::cell::RefCell::new(Self {
//direction,
//x, y, w_remaining, h_remaining,
//w_used: E::Unit::zero(), h_used: E::Unit::zero()
//}))
//}
//fn grow (&mut self, w: E::Unit, h: E::Unit) -> &mut Self {
//match self.direction {
//South => { self.y = self.y.plus(h);
//self.h_used = self.h_used.plus(h);
//self.h_remaining = self.h_remaining.minus(h);
//self.w_used = self.w_used.max(w); },
//East => { self.x = self.x.plus(w);
//self.w_used = self.w_used.plus(w);
//self.w_remaining = self.w_remaining.minus(w);
//self.h_used = self.h_used.max(h); },
//North | West => { todo!() },
//Above | Below => {},
//};
//self
//}
//fn area_remaining (&self) -> E::Area {
//[self.x, self.y, self.w_remaining, self.h_remaining].into()
//}
//}
////pub struct Stack<'a, E, F1> {
////__: PhantomData<&'a (E, F1)>,
////direction: Direction,
////callback: F1
////}
////impl<'a, E, F1> Stack<'a, E, F1> where
////E: Out, F1: Fn(&mut dyn FnMut(&'a dyn Draw<E>)) + Send + Sync,
////{
////pub fn north (callback: F1) -> Self { Self::new(North, callback) }
////pub fn south (callback: F1) -> Self { Self::new(South, callback) }
////pub fn east (callback: F1) -> Self { Self::new(East, callback) }
////pub fn west (callback: F1) -> Self { Self::new(West, callback) }
////pub fn above (callback: F1) -> Self { Self::new(Above, callback) }
////pub fn below (callback: F1) -> Self { Self::new(Below, callback) }
////pub fn new (direction: Direction, callback: F1) -> Self {
////Self { direction, callback, __: Default::default(), }
////}
////}
////impl<'a, E, F1> Draw<E> for Stack<'a, E, F1> where
////E: Out, F1: Fn(&mut dyn FnMut(&'a dyn Draw<E>)) + Send + Sync,
////{
////fn layout (&self, to: E::Area) -> E::Area {
////let state = StackLayoutState::<E>::new(self.direction, to);
////let mut adder = {
////let state = state.clone();
////move|component: &dyn Draw<E>|{
////let [w, h] = component.layout(state.borrow().area_remaining()).wh();
////state.borrow_mut().grow(w, h);
////}
////};
////(self.callback)(&mut adder);
////let StackLayoutState { w_used, h_used, .. } = *state.borrow();
////match self.direction {
////North | West => { todo!() },
////South | East => { [to.x(), to.y(), w_used, h_used].into() },
////Above | Below => { [to.x(), to.y(), to.w(), to.h()].into() },
////}
////}
////fn draw (&self, to: &mut E) {
////let state = StackLayoutState::<E>::new(self.direction, to.area());
////let mut adder = {
////let state = state.clone();
////move|component: &dyn Draw<E>|{
////let [x, y, w, h] = component.layout(state.borrow().area_remaining()).xywh();
////state.borrow_mut().grow(w, h);
////to.place_at([x, y, w, h].into(), component);
////}
////};
////(self.callback)(&mut adder);
////}
////}
//[>Stack::down(|add|{
//let mut i = 0;
//for (_, name) in self.dirs.iter() {
//if i >= self.scroll {
//add(&Tui::bold(i == self.index, name.as_str()))?;
//}
//i += 1;
//}
//for (_, name) in self.files.iter() {
//if i >= self.scroll {
//add(&Tui::bold(i == self.index, name.as_str()))?;
//}
//i += 1;
//}
//add(&format!("{}/{i}", self.index))?;
//Ok(())
//}));*/

View file

@ -1,97 +0,0 @@
use crate::*;
use Direction::*;
/// A split or layer.
pub struct Bsp<A, B>(
pub(crate) Direction,
pub(crate) A,
pub(crate) B,
);
impl<A, B> Bsp<A, B> {
#[inline] pub const fn n (a: A, b: B) -> Self { Self(North, a, b) }
#[inline] pub const fn s (a: A, b: B) -> Self { Self(South, a, b) }
#[inline] pub const fn e (a: A, b: B) -> Self { Self(East, a, b) }
#[inline] pub const fn w (a: A, b: B) -> Self { Self(West, a, b) }
#[inline] pub const fn a (a: A, b: B) -> Self { Self(Above, a, b) }
#[inline] pub const fn b (a: A, b: B) -> Self { Self(Below, a, b) }
}
impl<E: Output, A: Render<E>, B: Render<E>> Render<E> for Bsp<A, B> {
fn layout (&self, outer: E::Area) -> E::Area { let [_, _, c] = self.areas(outer); c }
fn render (&self, to: &mut E) {
let [area_a, area_b, _] = self.areas(to.area());
let (a, b) = self.contents();
match self.0 {
Below => { to.place(area_a, a); to.place(area_b, b); },
_ => { to.place(area_b, b); to.place(area_a, a); }
}
}
}
impl<E: Output, A: Render<E>, B: Render<E>> BspAreas<E, A, B> for Bsp<A, B> {
fn direction (&self) -> Direction { self.0 }
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
}
pub trait BspAreas<E: Output, A: Render<E>, B: Render<E>> {
fn direction (&self) -> Direction;
fn contents (&self) -> (&A, &B);
fn areas (&self, outer: E::Area) -> [E::Area;3] {
let direction = self.direction();
let [x, y, w, h] = outer.xywh();
let (a, b) = self.contents();
let [aw, ah] = a.layout(outer).wh();
let [bw, bh] = b.layout(match direction {
Above | Below => outer,
South => [x, y + ah, w, h.minus(ah)].into(),
North => [x, y, w, h.minus(ah)].into(),
East => [x + aw, y, w.minus(aw), h].into(),
West => [x, y, w.minus(aw), h].into(),
}).wh();
match direction {
Above | Below => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah.max(bh)]);
let a = [(x + w/2.into()).minus(aw/2.into()), (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
South => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
let a = [(x + w/2.into()).minus(aw/2.into()), y, aw, ah];
let b = [(x + w/2.into()).minus(bw/2.into()), y + ah, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
North => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
let a = [(x + (w/2.into())).minus(aw/2.into()), y + bh, aw, ah];
let b = [(x + (w/2.into())).minus(bw/2.into()), y, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
East => {
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
let a = [x, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x + aw, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
West => {
let [x, y, w, h] = outer.center_xy([aw + bw, ah.max(bh)]);
let a = [x + bw, (y + h/2.into()).minus(ah/2.into()), aw, ah];
let b = [x, (y + h/2.into()).minus(bh/2.into()), bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
}
}
}
/// Stack things on top of each other,
#[macro_export] macro_rules! lay (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::b(bsp, $expr);)*; bsp }});
/// Stack southward.
#[macro_export] macro_rules! col (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::s(bsp, $expr);)*; bsp }});
/// Stack northward.
#[macro_export] macro_rules! col_up (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::n(bsp, $expr);)*; bsp }});
/// Stack eastward.
#[macro_export] macro_rules! row (($($expr:expr),* $(,)?) =>
{{ let bsp = (); $(let bsp = Bsp::e(bsp, $expr);)*; bsp }});

View file

@ -1,168 +0,0 @@
use crate::*;
use Direction::*;
pub struct Stack<'x, E, F1> {
__: PhantomData<&'x (E, F1)>,
direction: Direction,
callback: F1
}
impl<'x, E, F1> Stack<'x, E, F1> {
pub fn new (direction: Direction, callback: F1) -> Self {
Self { direction, callback, __: Default::default(), }
}
pub fn above (callback: F1) -> Self {
Self::new(Above, callback)
}
pub fn below (callback: F1) -> Self {
Self::new(Below, callback)
}
pub fn north (callback: F1) -> Self {
Self::new(North, callback)
}
pub fn south (callback: F1) -> Self {
Self::new(South, callback)
}
pub fn east (callback: F1) -> Self {
Self::new(East, callback)
}
pub fn west (callback: F1) -> Self {
Self::new(West, callback)
}
}
impl<'x, E: Output, F1: Fn(&mut dyn FnMut(&dyn Render<E>))> Render<E> for Stack<'x, E, F1> {
fn layout (&self, to: E::Area) -> E::Area {
let state = StackLayoutState::<E>::new(self.direction, to);
(self.callback)(&mut |component: &dyn Render<E>|{
let StackLayoutState { x, y, w_remaining, h_remaining, .. } = *state.borrow();
let [_, _, w, h] = component.layout([x, y, w_remaining, h_remaining].into()).xywh();
state.borrow_mut().grow(w, h);
});
let StackLayoutState { w_used, h_used, .. } = *state.borrow();
match self.direction {
North | West => { todo!() },
South | East => { [to.x(), to.y(), w_used, h_used].into() },
_ => unreachable!(),
}
}
fn render (&self, to: &mut E) {
let state = StackLayoutState::<E>::new(self.direction, to.area());
let to = Rc::new(RefCell::new(to));
(self.callback)(&mut |component: &dyn Render<E>|{
let StackLayoutState { x, y, w_remaining, h_remaining, .. } = *state.borrow();
let layout = component.layout([x, y, w_remaining, h_remaining].into());
state.borrow_mut().grow(layout.w(), layout.h());
to.borrow_mut().place(layout, component);
});
}
}
#[derive(Copy, Clone)]
struct StackLayoutState<E: Output> {
direction: Direction,
x: E::Unit,
y: E::Unit,
w_used: E::Unit,
h_used: E::Unit,
w_remaining: E::Unit,
h_remaining: E::Unit,
}
impl<E: Output> StackLayoutState<E> {
fn new (direction: Direction, area: E::Area) -> std::rc::Rc<std::cell::RefCell<Self>> {
let [x, y, w_remaining, h_remaining] = area.xywh();
std::rc::Rc::new(std::cell::RefCell::new(Self {
direction,
x, y, w_remaining, h_remaining,
w_used: E::Unit::zero(), h_used: E::Unit::zero()
}))
}
fn grow (&mut self, w: E::Unit, h: E::Unit) -> &mut Self {
match self.direction {
South => { self.y = self.y.plus(h);
self.h_used = self.h_used.plus(h);
self.h_remaining = self.h_remaining.minus(h);
self.w_used = self.w_used.max(w); },
East => { self.x = self.x.plus(w);
self.w_used = self.w_used.plus(w);
self.w_remaining = self.w_remaining.minus(w);
self.h_used = self.h_used.max(h); },
North | West => { todo!() },
Above | Below => {},
};
self
}
fn area_remaining (&self) -> E::Area {
[self.x, self.y, self.w_remaining, self.h_remaining].into()
}
}
//pub struct Stack<'a, E, F1> {
//__: PhantomData<&'a (E, F1)>,
//direction: Direction,
//callback: F1
//}
//impl<'a, E, F1> Stack<'a, E, F1> where
//E: Output, F1: Fn(&mut dyn FnMut(&'a dyn Render<E>)) + Send + Sync,
//{
//pub fn north (callback: F1) -> Self { Self::new(North, callback) }
//pub fn south (callback: F1) -> Self { Self::new(South, callback) }
//pub fn east (callback: F1) -> Self { Self::new(East, callback) }
//pub fn west (callback: F1) -> Self { Self::new(West, callback) }
//pub fn above (callback: F1) -> Self { Self::new(Above, callback) }
//pub fn below (callback: F1) -> Self { Self::new(Below, callback) }
//pub fn new (direction: Direction, callback: F1) -> Self {
//Self { direction, callback, __: Default::default(), }
//}
//}
//impl<'a, E, F1> Render<E> for Stack<'a, E, F1> where
//E: Output, F1: Fn(&mut dyn FnMut(&'a dyn Render<E>)) + Send + Sync,
//{
//fn layout (&self, to: E::Area) -> E::Area {
//let state = StackLayoutState::<E>::new(self.direction, to);
//let mut adder = {
//let state = state.clone();
//move|component: &dyn Render<E>|{
//let [w, h] = component.layout(state.borrow().area_remaining()).wh();
//state.borrow_mut().grow(w, h);
//}
//};
//(self.callback)(&mut adder);
//let StackLayoutState { w_used, h_used, .. } = *state.borrow();
//match self.direction {
//North | West => { todo!() },
//South | East => { [to.x(), to.y(), w_used, h_used].into() },
//Above | Below => { [to.x(), to.y(), to.w(), to.h()].into() },
//}
//}
//fn render (&self, to: &mut E) {
//let state = StackLayoutState::<E>::new(self.direction, to.area());
//let mut adder = {
//let state = state.clone();
//move|component: &dyn Render<E>|{
//let [x, y, w, h] = component.layout(state.borrow().area_remaining()).xywh();
//state.borrow_mut().grow(w, h);
//to.place([x, y, w, h].into(), component);
//}
//};
//(self.callback)(&mut adder);
//}
//}
/*Stack::down(|add|{
let mut i = 0;
for (_, name) in self.dirs.iter() {
if i >= self.scroll {
add(&Tui::bold(i == self.index, name.as_str()))?;
}
i += 1;
}
for (_, name) in self.files.iter() {
if i >= self.scroll {
add(&Tui::bold(i == self.index, name.as_str()))?;
}
i += 1;
}
add(&format!("{}/{i}", self.index))?;
Ok(())
}));*/

View file

@ -1,148 +0,0 @@
//! Transform:
//! ```
//! use ::tengri::{output::*, tui::*};
//! let area: [u16;4] = [10, 10, 20, 20];
//! fn test (area: [u16;4], item: &impl Render<TuiOut>, expected: [u16;4]) {
//! assert_eq!(item.layout(area), expected);
//! };
//! test(area, &(), [20, 20, 0, 0]);
//!
//! test(area, &Fill::xy(()), area);
//! test(area, &Fill::x(()), [10, 20, 20, 0]);
//! test(area, &Fill::y(()), [20, 10, 0, 20]);
//!
//! //FIXME:test(area, &Fixed::x(4, ()), [18, 20, 4, 0]);
//! //FIXME:test(area, &Fixed::y(4, ()), [20, 18, 0, 4]);
//! //FIXME:test(area, &Fixed::xy(4, 4, unit), [18, 18, 4, 4]);
//! ```
use crate::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
macro_rules! transform_xy {
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$area:expr) => {
pub enum $Enum<A> { X(A), Y(A), XY(A) }
impl<A> $Enum<A> {
#[inline] pub const fn x (item: A) -> Self { Self::X(item) }
#[inline] pub const fn y (item: A) -> Self { Self::Y(item) }
#[inline] pub const fn xy (item: A) -> Self { Self::XY(item) }
}
impl<E: Output, T: Render<E>> Content<E> for $Enum<T> {
fn content (&self) -> Option<impl Render<E> + '_> {
use $Enum::*;
Some(match self { X(item) | Y(item) | XY(item) => item, })
}
}
impl<E: Output, T: Render<E>> Render<E> for $Enum<T> {
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
use $Enum::*;
$area
}
fn render (&self, output: &mut E) {
output.place(self.layout(output.area()), &self.content())
}
}
}
}
/// Defines an enum that parametrically transforms its content
/// along either the X axis, the Y axis, or both.
macro_rules! transform_xy_unit {
($x:literal $y:literal $xy:literal |$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
pub enum $Enum<U, A> { X(U, A), Y(U, A), XY(U, U, A), }
impl<U, A> $Enum<U, A> {
#[inline] pub const fn x (x: U, item: A) -> Self { Self::X(x, item) }
#[inline] pub const fn y (y: U, item: A) -> Self { Self::Y(y, item) }
#[inline] pub const fn xy (x: U, y: U, item: A) -> Self { Self::XY(x, y, item) }
}
impl<E: Output, T: Render<E>> Content<E> for $Enum<E::Unit, T> {
fn content (&self) -> Option<impl Render<E> + '_> {
use $Enum::*;
Some(match self { X(_, c) => c, Y(_, c) => c, XY(_, _, c) => c, })
}
}
impl<E: Output, T: Render<E>> Render<E> for $Enum<E::Unit, T> {
fn layout (&$self, $to: E::Area) -> E::Area {
$layout.into()
}
fn render (&self, output: &mut E) {
output.place(self.layout(output.area()), &self.content())
}
}
impl<U: Coordinate, T> $Enum<U, T> {
#[inline] pub fn dx (&self) -> U {
use $Enum::*;
match self { X(x, _) => *x, Y(_, _) => 0.into(), XY(x, _, _) => *x, }
}
#[inline] pub fn dy (&self) -> U {
use $Enum::*;
match self { X(_, _) => 0.into(), Y(y, _) => *y, XY(_, y, _) => *y, }
}
}
}
}
transform_xy!("fill/x" "fill/y" "fill/xy" |self: Fill, to|{
let [x0, y0, wmax, hmax] = to.xywh();
let [x, y, w, h] = self.content().layout(to).xywh();
match self {
X(_) => [x0, y, wmax, h],
Y(_) => [x, y0, w, hmax],
XY(_) => [x0, y0, wmax, hmax],
}.into()
});
transform_xy_unit!("fixed/x" "fixed/y" "fixed/xy"|self: Fixed, area|{
let [x, y, w, h] = area.xywh();
let [x, y, w, h] = self.content().layout(match self {
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
}.into()).xywh();
let fixed_area = match self {
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
};
fixed_area
});
transform_xy_unit!("min/x" "min/y" "min/xy"|self: Min, area|{
let [x, y, w, h] = self.content().layout(area).xywh();
match self {
Self::X(mw, _) => [x, y, w.max(*mw), h],
Self::Y(mh, _) => [x, y, w, h.max(*mh)],
Self::XY(mw, mh, _) => [x, y, w.max(*mw), h.max(*mh)], } });
transform_xy_unit!("max/x" "max/y" "max/xy"|self: Max, area|{
let [x, y, w, h] = area.xywh();
self.content().layout(match self {
Self::X(fw, _) => [x, y, *fw, h],
Self::Y(fh, _) => [x, y, w, *fh],
Self::XY(fw, fh, _) => [x, y, *fw, *fh], }.into()) });
transform_xy_unit!("shrink/x" "shrink/y" "shrink/xy"|self: Shrink, area|self.content().layout(
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())].into()));
transform_xy_unit!("expand/x" "expand/y" "expand/xy"|self: Expand, area|self.content().layout(
[area.x(), area.y(), area.w().plus(self.dx()), area.h().plus(self.dy())].into()));
transform_xy_unit!("push/x" "push/y" "push/xy"|self: Push, area|{
let area = self.content().layout(area);
[area.x().plus(self.dx()), area.y().plus(self.dy()), area.w(), area.h()] });
transform_xy_unit!("pull/x" "pull/y" "pull/xy"|self: Pull, area|{
let area = self.content().layout(area);
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()] });
transform_xy_unit!("margin/x" "margin/y" "margin/xy"|self: Margin, area|{
let area = self.content().layout(area);
let dx = self.dx();
let dy = self.dy();
[area.x().minus(dx), area.y().minus(dy), area.w().plus(dy.plus(dy)), area.h().plus(dy.plus(dy))] });
transform_xy_unit!("padding/x" "padding/y" "padding/xy"|self: Padding, area|{
let area = self.content().layout(area);
let dx = self.dx();
let dy = self.dy();
[area.x().plus(dx), area.y().plus(dy), area.w().minus(dy.plus(dy)), area.h().minus(dy.plus(dy))] });

View file

@ -1,35 +0,0 @@
#![feature(step_trait)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
//#![feature(non_lifetime_binders)]
pub(crate) use std::cell::RefCell;
pub(crate) use std::fmt::{Debug, Display};
pub(crate) use std::marker::PhantomData;
pub(crate) use std::ops::{Add, Sub, Mul, Div, Deref};
pub(crate) use std::rc::Rc;
pub(crate) use std::sync::RwLock;
pub(crate) use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
pub(crate) use tengri_core::*;
#[cfg(feature = "dsl")] pub(crate) use ::tengri_dsl::*;
mod output; pub use self::output::*;
mod output_render; pub use self::output_render::*;
mod output_content; pub use self::output_content::*;
mod output_thunk; pub use self::output_thunk::*;
mod space_area; pub use self::space_area::*;
mod space_coordinate; pub use self::space_coordinate::*;
mod space_direction; pub use self::space_direction::*;
mod space_measure; pub use self::space_measure::*;
mod space_size; pub use self::space_size::*;
mod layout_align; pub use self::layout_align::*;
mod layout_bsp; pub use self::layout_bsp::*;
mod layout_cond; pub use self::layout_cond::*;
mod layout_map; pub use self::layout_map::*;
mod layout_stack; pub use self::layout_stack::*;
mod layout_xy; pub use self::layout_xy::*;
pub(crate) use self::Direction::*;
#[cfg(test)] mod test;
#[cfg(test)] pub(crate) use proptest_derive::Arbitrary;

View file

@ -1,7 +1,28 @@
use crate::*;
#![feature(step_trait)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
#![feature(const_precise_live_drops)]
#![feature(type_changing_struct_update)]
//#![feature(non_lifetime_binders)]
/// Render target.
pub trait Output: Send + Sync + Sized {
mod content; pub use self::content::*;
mod draw; pub use self::draw::*;
mod group; pub use self::group::*;
mod layout; pub use self::layout::*;
mod space; pub use self::space::*;
mod thunk; pub use self::thunk::*;
mod widget; pub use self::widget::*;
pub(crate) use self::Direction::*;
pub(crate) use std::fmt::{Debug, Display};
pub(crate) use std::marker::PhantomData;
pub(crate) use std::ops::{Add, Sub, Mul, Div, Deref};
pub(crate) use std::rc::Rc;
pub(crate) use std::sync::{Arc, RwLock, atomic::{AtomicUsize, Ordering::Relaxed}};
pub(crate) use tengri_core::*;
#[cfg(feature = "dsl")] pub(crate) use ::tengri_dsl::*;
/// Draw target.
pub trait Out: Send + Sync + Sized {
/// Unit of length
type Unit: Coordinate;
/// Rectangle without offset
@ -12,11 +33,26 @@ pub trait Output: Send + Sync + Sized {
fn area (&self) -> Self::Area;
/// Mutable pointer to area
fn area_mut (&mut self) -> &mut Self::Area;
/// Render widget in area
fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: Self::Area, content: &'t T);
/// Draw widget in area
fn place_at <'t, T: Draw<Self> + ?Sized> (&mut self, area: Self::Area, content: &'t T);
fn place <'t, T: Draw<Self> + Layout<Self> + ?Sized> (&mut self, content: &'t T) {
self.place_at(content.layout(self.area()), content)
}
#[inline] fn x (&self) -> Self::Unit { self.area().x() }
#[inline] fn y (&self) -> Self::Unit { self.area().y() }
#[inline] fn w (&self) -> Self::Unit { self.area().w() }
#[inline] fn h (&self) -> Self::Unit { self.area().h() }
#[inline] fn wh (&self) -> Self::Size { self.area().wh().into() }
}
#[cfg(test)] mod test;
#[cfg(test)] pub(crate) use proptest_derive::Arbitrary;
//impl<E: Out, C: Content<E> + Layout<E>> Draw<E> for C { // if only
//fn draw (&self, to: &mut E) {
//if let Some(content) = self.content() {
//to.place_at(self.layout(to.area()), &content);
//}
//}
//}

View file

@ -1,60 +0,0 @@
use crate::*;
/// Composable renderable with static dispatch.
pub trait Content<E: Output>: Sized {
/// Return opaque [Render]able.
fn content (&self) -> Option<impl Render<E> + '_> { Option::<()>::None }
}
/// The platonic ideal unit of [Content]: total emptiness at dead center (e=1vg^sqrt(-1))
impl<E: Output> Content<E> for () {}
impl<E: Output> Content<E> for fn(&mut E) {
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self)
}
}
impl<E: Output, T: Render<E>> Content<E> for fn()->T {
fn content (&self) -> Option<impl Render<E> + '_> {
Some(self())
}
}
impl<E: Output, T: Content<E>> Content<E> for Option<T> {
fn content (&self) -> Option<impl Render<E> + '_> {
if let Some(content) = self {
content.content()
} else {
None
}
}
}
/// You can render from an opaque pointer.
impl<E: Output> Content<E> for &dyn Render<E> where Self: Sized {
fn content (&self) -> Option<impl Render<E> + '_> {
#[allow(suspicious_double_ref_op)]
Some(self.deref())
}
}
/// Implement composable content for a struct.
#[macro_export] macro_rules! content {
// Implement for all [Output]s.
(|$self:ident:$Struct:ty| $content:expr) => {
impl<E: Output> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> + '_ { Some($content) }
}
};
// Implement for specific [Output].
($Output:ty:|
$self:ident:
$Struct:ident$(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?
|$content:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn content (&$self) -> impl Render<$Output> + '_ { $content }
}
};
}

View file

@ -1,87 +0,0 @@
use crate::*;
/// Renderable with dynamic dispatch.
pub trait Render<E: Output> {
/// Write data to display.
fn render (&self, output: &mut E);
/// Compute layout.
fn layout (&self, area: E::Area) -> E::Area { area }
fn boxed <'a> (self) -> Box<dyn Render<E> + 'a> where Self: Sized + 'a {
Box::new(self) as Box<dyn Render<E> + 'a>
}
fn rc <'a> (self) -> Rc<dyn Render<E> + 'a> where Self: Sized + 'a {
Rc::new(self) as Rc<dyn Render<E> + 'a>
}
}
impl<E: Output> Render<E> for () {
fn render (&self, _: &mut E) {}
}
impl<E: Output> Render<E> for fn(&mut E) {
fn render (&self, output: &mut E) {
self(output)
}
}
impl<'x, E: Output> Render<E> for &(dyn Render<E> + 'x) {
fn render (&self, output: &mut E) {
(*self).render(output)
}
}
impl<E: Output, R: Render<E>> Render<E> for Box<R> {
fn render (&self, output: &mut E) {
(**self).render(output)
}
}
impl<E: Output, R: Render<E>> Render<E> for Option<R> {
fn render (&self, output: &mut E) {
if let Some(render) = self {
render.render(output)
}
}
}
impl<E: Output, R: Render<E>> Render<E> for &R {
fn render (&self, output: &mut E) {
(*self).render(output)
}
}
impl<E: Output, R: Render<E>> Render<E> for &mut R {
fn render (&self, output: &mut E) {
(**self).render(output)
}
}
impl<E: Output, R: Render<E>> Render<E> for [R] {
fn render (&self, output: &mut E) {
for render in self.iter() {
render.render(output)
}
}
}
/// Implement custom rendering for a struct.
#[macro_export] macro_rules! render {
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Output, $($T$(:$Trait)?),*> Render<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E) { $render }
}
};
($Output:ty:|
$self:ident:
$Struct:ident $(<$($($L:lifetime)? $($T:ident)? $(:$Trait:path)?),+>)?, $to:ident
|$render:expr) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Render<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut $Output) { $render }
}
};
}

View file

@ -1,53 +0,0 @@
use crate::*;
/// Lazily-evaluated [Render]able.
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T>(
PhantomData<E>,
F
);
impl<E: Output, T: Render<E>, F: Fn()->T> Thunk<E, T, F> {
pub const fn new (thunk: F) -> Self {
Self(PhantomData, thunk)
}
}
impl<E: Output, T: Render<E>, F: Fn()->T> Content<E> for Thunk<E, T, F> {
fn content (&self) -> Option<impl Render<E>> {
Some((self.1)())
}
}
pub struct ThunkRender<E: Output, F: Fn(&mut E)>(PhantomData<E>, F);
impl<E: Output, F: Fn(&mut E)> ThunkRender<E, F> {
pub fn new (render: F) -> Self { Self(PhantomData, render) }
}
impl<E: Output, F: Fn(&mut E)> Render<E> for ThunkRender<E, F> {
fn render (&self, to: &mut E) { (self.1)(to) }
}
#[derive(Debug, Default)] pub struct Memo<T, U> {
pub value: T,
pub view: Arc<RwLock<U>>
}
impl<T: PartialEq, U> Memo<T, U> {
pub fn new (value: T, view: U) -> Self {
Self { value, view: Arc::new(view.into()) }
}
pub fn update <R> (
&mut self,
newval: T,
render: impl Fn(&mut U, &T, &T)->R
) -> Option<R> {
if newval != self.value {
let result = render(&mut*self.view.write().unwrap(), &newval, &self.value);
self.value = newval;
return Some(result);
}
None
}
}
/// Clear a pre-allocated buffer, then write into it.
#[macro_export] macro_rules! rewrite {
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
}

5
output/src/space.rs Normal file
View file

@ -0,0 +1,5 @@
mod space_area; pub use self::space_area::*;
mod space_coordinate; pub use self::space_coordinate::*;
mod space_direction; pub use self::space_direction::*;
mod space_measure; pub use self::space_measure::*;
mod space_size; pub use self::space_size::*;

View file

@ -2,13 +2,13 @@ use crate::*;
/// A widget that tracks its render width and height
#[derive(Default)]
pub struct Measure<E: Output> {
pub struct Measure<E: Out> {
_engine: PhantomData<E>,
pub x: Arc<AtomicUsize>,
pub y: Arc<AtomicUsize>,
}
impl<E: Output> PartialEq for Measure<E> {
impl<E: Out> PartialEq for Measure<E> {
fn eq (&self, other: &Self) -> bool {
self.x.load(Relaxed) == other.x.load(Relaxed) &&
self.y.load(Relaxed) == other.y.load(Relaxed)
@ -16,14 +16,14 @@ impl<E: Output> PartialEq for Measure<E> {
}
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
impl<E: Output> Render<E> for Measure<E> {
fn render (&self, to: &mut E) {
impl<E: Out> Draw<E> for Measure<E> {
fn draw (&self, to: &mut E) {
self.x.store(to.area().w().into(), Relaxed);
self.y.store(to.area().h().into(), Relaxed);
}
}
impl<E: Output> Clone for Measure<E> {
impl<E: Out> Clone for Measure<E> {
fn clone (&self) -> Self {
Self {
_engine: Default::default(),
@ -33,7 +33,7 @@ impl<E: Output> Clone for Measure<E> {
}
}
impl<E: Output> std::fmt::Debug for Measure<E> {
impl<E: Out> std::fmt::Debug for Measure<E> {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("Measure")
.field("width", &self.x)
@ -42,7 +42,7 @@ impl<E: Output> std::fmt::Debug for Measure<E> {
}
}
impl<E: Output> Measure<E> {
impl<E: Out> Measure<E> {
pub fn new () -> Self {
Self {
_engine: PhantomData::default(),
@ -75,7 +75,7 @@ impl<E: Output> Measure<E> {
pub fn format (&self) -> Arc<str> {
format!("{}x{}", self.w(), self.h()).into()
}
pub fn of <T: Render<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> {
pub fn of <T: Draw<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> {
Bsp::b(Fill::xy(self), item)
}
}

View file

@ -38,7 +38,7 @@ impl<N: Coordinate> Size<N> for [N;2] {
fn y (&self) -> N { self[1] }
}
pub trait HasSize<E: Output> {
pub trait HasSize<E: Out> {
fn size (&self) -> &Measure<E>;
fn width (&self) -> usize {
self.size().w()
@ -48,7 +48,7 @@ pub trait HasSize<E: Output> {
}
}
impl<E: Output, T: Has<Measure<E>>> HasSize<E> for T {
impl<E: Out, T: Has<Measure<E>>> HasSize<E> for T {
fn size (&self) -> &Measure<E> {
self.get()
}

View file

@ -90,7 +90,7 @@ macro_rules! test_op_transform {
_ => None
} {
assert_eq!(Content::layout(&op, [x, y, w, h]),
Render::layout(&op, [x, y, w, h]));
Draw::layout(&op, [x, y, w, h]));
}
}
}
@ -124,15 +124,15 @@ proptest! {
let bsp = Bsp(d, a, b);
assert_eq!(
Content::layout(&bsp, [x, y, w, h]),
Render::layout(&bsp, [x, y, w, h]),
Draw::layout(&bsp, [x, y, w, h]),
);
}
}
#[test] fn test_stub_output () -> Usually<()> {
use crate::*;
struct TestOutput([u16;4]);
impl Output for TestOutput {
struct TestOut([u16;4]);
impl Out for TestOut {
type Unit = u16;
type Size = [u16;2];
type Area = [u16;4];
@ -142,12 +142,12 @@ proptest! {
fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.0
}
fn place <T: Render<Self> + ?Sized> (&mut self, _: [u16;4], _: &T) {
fn place <T: Draw<Self> + ?Sized> (&mut self, _: [u16;4], _: &T) {
()
}
}
impl Render<TestOutput> for String {
fn render (&self, to: &mut TestOutput) {
impl Draw<TestOut> for String {
fn draw (&self, to: &mut TestOut) {
to.area_mut().set_w(self.len() as u16);
}
}
@ -161,8 +161,8 @@ proptest! {
#[test] fn test_iter_map () {
struct Foo;
impl<T: Output> Content<T> for Foo {}
fn _make_map <T: Output, U: Content<T> + Send + Sync> (data: &Vec<U>) -> impl Render<T> {
impl<T: Out> Content<T> for Foo {}
fn _make_map <T: Out, U: Content<T> + Send + Sync> (data: &Vec<U>) -> impl Draw<T> {
Map::new(||data.iter(), |_foo, _index|{})
}
let _data = vec![Foo, Foo, Foo];

53
output/src/thunk.rs Normal file
View file

@ -0,0 +1,53 @@
use crate::*;
/// Lazily-evaluated [Draw]able.
pub struct Lazy<E, T, F>(PhantomData<(E, T)>, F);
impl<E: Out, T: Draw<E> + Layout<E>, F: Fn()->T> Lazy<E, T, F> {
pub const fn new (thunk: F) -> Self {
Self(PhantomData, thunk)
}
}
impl<E: Out, T: Draw<E> + Layout<E>, F: Fn()->T> Content<E> for Lazy<E, T, F> {
fn content (&self) -> impl Draw<E> + Layout<E> + '_ {
(self.1)()
}
}
pub struct Thunk<E: Out, F: Fn(&mut E)>(PhantomData<E>, F);
impl<E: Out, F: Fn(&mut E)> Thunk<E, F> {
pub const fn new (draw: F) -> Self { Self(PhantomData, draw) }
}
impl<E: Out, F: Fn(&mut E)> Draw<E> for Thunk<E, F> {
fn draw (&self, to: &mut E) { (self.1)(to) }
}
impl<E: Out, F: Fn(&mut E)> Layout<E> for Thunk<E, F> {
fn layout (&self, to: E::Area) -> E::Area { to }
}
#[derive(Debug, Default)] pub struct Memo<T, U> {
pub value: T,
pub view: Arc<RwLock<U>>
}
impl<T: PartialEq, U> Memo<T, U> {
pub fn new (value: T, view: U) -> Self {
Self { value, view: Arc::new(view.into()) }
}
pub fn update <R> (
&mut self,
newval: T,
draw: impl Fn(&mut U, &T, &T)->R
) -> Option<R> {
if newval != self.value {
let result = draw(&mut*self.view.write().unwrap(), &newval, &self.value);
self.value = newval;
return Some(result);
}
None
}
}
/// Clear a pre-allocated buffer, then write into it.
#[macro_export] macro_rules! rewrite {
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
}

4
output/src/widget.rs Normal file
View file

@ -0,0 +1,4 @@
mod widget_border; pub use self::widget_border::*;
mod widget_field; pub use self::widget_field::*;
mod widget_style; pub use self::widget_style::*;
mod widget_tryptich; pub use self::widget_tryptich::*;

View file

@ -0,0 +1,10 @@
use crate::*;
pub struct Border<S>(pub bool, pub S);
impl<O: Out, S: Layout<O>> Layout<O> for Border<S> {
fn layout (&self, area: O::Area) -> O::Area {
self.1.layout(area)
}
}
pub struct Bordered<S, W>(pub bool, pub S, pub W);

View file

@ -0,0 +1,25 @@
use crate::*;
pub struct FieldH<Theme, Label, Value>(pub Theme, pub Label, pub Value);
impl<O: Out, T, L: Draw<O>, V: Draw<O>> Layout<O> for FieldH<T, L, V> where Self: Content<O> {
fn layout (&self, to: O::Area) -> O::Area {
self.content().layout(to)
}
}
impl<O: Out, T, L: Draw<O>, V: Draw<O>> Draw<O> for FieldH<T, L, V> where Self: Content<O> {
fn draw (&self, to: &mut O) {
self.content().draw(to)
}
}
pub struct FieldV<Theme, Label, Value>(pub Theme, pub Label, pub Value);
impl<O: Out, T, L: Draw<O>, V: Draw<O>> Layout<O> for FieldV<T, L, V> where Self: Content<O> {
fn layout (&self, to: O::Area) -> O::Area {
self.content().layout(to)
}
}
impl<O: Out, T, L: Draw<O>, V: Draw<O>> Draw<O> for FieldV<T, L, V> where Self: Content<O> {
fn draw (&self, to: &mut O) {
self.content().draw(to)
}
}

View file

@ -0,0 +1,15 @@
#[allow(unused)] use crate::*;
pub struct Foreground<Color, Item>(pub Color, pub Item);
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Foreground<Color, Item> {
fn layout (&self, to: O::Area) -> O::Area {
self.1.layout(to)
}
}
pub struct Background<Color, Item>(pub Color, pub Item);
impl<O: Out, Color, Item: Layout<O>> Layout<O> for Background<Color, Item> {
fn layout (&self, to: O::Area) -> O::Area {
self.1.layout(to)
}
}

View file

@ -0,0 +1,31 @@
#[allow(unused)] use crate::*;
/// A three-column layout.
pub struct Tryptich<A, B, C> {
pub top: bool,
pub h: u16,
pub left: (u16, A),
pub middle: (u16, B),
pub right: (u16, C),
}
impl Tryptich<(), (), ()> {
pub fn center (h: u16) -> Self {
Self { h, top: false, left: (0, ()), middle: (0, ()), right: (0, ()) }
}
pub fn top (h: u16) -> Self {
Self { h, top: true, left: (0, ()), middle: (0, ()), right: (0, ()) }
}
}
impl<A, B, C> Tryptich<A, B, C> {
pub fn left <D> (self, w: u16, content: D) -> Tryptich<D, B, C> {
Tryptich { left: (w, content), ..self }
}
pub fn middle <D> (self, w: u16, content: D) -> Tryptich<A, D, C> {
Tryptich { middle: (w, content), ..self }
}
pub fn right <D> (self, w: u16, content: D) -> Tryptich<A, B, D> {
Tryptich { right: (w, content), ..self }
}
}

View file

@ -71,8 +71,8 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) {
}
#[cfg(test)] #[test] fn test_proc_view () {
let x: crate::proc_view::ViewMeta = pq! { SomeOutput };
let output: Ident = pq! { SomeOutput };
let x: crate::proc_view::ViewMeta = pq! { SomeOut };
let output: Ident = pq! { SomeOut };
assert_eq!(x.output, output);
// TODO
@ -103,10 +103,10 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) {
//]);
// FIXME
//let parsed: ViewDefinition = pq! {
//#[tengri_proc::view(SomeOutput)]
//#[tengri_proc::view(SomeOut)]
//impl SomeView {
//#[tengri::view(":view-1")]
//fn view_1 (&self) -> impl Render<SomeOutput> + use<'_> {
//fn view_1 (&self) -> impl Draw<SomeOut> + use<'_> {
//"view-1"
//}
//}
@ -114,19 +114,19 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) {
//let written = quote! { #parsed };
//assert_eq!(format!("{written}"), format!("{}", quote! {
//impl SomeView {
//fn view_1 (&self) -> impl Render<SomeOutput> + use<'_> {
//fn view_1 (&self) -> impl Draw<SomeOut> + use<'_> {
//"view-1"
//}
//}
///// Generated by [tengri_proc].
//impl ::tengri::output::Content<SomeOutput> for SomeView {
//fn content (&self) -> impl Render<SomeOutput> {
//impl ::tengri::output::Content<SomeOut> for SomeView {
//fn content (&self) -> impl Draw<SomeOut> {
//self.size.of(::tengri::output::View(self, self.config.view))
//}
//}
///// Generated by [tengri_proc].
//impl<'a> ::tengri::dsl::ViewContext<'a, SomeOutput> for SomeView {
//fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, SomeOutput>> {
//impl<'a> ::tengri::dsl::ViewContext<'a, SomeOut> for SomeView {
//fn get_content_sym (&'a self, value: &Value<'a>) -> Option<DrawBox<'a, SomeOut>> {
//match value {
//::tengri::dsl::Value::Sym(":view-1") => self.view_1().boxed(),
//_ => panic!("expected Sym(content), got: {value:?}")

View file

@ -77,12 +77,12 @@ impl ViewDef {
quote! {
/// Generated by [tengri_proc].
///
/// Makes [#self_ty] able to construct the [Render]able
/// Makes [#self_ty] able to construct the [Draw]able
/// which might correspond to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration.
impl<'state> ::tengri::dsl::DslInto<Box<dyn ::tengri::output::Render<#output> + 'state>> for #self_ty {
impl<'state> ::tengri::dsl::DslInto<Box<dyn ::tengri::output::Draw<#output> + 'state>> for #self_ty {
fn dsl_into (&self, dsl: &impl ::tengri::dsl::Dsl)
-> Perhaps<Box<dyn ::tengri::output::Render<#output> + 'state>>
-> Perhaps<Box<dyn ::tengri::output::Draw<#output> + 'state>>
{
#(#builtins)*
if let Some(sym) = dsl.sym()? {
@ -128,9 +128,9 @@ fn _builtins_with_holes () -> impl Iterator<Item=(Builtin, TokenStream2)> {
}
fn _builtins_with_boxes () -> impl Iterator<Item=(Builtin, TokenStream2)> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<_>> })
builtins_with(quote! { _ }, quote! { Box<dyn Draw<_>> })
}
fn builtins_with_boxes_output (o: TokenStream2) -> impl Iterator<Item=(Builtin, TokenStream2)> {
builtins_with(quote! { _ }, quote! { Box<dyn Render<#o>> })
builtins_with(quote! { _ }, quote! { Box<dyn Draw<#o>> })
}

View file

@ -5,7 +5,8 @@ version = { workspace = true }
edition = { workspace = true }
[features]
dsl = [ "tengri_dsl", "tengri_output/dsl" ]
dsl = [ "dep:tengri_dsl", "tengri_output/dsl" ]
bumpalo = [ "dep:bumpalo" ]
[dependencies]
tengri_core = { workspace = true }
@ -13,14 +14,15 @@ tengri_input = { workspace = true }
tengri_output = { workspace = true }
tengri_dsl = { workspace = true, optional = true }
palette = { workspace = true }
rand = { workspace = true }
crossterm = { workspace = true }
ratatui = { workspace = true }
better-panic = { workspace = true }
konst = { workspace = true }
atomic_float = { workspace = true }
better-panic = { workspace = true }
bumpalo = { workspace = true, optional = true }
crossterm = { workspace = true }
konst = { workspace = true }
palette = { workspace = true }
quanta = { workspace = true }
rand = { workspace = true }
ratatui = { workspace = true }
unicode-width = { workspace = true }
[dev-dependencies]

View file

@ -3,7 +3,7 @@
(bg/behind :bg2 (border/around :border2 (margin/xy 4 2 :label2)))
(bg/behind :bg3 (border/around :border3 (margin/xy 6 3 :label3)))))))
fn content (&self) -> dyn Render<Engine = Tui> {
fn content (&self) -> dyn Draw<Engine = Tui> {
let border_style = Style::default().fg(Color::Rgb(0,0,0));
Align::Center(Layers::new(move|add|{

View file

@ -76,25 +76,25 @@ content!(TuiOut: |self: Example|{
//#[tengri_proc::view(TuiOut)]
//impl Example {
//pub fn title (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn title (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(60, 10, 10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EXAMPLES.len())))).boxed()
//}
//pub fn code (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn code (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(10, 60, 10), Push::y(2, Align::n(format!("{}", EXAMPLES[self.0])))).boxed()
//}
//pub fn hello (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn hello (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(10, 100, 10), "Hello").boxed()
//}
//pub fn world (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn world (&self) -> impl Draw<TuiOut> + use<'_> {
//Tui::bg(Color::Rgb(100, 10, 10), "world").boxed()
//}
//pub fn hello_world (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn hello_world (&self) -> impl Draw<TuiOut> + use<'_> {
//"Hello world!".boxed()
//}
//pub fn map_e (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn map_e (&self) -> impl Draw<TuiOut> + use<'_> {
//Map::east(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed()
//}
//pub fn map_s (&self) -> impl Render<TuiOut> + use<'_> {
//pub fn map_s (&self) -> impl Draw<TuiOut> + use<'_> {
//Map::south(5u16, ||0..5u16, |n, _i|format!("{n}")).boxed()
//}
//}

View file

@ -27,7 +27,7 @@ pub(crate) use std::io::{stdout, Stdout};
//use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl Content<TuiOut> for TestComponent {
fn content (&self) -> impl Render<TuiOut> {
fn content (&self) -> impl Draw<TuiOut> {
Some(self.0.as_str())
}
}

View file

@ -1,56 +1,128 @@
use crate::*;
#[allow(unused)] use crate::*;
macro_rules! impl_content_layout_render {
($Output:ty: |$self:ident: $Struct:ty, $to:ident|
layout = $layout:expr;
render = $render:expr) =>
{
impl Render<$Output> for $Struct {
fn layout (&$self, $to: [u16;4]) -> [u16;4] { $layout }
fn render (&$self, $to: &mut $Output) { $render }
impl Tui {
pub const fn fg <T> (color: Color, w: T) -> TuiForeground<T> {
TuiForeground(Foreground(color, w))
}
pub const fn bg <T> (color: Color, w: T) -> TuiBackground<T> {
TuiBackground(Background(color, w))
}
pub const fn fg_bg <T> (fg: Color, bg: Color, w: T) -> TuiBackground<TuiForeground<T>> {
TuiBackground(Background(bg, TuiForeground(Foreground(fg, w))))
}
pub const fn modify <T> (enable: bool, modifier: Modifier, w: T) -> Modify<T> {
Modify(enable, modifier, w)
}
pub const fn bold <T> (enable: bool, w: T) -> Modify<T> {
Self::modify(enable, Modifier::BOLD, w)
}
pub const fn border <S, T> (enable: bool, style: S, w: T) -> Bordered<S, T> {
Bordered(enable, style, w)
}
}
#[macro_export] macro_rules! tui_layout ((|$self:ident:$Self:ty, $to:ident|$expr:expr)=>{
impl Layout<TuiOut> for $Self {
fn layout (&$self, $to: [u16;4]) -> [u16;4] {
$expr
}
}
}
});
#[macro_export] macro_rules! tui_draw ((|$self:ident:$Self:ty, $to:ident|$expr:expr)=>{
impl Draw<TuiOut> for $Self {
fn draw (&$self, $to: &mut TuiOut) {
$expr
}
}
});
mod tui_border; pub use self::tui_border::*;
mod tui_button; pub use self::tui_button::*;
mod tui_color; pub use self::tui_color::*;
mod tui_error; pub use self::tui_error::*;
mod tui_field; pub use self::tui_field::*;
mod tui_phat; pub use self::tui_phat::*;
mod tui_repeat; pub use self::tui_repeat::*;
mod tui_scroll; pub use self::tui_scroll::*;
mod tui_string; pub use self::tui_string::*;
mod tui_border; pub use self::tui_border::*;
mod tui_button; //pub use self::tui_button::*;
mod tui_color; pub use self::tui_color::*;
mod tui_error; pub use self::tui_error::*;
mod tui_field; pub use self::tui_field::*;
mod tui_number; //pub use self::tui_number::*;
mod tui_phat; pub use self::tui_phat::*;
mod tui_repeat; pub use self::tui_repeat::*;
mod tui_scroll; pub use self::tui_scroll::*;
mod tui_string; pub use self::tui_string::*;
mod tui_style; pub use self::tui_style::*;
mod tui_tryptich; pub use self::tui_tryptich::*;
mod tui_tryptich; //pub use self::tui_tryptich::*;
impl<T: Render<TuiOut>> Render<TuiOut> for std::sync::Arc<T> {
pub struct TuiForeground<T>(pub(crate) Foreground<Color, T>);
pub struct TuiBackground<T>(pub(crate) Background<Color, T>);
pub struct Modify<T>(pub bool, pub Modifier, pub T);
pub struct Styled<T>(pub Option<Style>, pub T);
impl<T: Layout<TuiOut>> Layout<TuiOut> for TuiForeground<T> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
Render::<TuiOut>::layout(&**self, to)
self.0.layout(to)
}
fn render (&self, to: &mut TuiOut) {
Render::<TuiOut>::render(&**self, to)
}
impl<T: Layout<TuiOut> + Draw<TuiOut>> Draw<TuiOut> for TuiForeground<T> {
fn draw (&self, to: &mut TuiOut) {
let area = self.layout(to.area());
to.fill_bg(area, self.0.0);
to.place_at(area, &self.0.1);
}
}
impl<T: Render<TuiOut>> Content<TuiOut> for Result<T, Box<dyn std::error::Error>> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
Some(Bsp::a(self.as_ref().ok(), self.as_ref().err()
.map(|e|Tui::fg_bg(Color::Rgb(255,255,255), Color::Rgb(32,32,32), e.to_string()))))
impl<T: Layout<TuiOut>> Layout<TuiOut> for TuiBackground<T> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.0.layout(to)
}
}
impl<T: Layout<TuiOut> + Draw<TuiOut>> Draw<TuiOut> for TuiBackground<T> {
fn draw (&self, to: &mut TuiOut) {
let area = self.layout(to.area());
to.fill_bg(area, self.0.0);
to.place_at(area, &self.0.1);
}
}
//impl<T: Render<TuiOut>> Render<TuiOut> for Result<T, Box<dyn std::error::Error>> {
impl<T: Layout<TuiOut>> Layout<TuiOut> for Modify<T> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.2.layout(to)
}
}
impl<T: Draw<TuiOut>> Draw<TuiOut> for Modify<T> {
fn draw (&self, to: &mut TuiOut) {
to.fill_mod(to.area(), self.0, self.1);
self.2.draw(to)
}
}
impl<T: Layout<TuiOut>> Layout<TuiOut> for Styled<T> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
}
impl<T: Layout<TuiOut> + Draw<TuiOut>> Draw<TuiOut> for Styled<T> {
fn draw (&self, to: &mut TuiOut) {
to.place(&self.1);
// TODO write style over area
}
}
//impl<T: Draw<TuiOut> + Layout<TuiOut>> Content<TuiOut> for Result<T, Box<dyn std::error::Error>> {
//fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
//Bsp::a(self.as_ref().ok(), self.as_ref().err().map(
//|e|Tui::fg_bg(Color::Rgb(255,255,255), Color::Rgb(32,32,32), e.to_string())
//))
//}
//}
//impl<T: Draw<TuiOut>> Draw<TuiOut> for Result<T, Box<dyn std::error::Error>> {
//fn layout (&self, to: [u16;4]) -> [u16;4] {
//match self {
//Ok(content) => content.layout(to),
//Err(e) => [0, 0, to.w(), to.h()]
//}
//}
//fn render (&self, to: &mut TuiOut) {
//fn draw (&self, to: &mut TuiOut) {
//match self {
//Ok(content) => content.render(to),
//Ok(content) => content.draw(to),
//Err(e) => to.blit(&e.to_string(), 0, 0, Some(Style::default()
//.bg(Color::Rgb(32,32,32))
//.fg(Color::Rgb(255,255,255))))

View file

@ -243,7 +243,7 @@ impl<T: FocusGrid + HasEnter> FocusOrder for T {
}
pub trait FocusWrap<T> {
fn wrap <W: Content<TuiOut>> (self, focus: T, content: &'_ W) -> impl Render<TuiOut> + '_;
fn wrap <W: Content<TuiOut>> (self, focus: T, content: &'_ W) -> impl Draw<TuiOut> + '_;
}
pub fn to_focus_command <T: Send + Sync> (input: &TuiIn) -> Option<FocusCommand<T>> {

View file

@ -1,20 +1,16 @@
use crate::*;
pub struct Bordered<S, W>(pub bool, pub S, pub W);
impl<S: BorderStyle, W: Render<TuiOut>> Content<TuiOut> for Bordered<S, W> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
Some(Fill::xy(
lay!(When::new(self.0, Border(self.0, self.1)), Padding::xy(1, 1, &self.2))
impl<S: BorderStyle, W: Draw<TuiOut> + Layout<TuiOut>> Content<TuiOut> for Bordered<S, W> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
Fill::xy(lay!(
When::new(self.0, Border(self.0, self.1)),
Padding::xy(1, 1, &self.2)
))
}
}
pub struct Border<S: BorderStyle>(pub bool, pub S);
impl<S: BorderStyle> Render<TuiOut> for Border<S> {
fn layout (&self, area: [u16;4]) -> [u16;4] {
self.1.layout(area)
}
fn render (&self, to: &mut TuiOut) {
impl<S: BorderStyle> Draw<TuiOut> for Border<S> {
fn draw (&self, to: &mut TuiOut) {
if self.0 {
let area = to.area();
if area.w() > 0 && area.y() > 0 {
@ -35,15 +31,15 @@ impl<S: BorderStyle> Render<TuiOut> for Border<S> {
}
}
pub trait BorderStyle: Render<TuiOut> + Copy {
pub trait BorderStyle: Draw<TuiOut> + Layout<TuiOut> + Copy {
fn enabled (&self) -> bool;
fn enclose <W: Render<TuiOut>> (self, w: W) -> impl Render<TuiOut> {
fn enclose (self, w: impl Draw<TuiOut> + Layout<TuiOut>) -> impl Draw<TuiOut> + Layout<TuiOut> {
Bsp::b(Fill::xy(Border(self.enabled(), self)), w)
}
fn enclose2 <W: Render<TuiOut>> (self, w: W) -> impl Render<TuiOut> {
fn enclose2 (self, w: impl Draw<TuiOut> + Layout<TuiOut>) -> impl Draw<TuiOut> + Layout<TuiOut> {
Bsp::b(Margin::xy(1, 1, Fill::xy(Border(self.enabled(), self))), w)
}
fn enclose_bg <W: Render<TuiOut>> (self, w: W) -> impl Render<TuiOut> {
fn enclose_bg (self, w: impl Draw<TuiOut> + Layout<TuiOut>) -> impl Draw<TuiOut> + Layout<TuiOut> {
Tui::bg(self.style().unwrap().bg.unwrap_or(Color::Reset),
Bsp::b(Fill::xy(Border(self.enabled(), self)), w))
}
@ -147,9 +143,10 @@ macro_rules! border {
fn enabled (&self) -> bool { self.0 }
}
#[derive(Copy, Clone)] pub struct $T(pub bool, pub Style);
impl Render<TuiOut> for $T {
fn render (&self, to: &mut TuiOut) {
if self.enabled() { let _ = self.draw(to); }
impl Layout<TuiOut> for $T {}
impl Draw<TuiOut> for $T {
fn draw (&self, to: &mut TuiOut) {
if self.enabled() { let _ = BorderStyle::draw(self, to); }
}
}
)+}
@ -260,11 +257,3 @@ border! {
fn style (&self) -> Option<Style> { Some(self.1) }
}
}
//impl<S: BorderStyle, R: Content<TuiOut>> Content<TuiOut> for Bordered<S, R> {
//fn content (&self) -> impl Render<TuiOut> {
//let content: &dyn Content<TuiOut> = &self.1;
//lay! { content.padding_xy(1, 1), Border(self.0) }.fill_xy()
//}
//}

View file

@ -1 +1,38 @@
//use crate::{*, Color::*};
use crate::{*, Color::*};
pub fn button_2 <'a> (
key: impl Draw<TuiOut> + Layout<TuiOut> + 'a,
label: impl Draw<TuiOut> + Layout<TuiOut> + 'a,
editing: bool,
) -> impl Draw<TuiOut> + Layout<TuiOut> + 'a {
let key = Tui::fg_bg(Tui::orange(), Tui::g(0), Bsp::e(
Tui::fg(Tui::g(0), ""),
Bsp::e(key, Tui::fg(Tui::g(96), ""))
));
let label = When::new(!editing, Tui::fg_bg(Tui::g(255), Tui::g(96), label));
Tui::bold(true, Bsp::e(key, label))
}
pub fn button_3 <'a> (
key: impl Draw<TuiOut> + Layout<TuiOut> + 'a,
label: impl Draw<TuiOut> + Layout<TuiOut> + 'a,
value: impl Draw<TuiOut> + Layout<TuiOut> + 'a,
editing: bool,
) -> impl Draw<TuiOut> + Layout<TuiOut> + 'a {
let key = Tui::fg_bg(Tui::orange(), Tui::g(0),
Bsp::e(Tui::fg(Tui::g(0), ""), Bsp::e(key, Tui::fg(if editing {
Tui::g(128)
} else {
Tui::g(96)
}, ""))));
let label = Bsp::e(
When::new(!editing, Bsp::e(
Tui::fg_bg(Tui::g(255), Tui::g(96), label),
Tui::fg_bg(Tui::g(128), Tui::g(96), ""),
)),
Bsp::e(
Tui::fg_bg(Tui::g(224), Tui::g(128), value),
Tui::fg_bg(Tui::g(128), Reset, ""),
));
Tui::bold(true, Bsp::e(key, label))
}

View file

@ -2,26 +2,28 @@ use crate::*;
use ratatui::style::Stylize;
// Thunks can be natural error boundaries!
pub struct ErrorBoundary<O: Output, T: Render<O>>(
pub struct ErrorBoundary<O: Out, T: Draw<O>>(
std::marker::PhantomData<O>, Perhaps<T>
);
impl<O: Output, T: Render<O>> ErrorBoundary<O, T> {
impl<O: Out, T: Draw<O>> ErrorBoundary<O, T> {
pub fn new (content: Perhaps<T>) -> Self {
Self(Default::default(), content)
}
}
impl<T: Render<TuiOut>> Render<TuiOut> for ErrorBoundary<TuiOut, T> {
fn render (&self, to: &mut TuiOut) {
impl<T: Draw<TuiOut>> Draw<TuiOut> for ErrorBoundary<TuiOut, T> {
fn draw (&self, to: &mut TuiOut) {
match self.1.as_ref() {
Ok(Some(content)) => content.render(to),
Ok(Some(content)) => content.draw(to),
Ok(None) => to.blit(&"empty?", 0, 0, Some(Style::default().yellow())),
Err(e) => Tui::fg_bg(
Color::Rgb(255,224,244), Color::Rgb(96,24,24), Bsp::s(
Bsp::e(Tui::bold(true, "oops. "), "rendering failed."),
Bsp::e("\"why?\" ", Tui::bold(true, &format!("{e}"))))
).render(to)
Err(e) => {
let err_fg = Color::Rgb(255,224,244);
let err_bg = Color::Rgb(96,24,24);
let title = Bsp::e(Tui::bold(true, "oops. "), "rendering failed.");
let error = Bsp::e("\"why?\" ", Tui::bold(true, format!("{e}")));
to.place(&Tui::fg_bg(err_fg, err_bg, Bsp::s(title, error)))
}
}
}
}

View file

@ -1,30 +1,34 @@
use crate::*;
pub struct FieldH<T, U>(pub ItemTheme, pub T, pub U);
impl<T: Render<TuiOut>, U: Render<TuiOut>> Content<TuiOut> for FieldH<T, U> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
impl<
Label: Draw<TuiOut> + Layout<TuiOut>,
Value: Draw<TuiOut> + Layout<TuiOut>
> Content<TuiOut> for FieldH<ItemTheme, Label, Value> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
let Self(ItemTheme { darkest, dark, lightest, .. }, title, value) = self;
Some(row!(
row!(
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lightest.rgb, dark.rgb, title),
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lightest.rgb, darkest.rgb, Tui::bold(true, value)),
))
)
}
}
pub struct FieldV<T, U>(pub ItemTheme, pub T, pub U);
impl<T: Render<TuiOut>, U: Render<TuiOut>> Content<TuiOut> for FieldV<T, U> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
impl<
Label: Draw<TuiOut> + Layout<TuiOut>,
Value: Draw<TuiOut> + Layout<TuiOut>
> Content<TuiOut> for FieldV<ItemTheme, Label, Value> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
let Self(ItemTheme { darkest, dark, lightest, .. }, title, value) = self;
Some(Bsp::n(
Bsp::n(
Align::w(Tui::bg(darkest.rgb, Tui::fg(lightest.rgb, Tui::bold(true, value)))),
Fill::x(Align::w(row!(
Tui::bg(darkest.rgb, Tui::fg(dark.rgb, "")),
Tui::bg(dark.rgb, Tui::fg(lightest.rgb, title)),
Tui::bg(darkest.rgb, Tui::fg(dark.rgb, "")),
)))
))
)
}
}
@ -40,9 +44,9 @@ pub struct Field<T, U> {
pub value_bg: Option<ItemColor>,
pub value_align: Option<Direction>,
}
impl<T: Render<TuiOut>, U: Render<TuiOut>> Content<TuiOut> for Field<T, U> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
Some("TODO")
impl<T: Draw<TuiOut>, U: Draw<TuiOut>> Content<TuiOut> for Field<T, U> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
"TODO"
}
}

View file

@ -1,13 +1,13 @@
use crate::*;
impl Render<TuiOut> for u64 {
fn render (&self, _to: &mut TuiOut) {
impl Draw<TuiOut> for u64 {
fn draw (&self, _to: &mut TuiOut) {
todo!()
}
}
impl Render<TuiOut> for f64 {
fn render (&self, _to: &mut TuiOut) {
impl Draw<TuiOut> for f64 {
fn draw (&self, _to: &mut TuiOut) {
todo!()
}
}

View file

@ -8,28 +8,26 @@ pub struct Phat<T> {
pub content: T,
pub colors: [Color;4],
}
impl<T> Phat<T> {
pub const LO: &'static str = "";
pub const HI: &'static str = "";
/// A phat line
pub fn lo (fg: Color, bg: Color) -> impl Render<TuiOut> {
pub fn lo (fg: Color, bg: Color) -> impl Draw<TuiOut> + Layout<TuiOut> {
Fixed::y(1, Tui::fg_bg(fg, bg, RepeatH(Self::LO)))
}
/// A phat line
pub fn hi (fg: Color, bg: Color) -> impl Render<TuiOut> {
pub fn hi (fg: Color, bg: Color) -> impl Draw<TuiOut> + Layout<TuiOut> {
Fixed::y(1, Tui::fg_bg(fg, bg, RepeatH(Self::HI)))
}
}
impl<T: Render<TuiOut>> Content<TuiOut> for Phat<T> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
impl<T: Layout<TuiOut> + Draw<TuiOut>> Content<TuiOut> for Phat<T> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
let [fg, bg, hi, lo] = self.colors;
let top = Fixed::y(1, Self::lo(bg, hi));
let low = Fixed::y(1, Self::hi(bg, lo));
let content = Tui::fg_bg(fg, bg, &self.content);
Some(Min::xy(
self.width,
self.height,
Bsp::s(top, Bsp::n(low, Fill::xy(content)))
))
Min::xy(self.width, self.height, Bsp::s(top, Bsp::n(low, Fill::xy(content))))
}
}

View file

@ -2,11 +2,13 @@ use crate::*;
use ratatui::prelude::Position;
pub struct Repeat<'a>(pub &'a str);
impl Render<TuiOut> for Repeat<'_> {
impl Layout<TuiOut> for Repeat<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
}
fn render (&self, to: &mut TuiOut) {
}
impl Draw<TuiOut> for Repeat<'_> {
fn draw (&self, to: &mut TuiOut) {
let [x, y, w, h] = to.area().xywh();
let a = self.0.len();
for (_v, y) in (y..y+h).enumerate() {
@ -21,11 +23,13 @@ impl Render<TuiOut> for Repeat<'_> {
}
pub struct RepeatV<'a>(pub &'a str);
impl Render<TuiOut> for RepeatV<'_> {
impl Layout<TuiOut> for RepeatV<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
}
fn render (&self, to: &mut TuiOut) {
}
impl Draw<TuiOut> for RepeatV<'_> {
fn draw (&self, to: &mut TuiOut) {
let [x, y, _w, h] = to.area().xywh();
for y in y..y+h {
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y))) {
@ -36,11 +40,13 @@ impl Render<TuiOut> for RepeatV<'_> {
}
pub struct RepeatH<'a>(pub &'a str);
impl Render<TuiOut> for RepeatH<'_> {
impl Layout<TuiOut> for RepeatH<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
}
fn render (&self, to: &mut TuiOut) {
}
impl Draw<TuiOut> for RepeatH<'_> {
fn draw (&self, to: &mut TuiOut) {
let [x, y, w, _h] = to.area().xywh();
for x in x..x+w {
if let Some(cell) = to.buffer.cell_mut(Position::from((x, y))) {

View file

@ -23,8 +23,8 @@ impl ScrollbarH {
const ICON_INC: &[char] = &[' ', '🞂', ' '];
}
impl Render<TuiOut> for ScrollbarV {
fn render (&self, to: &mut TuiOut) {
impl Draw<TuiOut> for ScrollbarV {
fn draw (&self, to: &mut TuiOut) {
let [x, y1, _w, h] = to.area().xywh();
let y2 = y1 + h;
for (i, y) in (y1..=y2).enumerate() {
@ -51,8 +51,8 @@ impl Render<TuiOut> for ScrollbarV {
}
}
impl Render<TuiOut> for ScrollbarH {
fn render (&self, to: &mut TuiOut) {
impl Draw<TuiOut> for ScrollbarH {
fn draw (&self, to: &mut TuiOut) {
let [x1, y, w, _h] = to.area().xywh();
let x2 = x1 + w;
for (i, x) in (x1..=x2).enumerate() {

View file

@ -2,26 +2,17 @@ use crate::*;
use crate::ratatui::prelude::Position;
use unicode_width::{UnicodeWidthStr, UnicodeWidthChar};
impl_content_layout_render!(TuiOut: |self: &str, to|
layout = to.center_xy([width_chars_max(to.w(), self), 1]);
render = {let [x, y, w, ..] = Render::layout(self, to.area());
to.text(self, x, y, w)});
tui_layout!(|self: &str, to|to.center_xy([width_chars_max(to.w(), self), 1]));
tui_draw!(|self: &str, to|{
let [x, y, w, ..] = self.layout(to.area());
to.text(&self, x, y, w)
});
impl_content_layout_render!(TuiOut: |self: String, to|
layout = Render::<TuiOut>::layout(&self.as_str(), to);
render = Render::<TuiOut>::render(&self.as_str(), to));
tui_layout!(|self: Arc<str>, to|self.as_ref().layout(to));
tui_draw!(|self: Arc<str>, to|self.as_ref().draw(to));
impl_content_layout_render!(TuiOut: |self: Arc<str>, to|
layout = Render::<TuiOut>::layout(&self.as_ref(), to);
render = Render::<TuiOut>::render(&self.as_ref(), to));
impl_content_layout_render!(TuiOut: |self: std::sync::RwLock<String>, to|
layout = Render::<TuiOut>::layout(&self.read().unwrap(), to);
render = Render::<TuiOut>::render(&self.read().unwrap(), to));
impl_content_layout_render!(TuiOut: |self: std::sync::RwLockReadGuard<'_, String>, to|
layout = Render::<TuiOut>::layout(&**self, to);
render = Render::<TuiOut>::render(&**self, to));
tui_layout!(|self: String, to|self.as_str().layout(to));
tui_draw!(|self: String, to|self.as_str().draw(to));
fn width_chars_max (max: u16, text: impl AsRef<str>) -> u16 {
let mut width: u16 = 0;
@ -61,12 +52,14 @@ impl<'a, T: AsRef<str>> TrimString<T> {
TrimStringRef(self.0, &self.1)
}
}
impl<'a, T: AsRef<str>> Render<TuiOut> for TrimString<T> {
impl<'a, T: AsRef<str>> Layout<TuiOut> for TrimString<T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
Render::layout(&self.as_ref(), to)
Layout::layout(&self.as_ref(), to)
}
fn render (&self, to: &mut TuiOut) {
Render::render(&self.as_ref(), to)
}
impl<'a, T: AsRef<str>> Draw<TuiOut> for TrimString<T> {
fn draw (&self, to: &mut TuiOut) {
Draw::draw(&self.as_ref(), to)
}
}
@ -75,11 +68,13 @@ impl<'a, T: AsRef<str>> Render<TuiOut> for TrimString<T> {
/// Width is computed using [unicode_width].
pub struct TrimStringRef<'a, T: AsRef<str>>(pub u16, pub &'a T);
impl<T: AsRef<str>> Render<TuiOut> for TrimStringRef<'_, T> {
impl<T: AsRef<str>> Layout<TuiOut> for TrimStringRef<'_, T> {
fn layout (&self, to: [u16; 4]) -> [u16;4] {
[to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h()]
}
fn render (&self, target: &mut TuiOut) {
}
impl<T: AsRef<str>> Draw<TuiOut> for TrimStringRef<'_, T> {
fn draw (&self, target: &mut TuiOut) {
let area = target.area();
let mut width: u16 = 1;
let mut chars = self.1.as_ref().chars();

View file

@ -1,70 +1 @@
use crate::*;
pub trait TuiStyle {
fn fg <R: Render<TuiOut>> (color: Color, w: R) -> Foreground<R> {
Foreground(color, w)
}
fn bg <R: Render<TuiOut>> (color: Color, w: R) -> Background<R> {
Background(color, w)
}
fn fg_bg <R: Render<TuiOut>> (fg: Color, bg: Color, w: R) -> Background<Foreground<R>> {
Background(bg, Foreground(fg, w))
}
fn modify <R: Render<TuiOut>> (enable: bool, modifier: Modifier, w: R) -> Modify<R> {
Modify(enable, modifier, w)
}
fn bold <R: Render<TuiOut>> (enable: bool, w: R) -> Modify<R> {
Self::modify(enable, Modifier::BOLD, w)
}
fn border <R: Render<TuiOut>, S: BorderStyle> (enable: bool, style: S, w: R) -> Bordered<S, R> {
Bordered(enable, style, w)
}
}
impl TuiStyle for Tui {}
pub struct Foreground<R>(pub Color, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Foreground<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
let area = self.layout(to.area());
to.fill_fg(area, self.0);
to.place(area, &self.1);
}
}
pub struct Background<R>(pub Color, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Background<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
let area = self.layout(to.area());
to.fill_bg(area, self.0);
to.place(area, &self.1);
}
}
pub struct Modify<R: Render<TuiOut>>(pub bool, pub Modifier, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Modify<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.2.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.fill_mod(to.area(), self.0, self.1);
self.2.render(to)
}
}
pub struct Styled<R: Render<TuiOut>>(pub Option<Style>, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Styled<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.place(self.1.layout(to.area()), &self.1);
// TODO write style over area
}
}

View file

@ -1,40 +1,13 @@
use crate::*;
/// A three-column layout.
pub struct Tryptich<A, B, C> {
pub top: bool,
pub h: u16,
pub left: (u16, A),
pub middle: (u16, B),
pub right: (u16, C),
}
impl Tryptich<(), (), ()> {
pub fn center (h: u16) -> Self {
Self { h, top: false, left: (0, ()), middle: (0, ()), right: (0, ()) }
}
pub fn top (h: u16) -> Self {
Self { h, top: true, left: (0, ()), middle: (0, ()), right: (0, ()) }
}
}
impl<A, B, C> Tryptich<A, B, C> {
pub fn left <D> (self, w: u16, content: D) -> Tryptich<D, B, C> {
Tryptich { left: (w, content), ..self }
}
pub fn middle <D> (self, w: u16, content: D) -> Tryptich<A, D, C> {
Tryptich { middle: (w, content), ..self }
}
pub fn right <D> (self, w: u16, content: D) -> Tryptich<A, B, D> {
Tryptich { right: (w, content), ..self }
}
}
impl<A, B, C> Content<TuiOut> for Tryptich<A, B, C>
where A: Render<TuiOut>, B: Render<TuiOut>, C: Render<TuiOut> {
fn content (&self) -> Option<impl Render<TuiOut> + '_> {
impl<
A: Draw<TuiOut> + Layout<TuiOut>,
B: Draw<TuiOut> + Layout<TuiOut>,
C: Draw<TuiOut> + Layout<TuiOut>,
> Content<TuiOut> for Tryptich<A, B, C> {
fn content (&self) -> impl Draw<TuiOut> + Layout<TuiOut> + '_ {
let Self { top, h, left: (w_a, ref a), middle: (w_b, ref b), right: (w_c, ref c) } = *self;
Some(Fixed::y(h, if top {
Fixed::y(h, if top {
Bsp::a(
Fill::x(Align::n(Fixed::x(w_b, Align::x(Tui::bg(Color::Reset, b))))),
Bsp::a(
@ -50,7 +23,7 @@ where A: Render<TuiOut>, B: Render<TuiOut>, C: Render<TuiOut> {
Fill::xy(Align::e(Fixed::x(w_c, Tui::bg(Color::Reset, c)))),
),
)
}))
})
}
}

View file

@ -8,7 +8,7 @@ mod tui_output; pub use self::tui_output::*;
mod tui_perf; pub use self::tui_perf::*;
// The `Tui` struct (the *engine*) implements the
// `tengri_input::Input` and `tengri_output::Output` traits.
// `tengri_input::Input` and `tengri_output::Out` traits.
// At launch, the `Tui` engine spawns two threads, the render thread and the input thread.
// the application may further spawn other threads. All threads communicate using shared ownership:
@ -73,12 +73,12 @@ impl Tui {
}
}
pub trait TuiRun<R: Render<TuiOut> + Handle<TuiIn> + 'static> {
pub trait TuiRun<R: Draw<TuiOut> + Handle<TuiIn> + 'static> {
/// Run an app in the main loop.
fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>;
}
impl<T: Render<TuiOut> + Handle<TuiIn> + Send + Sync + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
impl<T: Draw<TuiOut> + Handle<TuiIn> + Send + Sync + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> {
let _input_thread = TuiIn::run_input(self, state, Duration::from_millis(100));
self.write().unwrap().setup()?;
@ -90,7 +90,7 @@ impl<T: Render<TuiOut> + Handle<TuiIn> + Send + Sync + 'static> TuiRun<T> for Ar
},
Err(error) => {
self.write().unwrap().teardown()?;
panic!("\n\rRender thread failed: {error:?}.\n\r")
panic!("\n\rDraw thread failed: {error:?}.\n\r")
},
}
Ok(())

View file

@ -2,12 +2,14 @@ use crate::*;
use std::time::Duration;
use std::thread::{spawn, JoinHandle};
use unicode_width::*;
#[derive(Default)]
pub struct TuiOut {
pub buffer: Buffer,
pub area: [u16;4]
}
impl Output for TuiOut {
impl Out for TuiOut {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
@ -17,16 +19,20 @@ impl Output for TuiOut {
#[inline] fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.area
}
#[inline] fn place <'t, T: Render<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
#[inline] fn place_at <'t, T: Draw<Self> + ?Sized> (&mut self, area: [u16;4], content: &'t T) {
let last = self.area();
*self.area_mut() = area;
content.render(self);
content.draw(self);
*self.area_mut() = last;
}
}
impl Layout<TuiOut> for fn(&mut TuiOut) {}
impl TuiOut {
/// Spawn the output thread.
pub fn run_output <T: Render<TuiOut> + Send + Sync + 'static> (
pub fn run_output <T: Draw<TuiOut> + Send + Sync + 'static> (
engine: &Arc<RwLock<Tui>>,
state: &Arc<RwLock<T>>,
timer: Duration
@ -52,7 +58,7 @@ impl TuiOut {
buffer.reset();
}
let mut output = TuiOut { buffer, area: [0, 0, width, height] };
state.render(&mut output);
state.draw(&mut output);
buffer = engine.write().unwrap().flip(output.buffer, size);
}
//let t1 = engine.read().unwrap().perf.get_t1(t0).unwrap();