mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: providing content chunks with ednprovider
This commit is contained in:
parent
1ff35baea9
commit
8c54f8e426
8 changed files with 157 additions and 41 deletions
|
|
@ -3,6 +3,14 @@ use crate::*;
|
||||||
/// Implement `EdnProvide` for a type and context
|
/// Implement `EdnProvide` for a type and context
|
||||||
#[macro_export] macro_rules! edn_provide {
|
#[macro_export] macro_rules! edn_provide {
|
||||||
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||||
|
//impl EdnProvide<$type> for $State {
|
||||||
|
//fn get <S: AsRef<str>> (&$self, edn: &EdnItem<S>) -> Option<$type> {
|
||||||
|
//Some(match edn.to_ref() {
|
||||||
|
//$(EdnItem::Sym($pat) => $expr),*,
|
||||||
|
//_ => return None
|
||||||
|
//})
|
||||||
|
//}
|
||||||
|
//}
|
||||||
impl EdnProvide<$type> for $State {
|
impl EdnProvide<$type> for $State {
|
||||||
fn get <S: AsRef<str>> (&$self, edn: &EdnItem<S>) -> Option<$type> {
|
fn get <S: AsRef<str>> (&$self, edn: &EdnItem<S>) -> Option<$type> {
|
||||||
Some(match edn.to_ref() {
|
Some(match edn.to_ref() {
|
||||||
|
|
@ -7,6 +7,7 @@ mod edn_error; pub use self::edn_error::*;
|
||||||
mod edn_item; pub use self::edn_item::*;
|
mod edn_item; pub use self::edn_item::*;
|
||||||
mod edn_iter; pub use self::edn_iter::*;
|
mod edn_iter; pub use self::edn_iter::*;
|
||||||
mod edn_token; pub use self::edn_token::*;
|
mod edn_token; pub use self::edn_token::*;
|
||||||
|
mod edn_provide; pub use self::edn_provide::*;
|
||||||
|
|
||||||
#[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> {
|
#[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> {
|
||||||
use EdnItem::*;
|
use EdnItem::*;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ mod event_map; pub use self::event_map::*;
|
||||||
mod edn_command; pub use self::edn_command::*;
|
mod edn_command; pub use self::edn_command::*;
|
||||||
mod edn_input; pub use self::edn_input::*;
|
mod edn_input; pub use self::edn_input::*;
|
||||||
mod edn_keymap; pub use self::edn_keymap::*;
|
mod edn_keymap; pub use self::edn_keymap::*;
|
||||||
mod edn_provide; pub use self::edn_provide::*;
|
|
||||||
|
|
||||||
pub(crate) use ::tek_edn::EdnItem;
|
pub(crate) use ::tek_edn::EdnItem;
|
||||||
/// Standard error trait.
|
/// Standard error trait.
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,141 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use EdnItem::*;
|
use EdnItem::*;
|
||||||
|
|
||||||
pub type EdnCallback<'a, O, State> =
|
pub type EdnCallback<'a, O, State> =
|
||||||
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
dyn Fn(&'a State)-> RenderBox<'a, O> + Send + Sync + 'a;
|
||||||
|
|
||||||
pub type EdnRenderCallback<'a, O, State> =
|
pub type EdnRenderCallback<'a, O, State> =
|
||||||
Box<EdnCallback<'a, O, State>>;
|
Box<EdnCallback<'a, O, State>>;
|
||||||
|
|
||||||
/// Provides values to the template
|
/// Provides values to the template
|
||||||
pub trait EdnViewData<E: Output> {
|
pub trait EdnViewData<E: Output>:
|
||||||
fn get_bool (&self, _sym: EdnItem<&str>) -> bool {
|
EdnProvide<bool> +
|
||||||
panic!("no content")
|
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_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> {
|
fn get_usize (&self, item: EdnItem<&str>) -> usize {
|
||||||
panic!("no content")
|
match &item {
|
||||||
|
Num(n) => *n,
|
||||||
|
_ => 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_unit (&self, item: EdnItem<&str>) -> E::Unit {
|
||||||
|
match &item {
|
||||||
|
Num(n) => (*n as u16).into(),
|
||||||
|
_ => EdnProvide::get_or_fail(self, &item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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.
|
/// Renders from EDN source and context.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,10 @@ pub(crate) use std::time::Duration;
|
||||||
pub(crate) use std::path::PathBuf;
|
pub(crate) use std::path::PathBuf;
|
||||||
pub(crate) use std::ffi::OsString;
|
pub(crate) use std::ffi::OsString;
|
||||||
|
|
||||||
|
#[cfg(test)] fn test_tek () {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
//#[cfg(test)] mod test_focus {
|
//#[cfg(test)] mod test_focus {
|
||||||
//use super::focus::*;
|
//use super::focus::*;
|
||||||
//#[test] fn test_focus () {
|
//#[test] fn test_focus () {
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,22 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
render!(TuiOut: (self: App) => self.size.of(EdnView::from_source(self, self.edn.as_ref())));
|
render!(TuiOut: (self: App) => self.size.of(EdnView::from_source(self, self.edn.as_ref())));
|
||||||
impl EdnViewData<TuiOut> for &App {
|
edn_provide!(Box<dyn Render<TuiOut>>: |self: App|{
|
||||||
fn get_content <'a> (&'a self, item: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> {
|
":editor" => (&self.editor).boxed(),
|
||||||
use EdnItem::*;
|
":inputs" => self.input_row(self.w(), 3).boxed(),
|
||||||
let w = self.tracks_with_sizes().last().map(|x|x.3 as u16).unwrap_or(0);
|
":outputs" => self.output_row(self.w(), 3).boxed(),
|
||||||
match item {
|
":pool" => self.pool().boxed(),
|
||||||
Nil => Box::new(()),
|
":sample" => self.sample().boxed(),
|
||||||
Exp(items) => Box::new(EdnView::from_items(*self, items.as_slice())),
|
":sampler" => self.sampler().boxed(),
|
||||||
Sym(":editor") => (&self.editor).boxed(),
|
":scenes" => self.scene_row(self.w(), self.size.h().saturating_sub(9) as u16).boxed(),
|
||||||
Sym(":inputs") => self.input_row(w, 3).boxed(),
|
":status" => self.status(0).boxed(),
|
||||||
Sym(":outputs") => self.output_row(w, 3).boxed(),
|
":toolbar" => self.toolbar().boxed(),
|
||||||
Sym(":pool") => self.pool().boxed(),
|
":tracks" => self.track_row(self.w(), 3).boxed(),
|
||||||
Sym(":sample") => self.sample().boxed(),
|
});
|
||||||
Sym(":sampler") => self.sampler().boxed(),
|
|
||||||
Sym(":scenes") => self.scene_row(w, self.size.h().saturating_sub(9) as u16).boxed(),
|
|
||||||
Sym(":status") => self.status(0).boxed(),
|
|
||||||
Sym(":toolbar") => self.toolbar().boxed(),
|
|
||||||
Sym(":tracks") => self.track_row(w, 3).boxed(),
|
|
||||||
_ => panic!("no content for {item:?}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn get_unit (&self, item: EdnItem<&str>) -> u16 {
|
|
||||||
EdnProvide::<u16>::get_or_fail(self, &item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn compact (&self) -> bool { false }
|
pub fn compact (&self) -> bool { false }
|
||||||
|
fn w (&self) -> u16 {
|
||||||
|
self.tracks_with_sizes().last().map(|x|x.3 as u16).unwrap_or(0)
|
||||||
|
}
|
||||||
fn toolbar (&self) -> impl Content<TuiOut> + use<'_> {
|
fn toolbar (&self) -> impl Content<TuiOut> + use<'_> {
|
||||||
Fill::x(Fixed::y(2, Align::x(ClockView::new(true, &self.clock))))
|
Fill::x(Fixed::y(2, Align::x(ClockView::new(true, &self.clock))))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue