From 86f37fb2788076d7c42096f2381ab1ad3469380e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 24 Nov 2024 21:53:25 +0100 Subject: [PATCH] wip: midi import/export browser, pt.7 --- crates/tek_api/src/api_phrase.rs | 7 ++++--- crates/tek_tui/src/tui_command.rs | 14 +++++++++++--- crates/tek_tui/src/tui_model.rs | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/crates/tek_api/src/api_phrase.rs b/crates/tek_api/src/api_phrase.rs index a6bf6542..ab0d2b25 100644 --- a/crates/tek_api/src/api_phrase.rs +++ b/crates/tek_api/src/api_phrase.rs @@ -1,5 +1,6 @@ use crate::*; use tek_core::midly::Smf; +use std::path::PathBuf; pub trait HasPhrases { fn phrases (&self) -> &Vec>>; @@ -11,8 +12,8 @@ pub enum PhrasePoolCommand { Add(usize, Phrase), Delete(usize), Swap(usize, usize), - Import(usize, String), - Export(usize, String), + Import(usize, PathBuf), + Export(usize, PathBuf), SetName(usize, String), SetLength(usize, usize), SetColor(usize, ItemColor), @@ -54,7 +55,7 @@ impl Command for PhrasePoolCommand { } } } - let mut phrase = Phrase::new(&path, true, t as usize + 1, None, None); + let mut phrase = Phrase::new("imported", true, t as usize + 1, None, None); for event in events.iter() { phrase.notes[event.0 as usize].push(event.2); } diff --git a/crates/tek_tui/src/tui_command.rs b/crates/tek_tui/src/tui_command.rs index 8e672a50..f7e37329 100644 --- a/crates/tek_tui/src/tui_command.rs +++ b/crates/tek_tui/src/tui_command.rs @@ -265,20 +265,28 @@ pub enum FileBrowserCommand { impl Command for FileBrowserCommand { fn execute (self, state: &mut T) -> Perhaps { use FileBrowserCommand::*; + use PhrasesMode::{Import, Export}; let mode = state.phrases_mode_mut(); match mode { - Some(PhrasesMode::Import(index, ref mut browser)) => match self { + Some(Import(index, ref mut browser)) => match self { Cancel => { *mode = None; }, Chdir(cwd) => { - *mode = Some(PhrasesMode::Import(*index, FileBrowser::new(Some(cwd))?)); + *mode = Some(Import(*index, FileBrowser::new(Some(cwd))?)); }, Select(index) => { browser.index = index; }, Confirm => { - todo!("import midi to phrase"); + if browser.is_file() { + let index = *index; + let path = browser.path(); + *mode = None; + PhrasePoolCommand::Import(index, path).execute(state)?; + } else if browser.is_dir() { + *mode = Some(Import(*index, browser.chdir()?)); + } }, _ => todo!(), _ => unreachable!() diff --git a/crates/tek_tui/src/tui_model.rs b/crates/tek_tui/src/tui_model.rs index 11ff6c86..f45f6a33 100644 --- a/crates/tek_tui/src/tui_model.rs +++ b/crates/tek_tui/src/tui_model.rs @@ -209,6 +209,24 @@ impl FileBrowser { pub fn len (&self) -> usize { self.dirs.len() + self.files.len() } + pub fn is_dir (&self) -> bool { + self.index < self.dirs.len() + } + pub fn is_file (&self) -> bool { + self.index >= self.dirs.len() + } + pub fn path (&self) -> PathBuf { + self.cwd.join(if self.is_dir() { + &self.dirs[self.index].0 + } else if self.is_file() { + &self.files[self.index - self.dirs.len()].0 + } else { + unreachable!() + }) + } + pub fn chdir (&self) -> Usually { + Self::new(Some(self.path())) + } } /// Displays and edits phrase length.