mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-07 04:06:48 +01:00
tabula rasa
This commit is contained in:
commit
47b3413d7d
76 changed files with 7000 additions and 0 deletions
14
output/Cargo.lock
generated
Normal file
14
output/Cargo.lock
generated
Normal 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",
|
||||
]
|
||||
12
output/Cargo.toml
Normal file
12
output/Cargo.toml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[package]
|
||||
name = "tek_output"
|
||||
edition = "2021"
|
||||
version = "0.2.0"
|
||||
|
||||
[dependencies]
|
||||
tek_edn = { path = "../edn" }
|
||||
|
||||
[dev-dependencies]
|
||||
tek_tui = { path = "../tui" }
|
||||
proptest = "^1"
|
||||
proptest-derive = "^0.5.1"
|
||||
76
output/README.md
Normal file
76
output/README.md
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
# `tek_output`
|
||||
|
||||
## free floating layout primitives
|
||||
|
||||
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`
|
||||
|
||||
## example rendering loop
|
||||
|
||||
the **render thread** continually invokes the
|
||||
`Content::render` method of the application
|
||||
to redraw the display. it does this efficiently
|
||||
by using ratatui's double buffering.
|
||||
|
||||
thus, for a type to be a valid application for engine `E`,
|
||||
it must implement the trait `Content<E>`, which allows
|
||||
it to display content to the engine's output.
|
||||
|
||||
the most important thing about the `Content` trait is that
|
||||
it composes:
|
||||
* you can implement `Content::content` to build
|
||||
`Content`s out of other `Content`s
|
||||
* and/or `Content::area` for custom positioning and sizing,
|
||||
* and/or `Content::render` for custom rendering
|
||||
within the given `Content`'s area.
|
||||
|
||||
the manner of output is determined by the
|
||||
`Engine::Output` type, a mutable pointer to which
|
||||
is passed to the render method, e.g. in the case of
|
||||
the `Tui` engine: `fn render(&self, output: &mut TuiOut)`
|
||||
|
||||
you can use `TuiOut::blit` and `TuiOut::place`
|
||||
to draw at specified coordinates of the display, and/or
|
||||
directly modify the underlying `ratatui::Buffer` at
|
||||
`output.buffer`
|
||||
|
||||
rendering is intended to work with read-only access
|
||||
to the application state. if you really need to update
|
||||
values during rendering, use interior mutability.
|
||||
7
output/proptest-regressions/area.txt
Normal file
7
output/proptest-regressions/area.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Seeds for failure cases proptest has generated in the past. It is
|
||||
# automatically read and these particular cases re-run before any
|
||||
# novel cases are generated.
|
||||
#
|
||||
# It is recommended to check this file in to source control so that
|
||||
# everyone who runs the test benefits from these saved cases.
|
||||
cc d2cd65ec39a1bf43c14bb2d3196c7e84ba854411360e570f06dd7ede62b0fd61 # shrinks to x = 0, y = 43998, w = 0, h = 43076, a = 0, b = 0
|
||||
7
output/proptest-regressions/direction.txt
Normal file
7
output/proptest-regressions/direction.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Seeds for failure cases proptest has generated in the past. It is
|
||||
# automatically read and these particular cases re-run before any
|
||||
# novel cases are generated.
|
||||
#
|
||||
# It is recommended to check this file in to source control so that
|
||||
# everyone who runs the test benefits from these saved cases.
|
||||
cc 5b236150b286e479089d5bf6accc8ffbc3c0b0a1f955682af1987f342930d31e # shrinks to x = 0, y = 0, w = 0, h = 0, a = 1
|
||||
10
output/proptest-regressions/op_transform.txt
Normal file
10
output/proptest-regressions/op_transform.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# Seeds for failure cases proptest has generated in the past. It is
|
||||
# automatically read and these particular cases re-run before any
|
||||
# novel cases are generated.
|
||||
#
|
||||
# It is recommended to check this file in to source control so that
|
||||
# everyone who runs the test benefits from these saved cases.
|
||||
cc b05b448ca4eb29304cae506927639494cae99a9e1ab40c58ac9dcb70d1ea1298 # shrinks to op_x = Some(0), op_y = None, content = "", x = 0, y = 46377, w = 0, h = 38318
|
||||
cc efdb7136c68396fa7c632cc6d3b304545ada1ba134269278f890639559a17575 # shrinks to op_x = Some(0), op_y = Some(32768), content = "", x = 0, y = 0, w = 0, h = 0
|
||||
cc f6d43c39db04f4c0112fe998ef68cff0a4454cd9791775a3014cc81997fbadf4 # shrinks to op_x = Some(10076), op_y = None, content = "", x = 60498, y = 0, w = 0, h = 0
|
||||
cc 3cabc97f3fa3a83fd5f8cf2c619ed213c2be5e9b1cb13e5178bde87dd838e2f4 # shrinks to op_x = Some(3924), op_y = None, content = "", x = 63574, y = 0, w = 0, h = 0
|
||||
137
output/src/area.rs
Normal file
137
output/src/area.rs
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
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 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]
|
||||
}
|
||||
#[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().plus(self.w())
|
||||
}
|
||||
#[inline] fn y2 (&self) -> N {
|
||||
self.y().plus(self.h())
|
||||
}
|
||||
#[inline] fn lrtb (&self) -> [N;4] {
|
||||
[self.x(), self.x2(), self.y(), self.y2()]
|
||||
}
|
||||
#[inline] fn center (&self) -> [N;2] {
|
||||
[self.x().plus(self.w()/2.into()), self.y().plus(self.h()/2.into())]
|
||||
}
|
||||
#[inline] fn center_x (&self, n: N) -> [N;4] {
|
||||
let [x, y, w, h] = self.xywh();
|
||||
[(x.plus(w / 2.into())).minus(n / 2.into()), y.plus(h / 2.into()), n, 1.into()]
|
||||
}
|
||||
#[inline] fn center_y (&self, n: N) -> [N;4] {
|
||||
let [x, y, w, h] = self.xywh();
|
||||
[x.plus(w / 2.into()), (y.plus(h / 2.into())).minus(n / 2.into()), 1.into(), n]
|
||||
}
|
||||
#[inline] fn center_xy (&self, [n, m]: [N;2]) -> [N;4] {
|
||||
let [x, y, w, h] = self.xywh();
|
||||
[(x.plus(w / 2.into())).minus(n / 2.into()), (y.plus(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())]
|
||||
}
|
||||
|
||||
fn iter_x (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
||||
self.x()..(self.x()+self.w())
|
||||
}
|
||||
fn iter_y (&self) -> impl Iterator<Item = N> where N: std::iter::Step {
|
||||
self.y()..(self.y()+self.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] }
|
||||
}
|
||||
|
||||
#[cfg(test)] mod test_area {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
proptest! {
|
||||
#[test] fn test_area_prop (
|
||||
x in u16::MIN..u16::MAX,
|
||||
y in u16::MIN..u16::MAX,
|
||||
w in u16::MIN..u16::MAX,
|
||||
h in u16::MIN..u16::MAX,
|
||||
a in u16::MIN..u16::MAX,
|
||||
b in u16::MIN..u16::MAX,
|
||||
) {
|
||||
let _: [u16;4] = <[u16;4] as Area<u16>>::zero();
|
||||
let _: [u16;4] = <[u16;4] as Area<u16>>::from_position([a, b]);
|
||||
let _: [u16;4] = <[u16;4] as Area<u16>>::from_size([a, b]);
|
||||
let area: [u16;4] = [x, y, w, h];
|
||||
let _ = area.expect_min(a, b);
|
||||
let _ = area.xy();
|
||||
let _ = area.wh();
|
||||
let _ = area.xywh();
|
||||
let _ = area.clip_h(a);
|
||||
let _ = area.clip_w(b);
|
||||
let _ = area.clip([a, b]);
|
||||
let _ = area.set_w(a);
|
||||
let _ = area.set_h(b);
|
||||
let _ = area.x2();
|
||||
let _ = area.y2();
|
||||
let _ = area.lrtb();
|
||||
let _ = area.center();
|
||||
let _ = area.center_x(a);
|
||||
let _ = area.center_y(b);
|
||||
let _ = area.center_xy([a, b]);
|
||||
let _ = area.centered();
|
||||
}
|
||||
}
|
||||
}
|
||||
23
output/src/collection.rs
Normal file
23
output/src/collection.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
31
output/src/coordinate.rs
Normal file
31
output/src/coordinate.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use std::fmt::{Debug, Display};
|
||||
use std::ops::{Add, Sub, Mul, Div};
|
||||
|
||||
impl Coordinate for u16 {
|
||||
#[inline] fn plus (self, other: Self) -> Self {
|
||||
self.saturating_add(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 zero () -> Self { 0.into() }
|
||||
#[inline] fn minus (self, other: Self) -> Self {
|
||||
if self >= other {
|
||||
self - other
|
||||
} else {
|
||||
0.into()
|
||||
}
|
||||
}
|
||||
fn plus (self, other: Self) -> Self;
|
||||
}
|
||||
38
output/src/direction.rs
Normal file
38
output/src/direction.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
use crate::*;
|
||||
#[cfg(test)] use proptest_derive::Arbitrary;
|
||||
/// A cardinal direction.
|
||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||
#[cfg_attr(test, derive(Arbitrary))]
|
||||
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.plus(h).minus(a), w, a], [x, y, w, h.minus(a)]),
|
||||
South => ([x, y, w, a], [x, y.plus(a), w, h.minus(a)]),
|
||||
East => ([x, y, a, h], [x.plus(a), y, w.minus(a), h]),
|
||||
West => ([x.plus(w).minus(a), y, a, h], [x, y, w.minus(a), h]),
|
||||
Above | Below => (area.xywh(), area.xywh())
|
||||
}
|
||||
}
|
||||
}
|
||||
#[cfg(test)] mod test {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
proptest! {
|
||||
#[test] fn proptest_direction (
|
||||
d in prop_oneof![
|
||||
Just(North), Just(South),
|
||||
Just(East), Just(West),
|
||||
Just(Above), Just(Below)
|
||||
],
|
||||
x in u16::MIN..u16::MAX,
|
||||
y in u16::MIN..u16::MAX,
|
||||
w in u16::MIN..u16::MAX,
|
||||
h in u16::MIN..u16::MAX,
|
||||
a in u16::MIN..u16::MAX,
|
||||
) {
|
||||
let _ = d.split_fixed([x, y, w, h], a);
|
||||
}
|
||||
}
|
||||
}
|
||||
55
output/src/lib.rs
Normal file
55
output/src/lib.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
//#![feature(lazy_type_alias)]
|
||||
#![feature(step_trait)]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
mod direction; pub use self::direction::*;
|
||||
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 measure; pub use self::measure::*;
|
||||
mod thunk; pub use self::thunk::*;
|
||||
mod op_cond; pub use self::op_cond::*;
|
||||
mod op_iter; pub use self::op_iter::*;
|
||||
mod op_align; pub use self::op_align::*;
|
||||
mod op_bsp; pub use self::op_bsp::*;
|
||||
mod op_transform; pub use self::op_transform::*;
|
||||
mod view; pub use self::view::*;
|
||||
pub(crate) use std::marker::PhantomData;
|
||||
pub(crate) use std::error::Error;
|
||||
pub(crate) use ::tek_edn::*;
|
||||
/// 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>>;
|
||||
#[cfg(test)] #[test] fn test_stub_output () -> Usually<()> {
|
||||
use crate::*;
|
||||
struct TestOutput([u16;4]);
|
||||
impl Output for TestOutput {
|
||||
type Unit = u16;
|
||||
type Size = [u16;2];
|
||||
type Area = [u16;4];
|
||||
fn area (&self) -> [u16;4] {
|
||||
self.0
|
||||
}
|
||||
fn area_mut (&mut self) -> &mut [u16;4] {
|
||||
&mut self.0
|
||||
}
|
||||
fn place (&mut self, _: [u16;4], _: &impl Render<TestOutput>) {
|
||||
()
|
||||
}
|
||||
}
|
||||
impl Content<TestOutput> for String {
|
||||
fn render (&self, to: &mut TestOutput) {
|
||||
to.area_mut().set_w(self.len() as u16);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
#[cfg(test)] #[test] fn test_dimensions () {
|
||||
use crate::*;
|
||||
assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]);
|
||||
}
|
||||
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
|
||||
Ok(())
|
||||
}
|
||||
144
output/src/measure.rs
Normal file
144
output/src/measure.rs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
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 new () -> Self {
|
||||
Self {
|
||||
_engine: PhantomData::default(),
|
||||
x: Arc::new(0.into()),
|
||||
y: Arc::new(0.into()),
|
||||
}
|
||||
}
|
||||
pub fn set_w (&self, w: impl Into<usize>) -> &Self {
|
||||
self.x.store(w.into(), Relaxed);
|
||||
self
|
||||
}
|
||||
pub fn set_h (&self, h: impl Into<usize>) -> &Self {
|
||||
self.y.store(h.into(), Relaxed);
|
||||
self
|
||||
}
|
||||
pub fn set_wh (&self, w: impl Into<usize>, h: impl Into<usize>) -> &Self {
|
||||
self.set_w(w);
|
||||
self.set_h(h);
|
||||
self
|
||||
}
|
||||
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 format (&self) -> Arc<str> {
|
||||
format!("{}x{}", self.w(), self.h()).into()
|
||||
}
|
||||
pub fn of <T: Content<E>> (&self, item: T) -> Bsp<Fill<&Self>, T> {
|
||||
Bsp::b(Fill::xy(self), item)
|
||||
}
|
||||
}
|
||||
//#[cfg(test)] #[test] fn test_measure () {
|
||||
//use tek_tui::*;
|
||||
//let size: Measure<TuiOut> = Measure::default().set_w(1usize).set_h(1usize).clone();
|
||||
//let size: Measure<TuiOut> = (&Measure::new().set_wh(2usize, 1usize)).clone();
|
||||
//let _ = format!("{:?}", &size);
|
||||
//let _ = size.wh();
|
||||
//let _ = size.format();
|
||||
//let _ = size.of(());
|
||||
//}
|
||||
|
||||
///// 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())))
|
||||
//}
|
||||
//}
|
||||
102
output/src/op_align.rs
Normal file
102
output/src/op_align.rs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
//! Aligns things to the container. Comes with caveats.
|
||||
//! ```
|
||||
//! use ::tek_tui::{*, tek_output::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, area), expected);
|
||||
//! };
|
||||
//!
|
||||
//! let four = ||Fixed::xy(4, 4, "");
|
||||
//! test(area, &Align::nw(four()), [10, 10, 4, 4]);
|
||||
//! test(area, &Align::n(four()), [18, 10, 4, 4]);
|
||||
//! test(area, &Align::ne(four()), [26, 10, 4, 4]);
|
||||
//! test(area, &Align::e(four()), [26, 18, 4, 4]);
|
||||
//! test(area, &Align::se(four()), [26, 26, 4, 4]);
|
||||
//! test(area, &Align::s(four()), [18, 26, 4, 4]);
|
||||
//! test(area, &Align::sw(four()), [10, 26, 4, 4]);
|
||||
//! test(area, &Align::w(four()), [10, 18, 4, 4]);
|
||||
//!
|
||||
//! let two_by_four = ||Fixed::xy(4, 2, "");
|
||||
//! test(area, &Align::nw(two_by_four()), [10, 10, 4, 2]);
|
||||
//! test(area, &Align::n(two_by_four()), [18, 10, 4, 2]);
|
||||
//! test(area, &Align::ne(two_by_four()), [26, 10, 4, 2]);
|
||||
//! test(area, &Align::e(two_by_four()), [26, 19, 4, 2]);
|
||||
//! test(area, &Align::se(two_by_four()), [26, 28, 4, 2]);
|
||||
//! test(area, &Align::s(two_by_four()), [18, 28, 4, 2]);
|
||||
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
|
||||
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
|
||||
//! ```
|
||||
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);
|
||||
try_from_expr!(<'a, E>: Align<RenderBox<'a, E>>: |state, iter|
|
||||
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
|
||||
match key {
|
||||
"align/c"|"align/x"|"align/y"|
|
||||
"align/n"|"align/s"|"align/e"|"align/w"|
|
||||
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
|
||||
let _ = iter.next().unwrap();
|
||||
let c = iter.next().expect("no content specified");
|
||||
let c = state.get_content(&c.value).expect("no content provided");
|
||||
return Some(match key {
|
||||
"align/c" => Self::c(c),
|
||||
"align/x" => Self::x(c),
|
||||
"align/y" => Self::y(c),
|
||||
"align/n" => Self::n(c),
|
||||
"align/s" => Self::s(c),
|
||||
"align/e" => Self::e(c),
|
||||
"align/w" => Self::w(c),
|
||||
"align/nw" => Self::nw(c),
|
||||
"align/ne" => Self::ne(c),
|
||||
"align/sw" => Self::sw(c),
|
||||
"align/se" => Self::se(c),
|
||||
_ => unreachable!()
|
||||
})
|
||||
},
|
||||
_ => return None
|
||||
}
|
||||
});
|
||||
impl<A> Align<A> {
|
||||
#[inline] pub fn c (a: A) -> Self { Self(Alignment::Center, a) }
|
||||
#[inline] pub fn x (a: A) -> Self { Self(Alignment::X, a) }
|
||||
#[inline] pub fn y (a: A) -> Self { Self(Alignment::Y, a) }
|
||||
#[inline] pub fn n (a: A) -> Self { Self(Alignment::N, a) }
|
||||
#[inline] pub fn s (a: A) -> Self { Self(Alignment::S, a) }
|
||||
#[inline] pub fn e (a: A) -> Self { Self(Alignment::E, a) }
|
||||
#[inline] pub fn w (a: A) -> Self { Self(Alignment::W, a) }
|
||||
#[inline] pub fn nw (a: A) -> Self { Self(Alignment::NW, a) }
|
||||
#[inline] pub fn sw (a: A) -> Self { Self(Alignment::SW, a) }
|
||||
#[inline] pub fn ne (a: A) -> Self { Self(Alignment::NE, a) }
|
||||
#[inline] pub fn se (a: A) -> Self { Self(Alignment::SE, a) }
|
||||
}
|
||||
impl<E: Output, A: Content<E>> Content<E> for Align<A> {
|
||||
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 cx = on.x()+(on.w().minus(it.w())/2.into());
|
||||
let cy = on.y()+(on.h().minus(it.h())/2.into());
|
||||
let fx = (on.x()+on.w()).minus(it.w());
|
||||
let fy = (on.y()+on.h()).minus(it.h());
|
||||
let [x, y] = match self.0 {
|
||||
Center => [cx, cy],
|
||||
X => [cx, it.y()],
|
||||
Y => [it.x(), cy],
|
||||
NW => [on.x(), on.y()],
|
||||
N => [cx, on.y()],
|
||||
NE => [fx, on.y()],
|
||||
W => [on.x(), cy],
|
||||
E => [fx, cy],
|
||||
SW => [on.x(), fy],
|
||||
S => [cx, fy],
|
||||
SE => [fx, fy],
|
||||
}.into();
|
||||
[x, y, it.w(), it.h()].into()
|
||||
}
|
||||
fn render (&self, to: &mut E) {
|
||||
to.place(Content::layout(self, to.area()), &self.content())
|
||||
}
|
||||
}
|
||||
143
output/src/op_bsp.rs
Normal file
143
output/src/op_bsp.rs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
use crate::*;
|
||||
pub use Direction::*;
|
||||
/// A split or layer.
|
||||
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); }
|
||||
}
|
||||
}
|
||||
}
|
||||
try_from_expr!(<'a, E>: Bsp<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some(Token { value: Value::Key(key), .. }) = iter.peek() {
|
||||
match key {
|
||||
"bsp/n"|"bsp/s"|"bsp/e"|"bsp/w"|"bsp/a"|"bsp/b" => {
|
||||
let _ = iter.next().unwrap();
|
||||
let c1 = iter.next().expect("no content1 specified");
|
||||
let c2 = iter.next().expect("no content2 specified");
|
||||
let c1 = state.get_content(&c1.value).expect("no content1 provided");
|
||||
let c2 = state.get_content(&c2.value).expect("no content2 provided");
|
||||
return Some(match key {
|
||||
"bsp/n" => Self::n(c1, c2),
|
||||
"bsp/s" => Self::s(c1, c2),
|
||||
"bsp/e" => Self::e(c1, c2),
|
||||
"bsp/w" => Self::w(c1, c2),
|
||||
"bsp/a" => Self::a(c1, c2),
|
||||
"bsp/b" => Self::b(c1, c2),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
},
|
||||
_ => return None
|
||||
}
|
||||
}
|
||||
});
|
||||
impl<A, B> Bsp<A, B> {
|
||||
#[inline] pub fn n (a: A, b: B) -> Self { Self(North, a, b) }
|
||||
#[inline] pub fn s (a: A, b: B) -> Self { Self(South, a, b) }
|
||||
#[inline] pub fn e (a: A, b: B) -> Self { Self(East, a, b) }
|
||||
#[inline] pub fn w (a: A, b: B) -> Self { Self(West, a, b) }
|
||||
#[inline] pub fn a (a: A, b: B) -> Self { Self(Above, a, b) }
|
||||
#[inline] 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 [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()]
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
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 }};
|
||||
}
|
||||
#[cfg(test)] mod test {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
proptest! {
|
||||
#[test] fn proptest_op_bsp (
|
||||
d in prop_oneof![
|
||||
Just(North), Just(South),
|
||||
Just(East), Just(West),
|
||||
Just(Above), Just(Below)
|
||||
],
|
||||
a in "\\PC*",
|
||||
b in "\\PC*",
|
||||
x in u16::MIN..u16::MAX,
|
||||
y in u16::MIN..u16::MAX,
|
||||
w in u16::MIN..u16::MAX,
|
||||
h in u16::MIN..u16::MAX,
|
||||
) {
|
||||
let bsp = Bsp(d, a, b);
|
||||
assert_eq!(
|
||||
Content::layout(&bsp, [x, y, w, h]),
|
||||
Render::layout(&bsp, [x, y, w, h]),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
57
output/src/op_cond.rs
Normal file
57
output/src/op_cond.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use crate::*;
|
||||
/// Show an item only when a condition is true.
|
||||
pub struct When<A>(pub bool, pub A);
|
||||
impl<A> When<A> { #[inline] pub fn new (c: bool, a: A) -> Self { Self(c, a) } }
|
||||
/// 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> { #[inline] pub fn new (c: bool, a: A, b: B) -> Self { Self(c, a, b) } }
|
||||
try_from_expr!(<'a, E>: When<RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some(Token { value: Value::Key("when"), .. }) = iter.peek() {
|
||||
let _ = iter.next().unwrap();
|
||||
let condition = iter.next().expect("no condition specified");
|
||||
let content = iter.next().expect("no content specified");
|
||||
let condition = state.get(&condition.value).expect("no condition provided");
|
||||
let content = state.get_content(&content.value).expect("no content provided");
|
||||
return Some(Self(condition, content))
|
||||
}
|
||||
});
|
||||
try_from_expr!(<'a, E>: Either<RenderBox<'a, E>, RenderBox<'a, E>>: |state, iter| {
|
||||
if let Some(Token { value: Value::Key("either"), .. }) = iter.peek() {
|
||||
let _ = iter.next().unwrap();
|
||||
let condition = iter.next().expect("no condition specified");
|
||||
let content = iter.next().expect("no content specified");
|
||||
let alternate = iter.next().expect("no alternate specified");
|
||||
let condition = state.get(&condition.value).expect("no condition provided");
|
||||
let content = state.get_content(&content.value).expect("no content provided");
|
||||
let alternate = state.get_content(&alternate.value).expect("no alternate provided");
|
||||
return Some(Self(condition, content, alternate))
|
||||
}
|
||||
});
|
||||
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) }
|
||||
}
|
||||
}
|
||||
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) }
|
||||
}
|
||||
}
|
||||
77
output/src/op_iter.rs
Normal file
77
output/src/op_iter.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
use crate::*;
|
||||
#[inline] 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, Fixed::y(item_height, Fill::x(item)))
|
||||
}
|
||||
|
||||
#[inline] pub fn map_south_west<O: Output>(
|
||||
item_offset: O::Unit,
|
||||
item_height: O::Unit,
|
||||
item: impl Content<O>
|
||||
) -> impl Content<O> {
|
||||
Push::y(item_offset, Align::nw(Fixed::y(item_height, Fill::x(item))))
|
||||
}
|
||||
|
||||
#[inline] 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 [x,y,w,h] = callback(item, index).layout(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 = Content::layout(self, to.area());
|
||||
for item in get_iterator() {
|
||||
let item = callback(item, index);
|
||||
//to.place(area.into(), &item);
|
||||
to.place(item.layout(area), &item);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
232
output/src/op_transform.rs
Normal file
232
output/src/op_transform.rs
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
//! [Content] items that modify the inherent
|
||||
//! dimensions of their inner [Render]ables.
|
||||
//!
|
||||
//! Transform may also react to the [Area] provided.
|
||||
//! ```
|
||||
//! use ::tek_tui::{*, tek_output::*};
|
||||
//! let area: [u16;4] = [10, 10, 20, 20];
|
||||
//! fn test (area: [u16;4], item: &impl Content<TuiOut>, expected: [u16;4]) {
|
||||
//! assert_eq!(Content::layout(item, area), expected);
|
||||
//! assert_eq!(Render::layout(item, 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<T> { X(T), Y(T), XY(T) }
|
||||
impl<T> $Enum<T> {
|
||||
#[inline] pub fn x (item: T) -> Self { Self::X(item) }
|
||||
#[inline] pub fn y (item: T) -> Self { Self::Y(item) }
|
||||
#[inline] pub fn xy (item: T) -> Self { Self::XY(item) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
|
||||
for $Enum<RenderBox<'a, E>> {
|
||||
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
|
||||
let mut iter = iter.clone();
|
||||
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
|
||||
if k == $x || k == $y || k == $xy {
|
||||
let _ = iter.next().unwrap();
|
||||
let token = iter.next().expect("no content specified");
|
||||
let content = state.get_content(&token.value).expect("no content provided");
|
||||
return Some(match k {
|
||||
$x => Self::x(content),
|
||||
$y => Self::y(content),
|
||||
$xy => Self::xy(content),
|
||||
_ => unreachable!()
|
||||
})
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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, T> { X(U, T), Y(U, T), XY(U, U, T), }
|
||||
impl<U, T> $Enum<U, T> {
|
||||
#[inline] pub fn x (x: U, item: T) -> Self { Self::X(x, item) }
|
||||
#[inline] pub fn y (y: U, item: T) -> Self { Self::Y(y, item) }
|
||||
#[inline] pub fn xy (x: U, y: U, item: T) -> Self { Self::XY(x, y, item) }
|
||||
}
|
||||
impl<'a, E: Output + 'a, T: ViewContext<'a, E>> TryFromAtom<'a, T>
|
||||
for $Enum<E::Unit, RenderBox<'a, E>> {
|
||||
fn try_from_expr (state: &'a T, iter: TokenIter<'a>) -> Option<Self> {
|
||||
let mut iter = iter.clone();
|
||||
if let Some(Token { value: Value::Key(k), .. }) = iter.peek() {
|
||||
if k == $x || k == $y {
|
||||
let _ = iter.next().unwrap();
|
||||
let u = iter.next().expect("no unit specified");
|
||||
let c = iter.next().expect("no content specified");
|
||||
let u = state.get(&u.value).expect("no unit provided");
|
||||
let c = state.get_content(&c.value).expect("no content provided");
|
||||
return Some(match k {
|
||||
$x => Self::x(u, c),
|
||||
$y => Self::y(u, c),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
} else if k == $xy {
|
||||
let _ = iter.next().unwrap();
|
||||
let u = iter.next().expect("no unit specified");
|
||||
let v = iter.next().expect("no unit specified");
|
||||
let c = iter.next().expect("no content specified");
|
||||
let u = state.get(&u.value).expect("no unit provided");
|
||||
let v = state.get(&v.value).expect("no unit provided");
|
||||
let c = state.get_content(&c.value).expect("no content provided");
|
||||
return Some(Self::xy(u, v, c))
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
}
|
||||
impl<U: Copy + Coordinate, T> $Enum<U, T> {
|
||||
#[inline] pub fn dx (&self) -> U {
|
||||
match self {
|
||||
Self::X(x, _) => *x, Self::Y(_, _) => 0.into(), Self::XY(x, _, _) => *x,
|
||||
}
|
||||
}
|
||||
#[inline] pub fn dy (&self) -> U {
|
||||
match self {
|
||||
Self::X(_, _) => 0.into(), Self::Y(y, _) => *y, Self::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 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],
|
||||
};
|
||||
let [x, y, w, h] = Render::layout(&self.content(), fixed_area.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 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!("max/x" "max/y" "max/xy"|self: Max, area|{
|
||||
let [x, y, w, h] = area.xywh();
|
||||
Render::layout(&self.content(), 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|Render::layout(
|
||||
&self.content(),
|
||||
[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|Render::layout(
|
||||
&self.content(),
|
||||
[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 = Render::layout(&self.content(), 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 = Render::layout(&self.content(), 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 = Render::layout(&self.content(), 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 = Render::layout(&self.content(), 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)), ] });
|
||||
|
||||
#[cfg(test)] mod test_op_transform {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
use proptest::option::of;
|
||||
macro_rules! test_op_transform {
|
||||
($fn:ident, $Op:ident) => {
|
||||
proptest! {
|
||||
#[test] fn $fn (
|
||||
op_x in of(u16::MIN..u16::MAX),
|
||||
op_y in of(u16::MIN..u16::MAX),
|
||||
content in "\\PC*",
|
||||
x in u16::MIN..u16::MAX,
|
||||
y in u16::MIN..u16::MAX,
|
||||
w in u16::MIN..u16::MAX,
|
||||
h in u16::MIN..u16::MAX,
|
||||
) {
|
||||
if let Some(op) = match (op_x, op_y) {
|
||||
(Some(x), Some(y)) => Some($Op::xy(x, y, content)),
|
||||
(Some(x), None) => Some($Op::x(x, content)),
|
||||
(Some(y), None) => Some($Op::y(y, content)),
|
||||
_ => None
|
||||
} {
|
||||
assert_eq!(Content::layout(&op, [x, y, w, h]),
|
||||
Render::layout(&op, [x, y, w, h]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
test_op_transform!(test_op_fixed, Fixed);
|
||||
test_op_transform!(test_op_min, Min);
|
||||
test_op_transform!(test_op_max, Max);
|
||||
test_op_transform!(test_op_push, Push);
|
||||
test_op_transform!(test_op_pull, Pull);
|
||||
test_op_transform!(test_op_shrink, Shrink);
|
||||
test_op_transform!(test_op_expand, Expand);
|
||||
test_op_transform!(test_op_margin, Margin);
|
||||
test_op_transform!(test_op_padding, Padding);
|
||||
}
|
||||
132
output/src/output.rs
Normal file
132
output/src/output.rs
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
use crate::*;
|
||||
use std::ops::Deref;
|
||||
/// 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() }
|
||||
}
|
||||
/// Renderable with dynamic dispatch.
|
||||
pub trait Render<E: Output> {
|
||||
/// Compute layout.
|
||||
fn layout (&self, area: E::Area) -> E::Area;
|
||||
/// Write data to display.
|
||||
fn render (&self, output: &mut E);
|
||||
/// Perform type erasure, turning `self` into an opaque [RenderBox].
|
||||
fn boxed <'a> (self) -> RenderBox<'a, E> where Self: Send + Sync + Sized + 'a {
|
||||
Box::new(self) as RenderBox<'a, E>
|
||||
}
|
||||
}
|
||||
/// Most importantly, every [Content] is also a [Render].
|
||||
///
|
||||
/// However, the converse does not hold true.
|
||||
/// Instead, the [Content::content] method returns an
|
||||
/// opaque [Render] pointer.
|
||||
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) }
|
||||
}
|
||||
/// Opaque pointer to a renderable living on the heap.
|
||||
///
|
||||
/// Return this from [Content::content] to use dynamic dispatch.
|
||||
pub type RenderBox<'a, E> = Box<RenderDyn<'a, E>>;
|
||||
/// You can render from a box.
|
||||
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 }
|
||||
}
|
||||
/// Opaque pointer to a renderable.
|
||||
pub type RenderDyn<'a, E> = dyn Render<E> + Send + Sync + 'a;
|
||||
/// You can render from an opaque pointer.
|
||||
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) }
|
||||
}
|
||||
/// Composable renderable with static dispatch.
|
||||
pub trait Content<E: Output> {
|
||||
/// Return a [Render]able of a specific type.
|
||||
fn content (&self) -> impl Render<E> { () }
|
||||
/// Perform layout. By default, delegates to [Self::content].
|
||||
fn layout (&self, area: E::Area) -> E::Area { self.content().layout(area) }
|
||||
/// Draw to output. By default, delegates to [Self::content].
|
||||
fn render (&self, output: &mut E) { self.content().render(output) }
|
||||
}
|
||||
/// Every pointer to [Content] is a [Content].
|
||||
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 (e=1vg^sqrt(-1))
|
||||
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));
|
||||
}
|
||||
}
|
||||
/// Implement [Content] with 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 }
|
||||
}
|
||||
};
|
||||
}
|
||||
/// Implement [Content] with 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)?),*> 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)?),+>)?, $to:ident
|
||||
|$render:expr) => {
|
||||
impl $(<$($($L)? $($T)? $(:$Trait)?),+>)? Content<$Output>
|
||||
for $Struct $(<$($($L)? $($T)?),+>)? {
|
||||
fn render (&$self, $to: &mut $Output) { $render }
|
||||
}
|
||||
};
|
||||
}
|
||||
93
output/src/reduce.rs
Normal file
93
output/src/reduce.rs
Normal 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))
|
||||
//}
|
||||
//}
|
||||
|
||||
63
output/src/size.rs
Normal file
63
output/src/size.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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] }
|
||||
}
|
||||
|
||||
#[cfg(test)] mod test_size {
|
||||
use super::*;
|
||||
use proptest::prelude::*;
|
||||
proptest! {
|
||||
#[test] fn test_size (
|
||||
x in u16::MIN..u16::MAX,
|
||||
y in u16::MIN..u16::MAX,
|
||||
a in u16::MIN..u16::MAX,
|
||||
b in u16::MIN..u16::MAX,
|
||||
) {
|
||||
let size = [x, y];
|
||||
let _ = size.w();
|
||||
let _ = size.h();
|
||||
let _ = size.wh();
|
||||
let _ = size.clip_w(a);
|
||||
let _ = size.clip_h(b);
|
||||
let _ = size.expect_min(a, b);
|
||||
let _ = size.to_area_pos();
|
||||
let _ = size.to_area_size();
|
||||
}
|
||||
}
|
||||
}
|
||||
50
output/src/thunk.rs
Normal file
50
output/src/thunk.rs
Normal 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 ThunkBox<'a, E: Output>(PhantomData<E>, Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>);
|
||||
impl<'a, E: Output> ThunkBox<'a, E> {
|
||||
pub fn new (thunk: Box<dyn Fn()->RenderBox<'a, E> + Send + Sync + 'a>) -> Self {
|
||||
Self(Default::default(), thunk)
|
||||
}
|
||||
}
|
||||
impl<'a, E: Output> Content<E> for ThunkBox<'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 ThunkBox<'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 ThunkBox<'a, E> {
|
||||
//fn from (f: F) -> Self {
|
||||
//Self(Default::default(), Box::new(f))
|
||||
//}
|
||||
//}
|
||||
|
||||
pub struct ThunkRender<E: Output, F: Fn(&mut E) + Send + Sync>(PhantomData<E>, F);
|
||||
impl<E: Output, F: Fn(&mut E) + Send + Sync> ThunkRender<E, F> {
|
||||
pub fn new (render: F) -> Self { Self(Default::default(), render) }
|
||||
}
|
||||
impl<E: Output, F: Fn(&mut E) + Send + Sync> Content<E> for ThunkRender<E, F> {
|
||||
fn render (&self, to: &mut E) { (self.1)(to) }
|
||||
}
|
||||
|
||||
pub struct ThunkLayout<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> ThunkLayout<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 ThunkLayout<E, F1, F2> {
|
||||
fn layout (&self, to: E::Area) -> E::Area { (self.1)(to) }
|
||||
fn render (&self, to: &mut E) { (self.2)(to) }
|
||||
}
|
||||
87
output/src/view.rs
Normal file
87
output/src/view.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
use crate::*;
|
||||
#[macro_export] macro_rules! view {
|
||||
($Output:ty: |$self:ident: $State:ty| $expr:expr; {
|
||||
$($sym:literal => $body:expr),* $(,)?
|
||||
}) => {
|
||||
impl Content<$Output> for $State {
|
||||
fn content (&$self) -> impl Render<$Output> { $expr }
|
||||
}
|
||||
impl<'a> ViewContext<'a, $Output> for $State {
|
||||
fn get_content_sym (&'a $self, value: &Value<'a>) -> Option<RenderBox<'a, $Output>> {
|
||||
if let Value::Sym(s) = value {
|
||||
match *s {
|
||||
$($sym => Some($body),)*
|
||||
_ => None
|
||||
}
|
||||
} else {
|
||||
panic!("expected content, got: {value:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// An ephemeral wrapper around view state and view description,
|
||||
// that is meant to be constructed and returned from [Content::content].
|
||||
pub struct View<'a, T>(pub &'a T, pub SourceIter<'a>);
|
||||
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
|
||||
fn content (&self) -> impl Render<O> {
|
||||
let iter = self.1.clone();
|
||||
while let Some((Token { value, .. }, _)) = iter.next() {
|
||||
if let Some(content) = self.0.get_content(&value) {
|
||||
return Some(content)
|
||||
}
|
||||
}
|
||||
return None
|
||||
}
|
||||
}
|
||||
// Provides components to the view.
|
||||
pub trait ViewContext<'a, E: Output + 'a>: Send + Sync
|
||||
+ Context<bool>
|
||||
+ Context<usize>
|
||||
+ Context<E::Unit>
|
||||
{
|
||||
fn get_content (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
|
||||
match value {
|
||||
Value::Sym(_) => self.get_content_sym(value),
|
||||
Value::Exp(_, _) => self.get_content_exp(value),
|
||||
_ => panic!("only :symbols and (expressions) accepted here")
|
||||
}
|
||||
}
|
||||
fn get_content_sym (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>>;
|
||||
fn get_content_exp (&'a self, value: &Value<'a>) -> Option<RenderBox<'a, E>> {
|
||||
try_delegate!(self, *value, When::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Either::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Align::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Bsp::<RenderBox<'a, E>, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Fill::<RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Fixed::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Min::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Max::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Shrink::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Expand::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Push::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Pull::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Margin::<_, RenderBox<'a, E>>);
|
||||
try_delegate!(self, *value, Padding::<_, RenderBox<'a, E>>);
|
||||
None
|
||||
}
|
||||
}
|
||||
#[macro_export] macro_rules! try_delegate {
|
||||
($s:ident, $atom:expr, $T:ty) => {
|
||||
if let Some(value) = <$T>::try_from_atom($s, $atom) {
|
||||
return Some(value.boxed())
|
||||
}
|
||||
}
|
||||
}
|
||||
#[macro_export] macro_rules! try_from_expr {
|
||||
(<$l:lifetime, $E:ident>: $Struct:ty: |$state:ident, $iter:ident|$body:expr) => {
|
||||
impl<$l, $E: Output + $l, T: ViewContext<$l, $E>> TryFromAtom<$l, T> for $Struct {
|
||||
fn try_from_expr ($state: &$l T, $iter: TokenIter<'a>) -> Option<Self> {
|
||||
let mut $iter = $iter.clone();
|
||||
$body;
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue