wip: midi import/export browser, pt.7

This commit is contained in:
🪞👃🪞 2024-11-24 21:53:25 +01:00
parent f12a1dc2ca
commit 86f37fb278
3 changed files with 33 additions and 6 deletions

View file

@ -1,5 +1,6 @@
use crate::*;
use tek_core::midly::Smf;
use std::path::PathBuf;
pub trait HasPhrases {
fn phrases (&self) -> &Vec<Arc<RwLock<Phrase>>>;
@ -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<T: HasPhrases> Command<T> 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);
}

View file

@ -265,20 +265,28 @@ pub enum FileBrowserCommand {
impl<T: PhrasesControl> Command<T> for FileBrowserCommand {
fn execute (self, state: &mut T) -> Perhaps<Self> {
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!()

View file

@ -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> {
Self::new(Some(self.path()))
}
}
/// Displays and edits phrase length.