mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 03:36:41 +01:00
392 lines
16 KiB
Rust
392 lines
16 KiB
Rust
#![allow(unused, clippy::unit_arg)]
|
|
#![feature(adt_const_params, associated_type_defaults, if_let_guard, impl_trait_in_assoc_type,
|
|
type_alias_impl_trait, trait_alias, type_changing_struct_update, closure_lifetime_binder)]
|
|
mod app_deps; pub use self::app_deps::*;
|
|
mod app_jack; pub use self::app_jack::*;
|
|
mod app_bind; pub use self::app_bind::*;
|
|
mod app_data; pub use self::app_data::*;
|
|
mod app_view; pub use self::app_view::*;
|
|
|
|
#[cfg(test)] mod app_test;
|
|
/// Total state
|
|
#[derive(Default, Debug)]
|
|
pub struct App {
|
|
/// Must not be dropped for the duration of the process
|
|
pub jack: Jack<'static>,
|
|
/// Display size
|
|
pub size: Measure<TuiOut>,
|
|
/// Performance counter
|
|
pub perf: PerfModel,
|
|
/// Available view modes and input bindings
|
|
pub config: Config,
|
|
/// Currently selected mode
|
|
pub mode: Arc<Mode<Arc<str>>>,
|
|
/// Contains the currently edited musical arrangement
|
|
pub project: Arrangement,
|
|
/// Contains all recently created clips.
|
|
pub pool: Pool,
|
|
/// Undo history
|
|
pub history: Vec<(AppCommand, Option<AppCommand>)>,
|
|
/// Dialog overlay
|
|
pub dialog: Dialog,
|
|
/// Base color.
|
|
pub color: ItemTheme,
|
|
}
|
|
/// Configuration
|
|
#[derive(Default, Debug)]
|
|
pub struct Config {
|
|
pub dirs: BaseDirectories,
|
|
pub modes: Arc<RwLock<BTreeMap<Arc<str>, Arc<Mode<Arc<str>>>>>>,
|
|
pub views: Arc<RwLock<BTreeMap<Arc<str>, Arc<str>>>>,
|
|
pub binds: Arc<RwLock<BTreeMap<Arc<str>, EventMap<TuiEvent, Arc<str>>>>>,
|
|
}
|
|
#[derive(Default, Debug)]
|
|
pub struct Mode<D: Dsl + Ord> {
|
|
pub path: PathBuf,
|
|
pub name: Vec<D>,
|
|
pub info: Vec<D>,
|
|
pub view: Vec<D>,
|
|
pub keys: Vec<D>,
|
|
pub modes: BTreeMap<D, Mode<D>>,
|
|
}
|
|
/// Various possible dialog modes.
|
|
#[derive(Debug, Clone, Default)]
|
|
pub enum Dialog {
|
|
#[default] None,
|
|
Help(usize),
|
|
Menu(usize),
|
|
Device(usize),
|
|
Message(Arc<str>),
|
|
Browser(BrowserTarget, Arc<Browser>),
|
|
Options,
|
|
}
|
|
impl Config {
|
|
const CONFIG: &'static str = "tek.edn";
|
|
const DEFAULTS: &'static str = include_str!("../../tek.edn");
|
|
pub fn init () -> Usually<Self> {
|
|
let mut cfgs: Self = Default::default();
|
|
cfgs.dirs = BaseDirectories::with_profile("tek", "v0");
|
|
cfgs.init_file(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.load(dsl))?;
|
|
Ok(cfgs)
|
|
}
|
|
pub fn init_file (
|
|
&mut self, path: &str, defaults: &str, mut each: impl FnMut(&mut Self, &str)->Usually<()>
|
|
) -> Usually<()> {
|
|
if self.dirs.find_config_file(path).is_none() {
|
|
println!("Creating {path:?}");
|
|
std::fs::write(self.dirs.place_config_file(path)?, defaults);
|
|
}
|
|
Ok(if let Some(path) = self.dirs.find_config_file(path) {
|
|
println!("Loading {path:?}");
|
|
let src = std::fs::read_to_string(&path)?;
|
|
src.as_str().each(move|item|each(self, item))?;
|
|
} else {
|
|
return Err(format!("{path}: not found").into())
|
|
})
|
|
}
|
|
pub fn load (&mut self, dsl: impl Dsl) -> Usually<()> {
|
|
dsl.each(|item|if let Some(expr) = item.expr()? {
|
|
let head = expr.head()?;
|
|
let tail = expr.tail()?;
|
|
let name = tail.head()?;
|
|
let body = tail.tail()?;
|
|
println!("{} {} {}", head.unwrap_or_default(), name.unwrap_or_default(), body.unwrap_or_default());
|
|
match head {
|
|
Some("view") if let Some(name) = name => self.load_view(name.into(), body),
|
|
Some("keys") if let Some(name) = name => self.load_bind(name.into(), body),
|
|
Some("mode") if let Some(name) = name => self.load_mode(name.into(), body),
|
|
_ => return Err(format!("Config::load: expected view/keys/mode, got: {item:?}").into())
|
|
}
|
|
} else {
|
|
return Err(format!("Config::load: expected expr, got: {item:?}").into())
|
|
})
|
|
}
|
|
pub fn load_view (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
|
self.views.write().unwrap().insert(id, dsl.src()?.unwrap_or_default().into());
|
|
Ok(())
|
|
}
|
|
pub fn load_bind (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
|
let mut map = EventMap::new();
|
|
dsl.each(|item|if item.expr().head() == Ok(Some("see")) {
|
|
// TODO
|
|
Ok(())
|
|
} else if let Ok(Some(word)) = item.expr().head().word() {
|
|
if let Some(key) = TuiEvent::from_dsl(item.expr()?.head()?)? {
|
|
map.add(key, Binding {
|
|
commands: [item.expr()?.tail()?.unwrap_or_default().into()].into(),
|
|
condition: None,
|
|
description: None,
|
|
source: None
|
|
});
|
|
Ok(())
|
|
} else if Some(":char") == item.expr()?.head()? {
|
|
// TODO
|
|
return Ok(())
|
|
} else {
|
|
return Err(format!("Config::load_bind: invalid key: {:?}", item.expr()?.head()?).into())
|
|
}
|
|
} else {
|
|
return Err(format!("Config::load_bind: unexpected: {item:?}").into())
|
|
})?;
|
|
self.binds.write().unwrap().insert(id, map);
|
|
Ok(())
|
|
}
|
|
pub fn load_mode (&mut self, id: Arc<str>, dsl: impl Dsl) -> Usually<()> {
|
|
let mut mode = Mode::default();
|
|
dsl.each(|item|Self::load_mode_one(&mut mode, item))?;
|
|
self.modes.write().unwrap().insert(id.into(), Arc::new(mode));
|
|
Ok(())
|
|
}
|
|
pub fn load_mode_one (mode: &mut Mode<Arc<str>>, item: impl Dsl) -> Usually<()> {
|
|
Ok(if let Ok(Some(key)) = item.expr().head() {
|
|
match key {
|
|
"name" => mode.name.push(item.expr()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
|
"info" => mode.info.push(item.expr()?.tail()?.map(|x|x.trim()).unwrap_or("").into()),
|
|
"keys" => {
|
|
item.expr()?.tail()?.each(|item|{mode.keys.push(item.trim().into()); Ok(())})?;
|
|
},
|
|
"mode" => if let Some(id) = item.expr()?.tail()?.head()? {
|
|
let mut submode = Mode::default();
|
|
Self::load_mode_one(&mut submode, item.expr()?.tail()?.tail()?)?;
|
|
mode.modes.insert(id.into(), submode);
|
|
} else {
|
|
return Err(format!("load_mode_one: incomplete: {item:?}").into());
|
|
},
|
|
_ => mode.view.push(item.expr()?.unwrap().into()),
|
|
}
|
|
} else if let Ok(Some(word)) = item.word() {
|
|
mode.view.push(word.into());
|
|
} else {
|
|
return Err(format!("load_mode_one: unexpected: {item:?}").into());
|
|
})
|
|
}
|
|
}
|
|
has!(Jack<'static>: |self: App|self.jack);
|
|
has!(Pool: |self: App|self.pool);
|
|
has!(Dialog: |self: App|self.dialog);
|
|
has!(Clock: |self: App|self.project.clock);
|
|
has!(Option<MidiEditor>: |self: App|self.project.editor);
|
|
has!(Selection: |self: App|self.project.selection);
|
|
has!(Vec<MidiInput>: |self: App|self.project.midi_ins);
|
|
has!(Vec<MidiOutput>: |self: App|self.project.midi_outs);
|
|
has!(Vec<Scene>: |self: App|self.project.scenes);
|
|
has!(Vec<Track>: |self: App|self.project.tracks);
|
|
has!(Measure<TuiOut>: |self: App|self.size);
|
|
has_clips!( |self: App|self.pool.clips);
|
|
maybe_has!(Track: |self: App| { MaybeHas::<Track>::get(&self.project) };
|
|
{ MaybeHas::<Track>::get_mut(&mut self.project) });
|
|
maybe_has!(Scene: |self: App| { MaybeHas::<Scene>::get(&self.project) };
|
|
{ MaybeHas::<Scene>::get_mut(&mut self.project) });
|
|
impl HasClipsSize for App { fn clips_size (&self) -> &Measure<TuiOut> { &self.project.inner_size } }
|
|
impl HasTrackScroll for App { fn track_scroll (&self) -> usize { self.project.track_scroll() } }
|
|
impl HasSceneScroll for App { fn scene_scroll (&self) -> usize { self.project.scene_scroll() } }
|
|
pub fn view_nil (_: &App) -> Box<dyn Render<TuiOut>> {
|
|
Box::new(Fill::xy("·"))
|
|
}
|
|
content!(TuiOut:|self: App|Fill::xy(Stack::above(|add|{
|
|
for dsl in self.mode.view.iter() { add(&Fill::xy(self.view(dsl.as_ref()))); }
|
|
})));
|
|
impl App {
|
|
fn view <D: Dsl> (&self, index: D) -> Box<dyn Render<TuiOut>> {
|
|
match index.src() {
|
|
Ok(Some(src)) => render_dsl(self, src),
|
|
Ok(None) => Box::new(Tui::fg(Color::Rgb(192, 192, 192), "empty view")),
|
|
Err(e) => Box::new(format!("{e}")),
|
|
}
|
|
}
|
|
pub fn update_clock (&self) {
|
|
ViewCache::update_clock(&self.project.clock.view_cache, self.clock(), self.size.w() > 80)
|
|
}
|
|
}
|
|
fn render_dsl <'t> (
|
|
state: &'t impl DslNs<'t, Box<dyn Render<TuiOut>>>,
|
|
src: &str
|
|
) -> Box<dyn Render<TuiOut>> {
|
|
let err: Option<Box<dyn Error>> = match state.from(&src) {
|
|
Ok(Some(value)) => return value, Ok(None) => None, Err(e) => Some(e),
|
|
};
|
|
let (fg_1, bg_1) = (Color::Rgb(240, 160, 100), Color::Rgb(48, 0, 0));
|
|
let (fg_2, bg_2) = (Color::Rgb(250, 200, 180), Color::Rgb(48, 0, 0));
|
|
let (fg_3, bg_3) = (Color::Rgb(250, 200, 120), Color::Rgb(0, 0, 0));
|
|
let bg = Color::Rgb(24, 0, 0);
|
|
Box::new(col! {
|
|
Tui::fg(bg, Fixed::y(1, Fill::x(RepeatH("▄")))),
|
|
Tui::bg(bg, col! {
|
|
Fill::x(Bsp::e(
|
|
Tui::bold(true, Tui::fg_bg(fg_1, bg_1, " Render error: ")),
|
|
Tui::fg_bg(fg_2, bg_2, err.map(|e|format!(" {e} "))),
|
|
)),
|
|
Fill::x(Align::x(Tui::fg_bg(fg_3, bg_3, format!(" {src} ")))),
|
|
}),
|
|
Tui::fg(bg, Fixed::y(1, Fill::x(RepeatH("▀")))),
|
|
})
|
|
}
|
|
handle!(TuiIn:|self: App, input|{
|
|
for id in self.mode.keys.iter() {
|
|
if let Some(event_map) = self.config.binds.read().unwrap().get(id.as_ref()) {
|
|
if let Some(bindings) = event_map.query(input.event()) {
|
|
for binding in bindings {
|
|
for command in binding.commands.iter() {
|
|
let command: Option<AppCommand> = self.from(command)?;
|
|
if let Some(command) = command {
|
|
panic!("{command:?}");
|
|
}
|
|
}
|
|
panic!("{binding:?}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Ok(None)
|
|
});
|
|
impl Dialog {
|
|
pub fn menu_selected (&self) -> Option<usize> {
|
|
if let Self::Menu(selected) = self { Some(*selected) } else { None }
|
|
}
|
|
pub fn device_kind (&self) -> Option<usize> {
|
|
if let Self::Device(index) = self { Some(*index) } else { None }
|
|
}
|
|
pub fn device_kind_next (&self) -> Option<usize> {
|
|
self.device_kind().map(|index|(index + 1) % device_kinds().len())
|
|
}
|
|
pub fn device_kind_prev (&self) -> Option<usize> {
|
|
self.device_kind().map(|index|index.overflowing_sub(1).0.min(device_kinds().len().saturating_sub(1)))
|
|
}
|
|
pub fn message (&self) -> Option<&str> {
|
|
todo!()
|
|
}
|
|
pub fn browser (&self) -> Option<&Arc<Browser>> {
|
|
todo!()
|
|
}
|
|
pub fn browser_target (&self) -> Option<&BrowserTarget> {
|
|
todo!()
|
|
}
|
|
}
|
|
impl App {
|
|
pub fn editor_focused (&self) -> bool {
|
|
false
|
|
}
|
|
pub fn toggle_dialog (&mut self, mut dialog: Dialog) -> Dialog {
|
|
std::mem::swap(&mut self.dialog, &mut dialog);
|
|
dialog
|
|
}
|
|
pub fn toggle_editor (&mut self, value: Option<bool>) {
|
|
//FIXME: self.editing.store(value.unwrap_or_else(||!self.is_editing()), Relaxed);
|
|
let value = value.unwrap_or_else(||!self.editor().is_some());
|
|
if value {
|
|
// Create new clip in pool when entering empty cell
|
|
if let Selection::TrackClip { track, scene } = *self.selection()
|
|
&& let Some(scene) = self.project.scenes.get_mut(scene)
|
|
&& let Some(slot) = scene.clips.get_mut(track)
|
|
&& slot.is_none()
|
|
&& let Some(track) = self.project.tracks.get_mut(track)
|
|
{
|
|
let (index, mut clip) = self.pool.add_new_clip();
|
|
// autocolor: new clip colors from scene and track color
|
|
let color = track.color.base.mix(scene.color.base, 0.5);
|
|
clip.write().unwrap().color = ItemColor::random_near(color, 0.2).into();
|
|
if let Some(editor) = &mut self.project.editor {
|
|
editor.set_clip(Some(&clip));
|
|
}
|
|
*slot = Some(clip.clone());
|
|
//Some(clip)
|
|
} else {
|
|
//None
|
|
}
|
|
} else if let Selection::TrackClip { track, scene } = *self.selection()
|
|
&& let Some(scene) = self.project.scenes.get_mut(scene)
|
|
&& let Some(slot) = scene.clips.get_mut(track)
|
|
&& let Some(clip) = slot.as_mut()
|
|
{
|
|
// Remove clip from arrangement when exiting empty clip editor
|
|
let mut swapped = None;
|
|
if clip.read().unwrap().count_midi_messages() == 0 {
|
|
std::mem::swap(&mut swapped, slot);
|
|
}
|
|
if let Some(clip) = swapped {
|
|
self.pool.delete_clip(&clip.read().unwrap());
|
|
}
|
|
}
|
|
}
|
|
pub fn browser (&self) -> Option<&Browser> {
|
|
if let Dialog::Browser(_, ref b) = self.dialog { Some(b) } else { None }
|
|
}
|
|
pub fn device_pick (&mut self, index: usize) {
|
|
self.dialog = Dialog::Device(index);
|
|
}
|
|
pub fn add_device (&mut self, index: usize) -> Usually<()> {
|
|
match index {
|
|
0 => {
|
|
let name = self.jack.with_client(|c|c.name().to_string());
|
|
let midi = self.project.track().expect("no active track").sequencer.midi_outs[0].port_name();
|
|
let track = self.track().expect("no active track");
|
|
let port = format!("{}/Sampler", &track.name);
|
|
let connect = Connect::exact(format!("{name}:{midi}"));
|
|
let sampler = if let Ok(sampler) = Sampler::new(
|
|
&self.jack, &port, &[connect], &[&[], &[]], &[&[], &[]]
|
|
) {
|
|
self.dialog = Dialog::None;
|
|
Device::Sampler(sampler)
|
|
} else {
|
|
self.dialog = Dialog::Message("Failed to add device.".into());
|
|
return Err("failed to add device".into())
|
|
};
|
|
let track = self.track_mut().expect("no active track");
|
|
track.devices.push(sampler);
|
|
Ok(())
|
|
},
|
|
1 => {
|
|
todo!();
|
|
Ok(())
|
|
},
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
fn wrap_dialog (dialog: impl Content<TuiOut>) -> impl Content<TuiOut> {
|
|
Fixed::xy(70, 23, Tui::fg_bg(Rgb(255,255,255), Rgb(16,16,16), Bsp::b(
|
|
Repeat(" "), Outer(true, Style::default().fg(Tui::g(96))).enclose(dialog))))
|
|
}
|
|
impl ScenesView for App {
|
|
fn w_side (&self) -> u16 { 20 }
|
|
fn w_mid (&self) -> u16 { (self.width() as u16).saturating_sub(self.w_side()) }
|
|
fn h_scenes (&self) -> u16 { (self.height() as u16).saturating_sub(20) }
|
|
}
|
|
|
|
|
|
//#[dizzle::ns]
|
|
//#[dizzle::ns::include(TuiOut)]
|
|
//trait AppApi {
|
|
//#[dizzle::ns::word("sessions")]
|
|
//fn view_sessions (&self) -> Box<dyn Render<TuiOut>> {
|
|
//Min::x(30, Fixed::y(6, Stack::south(
|
|
//move|add: &mut dyn FnMut(&dyn Render<TuiOut>)|{
|
|
//let fg = Rgb(224, 192, 128);
|
|
//for (index, name) in ["session1", "session2", "session3"].iter().enumerate() {
|
|
//let bg = if index == 0 { Rgb(48,64,32) } else { Rgb(16, 32, 24) };
|
|
//add(&Fixed::y(2, Fill::x(Tui::bg(bg, Align::w(Tui::fg(fg, name))))));
|
|
//}
|
|
//}
|
|
//))).into()
|
|
//}
|
|
//#[dizzle::ns::expr("bold")]
|
|
//fn view_bold (&self, value: bool, x: Box<dyn Render<TuiOut>>) -> Box<dyn Render<TuiOut>> {
|
|
//Box::new(Tui::bold(value, x))
|
|
//}
|
|
//}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//has_editor!(|self: App|{
|
|
//editor = self.editor;
|
|
//editor_w = {
|
|
//let size = self.size.w();
|
|
//let editor = self.editor.as_ref().expect("missing editor");
|
|
//let time_len = editor.time_len().get();
|
|
//let time_zoom = editor.time_zoom().get().max(1);
|
|
//(5 + (time_len / time_zoom)).min(size.saturating_sub(20)).max(16)
|
|
//};
|
|
//editor_h = 15;
|
|
//is_editing = self.editor.is_some();
|
|
//});
|