From 26eb5f0315d561f30563ab9da5f722184c194df7 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 24 Nov 2024 04:01:55 +0100 Subject: [PATCH] mostly formatting --- crates/tek_api/src/api_phrase.rs | 49 ++++++++++++++------------ crates/tek_tui/src/tui_command.rs | 57 +++++++++++++------------------ 2 files changed, 51 insertions(+), 55 deletions(-) diff --git a/crates/tek_api/src/api_phrase.rs b/crates/tek_api/src/api_phrase.rs index 8d799dac..a6bf6542 100644 --- a/crates/tek_api/src/api_phrase.rs +++ b/crates/tek_api/src/api_phrase.rs @@ -20,8 +20,9 @@ pub enum PhrasePoolCommand { impl Command for PhrasePoolCommand { fn execute (self, model: &mut T) -> Perhaps { - match self { - Self::Add(mut index, phrase) => { + use PhrasePoolCommand::*; + Ok(match self { + Add(mut index, phrase) => { let phrase = Arc::new(RwLock::new(phrase)); let phrases = model.phrases_mut(); if index >= phrases.len() { @@ -30,24 +31,23 @@ impl Command for PhrasePoolCommand { } else { phrases.insert(index, phrase); } - return Ok(Some(Self::Delete(index))) + Some(Self::Delete(index)) }, - Self::Delete(index) => { - let phrase = model.phrases_mut().remove(index); - //view.phrase = view.phrase.min(view.model.phrases.len().saturating_sub(1)); + Delete(index) => { + let phrase = model.phrases_mut().remove(index).read().unwrap().clone(); + Some(Self::Add(index, phrase)) }, - Self::Swap(index, other) => { + Swap(index, other) => { model.phrases_mut().swap(index, other); - return Ok(Some(Self::Swap(index, other))) + Some(Self::Swap(index, other)) }, - Self::Import(index, path) => { + Import(index, path) => { let bytes = std::fs::read(&path)?; let smf = Smf::parse(bytes.as_slice())?; - println!("{:?}", &smf.header); let mut t = 0u32; let mut events = vec![]; - for (i, track) in smf.tracks.iter().enumerate() { - for (j, event) in track.iter().enumerate() { + for track in smf.tracks.iter() { + for event in track.iter() { t += event.delta.as_int(); if let TrackEventKind::Midi { channel, message } = event.kind { events.push((t, channel.as_int(), message)); @@ -56,24 +56,31 @@ impl Command for PhrasePoolCommand { } let mut phrase = Phrase::new(&path, true, t as usize + 1, None, None); for event in events.iter() { - println!("{event:?}"); phrase.notes[event.0 as usize].push(event.2); } - return Self::Add(index, phrase).execute(model) + Self::Add(index, phrase).execute(model)? }, - Self::Export(index, path) => { + Export(_index, _path) => { + todo!("export phrase to midi file"); }, - Self::SetName(index, name) => { + SetName(index, name) => { + let mut phrase = model.phrases()[index].write().unwrap(); + let old_name = phrase.name.clone(); + phrase.name = name; + Some(Self::SetName(index, old_name)) }, - Self::SetLength(index, length) => { + SetLength(index, length) => { + let mut phrase = model.phrases()[index].write().unwrap(); + let old_len = phrase.length; + phrase.length = length; + Some(Self::SetLength(index, old_len)) }, - Self::SetColor(index, color) => { + SetColor(index, color) => { let mut color = ItemColorTriplet::from(color); std::mem::swap(&mut color, &mut model.phrases()[index].write().unwrap().color); - return Ok(Some(Self::SetColor(index, color.base))) + Some(Self::SetColor(index, color.base)) }, - } - Ok(None) + }) } } diff --git a/crates/tek_tui/src/tui_command.rs b/crates/tek_tui/src/tui_command.rs index cfe5edda..48d8bd91 100644 --- a/crates/tek_tui/src/tui_command.rs +++ b/crates/tek_tui/src/tui_command.rs @@ -69,15 +69,15 @@ impl Command for ArrangerCommand { fn execute (self, state: &mut ArrangerTui) -> Perhaps { use ArrangerCommand::*; Ok(match self { - Focus(cmd) => cmd.execute(state)?.map(Focus), - Scene(cmd) => cmd.execute(state)?.map(Scene), - Track(cmd) => cmd.execute(state)?.map(Track), - Clip(cmd) => cmd.execute(state)?.map(Clip), - Phrases(cmd) => cmd.execute(state)?.map(Phrases), - Editor(cmd) => cmd.execute(state)?.map(Editor), - Clock(cmd) => cmd.execute(state)?.map(Clock), - Zoom(zoom) => { todo!(); }, - Select(selected) => { + Focus(cmd) => cmd.execute(state)?.map(Focus), + Scene(cmd) => cmd.execute(state)?.map(Scene), + Track(cmd) => cmd.execute(state)?.map(Track), + Clip(cmd) => cmd.execute(state)?.map(Clip), + Phrases(cmd) => cmd.execute(state)?.map(Phrases), + Editor(cmd) => cmd.execute(state)?.map(Editor), + Clock(cmd) => cmd.execute(state)?.map(Clock), + Zoom(zoom) => { todo!(); }, + Select(selected) => { *state.selected_mut() = selected; None }, @@ -121,11 +121,12 @@ pub enum PhrasesCommand { impl Command for PhrasesCommand { fn execute (self, state: &mut T) -> Perhaps { + use PhrasesCommand::*; Ok(match self { - Self::Phrase(command) => command.execute(state)?.map(Self::Phrase), - Self::Rename(command) => command.execute(state)?.map(Self::Rename), - Self::Length(command) => command.execute(state)?.map(Self::Length), - Self::Select(phrase) => { + Phrase(command) => command.execute(state)?.map(Phrase), + Rename(command) => command.execute(state)?.map(Rename), + Length(command) => command.execute(state)?.map(Length), + Select(phrase) => { state.set_phrase_index(phrase); None }, @@ -149,32 +150,22 @@ impl Command for PhraseLengthCommand { use PhraseLengthFocus::*; use PhraseLengthCommand::*; let mut mode = state.phrases_mode_mut().clone(); - if let Some(PhrasesMode::Length( - phrase, - ref mut length, - ref mut focus, - )) = mode { + if let Some(PhrasesMode::Length(phrase, ref mut length, ref mut focus)) = mode { match self { - Self::Cancel => { - *state.phrases_mode_mut() = None; - }, - Self::Prev => { - focus.prev() - }, - Self::Next => { - focus.next() - }, - Self::Inc => match focus { + Cancel => { *state.phrases_mode_mut() = None; }, + Prev => { focus.prev() }, + Next => { focus.next() }, + Inc => match focus { Bar => { *length += 4 * PPQ }, Beat => { *length += PPQ }, Tick => { *length += 1 }, }, - Self::Dec => match focus { + Dec => match focus { Bar => { *length = length.saturating_sub(4 * PPQ) }, Beat => { *length = length.saturating_sub(PPQ) }, Tick => { *length = length.saturating_sub(1) }, }, - Self::Set(length) => { + Set(length) => { let mut phrase = state.phrases()[phrase].write().unwrap(); let old_length = phrase.length; phrase.length = length; @@ -210,10 +201,8 @@ where { fn execute (self, state: &mut T) -> Perhaps { use PhraseRenameCommand::*; - if let Some(PhrasesMode::Rename( - phrase, - ref mut old_name - )) = state.phrases_mode_mut().clone() { + let mut mode = state.phrases_mode_mut().clone(); + if let Some(PhrasesMode::Rename(phrase, ref mut old_name)) = mode { match self { Set(s) => { state.phrases()[phrase].write().unwrap().name = s.into();