remove Atom. almost there

This commit is contained in:
🪞👃🪞 2025-01-18 15:37:53 +01:00
parent dc7b713108
commit cf1fd5b45a
20 changed files with 539 additions and 739 deletions

View file

@ -1,4 +1,5 @@
use crate::*;
use Value::*;
pub trait HasEditor {
fn editor (&self) -> &Option<MidiEditor>;
fn editor_mut (&mut self) -> &Option<MidiEditor>;
@ -31,6 +32,7 @@ pub trait HasEditor {
pub struct MidiEditor {
pub mode: PianoHorizontal,
pub size: Measure<TuiOut>,
keys: TokenIter<'static>
}
has_size!(<TuiOut>|self: MidiEditor|&self.size);
content!(TuiOut: |self: MidiEditor| {
@ -90,6 +92,7 @@ impl Default for MidiEditor {
Self {
mode: PianoHorizontal::new(None),
size: Measure::new(),
keys: TokenIter(KEYS_EDIT),
}
}
}
@ -207,27 +210,8 @@ atom_command!(MidiEditCommand: |state: MidiEditor| {
SetTimeLock(bool),
Show(Option<Arc<RwLock<MidiClip>>>),
}
impl MidiEditCommand {
fn from_tui_event (state: &MidiEditor, input: &impl AtomInput) -> Usually<Option<Self>> {
use AtomItem::*;
let atoms = AtomItem::read_all(KEYS_EDIT)?;
for atom in atoms.iter() {
if let Exp(e) = atom {
match e.as_slice() {
[Sym(key), command, args @ ..] if input.matches_atom(key) => {
return Ok(MidiEditCommand::from_atom(state, command, args))
}
_ => {}
}
} else {
panic!("invalid config")
}
}
Ok(None)
}
}
handle!(TuiIn: |self: MidiEditor, input|{
Ok(if let Some(command) = MidiEditCommand::from_tui_event(self, input)? {
Ok(if let Some(command) = self.keys.command::<_, MidiEditCommand, _>(self, input) {
let _undo = command.execute(self)?;
Some(true)
} else {

View file

@ -30,6 +30,11 @@ pub struct MidiPool {
pub clip: AtomicUsize,
/// Mode switch
pub mode: Option<PoolMode>,
keys: TokenIter<'static>,
keys_rename: TokenIter<'static>,
keys_length: TokenIter<'static>,
keys_file: TokenIter<'static>,
}
/// Modes for clip pool
#[derive(Debug, Clone)]
@ -50,6 +55,10 @@ impl Default for MidiPool {
clips: Arc::from(RwLock::from(vec![])),
clip: 0.into(),
mode: None,
keys: TokenIter(KEYS_POOL),
keys_rename: TokenIter(KEYS_RENAME),
keys_length: TokenIter(KEYS_LENGTH),
keys_file: TokenIter(KEYS_FILE),
}
}
}
@ -189,31 +198,13 @@ content!(TuiOut: |self: ClipLength| {
row!(" ", bars(), ".", beats(), "[", ticks()),
}
});
impl PoolCommand {
pub fn from_tui_event (state: &MidiPool, input: &impl AtomInput) -> Usually<Option<Self>> {
use AtomItem::*;
let atoms: Vec<AtomItem<_>> = AtomItem::read_all(match state.mode() {
Some(PoolMode::Rename(..)) => KEYS_RENAME,
Some(PoolMode::Length(..)) => KEYS_LENGTH,
Some(PoolMode::Import(..)) | Some(PoolMode::Export(..)) => KEYS_FILE,
_ => KEYS_POOL
})?;
for item in atoms {
match item {
Exp(e) => match e.as_slice() {
[Sym(key), command, args @ ..] if input.matches_atom(key) => {
return Ok(PoolCommand::from_atom(state, command, args))
}
_ => {}
},
_ => panic!("invalid config")
}
}
Ok(None)
}
}
handle!(TuiIn: |self: MidiPool, input|{
Ok(if let Some(command) = PoolCommand::from_tui_event(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) {
let _undo = command.execute(self)?;
Some(true)
} else {
@ -276,11 +267,11 @@ provide!(ItemColor: |self: MidiPool| {
atom_command!(PoolCommand: |state: MidiPool| {
("show" [a: bool] Self::Show(a.expect("no flag")))
("select" [i: usize] Self::Select(i.expect("no index")))
("rename" [a, ..b] ClipRenameCommand::from_atom(state, &a.to_ref(), b).map(Self::Rename).expect("invalid command"))
("length" [a, ..b] ClipLengthCommand::from_atom(state, &a.to_ref(), b).map(Self::Length).expect("invalid command"))
("import" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Import).expect("invalid command"))
("export" [a, ..b] FileBrowserCommand::from_atom(state, &a.to_ref(), b).map(Self::Export).expect("invalid command"))
("clip" [a, ..b] PoolClipCommand::from_atom(state, &a.to_ref(), b).map(Self::Clip).expect("invalid command"))
("rename" [,..a] ClipRenameCommand::try_from_expr(state, a).map(Self::Rename).expect("invalid command"))
("length" [,..a] ClipLengthCommand::try_from_expr(state, a).map(Self::Length).expect("invalid command"))
("import" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Import).expect("invalid command"))
("export" [,..a] FileBrowserCommand::try_from_expr(state, a).map(Self::Export).expect("invalid command"))
("clip" [,..a] PoolClipCommand::try_from_expr(state, a).map(Self::Clip).expect("invalid command"))
});
command!(|self: PoolCommand, state: MidiPool|{
use PoolCommand::*;