ughhh needs special case

This commit is contained in:
🪞👃🪞 2025-01-18 16:03:06 +01:00
parent cf1fd5b45a
commit a362028ae7
4 changed files with 124 additions and 97 deletions

View file

@ -92,28 +92,36 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
}
};
}
/// Implement `Context` for a context and content type.
/// Implement `Context` for a context and the boolean type.
///
/// This enables support for layout expressions.
#[macro_export] macro_rules! provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<E> + 'a>> {
Some(match (atom.kind(), atom.text()) {
/// This enables support for boolean literals.
#[macro_export] macro_rules! provide_bool {
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<$T: $Trait> Context<$type> for $T {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(Value::Sym($pat) => $expr,)*
_ => return Context::get(self, atom)
})
}
}
};
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<$type> for $State {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom {
Value::Num(n) => match *n { 0 => false, _ => true },
Value::Sym(":false") | Value::Sym(":f") => false,
Value::Sym(":true") | Value::Sym(":t") => true,
$(Value::Sym($pat) => $expr,)*
_ => return None
})
}
}
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match (atom.kind(), atom.text()) {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
}
}
}
}

View file

@ -1,5 +1,5 @@
use crate::*;
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
#[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
impl<'a> TokenIter<'a> {

View file

@ -7,44 +7,35 @@ use Value::*;
/// * render callback (implementation of [Content])
/// * value providers (implementations of [Context])
#[macro_export] macro_rules! view {
($Output:ty: |$self:ident: $App:ty| $content:expr; {
($Output:ty: |$self:ident: $App:ty| $content:expr $(;{
$( $type:ty { $($sym:literal => $value:expr),* } );*
}) => {
})?) => {
impl Content<$Output> for $App {
fn content(&$self) -> impl Render<$Output> { $content }
}
$(
$($(
impl Context<$type> for $App {
fn get (&$self, atom: &Value) -> Option<$type> {
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
}
}
)*
)*)?
}
}
/// Implements `Context` for content components and expressions
#[macro_export] macro_rules! provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> {
Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
})
}
}
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + 'a>> {
Some(match atom.to_ref() {
$(Atom::Sym($pat) => $expr),*,
_ => return None
})
pub struct View<'a, T>(pub &'a T, pub TokenIter<'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
}
}
/// Renders from EDN source and context.
///
/// Generic over:
@ -163,3 +154,28 @@ pub type AtomRenderCallback<'a, O, State> =
}
}
}
/// Implement `Context` for a context and content type.
///
/// This enables support for layout expressions.
#[macro_export] macro_rules! provide_content {
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl<E: Output> Context<Box<dyn Render<E> + '_>> for $State {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + '_>> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
}
}
};
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
impl Context<Box<dyn Render<$Output> + '_>> for $State {
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + '_>> {
Some(match atom {
$(Value::Sym($pat) => $expr,)*
_ => return None
})
}
}
}
}

View file

@ -118,8 +118,6 @@ impl TekCli {
#[derive(Default, Debug)] struct Tek {
/// Must not be dropped for the duration of the process
pub jack: Arc<RwLock<JackConnection>>,
/// View definition
pub atom: String,
/// Source of time
pub clock: Clock,
/// Theme
@ -143,11 +141,14 @@ impl TekCli {
pub editing: AtomicBool,
pub history: Vec<TekCommand>,
keys: TokenIter<'static>,
keys_clip: TokenIter<'static>,
keys_track: TokenIter<'static>,
keys_scene: TokenIter<'static>,
keys_mix: TokenIter<'static>,
/// View definition
pub view: TokenIter<'static>,
// Input definitions
pub keys: TokenIter<'static>,
pub keys_clip: TokenIter<'static>,
pub keys_track: TokenIter<'static>,
pub keys_scene: TokenIter<'static>,
pub keys_mix: TokenIter<'static>,
}
has_size!(<TuiOut>|self: Tek|&self.size);
has_clock!(|self: Tek|self.clock);
@ -174,24 +175,23 @@ provide_num!(usize: |self: Tek| {
":track" => self.selected.track().unwrap_or(0),
":track-next" => (self.selected.track().unwrap_or(0) + 1).min(self.tracks.len()),
":track-prev" => self.selected.track().unwrap_or(0).saturating_sub(1) });
view!(TuiOut: |self: Tek| self.size.of(AtomView::from_source(self, self.atom.as_ref())); {
bool {};
isize {};
Color {};
Selection {};
Arc<RwLock<MidiClip>> {};
Option<Arc<RwLock<MidiClip>>> {};
// Provide sizes:
u16 {
view!(TuiOut: |self: Tek| self.size.of(View(self, self.view)));
provide_bool!(bool: |self: Tek| {});
provide_num!(isize: |self: Tek| {});
provide!(Color: |self: Tek| {});
provide!(Selection: |self: Tek| {});
provide!(Arc<RwLock<MidiClip>>: |self: Tek| {});
provide!(Option<Arc<RwLock<MidiClip>>>: |self: Tek| {});
provide_num!(u16: |self: Tek| {
":sidebar-w" => self.sidebar_w(),
":sample-h" => if self.is_editing() { 0 } else { 5 },
":samples-w" => if self.is_editing() { 4 } else { 11 },
":samples-y" => if self.is_editing() { 1 } else { 0 },
":pool-w" => if self.is_editing() { 5 } else {
let w = self.size.w();
if w > 60 { 20 } else if w > 40 { 15 } else { 10 } } };
// Provide components:
Box<dyn Render<TuiOut> + 'a> {
if w > 60 { 20 } else if w > 40 { 15 } else { 10 } }
});
provide_content!(TuiOut: |self: Tek| {
":editor" => (&self.editor).boxed(),
":pool" => self.view_pool().boxed(),
":sample" => self.view_sample(self.is_editing()).boxed(),
@ -204,7 +204,7 @@ view!(TuiOut: |self: Tek| self.size.of(AtomView::from_source(self, self.atom.as_
":scenes" => Outer(false, Style::default().fg(TuiTheme::g(0))).enclose_bg(self.view_row(
self.w(), self.size.h().saturating_sub(12) as u16,
self.scene_header(), self.clip_columns()
)).boxed() }});
)).boxed() });
impl Tek {
fn new_clock (
jack: &Arc<RwLock<JackConnection>>,
@ -212,12 +212,17 @@ impl Tek {
midi_froms: &[PortConnection], midi_tos: &[PortConnection],
) -> Usually<Self> {
let tek = Self {
atom: include_str!("./view_transport.edn").to_string(),
view: TokenIter(include_str!("./view_transport.edn")),
jack: jack.clone(),
color: ItemPalette::random(),
clock: Clock::new(jack, bpm),
midi_ins: vec![JackPort::<MidiIn>::new(jack, "GlobalI", midi_froms)?],
midi_outs: vec![JackPort::<MidiOut>::new(jack, "GlobalO", midi_tos)?],
keys: TokenIter(KEYS_APP),
keys_clip: TokenIter(KEYS_CLIP),
keys_track: TokenIter(KEYS_TRACK),
keys_scene: TokenIter(KEYS_SCENE),
keys_mix: TokenIter(KEYS_MIX),
..Default::default()
};
tek.sync_lead(sync_lead);
@ -232,7 +237,7 @@ impl Tek {
let clip = MidiClip::new("Clip", true, 384usize, None, Some(ItemColor::random().into()));
let clip = Arc::new(RwLock::new(clip));
Ok(Self {
atom: include_str!("./view_sequencer.edn").to_string(),
view: TokenIter(include_str!("./view_sequencer.edn")),
pool: Some((&clip).into()),
editor: Some((&clip).into()),
editing: false.into(),
@ -248,7 +253,7 @@ impl Tek {
audio_froms: &[&[PortConnection];2], audio_tos: &[&[PortConnection];2],
) -> Usually<Self> {
let app = Self {
atom: include_str!("./view_groovebox.edn").to_string(),
view: TokenIter(include_str!("./view_groovebox.edn")),
sampler: Some(Sampler::new(jack, &"sampler", midi_froms, audio_froms, audio_tos)?),
..Self::new_sequencer(jack, bpm, sync_lead, sync_follow, midi_froms, midi_tos)?
};
@ -265,7 +270,7 @@ impl Tek {
scenes: usize, tracks: usize, track_width: usize,
) -> Usually<Self> {
let mut arranger = Self {
atom: include_str!("./view_arranger.edn").to_string(),
view: TokenIter(include_str!("./view_arranger.edn")),
..Self::new_groovebox(
jack, bpm, sync_lead, sync_follow,
midi_froms, midi_tos, audio_froms, audio_tos,
@ -600,19 +605,17 @@ handle!(TuiIn: |self: Tek, input|Ok({
}
}
// Handle from root keymap
if let Some(command) = KeyMap::new(KEYS_APP)?.command::<_, TekCommand>(self, input) {
if let Some(command) = self.keys.command::<_, TekCommand, _>(self, input) {
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
return Ok(Some(true))
}
// Handle from selection-dependent keymaps
if let Some(command) = KeyMap::new(match self.selected() {
Selection::Clip(t, s) => KEYS_CLIP,
Selection::Track(t) => KEYS_TRACK,
Selection::Scene(s) => KEYS_SCENE,
Selection::Mix => KEYS_MIX,
})?.command::<_, TekCommand>(
self, input
) {
if let Some(command) = match self.selected() {
Selection::Clip(_, _) => self.keys_clip,
Selection::Track(_) => self.keys_track,
Selection::Scene(_) => self.keys_scene,
Selection::Mix => self.keys_mix,
}.command::<_, TekCommand, _>(self, input) {
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
return Ok(Some(true))
}