refactor engine and layout into input and output

This commit is contained in:
🪞👃🪞 2025-01-07 21:30:07 +01:00
parent f052891473
commit 4d0f98acd2
40 changed files with 104 additions and 109 deletions

14
output/Cargo.lock generated Normal file
View file

@ -0,0 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "tek_engine"
version = "0.2.0"
[[package]]
name = "tek_layout"
version = "0.2.0"
dependencies = [
"tek_engine",
]

8
output/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "tek_output"
edition = "2021"
version = "0.2.0"
[dev-dependencies]
tek_tui = { path = "../tui" }
tek_engine = { path = "../engine" }

41
output/README.md Normal file
View file

@ -0,0 +1,41 @@
# `tek_layout`
this crate exposes several layout operators
which work entirely in unsigned coordinates
and are generic over `tek_engine::Engine`
and `tek_engine::Content`. chiefly, they
are not dependent on rendering framework.
|operator|description|
|-|-|
|**`When(x, a)`**|render `a` only when `x == true`|
|**`Either(x, a, b)`**|render `a` when `x == true`, otherwise render `b`|
|**`Map(get_iterator, callback)`**|transform items in uniform way|
|**`Bsp`**|concatenative layout|
|...|...|
|**`Align`**|pin content along axis|
|...|...|
|**`Fill`**|**make content's dimension equal to container's:**|
|`Fill::x(a)`|use container's width for content|
|`Fill::y(a)`|use container's height for content|
|`Fill::xy(a)`|use container's width and height for content|
|**`Fixed`**|**assign fixed dimension to content:**|
|`Fixed::x(w, a)`|use width `w` for content|
|`Fixed::y(w, a)`|use height `w` for content|
|`Fixed::xy(w, h, a)`|use width `w` and height `h` for content|
|**`Expand`/`Shrink`**|**change dimension of content:**|
|`Expand::x(n, a)`/`Shrink::x(n, a)`|increment/decrement width of content area by `n`|
|`Expand::y(n, a)`/`Shrink::y(n, a)`|increment/decrement height of content area by `m`|
|`Expand::xy(n, m, a)`/`Shrink::xy(n, m, a)`|increment/decrement width of content area by `n`, height by `m`|
|**`Min`/`Max`**|**constrain dimension of content:**|
|`Min::x(w, a)`/`Max::x(w, a)`|enforce minimum/maximum width `w` for content|
|`Min::y(h, a)`/`Max::y(h, a)`|enforce minimum/maximum height `h` for content|
|`Min::xy(w, h, a)`/`Max::xy(w, h, a)`|enforce minimum/maximum width `w` and height `h` for content|
|**`Push`/`Pull`**|**move content along axis:**|
|`Push::x(n, a)`/`Pull::x(n, a)`|increment/decrement `x` of content area|
|`Push::y(n, a)`/`Pull::y(n, a)`|increment/decrement `y` of content area|
|`Push::xy(n, m, a)`/`Pull::xy(n, m, a)`|increment/decrement `x` and `y` of content area|
**todo:**
* sensible `Margin`/`Padding`
* `Reduce`

54
output/src/align.rs Normal file
View file

@ -0,0 +1,54 @@
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<T>(Alignment, T);
impl<T> Align<T> {
pub fn c (a: T) -> Self { Self(Alignment::Center, a) }
pub fn x (a: T) -> Self { Self(Alignment::X, a) }
pub fn y (a: T) -> Self { Self(Alignment::Y, a) }
pub fn n (a: T) -> Self { Self(Alignment::N, a) }
pub fn s (a: T) -> Self { Self(Alignment::S, a) }
pub fn e (a: T) -> Self { Self(Alignment::E, a) }
pub fn w (a: T) -> Self { Self(Alignment::W, a) }
pub fn nw (a: T) -> Self { Self(Alignment::NW, a) }
pub fn sw (a: T) -> Self { Self(Alignment::SW, a) }
pub fn ne (a: T) -> Self { Self(Alignment::NE, a) }
pub fn se (a: T) -> Self { Self(Alignment::SE, a) }
}
impl<E: Output, T: Content<E>> Content<E> for Align<T> {
fn content (&self) -> impl Render<E> {
&self.1
}
fn layout (&self, on: E::Area) -> E::Area {
use Alignment::*;
let it = Render::layout(&self.content(), on).xywh();
let centered = on.center_xy(it.wh());
let far_x = (on.x() + on.w()).minus(it.w());
let far_y = (on.y() + on.h()).minus(it.h());
let [x, y] = match self.0 {
NW => [on.x(), on.y()],
N => [centered.x(), on.y()],
NE => [far_x, on.y()],
E => [far_x, centered.y()],
SE => [far_x, far_y ],
S => [centered.x(), far_y ],
SW => [on.x(), far_y ],
W => [on.x(), centered.y()],
Center => centered.xy(),
X => [centered.x(), it.y()],
Y => [it.x(), centered.y()],
};
[x, y, centered.w(), centered.h()].into()
}
fn render (&self, render: &mut E) {
let content = &self.content();
let it = Render::layout(content, render.area()).xywh();
render.place(it.into(), content)
}
}

92
output/src/area.rs Normal file
View file

@ -0,0 +1,92 @@
use crate::*;
use std::fmt::Debug;
pub trait Area<N: Coordinate>: From<[N;4]> + Debug + Copy {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N;
fn h (&self) -> N;
#[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
if self.w() < w || self.h() < h {
Err(format!("min {w}x{h}").into())
} else {
Ok(self)
}
}
#[inline] fn xy (&self) -> [N;2] {
[self.x(), self.y()]
}
#[inline] fn wh (&self) -> [N;2] {
[self.w(), self.h()]
}
#[inline] fn xywh (&self) -> [N;4] {
[self.x(), self.y(), self.w(), self.h()]
}
#[inline] fn clip_h (&self, h: N) -> [N;4] {
[self.x(), self.y(), self.w(), self.h().min(h)]
}
#[inline] fn clip_w (&self, w: N) -> [N;4] {
[self.x(), self.y(), self.w().min(w), self.h()]
}
#[inline] fn clip (&self, wh: impl Size<N>) -> [N;4] {
[self.x(), self.y(), wh.w(), wh.h()]
}
#[inline] fn set_w (&self, w: N) -> [N;4] {
[self.x(), self.y(), w, self.h()]
}
#[inline] fn set_h (&self, h: N) -> [N;4] {
[self.x(), self.y(), self.w(), h]
}
#[inline] fn x2 (&self) -> N {
self.x() + self.w()
}
#[inline] fn y2 (&self) -> N {
self.y() + self.h()
}
#[inline] fn lrtb (&self) -> [N;4] {
[self.x(), self.x2(), self.y(), self.y2()]
}
#[inline] fn center (&self) -> [N;2] {
[self.x() + self.w()/2.into(), self.y() + self.h()/2.into()]
}
#[inline] fn center_x (&self, n: N) -> [N;4] {
let [x, y, w, h] = self.xywh();
[(x + w / 2.into()).minus(n / 2.into()), y + h / 2.into(), n, 1.into()]
}
#[inline] fn center_y (&self, m: N) -> [N;4] {
let [x, y, w, h] = self.xywh();
[x + w / 2.into(), (y + h / 2.into()).minus(m / 2.into()), 1.into(), m]
}
#[inline] fn center_xy (&self, [n, m]: [N;2]) -> [N;4] {
let [x, y, w, h] = self.xywh();
[(x + w / 2.into()).minus(n / 2.into()), (y + h / 2.into()).minus(m / 2.into()), n, m]
}
#[inline] fn centered (&self) -> [N;2] {
[self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())]
}
#[inline] fn zero () -> [N;4] {
[N::zero(), N::zero(), N::zero(), N::zero()]
}
#[inline] fn from_position (pos: impl Size<N>) -> [N;4] {
let [x, y] = pos.wh();
[x, y, 0.into(), 0.into()]
}
#[inline] fn from_size (size: impl Size<N>) -> [N;4] {
let [w, h] = size.wh();
[0.into(), 0.into(), w, h]
}
}
impl<N: Coordinate> Area<N> for (N, N, N, N) {
#[inline] fn x (&self) -> N { self.0 }
#[inline] fn y (&self) -> N { self.1 }
#[inline] fn w (&self) -> N { self.2 }
#[inline] fn h (&self) -> N { self.3 }
}
impl<N: Coordinate> Area<N> for [N;4] {
#[inline] fn x (&self) -> N { self[0] }
#[inline] fn y (&self) -> N { self[1] }
#[inline] fn w (&self) -> N { self[2] }
#[inline] fn h (&self) -> N { self[3] }
}

23
output/src/collection.rs Normal file
View file

@ -0,0 +1,23 @@
//! Groupings of elements.
use crate::*;
/// A function or closure that emits renderables.
pub trait Collector<E: Engine>: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
/// Any function or closure that emits renderables for the given engine matches [CollectCallback].
impl<E, F> Collector<E> for F
where E: Engine, F: Send + Sync + Fn(&mut dyn FnMut(&dyn Render<E>)) {}
pub trait Render<E: Engine> {
fn area (&self, to: E::Area) -> E::Area;
fn render (&self, to: &mut E::Output);
}
impl<E: Engine, C: Content<E>> Render<E> for C {
fn area (&self, to: E::Area) -> E::Area {
Content::area(self, to)
}
fn render (&self, to: &mut E::Output) {
Content::render(self, to)
}
}

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

@ -0,0 +1,46 @@
use crate::*;
/// Build a [Render]able out of other [Render]ables,
/// then apply optional custom render/layout on top.
pub trait Content<E: Output>: Send + Sync + Sized {
fn content (&self) -> impl Render<E> { () }
fn layout (&self, area: E::Area) -> E::Area { self.content().layout(area) }
fn render (&self, output: &mut E) { self.content().render(output) }
}
impl<E: Output, C: Content<E>> Content<E> for &C {
fn content (&self) -> impl Render<E> { (*self).content() }
fn layout (&self, area: E::Area) -> E::Area { (*self).layout(area) }
fn render (&self, output: &mut E) { (*self).render(output) }
}
/// The platonic ideal unit of [Content]: total emptiness at dead center.
impl<E: Output> Content<E> for () {
fn layout (&self, area: E::Area) -> E::Area { area.center().to_area_pos().into() }
fn render (&self, _: &mut E) {}
}
impl<E: Output, T: Content<E>> Content<E> for Option<T> {
fn content (&self) -> impl Render<E> {
self.as_ref()
}
fn layout (&self, area: E::Area) -> E::Area {
self.as_ref()
.map(|content|content.layout(area))
.unwrap_or([0.into(), 0.into(), 0.into(), 0.into(),].into())
}
fn render (&self, output: &mut E) {
self.as_ref()
.map(|content|content.render(output));
}
}
//impl<E: Output, T: Content<E>, E: Content<E>> Content<E> for Option<T> {
//fn content (&self) -> impl Render<E> {
//self.as_ref()
//}
//fn layout (&self, area: E::Area) -> E::Area {
//self.as_ref()
//.map(|content|content.layout(area))
//.unwrap_or([0.into(), 0.into(), 0.into(), 0.into(),].into())
//}
//fn render (&self, output: &mut E) {
//self.as_ref()
//.map(|content|content.render(output));
//}
//}

28
output/src/coordinate.rs Normal file
View file

@ -0,0 +1,28 @@
use std::fmt::{Debug, Display};
use std::ops::{Add, Sub, Mul, Div};
impl Coordinate for u16 {}
/// A linear coordinate.
pub trait Coordinate: Send + Sync + Copy
+ Add<Self, Output=Self>
+ Sub<Self, Output=Self>
+ Mul<Self, Output=Self>
+ Div<Self, Output=Self>
+ Ord + PartialEq + Eq
+ Debug + Display + Default
+ From<u16> + Into<u16>
+ Into<usize>
+ Into<f64>
{
#[inline] fn minus (self, other: Self) -> Self {
if self >= other {
self - other
} else {
0.into()
}
}
#[inline] fn zero () -> Self {
0.into()
}
}

121
output/src/direction.rs Normal file
View file

@ -0,0 +1,121 @@
use crate::*;
pub use self::Direction::*;
/// A cardinal direction.
#[derive(Copy, Clone, PartialEq)]
pub enum Direction { North, South, East, West, Above, Below }
impl Direction {
pub fn split_fixed <N: Coordinate> (self, area: impl Area<N>, a: N) -> ([N;4],[N;4]) {
let [x, y, w, h] = area.xywh();
match self {
North => ([x, (y+h).minus(a), w, a], [x, y, w, h.minus(a)]),
South => ([x, y, w, a], [x, y + a, w, h.minus(a)]),
East => ([x, y, a, h], [x + a, y, w.minus(a), h]),
West => ([(x+w).minus(a), y, a, h], [x, y, w - a, h]),
Above | Below => (area.xywh(), area.xywh())
}
}
}
pub struct Bsp<X, Y>(Direction, X, Y);
impl<E: Output, A: Content<E>, B: Content<E>> Content<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<A, B> Bsp<A, B> {
pub fn n (a: A, b: B) -> Self { Self(North, a, b) }
pub fn s (a: A, b: B) -> Self { Self(South, a, b) }
pub fn e (a: A, b: B) -> Self { Self(East, a, b) }
pub fn w (a: A, b: B) -> Self { Self(West, a, b) }
pub fn a (a: A, b: B) -> Self { Self(Above, a, b) }
pub fn b (a: A, b: B) -> Self { Self(Below, a, b) }
}
pub trait BspAreas<E: Output, A: Content<E>, B: Content<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 [ax, ay, aw, ah] = a.layout(outer).xywh();
let [bx, by, 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(),
}).xywh();
match direction {
Above | Below => {
let x = ax.min(bx);
let w = (ax+aw).max(bx+bw).minus(x);
let y = ay.min(by);
let h = (ay+ah).max(by+bh).minus(y);
[[ax, ay, aw, ah].into(), [bx, by, bw, bh].into(), [x, y, w, h].into()]
},
South => {
let [x, y, w, h] = outer.center_xy([aw.max(bw), ah + bh]);
let a = [x, y, aw, ah];
let b = [x, 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, y + bh, aw, ah];
let b = [x, 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, aw, ah];
let b = [x + aw, y, 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, aw, ah];
let b = [x, y, bw, bh];
[a.into(), b.into(), [x, y, w, h].into()]
},
}
}
}
impl<E: Output, A: Content<E>, B: Content<E>> BspAreas<E, A, B> for Bsp<A, B> {
fn direction (&self) -> Direction { self. 0 }
fn contents (&self) -> (&A, &B) { (&self.1, &self.2) }
}
/// Renders multiple 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 }};
}

16
output/src/either.rs Normal file
View file

@ -0,0 +1,16 @@
use crate::*;
/// 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<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
fn layout (&self, to: E::Area) -> E::Area {
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) }
}
}

82
output/src/lib.rs Normal file
View file

@ -0,0 +1,82 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
mod coordinate; pub use self::coordinate::*;
mod size; pub use self::size::*;
mod area; pub use self::area::*;
mod output; pub use self::output::*;
mod content; pub use self::content::*;
mod render; pub use self::render::*;
mod thunk; pub use self::thunk::*;
mod when; pub use self::when::*;
mod either; pub use self::either::*;
mod map; pub use self::map::*;
mod reduce; pub use self::reduce::*;
mod align; pub use self::align::*;
mod direction; pub use self::direction::*;
mod measure; pub use self::measure::*;
mod transform_xy; pub use self::transform_xy::*;
mod transform_xy_unit; pub use self::transform_xy_unit::*;
pub(crate) use std::marker::PhantomData;
pub(crate) use std::error::Error;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
/// Prototypal case of implementor macro.
/// Saves 4loc per data pats.
#[macro_export] macro_rules! from {
($(<$($lt:lifetime),+>)?|$state:ident:$Source:ty|$Target:ty=$cb:expr) => {
impl $(<$($lt),+>)? From<$Source> for $Target {
fn from ($state:$Source) -> Self { $cb }
}
};
}
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
use ::tek_tui::Tui;
let area: [u16;4] = [10, 10, 20, 20];
let unit = ();
assert_eq!(Content::<TuiOut>::layout(&unit, area), [20, 20, 0, 0]);
assert_eq!(Fill::<TuiOut>::x(unit).layout(area), [10, 20, 20, 0]);
assert_eq!(Fill::<TuiOut>::y(unit).layout(area), [20, 10, 0, 20]);
assert_eq!(Fill::<TuiOut>::xy(unit).layout(area), area);
assert_eq!(Fixed::<TuiOut, u16>::x(4, unit).layout(area), [18, 20, 4, 0]);
assert_eq!(Fixed::<TuiOut, u16>::y(4, unit).layout(area), [20, 18, 0, 4]);
assert_eq!(Fixed::<TuiOut, u16>::xy(4, 4, unit).layout(area), [18, 18, 4, 4]);
let four = ||Fixed::<TuiOut>::xy(4, 4, unit);
assert_eq!(Align::nw(four()).layout(area), [10, 10, 4, 4]);
assert_eq!(Align::n(four()).layout(area), [18, 10, 4, 4]);
assert_eq!(Align::ne(four()).layout(area), [26, 10, 4, 4]);
assert_eq!(Align::e(four()).layout(area), [26, 18, 4, 4]);
assert_eq!(Align::se(four()).layout(area), [26, 26, 4, 4]);
assert_eq!(Align::s(four()).layout(area), [18, 26, 4, 4]);
assert_eq!(Align::sw(four()).layout(area), [10, 26, 4, 4]);
assert_eq!(Align::w(four()).layout(area), [10, 18, 4, 4]);
let two_by_four = ||Fixed::<TuiOut>::xy(4, 2, unit);
assert_eq!(Align::nw(two_by_four()).layout(area), [10, 10, 4, 2]);
assert_eq!(Align::n(two_by_four()).layout(area), [18, 10, 4, 2]);
assert_eq!(Align::ne(two_by_four()).layout(area), [26, 10, 4, 2]);
assert_eq!(Align::e(two_by_four()).layout(area), [26, 19, 4, 2]);
assert_eq!(Align::se(two_by_four()).layout(area), [26, 28, 4, 2]);
assert_eq!(Align::s(two_by_four()).layout(area), [18, 28, 4, 2]);
assert_eq!(Align::sw(two_by_four()).layout(area), [10, 28, 4, 2]);
assert_eq!(Align::w(two_by_four()).layout(area), [10, 19, 4, 2]);
Ok(())
}

71
output/src/map.rs Normal file
View file

@ -0,0 +1,71 @@
use crate::*;
pub fn map_south<O: Output>(
item_offset: O::Unit,
item_height: O::Unit,
item: impl Content<O>
) -> impl Content<O> {
Push::y(item_offset, Align::n(Fixed::y(item_height, Fill::x(item))))
}
pub fn map_east<O: Output>(
item_offset: O::Unit,
item_width: O::Unit,
item: impl Content<O>
) -> impl Content<O> {
Push::x(item_offset, Align::w(Fixed::x(item_width, Fill::y(item))))
}
pub struct Map<'a, A, B, I, F, G>(pub PhantomData<&'a()>, pub F, pub G) where
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync;
impl<'a, A, B, I, F, G> Map<'a, A, B, I, F, G> where
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
pub fn new (f: F, g: G) -> Self {
Self(Default::default(), f, g)
}
}
impl<'a, E, A, B, I, F, G> Content<E> for Map<'a, A, B, I, F, G> where
E: Output,
B: Render<E>,
I: Iterator<Item = A> + Send + Sync,
F: Fn() -> I + Send + Sync + 'a,
G: Fn(A, usize)->B + Send + Sync
{
fn layout (&self, area: E::Area) -> E::Area {
let Self(_, get_iterator, callback) = self;
let mut index = 0;
let [mut min_x, mut min_y] = area.center();
let [mut max_x, mut max_y] = area.center();
for item in get_iterator() {
let area = callback(item, index).layout(area).xywh();
let [x,y,w,h] = area.xywh();
min_x = min_x.min(x.into());
min_y = min_y.min(y.into());
max_x = max_x.max((x + w).into());
max_y = max_y.max((y + h).into());
index += 1;
}
let w = max_x - min_x;
let h = max_y - min_y;
//[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) {
let Self(_, get_iterator, callback) = self;
let mut index = 0;
//let area = self.layout(to.area());
for item in get_iterator() {
let item = callback(item, index);
//to.place(area.into(), &item);
to.place(item.layout(to.area()), &item);
index += 1;
}
}
}

125
output/src/measure.rs Normal file
View file

@ -0,0 +1,125 @@
use crate::*;
use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}};
//use ratatui::prelude::{Style, Color};
// TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small
pub trait HasSize<E: Output> {
fn size (&self) -> &Measure<E>;
}
#[macro_export] macro_rules! has_size {
(<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => {
impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? {
fn size (&$self) -> &Measure<$E> { $cb }
}
}
}
/// A widget that tracks its render width and height
#[derive(Default)]
pub struct Measure<E: Output> {
_engine: PhantomData<E>,
pub x: Arc<AtomicUsize>,
pub y: Arc<AtomicUsize>,
}
impl<E: Output> Content<E> for Measure<E> {
fn render (&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> {
fn clone (&self) -> Self {
Self {
_engine: Default::default(),
x: self.x.clone(),
y: self.y.clone(),
}
}
}
impl<E: Output> 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)
.field("height", &self.y)
.finish()
}
}
impl<E: Output> Measure<E> {
pub fn w (&self) -> usize { self.x.load(Relaxed) }
pub fn h (&self) -> usize { self.y.load(Relaxed) }
pub fn wh (&self) -> [usize;2] { [self.w(), self.h()] }
pub fn set_w (&self, w: impl Into<usize>) { self.x.store(w.into(), Relaxed) }
pub fn set_h (&self, h: impl Into<usize>) { self.y.store(h.into(), Relaxed) }
pub fn set_wh (&self, w: impl Into<usize>, h: impl Into<usize>) { self.set_w(w); self.set_h(h); }
pub fn format (&self) -> String { format!("{}x{}", self.w(), self.h()) }
pub fn new () -> Self {
Self {
_engine: PhantomData::default(),
x: Arc::new(0.into()),
y: Arc::new(0.into()),
}
}
pub fn of <T: Content<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> {
Bsp::b(Fill::xy(&self), item)
}
}
///// A scrollable area.
//pub struct Scroll<E, F>(pub F, pub Direction, pub u64, PhantomData<E>)
//where
//E: Output,
//F: Send + Sync + Fn(&mut dyn FnMut(&dyn Content<E>)->Usually<()>)->Usually<()>;
//pub trait ContentDebug<E: Output> {
//fn debug <W: Content<E>> (other: W) -> DebugOverlay<E, W> {
//DebugOverlay(Default::default(), other)
//}
//}
//impl<E: Output> ContentDebug<E> for E {}
//impl Render<TuiOut> for Measure<TuiOut> {
//fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> {
//Ok(Some([0u16.into(), 0u16.into()].into()))
//}
//fn render (&self, to: &mut TuiOut) -> Usually<()> {
//self.set_w(to.area().w());
//self.set_h(to.area().h());
//Ok(())
//}
//}
//impl Measure<TuiOut> {
//pub fn debug (&self) -> ShowMeasure {
//ShowMeasure(&self)
//}
//}
//render!(Tui: |self: ShowMeasure<'a>|render(|to: &mut TuiOut|Ok({
//let w = self.0.w();
//let h = self.0.h();
//to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some(
//Style::default().bold().italic().bg(Color::Rgb(255, 0, 255)).fg(Color::Rgb(0,0,0))
//))
//})));
//pub struct ShowMeasure<'a>(&'a Measure<TuiOut>);
//pub struct DebugOverlay<E: Output, W: Render<E>>(PhantomData<E>, pub W);
//impl<T: Render<TuiOut>> Render<TuiOut> for DebugOverlay<Tui, T> {
//fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> {
//self.1.min_size(to)
//}
//fn render (&self, to: &mut TuiOut) -> Usually<()> {
//let [x, y, w, h] = to.area();
//self.1.render(to)?;
//Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green())))
//}
//}

22
output/src/output.rs Normal file
View file

@ -0,0 +1,22 @@
use crate::*;
/// Render target
pub trait Output: Send + Sync + Sized {
/// Unit of length
type Unit: Coordinate;
/// Rectangle without offset
type Size: Size<Self::Unit>;
/// Rectangle with offset
type Area: Area<Self::Unit>;
/// Current output area
fn area (&self) -> Self::Area;
/// Mutable pointer to area
fn area_mut (&mut self) -> &mut Self::Area;
/// Render widget in area
fn place (&mut self, area: Self::Area, content: &impl Render<Self>);
#[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() }
}

93
output/src/reduce.rs Normal file
View file

@ -0,0 +1,93 @@
use crate::*;
pub struct Reduce<A, B, I, F, G>(pub PhantomData<A>, pub F, pub G) where
A: Send + Sync, B: Send + Sync,
I: Iterator<Item = B> + Send + Sync,
F: Fn() -> I + Send + Sync,
G: Fn(A, B, usize)->A + Send + Sync;
impl<A, B, I, F, G> Reduce<A, B, I, F, G> where
A: Send + Sync, B: Send + Sync,
I: Iterator<Item = B> + Send + Sync,
F: Fn() -> I + Send + Sync,
G: Fn(A, B, usize)->A + Send + Sync
{
pub fn new (f: F, g: G) -> Self { Self(Default::default(), f, g) }
}
impl<E: Output, A, B, I, F, G> Content<E> for Reduce<A, B, I, F, G> where
A: Send + Sync, B: Send + Sync,
I: Iterator<Item = B> + Send + Sync,
F: Fn() -> I + Send + Sync,
G: Fn(A, B, usize)->A + Send + Sync
{
fn content (&self) -> impl Render<E> {
}
}
/*
//pub fn reduce <E, T, I, R, F>(iterator: I, callback: F) -> Reduce<E, T, I, R, F> where
//E: Output,
//I: Iterator<Item = T> + Send + Sync,
//R: Render<E>,
//F: Fn(R, T, usize) -> R + Send + Sync
//{
//Reduce(Default::default(), iterator, callback)
//}
pub struct Reduce<E, T, I, R, F>(PhantomData<(E, R)>, I, F) where
E: Output,
I: Iterator<Item = T> + Send + Sync,
R: Render<E>,
F: Fn(R, T, usize) -> R + Send + Sync;
impl<E, T, I, R, F> Content<E> for Reduce<E, T, I, R, F> where
E: Output,
I: Iterator<Item = T> + Send + Sync,
R: Render<E>,
F: Fn(R, T, usize) -> R + Send + Sync
{
fn render (&self, to: &mut E) {
todo!()
}
}
*/
//macro_rules! define_ops {
//($Trait:ident<$E:ident:$Output:path> { $(
//$(#[$attr:meta $($attr_args:tt)*])*
//(
//$fn:ident
//$(<$($G:ident$(:$Gen:path)?, )+>)?
//$Op:ident
//($($arg:ident:$Arg:ty),*)
//)
//)* }) => {
//impl<$E: $Output> $Trait<E> for E {}
//pub trait $Trait<$E: $Output> {
//$(
//$(#[$attr $($attr_args)*])*
//fn $fn $(<$($G),+>)?
//($($arg:$Arg),*)-> $Op<$($(, $G)+)?>
//$(where $($G: $($Gen + Send + Sync)?),+)?
//{ $Op($($arg),*) }
//)*
//}
//}
//}
//define_ops! {
//Layout<E: Output> {
//(when <A: Render<E>,>
//When(cond: bool, item: A))
///// When `cond` is `true`, render `a`, otherwise render `b`.
//(either <A: Render<E>, B: Render<E>,>
//Either(cond: bool, a: A, b: B))
///// If `opt` is `Some(T)` renders `cb(t)`, otherwise nothing.
//(opt <A, F: Fn(A) -> B, B: Render<E>,>
//Opt(option: Option<A>, cb: F))
///// Maps items of iterator through callback.
//(map <A, B: Render<E>, I: Iterator<Item = A>, F: Fn() -> I, G: Fn(A, usize)->B,>
//Map(get_iterator: F, callback: G))
//}
//}

66
output/src/render.rs Normal file
View file

@ -0,0 +1,66 @@
use crate::*;
use std::ops::Deref;
/// Custom layout and rendering.
pub trait Render<E: Output>: Send + Sync {
fn layout (&self, area: E::Area) -> E::Area;
fn render (&self, output: &mut E);
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Sized + 'a {
Box::new(self) as RenderBox<'a, E>
}
}
pub type RenderDyn<'a, Output> = dyn Render<Output> + 'a;
impl<'a, E: Output> Content<E> for &RenderDyn<'a, E> where Self: Sized {
fn content (&self) -> impl Render<E> { self.deref() }
fn layout (&self, area: E::Area) -> E::Area { Render::layout(self.deref(), area) }
fn render (&self, output: &mut E) { Render::render(self.deref(), output) }
}
pub type RenderBox<'a, E: Output> = Box<RenderDyn<'a, E>>;
impl<'a, E: Output> Content<E> for RenderBox<'a, E> {
fn content (&self) -> impl Render<E> { self.deref() }
//fn boxed <'b> (self) -> RenderBox<'b, E> where Self: Sized + 'b { self }
}
impl<E: Output, C: Content<E>> Render<E> for C {
fn layout (&self, area: E::Area) -> E::Area { Content::layout(self, area) }
fn render (&self, output: &mut E) { Content::render(self, output) }
}
#[macro_export] macro_rules! render {
(($self:ident:$Struct:ty) => $content:expr) => {
impl <E: Output> Content<E> for $Struct {
fn content (&$self) -> impl Render<E> { Some($content) }
}
};
(|$self:ident:$Struct:ident $(<
$($L:lifetime),* $($T:ident $(:$Trait:path)?),*
>)?, $to:ident | $render:expr) => {
impl <$($($L),*)? E: Output, $($T$(:$Trait)?),*> Content<E>
for $Struct $(<$($L),* $($T),*>>)? {
fn render (&$self, $to: &mut E) { $render }
}
};
($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 }
}
};
($Output:ty:
|$self:ident : $Struct:ident $(<$(
$($L:lifetime)? $($T:ident)? $(:$Trait:path)?
),+>)?, $to:ident| $render:expr
) => {
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
for $Struct $(<$($($L)? $($T)?),+>)? {
fn render (&$self, $to: &mut $Output) { $render }
}
};
}

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

@ -0,0 +1,40 @@
use crate::*;
use std::fmt::Debug;
pub trait Size<N: Coordinate>: From<[N;2]> + Debug + Copy {
fn x (&self) -> N;
fn y (&self) -> N;
#[inline] fn w (&self) -> N { self.x() }
#[inline] fn h (&self) -> N { self.y() }
#[inline] fn wh (&self) -> [N;2] { [self.x(), self.y()] }
#[inline] fn clip_w (&self, w: N) -> [N;2] { [self.w().min(w), self.h()] }
#[inline] fn clip_h (&self, h: N) -> [N;2] { [self.w(), self.h().min(h)] }
#[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
if self.w() < w || self.h() < h {
Err(format!("min {w}x{h}").into())
} else {
Ok(self)
}
}
#[inline] fn zero () -> [N;2] {
[N::zero(), N::zero()]
}
#[inline] fn to_area_pos (&self) -> [N;4] {
let [x, y] = self.wh();
[x, y, 0.into(), 0.into()]
}
#[inline] fn to_area_size (&self) -> [N;4] {
let [w, h] = self.wh();
[0.into(), 0.into(), w, h]
}
}
impl<N: Coordinate> Size<N> for (N, N) {
#[inline] fn x (&self) -> N { self.0 }
#[inline] fn y (&self) -> N { self.1 }
}
impl<N: Coordinate> Size<N> for [N;2] {
#[inline] fn x (&self) -> N { self[0] }
#[inline] fn y (&self) -> N { self[1] }
}

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

@ -0,0 +1,50 @@
use crate::*;
use std::marker::PhantomData;
/// Lazily-evaluated [Render]able.
pub struct Thunk<E: Output, T: Render<E>, F: Fn()->T + Send + Sync>(PhantomData<E>, F);
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Thunk<E, T, F> {
pub fn new (thunk: F) -> Self {
Self(Default::default(), thunk)
}
}
impl<E: Output, T: Render<E>, F: Fn()->T + Send + Sync> Content<E> for Thunk<E, T, F> {
fn content (&self) -> impl Render<E> { (self.1)() }
}
pub struct BoxThunk<'a, E: Output>(PhantomData<E>, Box<dyn Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a>);
impl<'a, E: Output> BoxThunk<'a, E> {
pub fn new (thunk: Box<dyn Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a>) -> Self {
Self(Default::default(), thunk)
}
}
impl<'a, E: Output> Content<E> for BoxThunk<'a, E> {
fn content (&self) -> impl Render<E> { (self.1)() }
}
impl<'a, E: Output, F: Fn()->T + Send + Sync + 'a, T: Render<E> + Send + Sync + 'a> From<F> for BoxThunk<'a, E> {
fn from (f: F) -> Self {
Self(Default::default(), Box::new(move||f().boxed()))
}
}
//impl<'a, E: Output, F: Fn()->Box<dyn Render<E> + 'a> + Send + Sync + 'a> From<F> for BoxThunk<'a, E> {
//fn from (f: F) -> Self {
//Self(Default::default(), Box::new(f))
//}
//}
pub struct RenderThunk<E: Output, F: Fn(&mut E) + Send + Sync>(PhantomData<E>, F);
impl<E: Output, F: Fn(&mut E) + Send + Sync> RenderThunk<E, F> {
pub fn new (render: F) -> Self { Self(Default::default(), render) }
}
impl<E: Output, F: Fn(&mut E) + Send + Sync> Content<E> for RenderThunk<E, F> {
fn render (&self, to: &mut E) { (self.1)(to) }
}
pub struct LayoutThunk<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync>(PhantomData<E>, F1, F2);
impl<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> LayoutThunk<E, F1, F2> {
pub fn new (layout: F1, render: F2) -> Self { Self(Default::default(), layout, render) }
}
impl<E: Output, F1: Fn(E::Area)->E::Area + Send + Sync, F2: Fn(&mut E) + Send + Sync> Content<E> for LayoutThunk<E, F1, F2> {
fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) }
fn render (&self, to: &mut E) { (self.2)(to) }
}

View file

@ -0,0 +1,40 @@
use crate::*;
/// Defines an enum that transforms its content
/// along either the X axis, the Y axis, or both.
///
/// The `_Unused` variant wraps the `Output` type
/// using `PhantomData` to permit the double generic.
macro_rules! transform_xy {
($self:ident : $Enum:ident |$to:ident|$area:expr) => {
pub enum $Enum<T> { X(T), Y(T), XY(T) }
impl<T> $Enum<T> {
pub fn x (item: T) -> Self { Self::X(item) }
pub fn y (item: T) -> Self { Self::Y(item) }
pub fn xy (item: T) -> Self { Self::XY(item) }
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<T> {
fn content (&self) -> impl Render<E> {
match self {
Self::X(item) => item,
Self::Y(item) => item,
Self::XY(item) => item
}
}
fn layout (&$self, $to: <E as Output>::Area) -> <E as Output>::Area {
use $Enum::*;
$area
}
}
}
}
transform_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()
});

View file

@ -0,0 +1,96 @@
use crate::*;
/// Defines an enum that parametrically transforms its content
/// along either the X axis, the Y axis, or both.
macro_rules! transform_xy_unit {
(|$self:ident : $Enum:ident, $to:ident|$layout:expr) => {
pub enum $Enum<U, T> { X(U, T), Y(U, T), XY(U, U, T), }
impl<U, T> $Enum<U, T> {
pub fn x (x: U, item: T) -> Self { Self::X(x, item) }
pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
}
impl<U: Copy + Coordinate, T> $Enum<U, T> {
pub fn dx (&self) -> U {
match self {
Self::X(x, _) => *x,
Self::Y(_, _) => 0.into(),
Self::XY(x, _, _) => *x,
}
}
pub fn dy (&self) -> U {
match self {
Self::X(_, _) => 0.into(),
Self::Y(y, _) => *y,
Self::XY(_, y, _) => *y,
}
}
}
impl<E: Output, T: Content<E>> Content<E> for $Enum<E::Unit, T> {
fn content (&self) -> impl Render<E> {
Some(match self {
Self::X(_, content) => content,
Self::Y(_, content) => content,
Self::XY(_, _, content) => content,
})
}
fn layout (&$self, $to: E::Area) -> E::Area {
$layout.into()
}
}
}
}
transform_xy_unit!(|self: Fixed, area|{
let [w, h] = Render::layout(&self.content(), area.center_xy(match self {
Self::X(fw, _) => [*fw, area.h()],
Self::Y(fh, _) => [area.w(), *fh],
Self::XY(fw, fh, _) => [*fw, *fh],
}).into()).wh();
area.center_xy(match self {
Self::X(fw, _) => [*fw, h],
Self::Y(fh, _) => [w, *fh],
Self::XY(fw, fh, _) => [*fw, *fh],
})
});
transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())
].into()));
transform_xy_unit!(|self: Expand, area|Render::layout(&self.content(), [
area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()
].into()));
transform_xy_unit!(|self: Min, area|{
let area = Render::layout(&self.content(), area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().max(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)]
}});
transform_xy_unit!(|self: Max, area|{
let area = Render::layout(&self.content(), area);
match self {
Self::X(mw, _) => [area.x(), area.y(), area.w().min(*mw), area.h()],
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().min(*mh)],
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().min(*mw), area.h().min(*mh)],
}});
transform_xy_unit!(|self: Push, area|{
let area = Render::layout(&self.content(), area);
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
});
transform_xy_unit!(|self: Pull, area|{
let area = Render::layout(&self.content(), area);
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()]
});
transform_xy_unit!(|self: Margin, area|{
let area = Render::layout(&self.content(), area);
let dx = self.dx();
let dy = self.dy();
[area.x().minus(dx), area.y().minus(dy), area.w() + dy + dy, area.h() + dy + dy]
});
transform_xy_unit!(|self: Padding, area|{
let area = Render::layout(&self.content(), area);
let dx = self.dx();
let dy = self.dy();
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]
});

23
output/src/when.rs Normal file
View file

@ -0,0 +1,23 @@
use crate::*;
/// Show an item only when a condition is true.
pub struct When<A>(pub bool, pub A);
impl<E: Output, A: Render<E>> Content<E> for When<A> {
fn layout (&self, to: E::Area) -> E::Area {
let Self(cond, item) = self;
let mut area = E::Area::zero();
if *cond {
let item_area = item.layout(to);
area[0] = item_area.x();
area[1] = item_area.y();
area[2] = item_area.w();
area[3] = item_area.h();
}
area.into()
}
fn render (&self, to: &mut E) {
let Self(cond, item) = self;
if *cond { item.render(to) }
}
}