refactor: extract dizzle

This commit is contained in:
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 2026-01-17 03:43:48 +02:00
parent ac7fbdb779
commit 044f60ebcb
8 changed files with 176 additions and 181 deletions

View file

@ -27,8 +27,7 @@
xdg::BaseDirectories,
atomic_float::*,
tek_device::{*, tek_engine::*},
tengri::{*, dsl::*, input::*, output::*},
tengri::tui::*,
tengri::{*, input::*, output::*, tui::*},
tengri::tui::ratatui::{
self,
prelude::{Rect, Style, Stylize, Buffer, Modifier, buffer::Cell, Color::{self, *}},
@ -161,91 +160,6 @@ pub mod core {
..Default::default()
}
}
pub fn update_clock (&self) {
ViewCache::update_clock(&self.project.clock.view_cache, self.clock(), self.size.w() > 80)
}
/// Set modal dialog.
pub fn set_dialog (&mut self, mut dialog: Dialog) -> Dialog {
std::mem::swap(&mut self.dialog, &mut dialog);
dialog
}
/// Set picked device in device pick dialog.
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!(),
}
}
/// Return reference to content browser if open.
pub fn browser (&self) -> Option<&Browse> {
if let Dialog::Browse(_, ref b) = self.dialog { Some(b) } else { None }
}
/// Is a MIDI editor currently focused?
pub fn editor_focused (&self) -> bool { false }
/// Toggle MIDI editor.
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());
}
}
}
}
impl Config {
const CONFIG: &'static str = "tek.edn";
@ -259,11 +173,11 @@ pub mod core {
}
/// Write initial contents of configuration.
pub fn init (&mut self) -> Usually<()> {
self.init_file(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.load(&dsl))?;
self.init_one(Self::CONFIG, Self::DEFAULTS, |cfgs, dsl|cfgs.add(&dsl))?;
Ok(())
}
/// Write initial contents of a configuration file.
pub fn init_file (
pub fn init_one (
&mut self, path: &str, defaults: &str, mut each: impl FnMut(&mut Self, &str)->Usually<()>
) -> Usually<()> {
if self.dirs.find_config_file(path).is_none() {
@ -278,41 +192,69 @@ pub mod core {
return Err(format!("{path}: not found").into())
})
}
/// Load a configuration from [Dsl] source.
pub fn load (&mut self, dsl: impl Dsl) -> Usually<()> {
dsl.each(|item|if let Some(expr) = item.expr()? {
/// Add statements to configuration from [Dsl] source.
pub fn add (&mut self, dsl: impl Dsl) -> Usually<()> {
dsl.each(|item|self.add_one(item))
}
fn add_one (&self, item: impl Dsl) -> Usually<()> {
if let Some(expr) = item.expr()? {
let head = expr.head()?;
let tail = expr.tail()?;
let name = tail.head()?;
let body = tail.tail()?;
//println!("Config::load: {} {} {}", head.unwrap_or_default(), name.unwrap_or_default(), body.unwrap_or_default());
match head {
Some("mode") if let Some(name) = name =>
Mode::<Arc<str>>::load_into(&self.modes, &name, &body)?,
Some("keys") if let Some(name) = name =>
Bind::<TuiEvent, Arc<str>>::load_into(&self.binds, &name, &body)?,
Some("view") if let Some(name) = name => {
self.views.write().unwrap().insert(name.into(), body.src()?.unwrap_or_default().into());
},
Some("mode") if let Some(name) = name => load_mode(&self.modes, &name, &body)?,
Some("keys") if let Some(name) = name => load_bind(&self.binds, &name, &body)?,
Some("view") if let Some(name) = name => load_view(&self.views, &name, &body)?,
_ => return Err(format!("Config::load: expected view/keys/mode, got: {item:?}").into())
}
Ok(())
} else {
return Err(format!("Config::load: expected expr, got: {item:?}").into())
})
}
}
}
impl Mode<Arc<str>> {
pub fn load_into (modes: &Modes, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
let mut mode = Self::default();
//println!("Mode::load_into: {}: {body:?}", name.as_ref());
body.each(|item|mode.load_one(item))?;
modes.write().unwrap().insert(name.as_ref().into(), Arc::new(mode));
pub fn load_view (views: &Views, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
views.write().unwrap().insert(name.as_ref().into(), body.src()?.unwrap_or_default().into());
Ok(())
}
pub fn load_mode (modes: &Modes, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
let mut mode = Mode::default();
body.each(|item|mode.add(item))?;
modes.write().unwrap().insert(name.as_ref().into(), Arc::new(mode));
Ok(())
}
pub fn load_bind (binds: &Binds, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
let mut map = Bind::new();
body.each(|item|if item.expr().head() == Ok(Some("see")) {
// TODO
Ok(())
}
fn load_one (&mut self, dsl: impl Dsl) -> Usually<()> {
} 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())
})?;
binds.write().unwrap().insert(name.as_ref().into(), map);
Ok(())
}
impl Mode<Arc<str>> {
fn add (&mut self, dsl: impl Dsl) -> Usually<()> {
Ok(if let Ok(Some(expr)) = dsl.expr() && let Ok(Some(head)) = expr.head() {
//println!("Mode::load_one: {head} {:?}", expr.tail());
//println!("Mode::add: {head} {:?}", expr.tail());
let tail = expr.tail()?.map(|x|x.trim()).unwrap_or("");
match head {
"name" => self.name.push(tail.into()),
@ -323,18 +265,18 @@ pub mod core {
Ok(())
})?,
"mode" => if let Some(id) = tail.head()? {
Self::load_into(&self.modes, &id, &tail.tail())?;
load_mode(&self.modes, &id, &tail.tail())?;
} else {
return Err(format!("Mode::load_one: self: incomplete: {expr:?}").into());
return Err(format!("Mode::add: self: incomplete: {expr:?}").into());
},
_ => {
return Err(format!("Mode::load_one: unexpected expr: {head:?} {tail:?}").into())
return Err(format!("Mode::add: unexpected expr: {head:?} {tail:?}").into())
},
};
} else if let Ok(Some(word)) = dsl.word() {
self.view.push(word.into());
} else {
return Err(format!("Mode::load_one: unexpected: {dsl:?}").into());
return Err(format!("Mode::add: unexpected: {dsl:?}").into());
})
}
}
@ -367,35 +309,6 @@ pub mod core {
.flatten()
}
}
impl Bind<TuiEvent, Arc<str>> {
pub fn load_into (binds: &Binds, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
//println!("Bind::load_into: {}: {body:?}", name.as_ref());
let mut map = Self::new();
body.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())
})?;
binds.write().unwrap().insert(name.as_ref().into(), map);
Ok(())
}
}
impl<C> Binding<C> {
pub fn from_dsl (dsl: impl Dsl) -> Usually<Self> {
let command: Option<C> = None;
@ -411,6 +324,15 @@ pub mod core {
}
}
pub mod ns {
// TODO make these enumerable:
//
// impl Ns<Arc<str>> for App {
// const NS: To<App, Arc<str>> = To::new(|_, _|Default::default())
// .key(":foo", |_|"bar".into())
// .key(":bar", |_|"baz".into());
// }
//
//
use super::{*, model::*, gui::*};
// Allow source to be read as Literal string
dsl_ns!(App: Arc<str> { literal = |dsl|Ok(dsl.src()?.map(|x|x.into())); });
@ -537,7 +459,6 @@ pub mod ns {
None
});
});
impl<'a> DslNs<'a, AppCommand> for App {}
impl<'a> DslNsExprs<'a, AppCommand> for App {}
impl<'a> DslNsWords<'a, AppCommand> for App {
@ -550,6 +471,93 @@ pub mod ns {
"cancel" => AppCommand::Cancel,
});
}
impl App {
pub fn update_clock (&self) {
ViewCache::update_clock(&self.project.clock.view_cache, self.clock(), self.size.w() > 80)
}
/// Set modal dialog.
pub fn set_dialog (&mut self, mut dialog: Dialog) -> Dialog {
std::mem::swap(&mut self.dialog, &mut dialog);
dialog
}
/// Set picked device in device pick dialog.
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!(),
}
}
/// Return reference to content browser if open.
pub fn browser (&self) -> Option<&Browse> {
if let Dialog::Browse(_, ref b) = self.dialog { Some(b) } else { None }
}
/// Is a MIDI editor currently focused?
pub fn editor_focused (&self) -> bool { false }
/// Toggle MIDI editor.
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 mod tui {
use super::{*, model::*, gui::*};