diff --git a/edn/src/edn_provide.rs b/edn/src/edn_provide.rs index 9a79c80a..e47cb4ac 100644 --- a/edn/src/edn_provide.rs +++ b/edn/src/edn_provide.rs @@ -25,7 +25,7 @@ use crate::*; } /// Map EDN tokens to parameters of a given type for a given context -pub trait EdnProvide<'a, U> { +pub trait EdnProvide<'a, U>: Sized { fn get > (&'a self, _edn: &'a EdnItem) -> Option { None } diff --git a/edn/src/lib.rs b/edn/src/lib.rs index 1e9f10c0..c7d8b783 100644 --- a/edn/src/lib.rs +++ b/edn/src/lib.rs @@ -3,11 +3,12 @@ pub(crate) use std::{fmt::{Debug, Formatter, Error as FormatError}}; -mod edn_error; pub use self::edn_error::*; -mod edn_item; pub use self::edn_item::*; -mod edn_iter; pub use self::edn_iter::*; -mod edn_token; pub use self::edn_token::*; +mod edn_error; pub use self::edn_error::*; +mod edn_item; pub use self::edn_item::*; +mod edn_iter; pub use self::edn_iter::*; +mod edn_token; pub use self::edn_token::*; mod edn_provide; pub use self::edn_provide::*; +mod try_from_edn; pub use self::try_from_edn::*; #[cfg(test)] #[test] fn test_edn () -> Result<(), ParseError> { use EdnItem::*; diff --git a/edn/src/try_from_edn.rs b/edn/src/try_from_edn.rs new file mode 100644 index 00000000..84bae227 --- /dev/null +++ b/edn/src/try_from_edn.rs @@ -0,0 +1,6 @@ +use crate::*; + +pub trait TryFromEdn<'a, T>: Sized { + fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> + Option; +} diff --git a/midi/src/midi_pool_tui.rs b/midi/src/midi_pool_tui.rs index 28127243..9e0e6390 100644 --- a/midi/src/midi_pool_tui.rs +++ b/midi/src/midi_pool_tui.rs @@ -19,8 +19,8 @@ render!(TuiOut: (self: PoolView<'a>) => { Fixed::y(1, map_south(item_offset, item_height, Tui::bg(bg, lay!( Fill::x(Align::w(Tui::fg(fg, Tui::bold(selected, name)))), Fill::x(Align::e(Tui::fg(fg, Tui::bold(selected, length)))), - Fill::x(Align::w(When(selected, Tui::bold(true, Tui::fg(TuiTheme::g(255), "▶"))))), - Fill::x(Align::e(When(selected, Tui::bold(true, Tui::fg(TuiTheme::g(255), "◀"))))), + Fill::x(Align::w(When::new(selected, Tui::bold(true, Tui::fg(TuiTheme::g(255), "▶"))))), + Fill::x(Align::e(When::new(selected, Tui::bold(true, Tui::fg(TuiTheme::g(255), "◀"))))), )))) }))))) }); diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index 613ab0a0..af6fbf46 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -37,52 +37,60 @@ pub trait EdnViewData<'a, E: Output>: match item { Nil => Box::new(()), Exp(e) => if let [head, tail @ ..] = e.as_slice() { + if let Some(builtin) = When::<_, Box>>::try_from_edn(self, head, tail) { + return builtin.boxed() + } + if let Some(builtin) = Either::<_, Box>, Box>>::try_from_edn(self, head, tail) { + return builtin.boxed() + } + panic!("{item:?}"); + match (head, tail) { - (Key("when"), [c, a]) => - When(self.arg(c).unwrap(), self.item(a)).boxed(), - (Key("either"), [c, a, b]) => - Either(self.arg(c).unwrap(), self.item(a), self.item(b)).boxed(), + //(Key("when"), [c, a]) => + //When(self.arg(c).unwrap(), self.get_content(a)).boxed(), + //(Key("either"), [c, a, b]) => + //Either(self.arg(c).unwrap(), self.get_content(a), self.get_content(b)).boxed(), - (Key("align/c"), [a]) => Align::c(self.item(a)).boxed(), - (Key("align/x"), [a]) => Align::x(self.item(a)).boxed(), - (Key("align/y"), [a]) => Align::y(self.item(a)).boxed(), - (Key("align/n"), [a]) => Align::n(self.item(a)).boxed(), - (Key("align/s"), [a]) => Align::s(self.item(a)).boxed(), - (Key("align/e"), [a]) => Align::e(self.item(a)).boxed(), - (Key("align/w"), [a]) => Align::w(self.item(a)).boxed(), - (Key("align/nw"), [a]) => Align::nw(self.item(a)).boxed(), - (Key("align/ne"), [a]) => Align::ne(self.item(a)).boxed(), - (Key("align/sw"), [a]) => Align::sw(self.item(a)).boxed(), - (Key("align/se"), [a]) => Align::se(self.item(a)).boxed(), + (Key("align/c"), [a]) => Align::c(self.get_content(a)).boxed(), + (Key("align/x"), [a]) => Align::x(self.get_content(a)).boxed(), + (Key("align/y"), [a]) => Align::y(self.get_content(a)).boxed(), + (Key("align/n"), [a]) => Align::n(self.get_content(a)).boxed(), + (Key("align/s"), [a]) => Align::s(self.get_content(a)).boxed(), + (Key("align/e"), [a]) => Align::e(self.get_content(a)).boxed(), + (Key("align/w"), [a]) => Align::w(self.get_content(a)).boxed(), + (Key("align/nw"), [a]) => Align::nw(self.get_content(a)).boxed(), + (Key("align/ne"), [a]) => Align::ne(self.get_content(a)).boxed(), + (Key("align/sw"), [a]) => Align::sw(self.get_content(a)).boxed(), + (Key("align/se"), [a]) => Align::se(self.get_content(a)).boxed(), - (Key("bsp/a"), [a, b]) => Bsp::a(self.item(a), self.item(b),).boxed(), - (Key("bsp/b"), [a, b]) => Bsp::b(self.item(a), self.item(b),).boxed(), - (Key("bsp/e"), [a, b]) => Bsp::e(self.item(a), self.item(b),).boxed(), - (Key("bsp/n"), [a, b]) => Bsp::n(self.item(a), self.item(b),).boxed(), - (Key("bsp/s"), [a, b]) => Bsp::s(self.item(a), self.item(b),).boxed(), - (Key("bsp/w"), [a, b]) => Bsp::w(self.item(a), self.item(b),).boxed(), + (Key("bsp/a"), [a, b]) => Bsp::a(self.get_content(a), self.get_content(b),).boxed(), + (Key("bsp/b"), [a, b]) => Bsp::b(self.get_content(a), self.get_content(b),).boxed(), + (Key("bsp/e"), [a, b]) => Bsp::e(self.get_content(a), self.get_content(b),).boxed(), + (Key("bsp/n"), [a, b]) => Bsp::n(self.get_content(a), self.get_content(b),).boxed(), + (Key("bsp/s"), [a, b]) => Bsp::s(self.get_content(a), self.get_content(b),).boxed(), + (Key("bsp/w"), [a, b]) => Bsp::w(self.get_content(a), self.get_content(b),).boxed(), - (Key("fill/x"), [a]) => Fill::x(self.item(a)).boxed(), - (Key("fill/y"), [a]) => Fill::y(self.item(a)).boxed(), - (Key("fill/xy"), [a]) => Fill::xy(self.item(a)).boxed(), + (Key("fill/x"), [a]) => Fill::x(self.get_content(a)).boxed(), + (Key("fill/y"), [a]) => Fill::y(self.get_content(a)).boxed(), + (Key("fill/xy"), [a]) => Fill::xy(self.get_content(a)).boxed(), - (Key("fixed/x"), [x, a]) => Fixed::x(self.arg(x).unwrap(), self.item(a)).boxed(), - (Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.item(a)).boxed(), - (Key("fixed/xy"), [x, y, a]) => Fixed::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(), + (Key("fixed/x"), [x, a]) => Fixed::x(self.arg(x).unwrap(), self.get_content(a)).boxed(), + (Key("fixed/y"), [y, a]) => Fixed::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), + (Key("fixed/xy"), [x, y, a]) => Fixed::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(), - (Key("max/x"), [x, a]) => Max::x(self.arg(x).unwrap(), self.item(a)).boxed(), - (Key("max/y"), [y, a]) => Max::y(self.arg(y).unwrap(), self.item(a)).boxed(), + (Key("max/x"), [x, a]) => Max::x(self.arg(x).unwrap(), self.get_content(a)).boxed(), + (Key("max/y"), [y, a]) => Max::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), - (Key("push/x"), [x, a]) => Push::x(self.arg(x).unwrap(), self.item(a)).boxed(), - (Key("push/y"), [y, a]) => Push::y(self.arg(y).unwrap(), self.item(a)).boxed(), + (Key("push/x"), [x, a]) => Push::x(self.arg(x).unwrap(), self.get_content(a)).boxed(), + (Key("push/y"), [y, a]) => Push::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), - (Key("pad/x"), [x, a]) => Padding::x(self.arg(x).unwrap(), self.item(a)).boxed(), - (Key("pad/y"), [y, a]) => Padding::y(self.arg(y).unwrap(), self.item(a)).boxed(), - (Key("pad/xy"), [x, y, a]) => Padding::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(), + (Key("pad/x"), [x, a]) => Padding::x(self.arg(x).unwrap(), self.get_content(a)).boxed(), + (Key("pad/y"), [y, a]) => Padding::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), + (Key("pad/xy"), [x, y, a]) => Padding::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(), - (Key("grow/x"), [x, a]) => Margin::x(self.arg(x).unwrap(), self.item(a)).boxed(), - (Key("grow/y"), [y, a]) => Margin::y(self.arg(y).unwrap(), self.item(a)).boxed(), - (Key("grow/xy"), [x, y, a]) => Margin::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.item(a)).boxed(), + (Key("grow/x"), [x, a]) => Margin::x(self.arg(x).unwrap(), self.get_content(a)).boxed(), + (Key("grow/y"), [y, a]) => Margin::y(self.arg(y).unwrap(), self.get_content(a)).boxed(), + (Key("grow/xy"), [x, y, a]) => Margin::xy(self.arg(x).unwrap(), self.arg(y).unwrap(), self.get_content(a)).boxed(), _ => todo!("{:?} {:?}", &head, &tail) } @@ -103,7 +111,7 @@ impl<'a, E: Output, T> EdnViewData<'a, E> for T where T: /// Renders from EDN source and context. #[derive(Default)] -pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E>> { +pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> { #[default] Inert, _Unused(PhantomData<&'a E>), @@ -111,12 +119,21 @@ pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E>> { //render: BoxBox + Send + Sync + 'a> + Send + Sync + 'a> Err(String) } +impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> std::fmt::Debug for EdnView<'a, E, T> { + fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + match self { + Self::Inert | Self::_Unused(_) => write!(f, "EdnView::Inert"), + Self::Ok(state, view) => write!(f, "EdnView::Ok(state={state:?} view={view:?}"), + Self::Err(error) => write!(f, "EdnView::Err({error})"), + } + } +} -impl<'a, E: Output, T: EdnViewData<'a, E>> EdnView<'a, E, T> { +impl<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> EdnView<'a, E, T> { pub fn from_source (state: T, source: &'a str) -> Self { match EdnItem::read_one(&source) { Ok((layout, _)) => Self::Ok(state, layout), - Err(error) => Self::Err(format!("{error}")) + Err(error) => Self::Err(format!("{error} in {source}")) } } pub fn from_items (state: T, items: Vec>) -> Self { @@ -124,14 +141,13 @@ impl<'a, E: Output, T: EdnViewData<'a, E>> EdnView<'a, E, T> { } } -implEdnViewData<'a, E> + Send + Sync> Content for EdnView<'_, E, T> { +implEdnViewData<'a, E> + Send + Sync + std::fmt::Debug> Content for EdnView<'_, E, T> { fn content (&self) -> impl Render { match self { - Self::Ok(state, layout) => { - state.get_content(layout) - }, + Self::Ok(state, layout) => state.get_content(layout), Self::Err(_error) => { - Box::new(())//&format!("EdnView error: {error:?}")) // FIXME: String is not Render + panic!("{self:?}"); + //TODO:&format!("EdnView error: {error:?}")) // FIXME: String is not Render }, _ => todo!() } diff --git a/output/src/either.rs b/output/src/either.rs index df904a44..f796da41 100644 --- a/output/src/either.rs +++ b/output/src/either.rs @@ -1,15 +1,38 @@ use crate::*; - /// Show one item if a condition is true and another if the condition is false -pub struct Either(pub bool, pub A, pub B); - -impl, B: Render> Content for Either { +pub struct Either(pub PhantomData, pub bool, pub A, pub B); +impl Either { + pub fn new (c: bool, a: A, b: B) -> Self { + Self(Default::default(), c, a, b) + } +} +impl<'a, T, E, A, B> TryFromEdn<'a, T> for Either +where + T: EdnProvide<'a, bool> + EdnProvide<'a, B> + EdnProvide<'a, A> + 'a, + E: Output, + A: Render + 'a, + B: Render + 'a, +{ + fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option { + use EdnItem::*; + if let (Key("either"), [condition, content, alternative]) = (head, tail) { + Some(Self::new( + state.get(condition).expect("either: no condition"), + state.get(content).expect("either: no content"), + state.get(alternative).expect("either: no alternative") + )) + } else { + None + } + } +} +impl, B: Render> Content for Either { fn layout (&self, to: E::Area) -> E::Area { - let Self(cond, a, b) = self; + 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; + let Self(_, cond, a, b) = self; if *cond { a.render(to) } else { b.render(to) } } } diff --git a/output/src/when.rs b/output/src/when.rs index 271ce281..7ee96bb0 100644 --- a/output/src/when.rs +++ b/output/src/when.rs @@ -1,11 +1,35 @@ use crate::*; - /// Show an item only when a condition is true. -pub struct When(pub bool, pub A); - -impl> Content for When { +pub struct When(pub PhantomData, pub bool, pub A); +impl When { + pub fn new (c: bool, a: A) -> Self { + Self(Default::default(), c, a) + } +} +impl<'a, T, E, A> TryFromEdn<'a, T> for When +where + T: EdnProvide<'a, bool> + EdnProvide<'a, A> + 'a, + E: Output, + A: Render + 'a +{ + fn try_from_edn ( + state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>] + ) -> Option { + use EdnItem::*; + if let (Key("when"), [condition, content]) = (head, tail) { + Some(Self( + Default::default(), + state.get(condition).expect("when: no condition"), + state.get(content).expect("when: no content") + )) + } else { + None + } + } +} +impl> Content for When { fn layout (&self, to: E::Area) -> E::Area { - let Self(cond, item) = self; + let Self(_, cond, item) = self; let mut area = E::Area::zero(); if *cond { let item_area = item.layout(to); @@ -17,7 +41,7 @@ impl> Content for When { area.into() } fn render (&self, to: &mut E) { - let Self(cond, item) = self; + let Self(_, cond, item) = self; if *cond { item.render(to) } } } diff --git a/tek/src/arranger-view.edn b/tek/src/arranger-view.edn index 2e5088de..669e7852 100644 --- a/tek/src/arranger-view.edn +++ b/tek/src/arranger-view.edn @@ -1,3 +1 @@ -(bsp/s (fill/x (fixed/y (align/x :toolbar))) - (fill/x (align/c (bsp/w (align/e fixed/x :sidebar-w :pool)) - (bsp/n :outputs (bsp/n :inputs (bsp/n :tracks :scenes))))))) +(fill/x :toolbar) diff --git a/tek/src/lib.rs b/tek/src/lib.rs index 7b9c710a..b2d3c8f9 100644 --- a/tek/src/lib.rs +++ b/tek/src/lib.rs @@ -28,7 +28,7 @@ pub use ::tek_plugin::{self, *}; pub(crate) use std::error::Error; pub(crate) use std::sync::atomic::{AtomicBool, Ordering::{self, *}}; pub(crate) use std::sync::{Arc, RwLock}; -#[derive(Default)] pub struct App { +#[derive(Default, Debug)] pub struct App { pub jack: Arc>, pub edn: String, pub clock: Clock, @@ -202,9 +202,6 @@ has_clock!(|self: Track|self.player.clock()); has_player!(|self: Track|self.player); impl Track { const MIN_WIDTH: usize = 9; - fn longest_name (tracks: &[Self]) -> usize { - tracks.iter().map(|s|s.name.len()).fold(0, usize::max) - } fn width_inc (&mut self) { self.width += 1; } @@ -225,6 +222,9 @@ pub trait HasTracks: HasSelection + HasClock + HasJack + HasEditor + Send + Sync fn midi_outs (&self) -> &Vec>; fn tracks (&self) -> &Vec; fn tracks_mut (&mut self) -> &mut Vec; + fn track_longest (&self) -> usize { + self.tracks().iter().map(|s|s.name.len()).fold(0, usize::max) + } fn tracks_sizes <'a> (&'a self, editing: bool, bigger: usize) -> impl Iterator + Send + Sync + 'a { @@ -341,9 +341,6 @@ impl Device for Plugin {} color: ItemPalette, } impl Scene { - pub fn longest_name (scenes: &[Self]) -> usize { - scenes.iter().map(|s|s.name.len()).fold(0, usize::max) - } /// Returns the pulse length of the longest clip in the scene pub fn pulses (&self) -> usize { self.clips.iter().fold(0, |a, p|{ @@ -397,6 +394,9 @@ impl HasScenes for App { pub trait HasScenes: HasSelection + HasEditor + Send + Sync { fn scenes (&self) -> &Vec; fn scenes_mut (&mut self) -> &mut Vec; + fn scene_longest (&self) -> usize { + self.scenes().iter().map(|s|s.name.len()).fold(0, usize::max) + } fn scenes_sizes (&self, editing: bool, height: usize, larger: usize,) -> impl Iterator + Send + Sync { @@ -943,7 +943,7 @@ pub trait Arrangement: HasEditor + HasTracks + HasScenes + HasSelection + HasClo bg.into(), bg.into(), )); - let cell = Either(active, editor, cell); + let cell = Either::new(active, editor, cell); *last_color.write().unwrap() = bg.into(); map_south( y1 as u16, diff --git a/time/src/clock_tui.rs b/time/src/clock_tui.rs index 4c371db2..c09ca8c6 100644 --- a/time/src/clock_tui.rs +++ b/time/src/clock_tui.rs @@ -39,11 +39,11 @@ render!(TuiOut: (self: ClockView<'a>) => Outer( pub struct PlayPause { pub compact: bool, pub playing: bool } render!(TuiOut: (self: PlayPause) => Tui::bg( if self.playing{Color::Rgb(0,128,0)}else{Color::Rgb(128,64,0)}, - Either(self.compact, - Thunk::new(||Fixed::x(9, Either(self.playing, + Either::new(self.compact, + Thunk::new(||Fixed::x(9, Either::new(self.playing, Tui::fg(Color::Rgb(0, 255, 0), " PLAYING "), Tui::fg(Color::Rgb(255, 128, 0), " STOPPED ")))), - Thunk::new(||Fixed::x(5, Either(self.playing, + Thunk::new(||Fixed::x(5, Either::new(self.playing, Tui::fg(Color::Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)), Tui::fg(Color::Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",)))))))); @@ -63,7 +63,7 @@ impl BeatStats { Self { compact, bpm, beat, time } } } -render!(TuiOut: (self: BeatStats) => Either(self.compact, +render!(TuiOut: (self: BeatStats) => Either::new(self.compact, row!( FieldV(TuiTheme::g(128).into(), "BPM", &self.bpm), FieldV(TuiTheme::g(128).into(), "Beat", &self.beat), @@ -92,7 +92,7 @@ impl OutputStats { } } } -render!(TuiOut: (self: OutputStats) => Either(self.compact, +render!(TuiOut: (self: OutputStats) => Either::new(self.compact, row!( FieldV(TuiTheme::g(128).into(), "SR", &self.sample_rate), FieldV(TuiTheme::g(128).into(), "Buf", &self.buffer_size), diff --git a/time/src/perf.rs b/time/src/perf.rs index 7d659867..b7e0adb5 100644 --- a/time/src/perf.rs +++ b/time/src/perf.rs @@ -1,6 +1,7 @@ use crate::*; /// Performance counter +#[derive(Debug)] pub struct PerfModel { pub enabled: bool, clock: quanta::Clock, diff --git a/tui/src/tui_content.rs b/tui/src/tui_content.rs index 2ff25470..1a8838db 100644 --- a/tui/src/tui_content.rs +++ b/tui/src/tui_content.rs @@ -112,7 +112,7 @@ pub fn phat_sel_3 > ( selected: bool, field_1: T, field_2: T, top: Option, middle: Color, bottom: Color ) -> impl Content { let border = Style::default().fg(Color::Rgb(255,255,255)).bg(middle); - Either(selected, + Either::new(selected, Tui::bg(middle, Outer(border).enclose(Align::w(Bsp::s("", Bsp::n("", Fill::y(field_1)))))), Bsp::s(Fixed::y(1, top.map(|top|phat_lo(middle, top))), Bsp::n(Fixed::y(1, phat_hi(middle, bottom)),