move edn_view into layout

This commit is contained in:
🪞👃🪞 2025-01-07 21:41:51 +01:00
parent 4d0f98acd2
commit 9a70fbc416
13 changed files with 91 additions and 89 deletions

View file

@ -5,4 +5,6 @@ version = "0.2.0"
[dev-dependencies]
tek_tui = { path = "../tui" }
tek_engine = { path = "../engine" }
[dependencies]
tek_edn = { path = "../edn" }

View file

@ -1,4 +1,6 @@
# `tek_layout`
# `tek_output`
## free floating layout primitives
this crate exposes several layout operators
which work entirely in unsigned coordinates
@ -39,3 +41,36 @@ are not dependent on rendering framework.
**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.

101
output/src/edn_view.rs Normal file
View file

@ -0,0 +1,101 @@
use crate::*;
use std::marker::PhantomData;
use EdnItem::*;
pub type EdnCallback<'a, Output, State> =
dyn Fn(&'a State)-> RenderBox<'a, Output> + Send + Sync + 'a;
pub type EdnRenderCallback<'a, Output, State> =
Box<EdnCallback<'a, Output, State>>;
/// Provides values to the template
pub trait EdnViewData<E: Output> {
fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false }
fn get_unit (&self, _sym: EdnItem<&str>) -> E::Unit { 0.into() }
fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 }
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) }
}
/// Renders from EDN source and context.
pub enum EdnView<E: Output, T: EdnViewData<E>> {
_Unused(PhantomData<E>),
Ok(T, EdnItem<String>),
//render: Box<dyn Fn(&'a T)->Box<dyn Render<E> + Send + Sync + 'a> + Send + Sync + 'a>
Err(String)
}
impl<E: Output, T: EdnViewData<E>> EdnView<E, T> {
pub fn from_source (state: T, source: &str) -> Self {
match EdnItem::read_one(&source) {
Ok((layout, _)) => Self::Ok(state, layout),
Err(error) => Self::Err(format!("{error}"))
}
}
pub fn from_items (state: T, items: &[EdnItem<&str>]) -> Self {
Self::Ok(state, EdnItem::Exp(items.iter().map(|i|(*i).clone()).collect()))
}
}
impl<E: Output, T: EdnViewData<E> + Send + Sync> Content<E> for EdnView<E, T> {
fn content (&self) -> impl Render<E> {
use EdnItem::*;
match self {
Self::Ok(s, layout) => match layout {
Nil => ().boxed(),
Sym(t) => s.get_content(Sym(t.as_str())).boxed(),
Key(t) => panic!("todo: add error handling to content() chain. unexpected key {t}"),
Num(n) => panic!("todo: add error handling to content() chain. unexpected num {n}"),
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
let head = &head.to_ref();
match (head, tail) {
(Key("align/c"), [a]) => Align::c(s.get_content(a.to_ref())).boxed(),
(Key("align/x"), [a]) => Align::x(s.get_content(a.to_ref())).boxed(),
(Key("align/y"), [a]) => Align::y(s.get_content(a.to_ref())).boxed(),
(Key("align/n"), [a]) => Align::n(s.get_content(a.to_ref())).boxed(),
(Key("align/s"), [a]) => Align::s(s.get_content(a.to_ref())).boxed(),
(Key("align/e"), [a]) => Align::e(s.get_content(a.to_ref())).boxed(),
(Key("align/w"), [a]) => Align::w(s.get_content(a.to_ref())).boxed(),
(Key("align/nw"), [a]) => Align::nw(s.get_content(a.to_ref())).boxed(),
(Key("align/ne"), [a]) => Align::ne(s.get_content(a.to_ref())).boxed(),
(Key("align/sw"), [a]) => Align::sw(s.get_content(a.to_ref())).boxed(),
(Key("align/se"), [a]) => Align::se(s.get_content(a.to_ref())).boxed(),
(Key("bsp/a"), [a, b]) => Bsp::a(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("bsp/b"), [a, b]) => Bsp::b(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("bsp/e"), [a, b]) => Bsp::e(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("bsp/n"), [a, b]) => Bsp::n(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("bsp/s"), [a, b]) => Bsp::s(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("bsp/w"), [a, b]) => Bsp::w(s.get_content(a.to_ref()), s.get_content(b.to_ref()),).boxed(),
(Key("fill/x"), [a]) => Fill::x(s.get_content(a.to_ref())).boxed(),
(Key("fill/y"), [a]) => Fill::y(s.get_content(a.to_ref())).boxed(),
(Key("fill/xy"), [a]) => Fill::xy(s.get_content(a.to_ref())).boxed(),
(Key("fixed/x"), [x, a]) => Fixed::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("fixed/y"), [y, a]) => Fixed::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("fixed/xy"), [x, y, a]) => Fixed::xy(s.get_unit(x.to_ref()), s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("max/x"), [x, a]) => Max::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("max/y"), [y, a]) => Max::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("push/x"), [x, a]) => Push::x(s.get_unit(x.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("push/y"), [y, a]) => Push::y(s.get_unit(y.to_ref()), s.get_content(a.to_ref())).boxed(),
(Key("when"), [c, a]) => When(s.get_bool(c.to_ref()), s.get_content(a.to_ref())).boxed(),
_ => todo!("{:?} {:?}", &head, &tail)
}
} else {
panic!("todo: add error handling to content() chain. invalid expression {e:?}")
},
},
Self::Err(error) => {
Box::new(())//&format!("EdnView error: {error:?}"))
},
_ => todo!()
}
}
}

View file

@ -21,24 +21,18 @@ mod measure; pub use self::measure::*;
mod transform_xy; pub use self::transform_xy::*;
mod transform_xy_unit; pub use self::transform_xy_unit::*;
mod edn_view;
pub use self::edn_view::*;
pub(crate) use ::tek_edn::*;
pub(crate) use std::marker::PhantomData;
pub(crate) use std::error::Error;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
pub(crate) 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 }
}
};
}
pub(crate) type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
use ::tek_tui::Tui;