tek/crates/app/app_bind.rs
2025-08-24 02:42:30 +03:00

230 lines
8.4 KiB
Rust

use crate::*;
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<'t> DslNs<'t, AppCommand> for App {
dsl_exprs!(|app| -> AppCommand { /* TODO */ });
dsl_words!(|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 })
},
});
pub fn wrap_inc (index: usize, count: usize) -> usize {
if count > 0 { (index + 1) % count } else { 0 }
}
pub fn wrap_dec (index: usize, count: usize) -> usize {
if count > 0 { index.overflowing_sub(1).0.min(count.saturating_sub(1)) } else { 0 }
}
impl 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));