// ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ //██Let me play the world's tiniest piano for you. ██ //█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█ //█▙▙█▙▙▙█▙▙█▙▙▙█▙▙█▙▙▙█▙▙█▙▙▙█▙▙█▙▙▙█▙▙█▙▙▙█▙▙█▙▙▙██ //█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█ //███████████████████████████████████████████████████ //█ ▀ ▀ ▀ █ #![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)] pub use ::{ tek_engine::*, tek_device, tek_device::*, tengri::{ Usually, Perhaps, Has, MaybeHas, has, maybe_has, dsl::*, input::*, output::*, tui::*, tui::ratatui, tui::ratatui::prelude::buffer::Cell, tui::ratatui::prelude::Color::{self, *}, tui::ratatui::prelude::{Style, Stylize, Buffer, Modifier}, tui::crossterm, tui::crossterm::event::{Event, KeyCode::{self, *}}, }, std::{ path::{Path, PathBuf}, sync::{Arc, RwLock}, sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed}, error::Error, collections::BTreeMap, fmt::Write, }, xdg::BaseDirectories, }; mod app_jack; pub use self::app_jack::*; #[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, /// Performance counter pub perf: PerfModel, /// Available view modes and input bindings pub config: Config, /// Currently selected mode pub mode: Mode>, /// Contains the currently edited musical arrangement pub project: Arrangement, /// Contains all recently created clips. pub pool: Pool, /// Undo history pub history: Vec<(AppCommand, Option)>, /// Dialog overlay pub dialog: Dialog, /// Base color. pub color: ItemTheme, } /// Configuration #[derive(Default, Debug)] pub struct Config { pub dirs: BaseDirectories, pub modes: Arc, Arc>>>>>, pub binds: Arc, EventMap, Arc>>>>, } #[derive(Default, Debug)] pub struct Mode { pub path: PathBuf, pub name: Vec, pub info: Vec, pub view: Vec, pub keys: Vec, pub modes: BTreeMap>, } /// Various possible dialog modes. #[derive(Debug, Clone, Default)] pub enum Dialog { #[default] None, Help(usize), Menu(usize), Device(usize), Message(Arc), Browser(BrowserTarget, Arc), Options, } impl Config { const CONFIG: &'static str = "tek.edn"; const DEFAULTS: &'static str = include_str!("../../tek.edn"); pub fn init () -> Usually { let mut cfgs: Self = Default::default(); cfgs.dirs = BaseDirectories::with_profile("tek", "v0"); cfgs.init_file(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.load_defs(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_defs (&mut self, dsl: impl Dsl) -> Usually<()> { dsl.each(|item|{ println!("{item:?} {:?}", item.expr().head()); match item.expr().head() { Ok(Some("keys")) if let Some(id) = item.expr().tail().head()? => self.load_bind(id.into(), item), Ok(Some("mode")) if let Some(id) = item.expr().tail().head()? => self.load_mode(id.into(), item), _ => return Err(format!("load_defs: unexpected: {item:?}").into()) } }) } pub fn load_bind (&mut self, id: Arc, item: impl Dsl) -> Usually<()> { let mut map = EventMap::new(); item.expr().tail().tail()?.each(|item|if item.expr().head() == Ok(Some("see")) { // TODO Ok(()) } else if let Ok(Some(word)) = item.expr().head().word() { map.add(TuiEvent::from_dsl(item.expr()?.head()?)?, Binding { commands: [item.expr()?.tail()?.unwrap_or_default().into()].into(), condition: None, description: None, source: None }); Ok(()) } else { return Err(format!("load_bind: unexpected: {item:?}").into()) })?; self.binds.write().unwrap().insert(id, map); Ok(()) } pub fn load_mode (&mut self, id: Arc, item: impl Dsl) -> Usually<()> { let mut mode = Mode::default(); item.expr().tail().tail()?.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>, 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" => mode.keys.push(item.expr()?.tail()?.map(|x|x.trim()).unwrap_or("").into()), "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: |self: App|self.project.editor); has!(Selection: |self: App|self.project.selection); has!(Vec: |self: App|self.project.midi_ins); has!(Vec: |self: App|self.project.midi_outs); has!(Vec: |self: App|self.project.scenes); has!(Vec: |self: App|self.project.tracks); has!(Measure: |self: App|self.size); has_clips!( |self: App|self.pool.clips); maybe_has!(Track: |self: App| { MaybeHas::::get(&self.project) }; { MaybeHas::::get_mut(&mut self.project) }); maybe_has!(Scene: |self: App| { MaybeHas::::get(&self.project) }; { MaybeHas::::get_mut(&mut self.project) }); impl HasClipsSize for App { fn clips_size (&self) -> &Measure { &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> { 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 (&self, index: D) -> Box> { 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>>, src: &str ) -> Box> { let err: Option> = 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("▀")))), }) } fn wrap_dialog (dialog: impl Content) -> impl Content { 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 h_scenes (&self) -> u16 { (self.height() as u16).saturating_sub(20) } fn w_side (&self) -> u16 { 20 } fn w_mid (&self) -> u16 { (self.width() as u16).saturating_sub(self.w_side()) } } handle!(TuiIn:|self: App, input|{ panic!("wat: {:?}", self.mode); for keys in self.mode.keys.iter() { panic!("{keys} {:?}", self.config.binds.read().unwrap()); if let Some(binding) = self.config.binds.read().unwrap().get(keys.as_ref()) { panic!("{binding:?}"); } } Ok(None) }); #[derive(Debug)] pub enum AppCommand { /* TODO */ } impl Dialog { pub fn menu_selected (&self) -> Option { if let Self::Menu(selected) = self { Some(*selected) } else { None } } pub fn device_kind (&self) -> Option { if let Self::Device(index) = self { Some(*index) } else { None } } pub fn device_kind_next (&self) -> Option { self.device_kind().map(|index|(index + 1) % device_kinds().len()) } pub fn device_kind_prev (&self) -> Option { 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> { todo!() } pub fn browser_target (&self) -> Option<&BrowserTarget> { todo!() } } #[tengri_proc::command(Option)] impl DialogCommand { fn open (dialog: &mut Option, new: Dialog) -> Perhaps { *dialog = Some(new); Ok(None) } fn close (dialog: &mut Option) -> Perhaps { *dialog = None; Ok(None) } } 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) { //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!(), } } } dsl_ns! { |app: App| Box> => { ("bold", value: bool, x: Box>) => Box::new(Tui::bold(value, x)), ("fg", color: Color, x: Box>) => Box::new(Tui::fg(color, x)), ("bg", color: Color, x: Box>) => Box::new(Tui::bg(color, x)), ("fg/bg", fg: Color, bg: Color, x: Box>) => Box::new(Tui::fg_bg(fg, bg, x)), ("bsp/n", a: Box>, b: Box>) => Box::new(Bsp::n(a, b)), ("bsp/s", a: Box>, b: Box>) => Box::new(Bsp::s(a, b)), ("bsp/e", a: Box>, b: Box>) => Box::new(Bsp::e(a, b)), ("bsp/w", a: Box>, b: Box>) => Box::new(Bsp::w(a, b)), ("bsp/a", a: Box>, b: Box>) => Box::new(Bsp::a(a, b)), ("bsp/b", a: Box>, b: Box>) => Box::new(Bsp::b(a, b)), ("align/n", x: Box>) => Box::new(Align::n(x)), ("align/s", x: Box>) => Box::new(Align::s(x)), ("align/e", x: Box>) => Box::new(Align::e(x)), ("align/w", x: Box>) => Box::new(Align::w(x)), ("align/x", x: Box>) => Box::new(Align::x(x)), ("align/y", x: Box>) => Box::new(Align::y(x)), ("align/c", x: Box>) => Box::new(Align::c(x)), ("fill/x", x: Box>) => Box::new(Fill::x(x)), ("fill/y", x: Box>) => Box::new(Fill::y(x)), ("fill/xy", x: Box>) => Box::new(Fill::xy(x)), ("fixed/x", x: u16, c: Box>) => Box::new(Fixed::x(x, c)), ("fixed/y", y: u16, c: Box>) => Box::new(Fixed::y(y, c)), ("fixed/xy", x: u16, y: u16, c: Box>) => Box::new(Fixed::xy(x, y, c)), ("min/x", x: u16, c: Box>) => Box::new(Min::x(x, c)), ("min/y", y: u16, c: Box>) => Box::new(Min::y(y, c)), ("min/xy", x: u16, y: u16, c: Box>) => Box::new(Min::xy(x, y, c)), ("max/x", x: u16, c: Box>) => Box::new(Max::x(x, c)), ("max/y", y: u16, c: Box>) => Box::new(Max::y(y, c)), ("max/xy", x: u16, y: u16, c: Box>) => Box::new(Max::xy(x, y, c)), ":view/menu" => app.view(stringify!( (bg :color/bg (bsp/s :view/ports/outs (bsp/n :view/ports/ins :view/modes))) )), ":view/modes" => Box::new({ let modes = app.config.modes.clone(); let height = (modes.read().unwrap().len() * 2) as u16; Fixed::y(height, Fill::x(Stack::south(move|add: &mut dyn FnMut(&dyn Render)|{ for (index, (id, profile)) in modes.read().unwrap().iter().enumerate() { let bg = if index == 0 { Rgb(48,64,32) } else { Rgb(16, 32, 24) }; let name = profile.name.get(0).map(|x|x.as_ref()).unwrap_or(""); let info = profile.info.get(0).map(|x|x.as_ref()).unwrap_or(""); let fg1 = Rgb(224, 192, 128); let fg2 = Rgb(224, 128, 32); add(&Fixed::y(2, Fill::x(Tui::bg(bg, Bsp::s( Fill::x(Bsp::a(Fill::x(Align::w(Tui::fg(fg1, name))), Fill::x(Align::e(Tui::fg(fg2, id))))), Fill::x(Align::w(info))))))); } })))}), ":view/ports/outs" => app.view(stringify!((fill/x (fixed/y 3 (bsp/a (fill/x (align/w "L AUDIO OUT") (bsp/a "MIDI OUT" (fill/x (align/e "AUDIO OUT R"))))))))), ":view/ports/ins" => app.view(stringify!(fill/x (fixed/y 3 (bsp/a (fill/x (align/w "L AUDIO IN ") (bsp/a "MIDI IN " (fill/x (align/e "AUDIO IN R")))))))), ":view/browse" => app.view(stringify!(bsp/s (padding/xy 3 1 :view/browse-title) (enclose (fg (g 96)) :view/browser))), ":view/browse/title" => Box::new(Fill::x(Align::w(FieldV(Default::default(), match app.dialog.browser_target().unwrap() { BrowserTarget::SaveProject => "Save project:", BrowserTarget::LoadProject => "Load project:", BrowserTarget::ImportSample(_) => "Import sample:", BrowserTarget::ExportSample(_) => "Export sample:", BrowserTarget::ImportClip(_) => "Import clip:", BrowserTarget::ExportClip(_) => "Export clip:", }, Shrink::x(3, Fixed::y(1, Tui::fg(Tui::g(96), RepeatH("🭻")))))))), ":view/device" => { let selected = app.dialog.device_kind().unwrap(); Box::new(Bsp::s(Tui::bold(true, "Add device"), Map::south(1, move||device_kinds().iter(), move|label: &&'static str, i|{ let bg = if i == selected { Rgb(64,128,32) } else { Rgb(0,0,0) }; let lb = if i == selected { "[ " } else { " " }; let rb = if i == selected { " ]" } else { " " }; Fill::x(Tui::bg(bg, Bsp::e(lb, Bsp::w(rb, "FIXME device name")))) }))) }, //(":view/options", view_options), }; bool => { ":focused/editor" => app.project.editor.is_some(), ":focused/dialog" => !matches!(app.dialog, Dialog::None), ":focused/message" => matches!(app.dialog, Dialog::Message(..)), ":focused/add_device" => matches!(app.dialog, Dialog::Device(..)), ":focused/browser" => app.dialog.browser().is_some(), ":focused/pool/import" => matches!(app.pool.mode, Some(PoolMode::Import(..))), ":focused/pool/export" => matches!(app.pool.mode, Some(PoolMode::Export(..))), ":focused/pool/rename" => matches!(app.pool.mode, Some(PoolMode::Rename(..))), ":focused/pool/length" => matches!(app.pool.mode, Some(PoolMode::Length(..))), ":focused/clip" => !app.editor_focused() && matches!(app.selection(), Selection::TrackClip{..}), ":focused/track" => !app.editor_focused() && matches!(app.selection(), Selection::Track(..)), ":focused/scene" => !app.editor_focused() && matches!(app.selection(), Selection::Scene(..)), ":focused/mix" => !app.editor_focused() && matches!(app.selection(), Selection::Mix), }; ItemTheme => { ":_theme_stub" => Default::default() }; Dialog => { ":dialog/none" => Dialog::None, ":dialog/options" => Dialog::Options, ":dialog/device" => Dialog::Device(0), ":dialog/device/prev" => Dialog::Device(0), ":dialog/device/next" => Dialog::Device(0), ":dialog/help" => Dialog::Help(0), ":dialog/menu" => Dialog::Menu(0), ":dialog/save" => Dialog::Browser(BrowserTarget::SaveProject, Browser::new(None).unwrap().into()), ":dialog/load" => Dialog::Browser(BrowserTarget::LoadProject, Browser::new(None).unwrap().into()), ":dialog/import/clip" => Dialog::Browser(BrowserTarget::ImportClip(Default::default()), Browser::new(None).unwrap().into()), ":dialog/export/clip" => Dialog::Browser(BrowserTarget::ExportClip(Default::default()), Browser::new(None).unwrap().into()), ":dialog/import/sample" => Dialog::Browser(BrowserTarget::ImportSample(Default::default()), Browser::new(None).unwrap().into()), ":dialog/export/sample" => Dialog::Browser(BrowserTarget::ExportSample(Default::default()), Browser::new(None).unwrap().into()), }; Selection => { ":select/scene" => app.selection().select_scene(app.tracks().len()), ":select/scene/next" => app.selection().select_scene_next(app.scenes().len()), ":select/scene/prev" => app.selection().select_scene_prev(), ":select/track" => app.selection().select_track(app.tracks().len()), ":select/track/next" => app.selection().select_track_next(app.tracks().len()), ":select/track/prev" => app.selection().select_track_prev(), }; Option => { ":editor/pitch" => Some((app.editor().as_ref().map(|e|e.get_note_pos()).unwrap() as u8).into()) }; Option => { ":selected/scene" => app.selection().scene(), ":selected/track" => app.selection().track(), }; Option>> => { ":selected/clip" => if let Selection::TrackClip { track, scene } = app.selection() { app.scenes()[*scene].clips[*track].clone() } else { None } }; AppCommand => { ("stop-all") => todo!(),//app.project.stop_all(), ("enqueue", clip: Option>>) => todo!(), ("history", delta: isize) => todo!(), ("zoom", zoom: usize) => todo!(), ("select", selection: Selection) => todo!(), ("dialog" / command: DialogCommand) => todo!(), ("project" / command: ArrangementCommand) => todo!(), ("clock" / command: ClockCommand) => todo!(), ("sampler" / command: SamplerCommand) => todo!(), ("pool" / command: PoolCommand) => todo!(), ("pool" / editor: MidiEditCommand) => todo!(), }; DialogCommand; ArrangementCommand; ClockCommand; SamplerCommand; PoolCommand; MidiEditCommand; Color => { ("g", n: u8) => Color::Rgb(n, n, n), ("rgb", red: u8, green: u8, blue: u8) => Color::Rgb(red, green, blue), ":color/bg" => Color::Rgb(28, 32, 36), }; } dsl_ns! { num |app: App| u8; u16 => { ":w/sidebar" => app.project.w_sidebar(app.editor().is_some()), ":h/sample-detail" => 6.max(app.height() as u16 * 3 / 9), }; usize => { ":scene-count" => app.scenes().len(), ":track-count" => app.tracks().len(), ":device-kind" => app.dialog.device_kind().unwrap_or(0), ":device-kind/next" => app.dialog.device_kind_next().unwrap_or(0), ":device-kind/prev" => app.dialog.device_kind_prev().unwrap_or(0), }; isize; } /////////////////////////////////////////////////////////////////////////////////////////////////// //#[derive(Clone, Debug)] //pub enum DialogCommand { //Open { dialog: Dialog }, //Close //} //impl Command> for DialogCommand { //fn execute (self, state: &mut Option) -> Perhaps { //match self { //Self::Open { dialog } => { //*state = Some(dialog); //}, //Self::Close => { //*state = None; //} //}; //Ok(None) //} //} //dsl!(DialogCommand: |self: Dialog, iter|todo!()); //Dsl::take(&mut self.dialog, iter)); //#[tengri_proc::command(Option)]//Nope. //impl DialogCommand { //fn open (dialog: &mut Option, new: Dialog) -> Perhaps { //*dialog = Some(new); //Ok(None) //} //fn close (dialog: &mut Option) -> Perhaps { //*dialog = None; //Ok(None) //} //} // //dsl_bind!(AppCommand: App { //enqueue = |app, clip: Option>>| { todo!() }; //history = |app, delta: isize| { todo!() }; //zoom = |app, zoom: usize| { todo!() }; //stop_all = |app| { app.tracks_stop_all(); Ok(None) }; ////dialog = |app, command: DialogCommand| ////Ok(command.delegate(&mut app.dialog, |c|Self::Dialog{command: c})?); //project = |app, command: ArrangementCommand| //Ok(command.delegate(&mut app.project, |c|Self::Project{command: c})?); //clock = |app, command: ClockCommand| //Ok(command.execute(app.clock_mut())?.map(|c|Self::Clock{command: c})); //sampler = |app, command: SamplerCommand| //Ok(app.project.sampler_mut().map(|s|command.delegate(s, |command|Self::Sampler{command})) //.transpose()?.flatten()); //pool = |app, command: PoolCommand| { //let undo = command.clone().delegate(&mut app.pool, |command|AppCommand::Pool{command})?; //// update linked editor after pool action //match command { //// autoselect: automatically load selected clip in editor //PoolCommand::Select { .. } | //// autocolor: update color in all places simultaneously //PoolCommand::Clip { command: PoolClipCommand::SetColor { .. } } => { //let clip = app.pool.clip().clone(); //app.editor_mut().map(|editor|editor.set_clip(clip.as_ref())) //}, //_ => None //}; //Ok(undo) //}; //select = |app, selection: Selection| { //*app.project.selection_mut() = selection; ////todo! ////if let Some(ref mut editor) = app.editor_mut() { ////editor.set_clip(match selection { ////Selection::TrackClip { track, scene } if let Some(Some(Some(clip))) = app ////.project ////.scenes.get(scene) ////.map(|s|s.clips.get(track)) ////=> ////Some(clip), ////_ => ////None ////}); ////} //Ok(None) ////("select" [t: usize, s: usize] Some(match (t.expect("no track"), s.expect("no scene")) { ////(0, 0) => Self::Select(Selection::Mix), ////(t, 0) => Self::Select(Selection::Track(t)), ////(0, s) => Self::Select(Selection::Scene(s)), ////(t, s) => Self::Select(Selection::TrackClip { track: t, scene: s }) }))) //// autoedit: load focused clip in editor. //}; ////fn color (app: &mut App, theme: ItemTheme) -> Perhaps { ////Ok(app.set_color(Some(theme)).map(|theme|Self::Color{theme})) ////} ////fn launch (app: &mut App) -> Perhaps { ////app.project.launch(); ////Ok(None) ////} //toggle_editor = |app, value: bool|{ app.toggle_editor(Some(value)); Ok(None) }; //editor = |app, command: MidiEditCommand| Ok(if let Some(editor) = app.editor_mut() { //let undo = command.clone().delegate(editor, |command|AppCommand::Editor{command})?; //// update linked sampler after editor action //app.project.sampler_mut().map(|sampler|match command { //// autoselect: automatically select sample in sampler //MidiEditCommand::SetNotePos { pos } => { sampler.set_note_pos(pos); }, //_ => {} //}); //undo //} else { //None //}); //}); //take!(ClockCommand |state: App, iter|Take::take(state.clock(), iter)); //take!(MidiEditCommand |state: App, iter|Ok(state.editor().map(|x|Take::take(x, iter)).transpose()?.flatten())); //take!(PoolCommand |state: App, iter|Take::take(&state.pool, iter)); //take!(SamplerCommand |state: App, iter|Ok(state.project.sampler().map(|x|Take::take(x, iter)).transpose()?.flatten())); //take!(ArrangementCommand |state: App, iter|Take::take(&state.project, iter)); //take!(DialogCommand |state: App, iter|Take::take(&state.dialog, iter)); //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(); //}); //Bsp::s("", //Map::south(1, //move||app.config.binds.layers.iter() //.filter_map(|a|(a.0)(app).then_some(a.1)) //.flat_map(|a|a) //.filter_map(|x|if let Value::Exp(_, iter)=x.value{ Some(iter) } else { None }) //.skip(offset) //.take(20), //|mut b,i|Fixed::x(60, Align::w(Bsp::e("(", Bsp::e( //b.next().map(|t|Fixed::x(16, Align::w(Tui::fg(Rgb(64,224,0), format!("{}", t.value))))), //Bsp::e(" ", Align::w(format!("{}", b.0.0.trim()))))))))))), //Dialog::Browser(BrowserTarget::Load, browser) => { //"bobcat".boxed() ////Bsp::s( ////Fill::x(Align::w(Margin::xy(1, 1, Bsp::e( ////Tui::bold(true, " Load project: "), ////Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))), ////Outer(true, Style::default().fg(Tui::g(96))) ////.enclose(Fill::xy(browser))) //}, //Dialog::Browser(BrowserTarget::Export, browser) => { //"bobcat".boxed() ////Bsp::s( ////Fill::x(Align::w(Margin::xy(1, 1, Bsp::e( ////Tui::bold(true, " Export: "), ////Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))), ////Outer(true, Style::default().fg(Tui::g(96))) ////.enclose(Fill::xy(browser))) //}, //Dialog::Browser(BrowserTarget::Import, browser) => { //"bobcat".boxed() ////Bsp::s( ////Fill::x(Align::w(Margin::xy(1, 1, Bsp::e( ////Tui::bold(true, " Import: "), ////Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))), ////Outer(true, Style::default().fg(Tui::g(96))) ////.enclose(Fill::xy(browser))) //}, // //pub fn view_meters_input (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s| //s.view_meters_input()) //} //pub fn view_meters_output (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s| //s.view_meters_output()) //} //pub fn view_history (&self) -> impl Content { //Fixed::y(1, Fill::x(Align::w(FieldH(self.color, //format!("History ({})", self.history.len()), //self.history.last().map(|last|Fill::x(Align::w(format!("{:?}", last.0)))))))) //} //pub fn view_status_h2 (&self) -> impl Content { //self.update_clock(); //let theme = self.color; //let clock = self.clock(); //let playing = clock.is_rolling(); //let cache = clock.view_cache.clone(); ////let selection = self.selection().describe(self.tracks(), self.scenes()); //let hist_len = self.history.len(); //let hist_last = self.history.last(); //Fixed::y(2, Stack::east(move|add: &mut dyn FnMut(&dyn Render)|{ //add(&Fixed::x(5, Tui::bg(if playing { Rgb(0, 128, 0) } else { Rgb(128, 64, 0) }, //Either::new(false, // TODO //Thunk::new(move||Fixed::x(9, Either::new(playing, //Tui::fg(Rgb(0, 255, 0), " PLAYING "), //Tui::fg(Rgb(255, 128, 0), " STOPPED "))) //), //Thunk::new(move||Fixed::x(5, Either::new(playing, //Tui::fg(Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)), //Tui::fg(Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",)))) //) //) //))); //add(&" "); //{ //let cache = cache.read().unwrap(); //add(&Fixed::x(15, Align::w(Bsp::s( //FieldH(theme, "Beat", cache.beat.view.clone()), //FieldH(theme, "Time", cache.time.view.clone()), //)))); //add(&Fixed::x(13, Align::w(Bsp::s( //Fill::x(Align::w(FieldH(theme, "BPM", cache.bpm.view.clone()))), //Fill::x(Align::w(FieldH(theme, "SR ", cache.sr.view.clone()))), //)))); //add(&Fixed::x(12, Align::w(Bsp::s( //Fill::x(Align::w(FieldH(theme, "Buf", cache.buf.view.clone()))), //Fill::x(Align::w(FieldH(theme, "Lat", cache.lat.view.clone()))), //)))); ////add(&Bsp::s( //////Fill::x(Align::w(FieldH(theme, "Selected", Align::w(selection)))), ////Fill::x(Align::w(FieldH(theme, format!("History ({})", hist_len), ////hist_last.map(|last|Fill::x(Align::w(format!("{:?}", last.0))))))), ////"" ////)); //////if let Some(last) = self.history.last() { //////add(&FieldV(theme, format!("History ({})", self.history.len()), //////Fill::x(Align::w(format!("{:?}", last.0))))); //////} //} //})) //} //pub fn view_status_v (&self) -> impl Content + use<'_> { //self.update_clock(); //let cache = self.project.clock.view_cache.read().unwrap(); //let theme = self.color; //let playing = self.clock().is_rolling(); //Tui::bg(theme.darker.rgb, Fixed::xy(20, 5, Outer(true, Style::default().fg(Tui::g(96))).enclose( //col!( //Fill::x(Align::w(Bsp::e( //Align::w(Tui::bg(if playing { Rgb(0, 128, 0) } else { Rgb(128, 64, 0) }, //Either::new(false, // TODO //Thunk::new(move||Fixed::x(9, Either::new(playing, //Tui::fg(Rgb(0, 255, 0), " PLAYING "), //Tui::fg(Rgb(255, 128, 0), " STOPPED "))) //), //Thunk::new(move||Fixed::x(5, Either::new(playing, //Tui::fg(Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)), //Tui::fg(Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",)))) //) //) //)), //Bsp::s( //FieldH(theme, "Beat", cache.beat.view.clone()), //FieldH(theme, "Time", cache.time.view.clone()), //), //))), //Fill::x(Align::w(FieldH(theme, "BPM", cache.bpm.view.clone()))), //Fill::x(Align::w(FieldH(theme, "SR ", cache.sr.view.clone()))), //Fill::x(Align::w(FieldH(theme, "Buf", Bsp::e(cache.buf.view.clone(), Bsp::e(" = ", cache.lat.view.clone()))))), //)))) //} //pub fn view_status (&self) -> impl Content + use<'_> { //self.update_clock(); //let cache = self.project.clock.view_cache.read().unwrap(); //view_status(Some(self.project.selection.describe(self.tracks(), self.scenes())), //cache.sr.view.clone(), cache.buf.view.clone(), cache.lat.view.clone()) //} //pub fn view_transport (&self) -> impl Content + use<'_> { //self.update_clock(); //let cache = self.project.clock.view_cache.read().unwrap(); //view_transport(self.project.clock.is_rolling(), //cache.bpm.view.clone(), cache.beat.view.clone(), cache.time.view.clone()) //} //pub fn view_editor (&self) -> impl Content + use<'_> { //let bg = self.editor() //.and_then(|editor|editor.clip().clone()) //.map(|clip|clip.read().unwrap().color.darker) //.unwrap_or(self.color.darker); //Fill::xy(Tui::bg(bg.rgb, self.editor())) //} //pub fn view_editor_status (&self) -> impl Content + use<'_> { //self.editor().map(|e|Fixed::x(20, Outer(true, Style::default().fg(Tui::g(96))).enclose( //Fill::y(Align::n(Bsp::s(e.clip_status(), e.edit_status())))))) //} //pub fn view_midi_ins_status (&self) -> impl Content + use<'_> { //self.project.view_midi_ins_status(self.color) //} //pub fn view_midi_outs_status (&self) -> impl Content + use<'_> { //self.project.view_midi_outs_status(self.color) //} //pub fn view_audio_ins_status (&self) -> impl Content + use<'_> { //self.project.view_audio_ins_status(self.color) //} //pub fn view_audio_outs_status (&self) -> impl Content + use<'_> { //self.project.view_audio_outs_status(self.color) //} //pub fn view_scenes (&self) -> impl Content + use<'_> { //Bsp::e( //Fixed::x(20, Align::nw(self.project.view_scenes_names())), //self.project.view_scenes_clips(), //) //} //pub fn view_scenes_names (&self) -> impl Content + use<'_> { //self.project.view_scenes_names() //} //pub fn view_scenes_clips (&self) -> impl Content + use<'_> { //self.project.view_scenes_clips() //} //pub fn view_tracks_inputs <'a> (&'a self) -> impl Content + use<'a> { //Fixed::y(1 + self.project.midi_ins.len() as u16, //self.project.view_inputs(self.color)) //} //pub fn view_tracks_outputs <'a> (&'a self) -> impl Content + use<'a> { //self.project.view_outputs(self.color) //} //pub fn view_tracks_devices <'a> (&'a self) -> impl Content + use<'a> { //Fixed::y(4, self.project.view_track_devices(self.color)) //} //pub fn view_tracks_names <'a> (&'a self) -> impl Content + use<'a> { //Fixed::y(2, self.project.view_track_names(self.color)) //} //pub fn view_pool (&self) -> impl Content + use<'_> { //Fixed::x(20, Bsp::s( //Fill::x(Align::w(FieldH(self.color, "Clip pool:", ""))), //Fill::y(Align::n(Tui::bg(Rgb(0, 0, 0), Outer(true, Style::default().fg(Tui::g(96))) //.enclose(PoolView(&self.pool))))))) //} //pub fn view_samples_keys (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s|s.view_list(true, self.editor().unwrap())) //} //pub fn view_samples_grid (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s|s.view_grid()) //} //pub fn view_sample_viewer (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s|s.view_sample(self.editor().unwrap().get_note_pos())) //} //pub fn view_sample_info (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s|s.view_sample_info(self.editor().unwrap().get_note_pos())) //} //pub fn view_sample_status (&self) -> impl Content + use<'_> { //self.project.sampler().map(|s|Outer(true, Style::default().fg(Tui::g(96))).enclose( //Fill::y(Align::n(s.view_sample_status(self.editor().unwrap().get_note_pos()))))) //} ////let options = ||["Projects", "Settings", "Help", "Quit"].iter(); ////let option = |a,i|Tui::fg(Rgb(255,255,255), format!("{}", a)); ////Bsp::s(Tui::bold(true, "tek!"), Bsp::s("", Map::south(1, options, option)))