tek/app/tek_bind.rs
unspeaker ef81b085a0
Some checks failed
/ build (push) Has been cancelled
break up into crates again
2025-09-10 01:58:32 +03:00

325 lines
12 KiB
Rust

use crate::*;
pub type Binds = Arc<RwLock<BTreeMap<Arc<str>, EventMap<TuiEvent, Arc<str>>>>>;
/// A collection of input bindings.
#[derive(Debug)]
pub struct EventMap<E, C>(
/// Map of each event (e.g. key combination) to
/// all command expressions bound to it by
/// all loaded input layers.
pub BTreeMap<E, Vec<Binding<C>>>
);
/// An input binding.
#[derive(Debug, Clone)]
pub struct Binding<C> {
pub commands: Arc<[C]>,
pub condition: Option<Condition>,
pub description: Option<Arc<str>>,
pub source: Option<Arc<PathBuf>>,
}
/// Input bindings are only returned if this evaluates to true
#[derive(Clone)]
pub struct Condition(Arc<Box<dyn Fn()->bool + Send + Sync>>);
/// Default is always empty map regardless if `E` and `C` implement [Default].
impl<E, C> Default for EventMap<E, C> {
fn default () -> Self { Self(Default::default()) }
}
impl<E: Clone + Ord, C> EventMap<E, C> {
/// Create a new event map
pub fn new () -> Self {
Default::default()
}
/// Add a binding to an owned event map.
pub fn def (mut self, event: E, binding: Binding<C>) -> Self {
self.add(event, binding);
self
}
/// Add a binding to an event map.
pub fn add (&mut self, event: E, binding: Binding<C>) -> &mut Self {
if !self.0.contains_key(&event) {
self.0.insert(event.clone(), Default::default());
}
self.0.get_mut(&event).unwrap().push(binding);
self
}
/// Return the binding(s) that correspond to an event.
pub fn query (&self, event: &E) -> Option<&[Binding<C>]> {
self.0.get(event).map(|x|x.as_slice())
}
/// Return the first binding that corresponds to an event, considering conditions.
pub fn dispatch (&self, event: &E) -> Option<&Binding<C>> {
self.query(event)
.map(|bb|bb.iter().filter(|b|b.condition.as_ref().map(|c|(c.0)()).unwrap_or(true)).next())
.flatten()
}
}
impl EventMap<TuiEvent, Arc<str>> {
pub fn load_into (binds: &Binds, name: &impl AsRef<str>, body: &impl Dsl) -> Usually<()> {
println!("EventMap::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;
let condition: Option<Condition> = None;
let description: Option<Arc<str>> = None;
let source: Option<Arc<PathBuf>> = None;
if let Some(command) = command {
Ok(Self { commands: [command].into(), condition, description, source })
} else {
Err(format!("no command in {dsl:?}").into())
}
}
}
impl_debug!(Condition |self, w| { write!(w, "*") });
handle!(TuiIn:|self: App, input|{
let mut commands = vec![];
for id in self.mode.keys.iter() {
if let Some(event_map) = self.config.binds.clone().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() {
if let Some(command) = self.from(command)? as Option<AppCommand> {
commands.push(command)
}
}
}
}
}
}
for command in commands.into_iter() {
let result = command.execute(self);
match result {
Ok(undo) => {
self.history.push((command, undo));
},
Err(e) => {
self.history.push((command, None));
return Err(e)
}
}
}
Ok(None)
});
#[derive(Debug, Copy, Clone)]
pub enum Axis { X, Y, Z, I }
impl<'a> DslNs<'a, AppCommand> for App {}
impl<'a> DslNsExprs<'a, AppCommand> for App {}
impl<'a> DslNsWords<'a, AppCommand> for App {
dsl_words!('a |app| -> AppCommand {
"x/inc" => AppCommand::Inc { axis: Axis::X },
"x/dec" => AppCommand::Dec { axis: Axis::X },
"y/inc" => AppCommand::Inc { axis: Axis::Y },
"y/dec" => AppCommand::Dec { axis: Axis::Y },
"confirm" => AppCommand::Confirm,
"cancel" => AppCommand::Cancel,
});
}
impl Default for AppCommand { fn default () -> Self { Self::Nop } }
def_command!(AppCommand: |app: App| {
Nop => Ok(None),
Confirm => Ok(match &app.dialog {
Dialog::Menu(index, items) => {
let callback = items.0[*index].1.clone();
callback(app)?;
None
},
_ => todo!(),
}),
Cancel => todo!(), // TODO delegate:
Inc { axis: Axis } => Ok(match (&app.dialog, axis) {
(Dialog::None, _) => todo!(),
(Dialog::Menu(_, _), Axis::Y) => AppCommand::SetDialog { dialog: app.dialog.menu_next() }
.execute(app)?,
_ => todo!()
}),
Dec { axis: Axis } => Ok(match (&app.dialog, axis) {
(Dialog::None, _) => None,
(Dialog::Menu(_, _), Axis::Y) => AppCommand::SetDialog { dialog: app.dialog.menu_prev() }
.execute(app)?,
_ => todo!()
}),
SetDialog { dialog: Dialog } => {
swap_value(&mut app.dialog, dialog, |dialog|Self::SetDialog { dialog })
},
});
//AppCommand => {
//("x/inc" /
//("stop-all") => todo!(),//app.project.stop_all(),
//("enqueue", clip: Option<Arc<RwLock<MidiClip>>>) => 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!(),
//("edit" / editor: MidiEditCommand) => todo!(),
//};
//DialogCommand;
//ArrangementCommand;
//ClockCommand;
//SamplerCommand;
//PoolCommand;
//MidiEditCommand;
//take!(DialogCommand |state: App, iter|Take::take(&state.dialog, iter));
//#[derive(Clone, Debug)]
//pub enum DialogCommand {
//Open { dialog: Dialog },
//Close
//}
//impl Command<Option<Dialog>> for DialogCommand {
//fn execute (self, state: &mut Option<Dialog>) -> Perhaps<Self> {
//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<Dialog>)]//Nope.
//impl DialogCommand {
//fn open (dialog: &mut Option<Dialog>, new: Dialog) -> Perhaps<Self> {
//*dialog = Some(new);
//Ok(None)
//}
//fn close (dialog: &mut Option<Dialog>) -> Perhaps<Self> {
//*dialog = None;
//Ok(None)
//}
//}
//
//dsl_bind!(AppCommand: App {
//enqueue = |app, clip: Option<Arc<RwLock<MidiClip>>>| { 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<Self> {
////Ok(app.set_color(Some(theme)).map(|theme|Self::Color{theme}))
////}
////fn launch (app: &mut App) -> Perhaps<Self> {
////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));