wip: move to layered handlers
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-04-28 04:49:28 +03:00
parent 3561867640
commit 5696cbbebb
6 changed files with 45 additions and 68 deletions

View file

@ -6,16 +6,6 @@ handle!(TuiIn: |self: Tek, input|if let Some(handler) = self.handler {
Ok(None)
});
fn layer (app: &mut Tek, input: &TuiIn, keymap: &str) -> Usually<bool> {
if let Some(command) = SourceIter(keymap).command::<_, TekCommand, _>(app, input) {
if let Some(undo) = command.execute(app)? {
app.history.push(undo);
}
return Ok(true)
}
return Ok(false)
}
pub fn handle_arranger (app: &mut Tek, input: &TuiIn) -> Perhaps<bool> {
Ok((app.is_editing() && app.editor.handle(input)? == Some(true) ||
layer(app, input, include_str!("../../../config/keys_global.edn"))? ||
@ -43,34 +33,12 @@ pub fn handle_sampler (app: &mut Tek, input: &TuiIn) -> Perhaps<bool> {
layer(app, input, include_str!("../../../config/keys_sampler.edn"))?).then_some(true))
}
// TODO:
//#[derive(Default)]
//struct Handler<T, U> {
//layers: Vec<(Box<dyn Fn(&T)->bool>, &'static str)>,
//__: std::marker::PhantomData<U>
//}
//impl<T, U: AtomInput> Handler<T, U> {
//fn layer (mut self, condition: impl Fn(&T)->bool + 'static, keymap: &'static str) -> Self {
//self.layers.push((Box::new(condition), keymap));
//self
//}
//fn layer_if (mut self, keymap: &'static str) -> Self {
//self.layers.push((Box::new(|_|true), keymap));
//self
//}
//fn handle <'a, C: Command<T> + TryFromAtom<'a, T>> (&self, state: &mut T, input: &U) -> Perhaps<bool> {
//for layer in self.layers.iter() {
//if !(layer.0)(state) {
//continue
//}
//if let Some(command) = SourceIter(layer.1).command::<_, C, _>(state, input) {
//if let Some(undo) = command.execute(state)? {
////app.history.push(undo);
//}
//return Ok(Some(true))
//}
//}
//Ok(None)
//}
//}
fn layer (app: &mut Tek, input: &TuiIn, keymap: &str) -> Usually<bool> {
if let Some(command) = SourceIter(keymap).command::<_, TekCommand, _>(app, input) {
if let Some(undo) = command.execute(app)? {
app.history.push(undo);
}
return Ok(true)
}
return Ok(false)
}

View file

@ -5,7 +5,7 @@ use crate::*;
pub struct MidiEditor {
pub mode: PianoHorizontal,
pub size: Measure<TuiOut>,
pub keys: SourceIter<'static>
pub keys: InputMap<'static, Self, MidiEditCommand, TuiIn>
}
impl std::fmt::Debug for MidiEditor {
@ -21,7 +21,8 @@ impl Default for MidiEditor {
Self {
mode: PianoHorizontal::new(None),
size: Measure::new(),
keys: SourceIter(include_str!("../../../../config/keys_edit.edn")),
keys: InputMap::new()
.layer(SourceIter(include_str!("../../../../config/keys_edit.edn"))),
}
}
}
@ -206,7 +207,7 @@ atom_command!(MidiEditCommand: |state: MidiEditor| {
}
handle!(TuiIn: |self: MidiEditor, input|{
Ok(if let Some(command) = self.keys.command::<_, MidiEditCommand, _>(self, input) {
Ok(if let Some(command) = self.keys.command(self, input) {
let _undo = command.execute(self)?;
Some(true)
} else {

View file

@ -1,12 +1,13 @@
use crate::*;
handle!(TuiIn: |self: MidiPool, input|{
Ok(if let Some(command) = match self.mode() {
Some(PoolMode::Rename(..)) => self.keys_rename,
Some(PoolMode::Length(..)) => self.keys_length,
Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => self.keys_file,
_ => self.keys
}.command::<Self, PoolCommand, TuiIn>(self, input) {
//Ok(if let Some(command) = match self.mode() {
//Some(PoolMode::Rename(..)) => self.keys_rename,
//Some(PoolMode::Length(..)) => self.keys_length,
//Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => self.keys_file,
//_ => self.keys
//}.command::<Self, PoolCommand, TuiIn>(self, input) {
Ok(if let Some(command) = self.keys.command(self, input) {
let _undo = command.execute(self)?;
Some(true)
} else {

View file

@ -1,6 +1,6 @@
use crate::*;
#[derive(Debug)]
//#[derive(Debug)]
pub struct MidiPool {
pub visible: bool,
/// Collection of clips
@ -10,23 +10,30 @@ pub struct MidiPool {
/// Mode switch
pub mode: Option<PoolMode>,
pub keys: SourceIter<'static>,
pub keys_rename: SourceIter<'static>,
pub keys_length: SourceIter<'static>,
pub keys_file: SourceIter<'static>,
pub keys: InputMap<'static, Self, PoolCommand, TuiIn>,
//pub keys: SourceIter<'static>,
//pub keys_rename: SourceIter<'static>,
//pub keys_length: SourceIter<'static>,
//pub keys_file: SourceIter<'static>,
}
impl Default for MidiPool {
fn default () -> Self {
use PoolMode::*;
Self {
visible: true,
clips: Arc::from(RwLock::from(vec![])),
clip: 0.into(),
mode: None,
keys: SourceIter(include_str!("../../../../config/keys_pool.edn")),
keys_file: SourceIter(include_str!("../../../../config/keys_pool_file.edn")),
keys_rename: SourceIter(include_str!("../../../../config/keys_clip_rename.edn")),
keys_length: SourceIter(include_str!("../../../../config/keys_clip_length.edn")),
clips: Arc::from(RwLock::from(vec![])),
clip: 0.into(),
mode: None,
keys: InputMap::new()
.layer(
SourceIter(include_str!("../../../../config/keys_edit.edn")))
.layer_if(|pool: &Self|matches!(pool.mode, Some(Import(..))|Some(Export(..))),
SourceIter(include_str!("../../../../config/keys_pool_file.edn")))
.layer_if(|pool: &Self|matches!(pool.mode, Some(Rename(..))),
SourceIter(include_str!("../../../../config/keys_clip_rename.edn")))
.layer_if(|pool: &Self|matches!(pool.mode, Some(Length(..))),
SourceIter(include_str!("../../../../config/keys_clip_length.edn")))
}
}
}