mirror of
https://codeberg.org/unspeaker/tek.git
synced 2026-04-03 21:00:44 +02:00
wip: nomralize
This commit is contained in:
parent
35197fb826
commit
244e2b388e
16 changed files with 1880 additions and 1866 deletions
307
src/browse.rs
307
src/browse.rs
|
|
@ -1,7 +1,17 @@
|
|||
use crate::*;
|
||||
use ::std::sync::{Arc, RwLock, atomic::{AtomicUsize, Ordering::*}};
|
||||
use crate::sequence::MidiClip;
|
||||
use crate::sample::Sample;
|
||||
|
||||
def_command!(FileBrowserCommand: |sampler: Sampler|{
|
||||
//("begin" [] Some(Self::Begin))
|
||||
//("cancel" [] Some(Self::Cancel))
|
||||
//("confirm" [] Some(Self::Confirm))
|
||||
//("select" [i: usize] Some(Self::Select(i.expect("no index"))))
|
||||
//("chdir" [p: PathBuf] Some(Self::Chdir(p.expect("no path"))))
|
||||
//("filter" [f: Arc<str>] Some(Self::Filter(f.expect("no filter")))))
|
||||
});
|
||||
|
||||
/// Browses for files to load/save.
|
||||
///
|
||||
/// ```
|
||||
|
|
@ -276,76 +286,247 @@ impl ClipLength {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl Browse {
|
||||
pub fn new (cwd: Option<PathBuf>) -> Usually<Self> {
|
||||
let cwd = if let Some(cwd) = cwd { cwd } else { std::env::current_dir()? };
|
||||
let mut dirs = vec![];
|
||||
let mut files = vec![];
|
||||
for entry in std::fs::read_dir(&cwd)? {
|
||||
let entry = entry?;
|
||||
let name = entry.file_name();
|
||||
let decoded = name.clone().into_string().unwrap_or_else(|_|"<unreadable>".to_string());
|
||||
let meta = entry.metadata()?;
|
||||
if meta.is_dir() {
|
||||
dirs.push((name, format!("📁 {decoded}")));
|
||||
} else if meta.is_file() {
|
||||
files.push((name, format!("📄 {decoded}")));
|
||||
|
||||
impl Browse {
|
||||
pub fn new (cwd: Option<PathBuf>) -> Usually<Self> {
|
||||
let cwd = if let Some(cwd) = cwd { cwd } else { std::env::current_dir()? };
|
||||
let mut dirs = vec![];
|
||||
let mut files = vec![];
|
||||
for entry in std::fs::read_dir(&cwd)? {
|
||||
let entry = entry?;
|
||||
let name = entry.file_name();
|
||||
let decoded = name.clone().into_string().unwrap_or_else(|_|"<unreadable>".to_string());
|
||||
let meta = entry.metadata()?;
|
||||
if meta.is_dir() {
|
||||
dirs.push((name, format!("📁 {decoded}")));
|
||||
} else if meta.is_file() {
|
||||
files.push((name, format!("📄 {decoded}")));
|
||||
}
|
||||
}
|
||||
Ok(Self { cwd, dirs, files, ..Default::default() })
|
||||
}
|
||||
pub fn chdir (&self) -> Usually<Self> { Self::new(Some(self.path())) }
|
||||
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!()
|
||||
})
|
||||
}
|
||||
fn _todo_stub_path_buf (&self) -> PathBuf { todo!() }
|
||||
fn _todo_stub_usize (&self) -> usize { todo!() }
|
||||
fn _todo_stub_arc_str (&self) -> Arc<str> { todo!() }
|
||||
}
|
||||
impl Browse {
|
||||
fn tui (&self) -> impl Draw<Tui> {
|
||||
iter_south(1, ||EntriesIterator {
|
||||
offset: 0,
|
||||
index: 0,
|
||||
length: self.dirs.len() + self.files.len(),
|
||||
browser: self,
|
||||
}, |entry, _index|w_full(origin_w(entry)))
|
||||
}
|
||||
}
|
||||
impl<'a> Iterator for EntriesIterator<'a> {
|
||||
type Item = Modify<&'a str>;
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
let dirs = self.browser.dirs.len();
|
||||
let files = self.browser.files.len();
|
||||
let index = self.index;
|
||||
if self.index < dirs {
|
||||
self.index += 1;
|
||||
Some(Tui::bold(true, self.browser.dirs[index].1.as_str()))
|
||||
} else if self.index < dirs + files {
|
||||
self.index += 1;
|
||||
Some(Tui::bold(false, self.browser.files[index - dirs].1.as_str()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
impl PartialEq for BrowseTarget {
|
||||
fn eq (&self, other: &Self) -> bool {
|
||||
match self {
|
||||
Self::ImportSample(_) => false,
|
||||
Self::ExportSample(_) => false,
|
||||
Self::ImportClip(_) => false,
|
||||
Self::ExportClip(_) => false,
|
||||
#[allow(unused)] t => matches!(other, t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def_command!(BrowseCommand: |browse: Browse| {
|
||||
SetVisible => Ok(None),
|
||||
SetPath { address: PathBuf } => Ok(None),
|
||||
SetSearch { filter: Arc<str> } => Ok(None),
|
||||
SetCursor { cursor: usize } => Ok(None),
|
||||
});
|
||||
|
||||
def_command!(PoolCommand: |pool: Pool| {
|
||||
// Toggle visibility of pool
|
||||
Show { visible: bool } => { pool.visible = *visible; Ok(Some(Self::Show { visible: !visible })) },
|
||||
// Select a clip from the clip pool
|
||||
Select { index: usize } => { pool.set_clip_index(*index); Ok(None) },
|
||||
// Update the contents of the clip pool
|
||||
Clip { command: PoolClipCommand } => Ok(command.execute(pool)?.map(|command|Self::Clip{command})),
|
||||
// Rename a clip
|
||||
Rename { command: RenameCommand } => Ok(command.delegate(pool, |command|Self::Rename{command})?),
|
||||
// Change the length of a clip
|
||||
Length { command: CropCommand } => Ok(command.delegate(pool, |command|Self::Length{command})?),
|
||||
// Import from file
|
||||
Import { command: BrowseCommand } => Ok(if let Some(browse) = pool.browse.as_mut() {
|
||||
command.delegate(browse, |command|Self::Import{command})?
|
||||
} else {
|
||||
None
|
||||
}),
|
||||
// Export to file
|
||||
Export { command: BrowseCommand } => Ok(if let Some(browse) = pool.browse.as_mut() {
|
||||
command.delegate(browse, |command|Self::Export{command})?
|
||||
} else {
|
||||
None
|
||||
}),
|
||||
});
|
||||
|
||||
def_command!(PoolClipCommand: |pool: Pool| {
|
||||
Delete { index: usize } => {
|
||||
let index = *index;
|
||||
let clip = pool.clips_mut().remove(index).read().unwrap().clone();
|
||||
Ok(Some(Self::Add { index, clip }))
|
||||
},
|
||||
Swap { index: usize, other: usize } => {
|
||||
let index = *index;
|
||||
let other = *other;
|
||||
pool.clips_mut().swap(index, other);
|
||||
Ok(Some(Self::Swap { index, other }))
|
||||
},
|
||||
Export { index: usize, path: PathBuf } => {
|
||||
todo!("export clip to midi file");
|
||||
},
|
||||
Add { index: usize, clip: MidiClip } => {
|
||||
let index = *index;
|
||||
let mut index = index;
|
||||
let clip = Arc::new(RwLock::new(clip.clone()));
|
||||
let mut clips = pool.clips_mut();
|
||||
if index >= clips.len() {
|
||||
index = clips.len();
|
||||
clips.push(clip)
|
||||
} else {
|
||||
clips.insert(index, clip);
|
||||
}
|
||||
Ok(Some(Self::Delete { index }))
|
||||
},
|
||||
Import { index: usize, path: PathBuf } => {
|
||||
let index = *index;
|
||||
let bytes = std::fs::read(&path)?;
|
||||
let smf = Smf::parse(bytes.as_slice())?;
|
||||
let mut t = 0u32;
|
||||
let mut events = vec![];
|
||||
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));
|
||||
}
|
||||
}
|
||||
Ok(Self { cwd, dirs, files, ..Default::default() })
|
||||
}
|
||||
pub fn chdir (&self) -> Usually<Self> { Self::new(Some(self.path())) }
|
||||
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!()
|
||||
})
|
||||
let mut clip = MidiClip::new("imported", true, t as usize + 1, None, None);
|
||||
for event in events.iter() {
|
||||
clip.notes[event.0 as usize].push(event.2);
|
||||
}
|
||||
fn _todo_stub_path_buf (&self) -> PathBuf { todo!() }
|
||||
fn _todo_stub_usize (&self) -> usize { todo!() }
|
||||
fn _todo_stub_arc_str (&self) -> Arc<str> { todo!() }
|
||||
}
|
||||
impl Browse {
|
||||
fn tui (&self) -> impl Draw<Tui> {
|
||||
iter_south(1, ||EntriesIterator {
|
||||
offset: 0,
|
||||
index: 0,
|
||||
length: self.dirs.len() + self.files.len(),
|
||||
browser: self,
|
||||
}, |entry, _index|w_full(origin_w(entry)))
|
||||
Ok(Self::Add { index, clip }.execute(pool)?)
|
||||
},
|
||||
SetName { index: usize, name: Arc<str> } => {
|
||||
let index = *index;
|
||||
let clip = &mut pool.clips_mut()[index];
|
||||
let old_name = clip.read().unwrap().name.clone();
|
||||
clip.write().unwrap().name = name.clone();
|
||||
Ok(Some(Self::SetName { index, name: old_name }))
|
||||
},
|
||||
SetLength { index: usize, length: usize } => {
|
||||
let index = *index;
|
||||
let clip = &mut pool.clips_mut()[index];
|
||||
let old_len = clip.read().unwrap().length;
|
||||
clip.write().unwrap().length = *length;
|
||||
Ok(Some(Self::SetLength { index, length: old_len }))
|
||||
},
|
||||
SetColor { index: usize, color: ItemColor } => {
|
||||
let index = *index;
|
||||
let mut color = ItemTheme::from(*color);
|
||||
std::mem::swap(&mut color, &mut pool.clips()[index].write().unwrap().color);
|
||||
Ok(Some(Self::SetColor { index, color: color.base }))
|
||||
},
|
||||
});
|
||||
|
||||
def_command!(RenameCommand: |pool: Pool| {
|
||||
Begin => unreachable!(),
|
||||
Cancel => {
|
||||
if let Some(PoolMode::Rename(clip, ref mut old_name)) = pool.mode_mut().clone() {
|
||||
pool.clips()[clip].write().unwrap().name = old_name.clone().into();
|
||||
}
|
||||
}
|
||||
impl<'a> Iterator for EntriesIterator<'a> {
|
||||
type Item = Modify<&'a str>;
|
||||
fn next (&mut self) -> Option<Self::Item> {
|
||||
let dirs = self.browser.dirs.len();
|
||||
let files = self.browser.files.len();
|
||||
let index = self.index;
|
||||
if self.index < dirs {
|
||||
self.index += 1;
|
||||
Some(Tui::bold(true, self.browser.dirs[index].1.as_str()))
|
||||
} else if self.index < dirs + files {
|
||||
self.index += 1;
|
||||
Some(Tui::bold(false, self.browser.files[index - dirs].1.as_str()))
|
||||
} else {
|
||||
None
|
||||
Ok(None)
|
||||
},
|
||||
Confirm => {
|
||||
if let Some(PoolMode::Rename(_clip, ref mut old_name)) = pool.mode_mut().clone() {
|
||||
let old_name = old_name.clone(); *pool.mode_mut() = None; return Ok(Some(Self::Set { value: old_name }))
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Set { value: Arc<str> } => {
|
||||
if let Some(PoolMode::Rename(clip, ref mut _old_name)) = pool.mode_mut().clone() {
|
||||
pool.clips()[clip].write().unwrap().name = value.clone();
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
});
|
||||
|
||||
def_command!(CropCommand: |pool: Pool| {
|
||||
Begin => unreachable!(),
|
||||
Cancel => { if let Some(PoolMode::Length(..)) = pool.mode_mut().clone() { *pool.mode_mut() = None; } Ok(None) },
|
||||
Set { length: usize } => {
|
||||
if let Some(PoolMode::Length(clip, ref mut length, ref mut _focus))
|
||||
= pool.mode_mut().clone()
|
||||
{
|
||||
let old_length;
|
||||
{
|
||||
let clip = pool.clips()[clip].clone();//.write().unwrap();
|
||||
old_length = Some(clip.read().unwrap().length);
|
||||
clip.write().unwrap().length = *length;
|
||||
}
|
||||
*pool.mode_mut() = None;
|
||||
return Ok(old_length.map(|length|Self::Set { length }))
|
||||
}
|
||||
Ok(None)
|
||||
},
|
||||
Next => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut _length, ref mut focus)) = pool.mode_mut().clone() { focus.next() }; Ok(None)
|
||||
},
|
||||
Prev => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut _length, ref mut focus)) = pool.mode_mut().clone() { focus.prev() }; Ok(None)
|
||||
},
|
||||
Inc => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut length, ref mut focus)) = pool.mode_mut().clone() {
|
||||
match focus {
|
||||
ClipLengthFocus::Bar => { *length += 4 * PPQ },
|
||||
ClipLengthFocus::Beat => { *length += PPQ },
|
||||
ClipLengthFocus::Tick => { *length += 1 },
|
||||
}
|
||||
}
|
||||
}
|
||||
impl PartialEq for BrowseTarget {
|
||||
fn eq (&self, other: &Self) -> bool {
|
||||
match self {
|
||||
Self::ImportSample(_) => false,
|
||||
Self::ExportSample(_) => false,
|
||||
Self::ImportClip(_) => false,
|
||||
Self::ExportClip(_) => false,
|
||||
#[allow(unused)] t => matches!(other, t)
|
||||
Ok(None)
|
||||
},
|
||||
Dec => {
|
||||
if let Some(PoolMode::Length(_clip, ref mut length, ref mut focus)) = pool.mode_mut().clone() {
|
||||
match focus {
|
||||
ClipLengthFocus::Bar => { *length = length.saturating_sub(4 * PPQ) },
|
||||
ClipLengthFocus::Beat => { *length = length.saturating_sub(PPQ) },
|
||||
ClipLengthFocus::Tick => { *length = length.saturating_sub(1) },
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue