From fc038dbd97fa8943ec47a019e4b924939869a2fd Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 19 May 2025 00:06:31 +0300 Subject: [PATCH] wip: use only Dsl trait --- config/keys_arranger.edn | 2 +- crates/app/src/api.rs | 40 +++++--------------- crates/app/src/model.rs | 21 ++++------- crates/device/src/arranger/arranger_api.rs | 43 ++++------------------ crates/device/src/dialog/dialog_api.rs | 41 ++++++++++++++++++++- crates/device/src/pool/pool_api.rs | 7 +--- crates/engine/src/lib.rs | 2 +- crates/engine/src/midi/midi_in.rs | 1 + crates/engine/src/midi/midi_out.rs | 1 + deps/tengri | 2 +- 10 files changed, 71 insertions(+), 89 deletions(-) diff --git a/config/keys_arranger.edn b/config/keys_arranger.edn index a6411816..e3949d3f 100644 --- a/config/keys_arranger.edn +++ b/config/keys_arranger.edn @@ -7,7 +7,7 @@ (@shift-O project output-add) (@shift-S project scene-add) (@shift-T project track-add) -(@shift-D dialog :dialog-device) +(@shift-D dialog open :dialog-device) (@up select :select-scene-prev) (@down select :select-scene-next) diff --git a/crates/app/src/api.rs b/crates/app/src/api.rs index 4e381025..39902a89 100644 --- a/crates/app/src/api.rs +++ b/crates/app/src/api.rs @@ -7,7 +7,7 @@ macro_rules! cmd_todo { ($msg:literal) => {{ println!($msg); None }}; } handle!(TuiIn: |self: App, input|self.handle_tui_key_with_history(input)); impl App { fn handle_tui_key_with_history (&mut self, input: &TuiIn) -> Perhaps { - Ok(if let Some(command) = self.config.keys.command(self, input) { + Ok(if let Some(command) = self.config.keys.keybind_resolve(self, input)? { // FIXME failed commands not persisted in undo history let undo = command.clone().execute(self)?; self.history.push((command, undo)); @@ -38,6 +38,7 @@ impl AppCommand { }) } fn dialog (app: &mut App, command: DialogCommand) -> Perhaps { + panic!("dialog"); Ok(command.delegate(&mut app.dialog, |command|Self::Dialog{command})?) } fn project (app: &mut App, command: ArrangementCommand) -> Perhaps { @@ -111,33 +112,10 @@ impl AppCommand { //Ok(None) //} } -impl<'state> Context<'state, ClockCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self.clock(), iter) - } -} -impl<'state> Context<'state, MidiEditCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - self.editor().map(|e|Context::get(e, iter)).flatten() - } -} -impl<'state> Context<'state, PoolCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self.pool, iter) - } -} -impl<'state> Context<'state, SamplerCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - self.project.sampler().map(|p|Context::get(p, iter)).flatten() - } -} -impl<'state> Context<'state, ArrangementCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self.project, iter) - } -} -impl<'state> Context<'state, DialogCommand> for App { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} + +dsl!(ClockCommand: |self: App, iter|self.clock().take(iter)); +dsl!(MidiEditCommand: |self: App, iter|Ok(self.editor().map(|x|x.take(iter)).transpose()?.flatten())); +dsl!(PoolCommand: |self: App, iter|self.pool.take(iter)); +dsl!(SamplerCommand: |self: App, iter|Ok(self.project.sampler().map(|x|x.take(iter)).transpose()?.flatten())); +dsl!(ArrangementCommand: |self: App, iter|self.project.take(iter)); +dsl!(DialogCommand: |self: App, iter|Dsl::take(&self.dialog, iter)); diff --git a/crates/app/src/model.rs b/crates/app/src/model.rs index 9d833fab..f90f5978 100644 --- a/crates/app/src/model.rs +++ b/crates/app/src/model.rs @@ -332,7 +332,7 @@ pub struct Configuration { /// View definition pub view: TokenIter<'static>, // Input keymap - pub keys: InputMap<'static, App, AppCommand, TuiIn, TokenIter<'static>>, + pub keys: InputMap>, } impl Configuration { @@ -406,7 +406,7 @@ impl Configuration { } fn parse_keys (base: &impl AsRef, iter: Option>) - -> Usually>> + -> Usually>> { if iter.is_none() { return Err(format!("missing keys definition").into()) @@ -435,15 +435,13 @@ impl Configuration { "layer-if" => { let mut cond = None; - let next = exp.next(); - if let Some(Token { value: Value::Sym(sym), .. }) = next { + if let Some(Token { value: Value::Sym(sym), .. }) = exp.next() { cond = Some(leak(sym)); } else { return Err(format!("(e4) unexpected non-symbol {next:?}").into()) }; - let token = exp.peek(); - if let Some(Token { value: Value::Str(path), .. }) = token { + if let Some(Token { value: Value::Str(path), .. }) = exp.peek() { let path = base.as_ref().parent().unwrap().join(unquote(path)); if !std::fs::exists(&path)? { return Err(format!("(e5) not found: {path:?}").into()) @@ -454,13 +452,10 @@ impl Configuration { let cond = cond.unwrap(); println!("ok"); map.add_layer_if( - Box::new(move |state: &App|{ - Context::get(state, - &mut format!("{cond}").as_str().into()) - .unwrap_or_else(||panic!( - "missing input layer conditional {cond} from {exp:?}")) - }), - keys + Box::new(move |state: &App|Dsl::take_or_fail( + state, &mut exp.clone(), + format!("missing input layer conditional") + )), keys ); } else { return Err(format!("(e4) unexpected non-symbol {next:?}").into()) diff --git a/crates/device/src/arranger/arranger_api.rs b/crates/device/src/arranger/arranger_api.rs index 07377856..2feaae37 100644 --- a/crates/device/src/arranger/arranger_api.rs +++ b/crates/device/src/arranger/arranger_api.rs @@ -11,6 +11,13 @@ impl Arrangement { } } +dsl!(TrackCommand: |self: Arrangement, iter|self.take(iter)); +dsl!(MidiInputCommand: |self: Arrangement, iter|self.take(iter)); +dsl!(MidiOutputCommand: |self: Arrangement, iter|self.take(iter)); +dsl!(DeviceCommand: |self: Arrangement, iter|self.take(iter)); +dsl!(SceneCommand: |self: Arrangement, iter|self.take(iter)); +dsl!(ClipCommand: |self: Arrangement, iter|self.take(iter)); + #[tengri_proc::command(Arrangement)] impl ArrangementCommand { fn home (arranger: &mut Arrangement) -> Perhaps { @@ -243,39 +250,3 @@ impl ArrangementCommand { todo!() } } - -impl<'state> Context<'state, TrackCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} - -impl<'state> Context<'state, MidiInputCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} - -impl<'state> Context<'state, MidiOutputCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} - -impl<'state> Context<'state, DeviceCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} - -impl<'state> Context<'state, SceneCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} - -impl<'state> Context<'state, ClipCommand> for Arrangement { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - Context::get(&self, iter) - } -} diff --git a/crates/device/src/dialog/dialog_api.rs b/crates/device/src/dialog/dialog_api.rs index 9c39a26a..7d8bafb2 100644 --- a/crates/device/src/dialog/dialog_api.rs +++ b/crates/device/src/dialog/dialog_api.rs @@ -2,8 +2,47 @@ use crate::*; #[tengri_proc::command(Option)] impl DialogCommand { - fn dismiss (dialog: &mut Option) -> Perhaps { + fn open (dialog: &mut Option, new: Dialog) -> Perhaps { + *dialog = Some(new); + Ok(None) + } + fn close (dialog: &mut Option) -> Perhaps { *dialog = None; Ok(None) } } + +//#[derive(Clone, Debug)] +//pub enum DialogCommand { + //Open { dialog: Dialog }, + //Close +//} + +//impl Command> for DialogCommand { + //fn execute (self, state: &mut Option) -> Perhaps { + //match self { + //Self::Open { dialog } => { + //*state = Some(dialog); + //}, + //Self::Close => { + //*state = None; + //} + //}; + //Ok(None) + //} +//} + +//dsl!(DialogCommand: |self: Dialog, iter|todo!()); +//Dsl::take(&mut self.dialog, iter)); + +//#[tengri_proc::command(Option)]//Nope. +//impl DialogCommand { + //fn open (dialog: &mut Option, new: Dialog) -> Perhaps { + //*dialog = Some(new); + //Ok(None) + //} + //fn close (dialog: &mut Option) -> Perhaps { + //*dialog = None; + //Ok(None) + //} +//} diff --git a/crates/device/src/pool/pool_api.rs b/crates/device/src/pool/pool_api.rs index 27349f2f..6f9d8b90 100644 --- a/crates/device/src/pool/pool_api.rs +++ b/crates/device/src/pool/pool_api.rs @@ -81,11 +81,8 @@ impl PoolCommand { } -impl<'state> Context<'state, BrowserCommand> for Pool { - fn get <'source> (&'state self, iter: &mut TokenIter<'source>) -> Option { - self.browser.as_ref().map(|p|Context::get(p, iter)).flatten() - } -} +dsl!(BrowserCommand: |self: Pool, iter|Ok(self.browser + .as_ref().map(|p|p.take(iter)).transpose()?.flatten())); #[tengri_proc::command(Pool)] impl PoolClipCommand { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 31eb7e92..4791132d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -9,7 +9,7 @@ pub(crate) use std::sync::{Arc, atomic::{AtomicUsize, AtomicBool, Ordering::Rela pub(crate) use std::fmt::Debug; pub(crate) use std::ops::{Add, Sub, Mul, Div, Rem}; -pub(crate) use ::tengri::{from, Usually, Perhaps, Has, has, maybe_has, tui::*}; +pub(crate) use ::tengri::{from, Usually, Perhaps, Has, has, maybe_has, tui::*, dsl::*}; pub use ::atomic_float; pub(crate) use atomic_float::*; diff --git a/crates/engine/src/midi/midi_in.rs b/crates/engine/src/midi/midi_in.rs index 027025d0..f3282937 100644 --- a/crates/engine/src/midi/midi_in.rs +++ b/crates/engine/src/midi/midi_in.rs @@ -8,6 +8,7 @@ impl JackMidiIn { #[tengri_proc::command(JackMidiIn)] impl MidiInputCommand { + fn _todo_ (_port: &mut JackMidiIn) -> Perhaps { Ok(None) } } impl>> HasMidiIns for T { diff --git a/crates/engine/src/midi/midi_out.rs b/crates/engine/src/midi/midi_out.rs index 514f6487..bb896c2a 100644 --- a/crates/engine/src/midi/midi_out.rs +++ b/crates/engine/src/midi/midi_out.rs @@ -148,6 +148,7 @@ impl JackPortAutoconnect for JackMidiOut { #[tengri_proc::command(JackMidiOut)] impl MidiOutputCommand { + fn _todo_ (_port: &mut JackMidiOut) -> Perhaps { Ok(None) } } impl>> HasMidiOuts for T { diff --git a/deps/tengri b/deps/tengri index 3bc73932..90f5699f 160000 --- a/deps/tengri +++ b/deps/tengri @@ -1 +1 @@ -Subproject commit 3bc739328eed0c8fa67b432c7354c7929ddb505f +Subproject commit 90f5699fff48d2e8e0a24c36741a7d4ff771385d