wip: providing content chunks with ednprovider

This commit is contained in:
🪞👃🪞 2025-01-12 16:12:26 +01:00
parent 1ff35baea9
commit 8c54f8e426
8 changed files with 157 additions and 41 deletions

144
output/examples/demo.rs.old Normal file
View file

@ -0,0 +1,144 @@
use tek::*;
fn main () -> Usually<()> {
Tui::run(Arc::new(RwLock::new(Demo::new())))?;
Ok(())
}
pub struct Demo<E: Engine> {
index: usize,
items: Vec<Box<dyn Render<Engine = E>>>
}
impl Demo<Tui> {
fn new () -> Self {
Self {
index: 0,
items: vec![
//Box::new(tek_sequencer::TransportPlayPauseButton {
//_engine: Default::default(),
//transport: None,
//value: Some(TransportState::Stopped),
//focused: true
//}),
//Box::new(tek_sequencer::TransportPlayPauseButton {
//_engine: Default::default(),
//transport: None,
//value: Some(TransportState::Rolling),
//focused: false
//}),
]
}
}
}
impl Content for Demo<Tui> {
type Engine = Tui;
fn content (&self) -> dyn Render<Engine = Tui> {
let border_style = Style::default().fg(Color::Rgb(0,0,0));
Align::Center(Layers::new(move|add|{
add(&Background(Color::Rgb(0,128,128)))?;
add(&Margin::XY(1, 1, Stack::down(|add|{
add(&Layers::new(|add|{
add(&Background(Color::Rgb(128,96,0)))?;
add(&Border(Square(border_style)))?;
add(&Margin::XY(2, 1, "..."))?;
Ok(())
}).debug())?;
add(&Layers::new(|add|{
add(&Background(Color::Rgb(128,64,0)))?;
add(&Border(Lozenge(border_style)))?;
add(&Margin::XY(4, 2, "---"))?;
Ok(())
}).debug())?;
add(&Layers::new(|add|{
add(&Background(Color::Rgb(96,64,0)))?;
add(&Border(SquareBold(border_style)))?;
add(&Margin::XY(6, 3, "~~~"))?;
Ok(())
}).debug())?;
Ok(())
})).debug())?;
Ok(())
}))
//Align::Center(Margin::X(1, Layers::new(|add|{
//add(&Background(Color::Rgb(128,0,0)))?;
//add(&Stack::down(|add|{
//add(&Margin::Y(1, Layers::new(|add|{
//add(&Background(Color::Rgb(0,128,0)))?;
//add(&Align::Center("12345"))?;
//add(&Align::Center("FOO"))
//})))?;
//add(&Margin::XY(1, 1, Layers::new(|add|{
//add(&Align::Center("1234567"))?;
//add(&Align::Center("BAR"))?;
//add(&Background(Color::Rgb(0,0,128)))
//})))
//}))
//})))
//Align::Y(Layers::new(|add|{
//add(&Background(Color::Rgb(128,0,0)))?;
//add(&Margin::X(1, Align::Center(Stack::down(|add|{
//add(&Align::X(Margin::Y(1, Layers::new(|add|{
//add(&Background(Color::Rgb(0,128,0)))?;
//add(&Align::Center("12345"))?;
//add(&Align::Center("FOO"))
//})))?;
//add(&Margin::XY(1, 1, Layers::new(|add|{
//add(&Align::Center("1234567"))?;
//add(&Align::Center("BAR"))?;
//add(&Background(Color::Rgb(0,0,128)))
//})))?;
//Ok(())
//})))))
//}))
}
}
impl Handle<TuiIn> for Demo<Tui> {
fn handle (&mut self, from: &TuiIn) -> Perhaps<bool> {
use KeyCode::{PageUp, PageDown};
match from.event() {
kexp!(PageUp) => {
self.index = (self.index + 1) % self.items.len();
},
kexp!(PageDown) => {
self.index = if self.index > 1 {
self.index - 1
} else {
self.items.len() - 1
};
},
_ => return Ok(None)
}
Ok(Some(true))
}
}
//lisp!(CONTENT Demo (LET
//(BORDER-STYLE (STYLE (FG (RGB 0 0 0))))
//(BG-COLOR-0 (RGB 0 128 128))
//(BG-COLOR-1 (RGB 128 96 0))
//(BG-COLOR-2 (RGB 128 64 0))
//(BG-COLOR-3 (RGB 96 64 0))
//(CENTER (LAYERS
//(BACKGROUND BG-COLOR-0)
//(OUTSET-XY 1 1 (SPLIT-DOWN
//(LAYERS (BACKGROUND BG-COLOR-1)
//(BORDER SQUARE BORDER-STYLE)
//(OUTSET-XY 2 1 "..."))
//(LAYERS (BACKGROUND BG-COLOR-2)
//(BORDER LOZENGE BORDER-STYLE)
//(OUTSET-XY 4 2 "---"))
//(LAYERS (BACKGROUND BG-COLOR-3)
//(BORDER SQUARE-BOLD BORDER-STYLE)
//(OUTSET-XY 2 1 "~~~"))))))))

View file

@ -1,28 +1,141 @@
use crate::*;
use std::marker::PhantomData;
use EdnItem::*;
pub type EdnCallback<'a, O, State> =
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
pub type EdnRenderCallback<'a, O, State> =
Box<EdnCallback<'a, O, State>>;
/// Provides values to the template
pub trait EdnViewData<E: Output> {
fn get_bool (&self, _sym: EdnItem<&str>) -> bool {
panic!("no content")
pub trait EdnViewData<E: Output>:
EdnProvide<bool> +
EdnProvide<usize> +
EdnProvide<E::Unit> +
EdnProvide<Box<dyn Render<E>>>
{
fn get_bool (&self, item: EdnItem<&str>) -> bool {
match &item {
Num(0) => false,
Num(_) => true,
Sym(":true") | Sym(":t") => true,
Sym(":false") | Sym(":f") => false,
_ => EdnProvide::get_or_fail(self, &item)
}
}
fn get_usize (&self, _sym: EdnItem<&str>) -> usize {
panic!("no content")
fn get_usize (&self, item: EdnItem<&str>) -> usize {
match &item {
Num(n) => *n,
_ => EdnProvide::get_or_fail(self, &item)
}
}
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> {
panic!("no content")
fn get_unit (&self, item: EdnItem<&str>) -> E::Unit {
match &item {
Num(n) => (*n as u16).into(),
_ => EdnProvide::get_or_fail(self, &item)
}
}
fn get_unit (&self, num: EdnItem<&str>) -> E::Unit {
if let EdnItem::Num(n) = num { (n as u16).into() } else { panic!("not a number") }
fn get_content <'a> (&'a self, item: EdnItem<&str>) -> Box<dyn Render<E> + 'a> where E: 'a {
match item.to_ref() {
Nil => Box::new(()),
Exp(e) => if let [head, tail @ ..] = e.as_slice() {
match (head, tail) {
(Key("when"), [c, a]) => When(
self.get_bool(c.to_ref()),
self.get_content(a.to_ref())).boxed(),
(Key("either"),[c, a, b]) => Either(
self.get_bool(c.to_ref()),
self.get_content(a.to_ref()),
self.get_content(b.to_ref())).boxed(),
(Key("align/c"), [a]) => Align::c(
self.get_content(a.to_ref())).boxed(),
(Key("align/x"), [a]) => Align::x(
self.get_content(a.to_ref())).boxed(),
(Key("align/y"), [a]) => Align::y(
self.get_content(a.to_ref())).boxed(),
(Key("align/n"), [a]) => Align::n(
self.get_content(a.to_ref())).boxed(),
(Key("align/s"), [a]) => Align::s(
self.get_content(a.to_ref())).boxed(),
(Key("align/e"), [a]) => Align::e(
self.get_content(a.to_ref())).boxed(),
(Key("align/w"), [a]) => Align::w(
self.get_content(a.to_ref())).boxed(),
(Key("align/nw"), [a]) => Align::nw(
self.get_content(a.to_ref())).boxed(),
(Key("align/ne"), [a]) => Align::ne(
self.get_content(a.to_ref())).boxed(),
(Key("align/sw"), [a]) => Align::sw(
self.get_content(a.to_ref())).boxed(),
(Key("align/se"), [a]) => Align::se(
self.get_content(a.to_ref())).boxed(),
(Key("bsp/a"), [a, b]) => Bsp::a(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("bsp/b"), [a, b]) => Bsp::b(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("bsp/e"), [a, b]) => Bsp::e(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("bsp/n"), [a, b]) => Bsp::n(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("bsp/s"), [a, b]) => Bsp::s(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("bsp/w"), [a, b]) => Bsp::w(
self.get_content(a.to_ref()), self.get_content(b.to_ref()),).boxed(),
(Key("fill/x"), [a]) => Fill::x(
self.get_content(a.to_ref())).boxed(),
(Key("fill/y"), [a]) => Fill::y(
self.get_content(a.to_ref())).boxed(),
(Key("fill/xy"), [a]) => Fill::xy(
self.get_content(a.to_ref())).boxed(),
(Key("fixed/x"), [x, a]) => Fixed::x(
self.get_unit(x.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("fixed/y"), [y, a]) => Fixed::y(
self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("fixed/xy"), [x, y, a]) => Fixed::xy(
self.get_unit(x.to_ref()), self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("max/x"), [x, a]) => Max::x(
self.get_unit(x.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("max/y"), [y, a]) => Max::y(
self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("push/x"), [x, a]) => Push::x(
self.get_unit(x.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("push/y"), [y, a]) => Push::y(
self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("pad/x"), [x, a]) => Padding::x(
self.get_unit(x.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("pad/y"), [y, a]) => Padding::y(
self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("pad/xy"), [x, y, a]) => Padding::xy(
self.get_unit(x.to_ref()), self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("grow/x"), [x, a]) => Margin::x(
self.get_unit(x.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("grow/y"), [y, a]) => Margin::y(
self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
(Key("grow/xy"), [x, y, a]) => Margin::xy(
self.get_unit(x.to_ref()), self.get_unit(y.to_ref()), self.get_content(a.to_ref())).boxed(),
_ => todo!("{:?} {:?}", &head, &tail)
}
} else {
panic!("invalid expression: {e:?}")
},
_ => EdnProvide::get_or_fail(self, &item)
}
//panic!("no content")
}
}
impl<E: Output, T> EdnViewData<E> for T where T:
EdnProvide<bool> +
EdnProvide<usize> +
EdnProvide<E::Unit> +
EdnProvide<Box<dyn Render<E>>>
{}
/// Renders from EDN source and context.
#[derive(Default)]