modularize keymaps

This commit is contained in:
🪞👃🪞 2024-07-09 13:30:08 +03:00
parent d8c9abf744
commit 876d26e287
8 changed files with 235 additions and 275 deletions

View file

@ -1,34 +1,33 @@
pub mod mixer;
pub mod plugin;
pub mod sampler;
use crate::{core::*, handle, App, AppSection};
handle!(App |self, e| {
if let Some(ref mut modal) = self.modal {
if modal.handle(e)? {
self.modal = None;
return Ok(true)
};
pubmod!{ arranger chain mixer plugin sampler sequencer }
handle!{
App |self, e| {
if let Some(ref mut modal) = self.modal {
if modal.handle(e)? {
self.modal = None;
return Ok(true)
};
}
let handle_focused = |state: &mut Self|handle_keymap(
state, e, match state.section {
AppSection::Arranger => self::arranger::KEYMAP_ARRANGER,
AppSection::Sequencer => self::sequencer::KEYMAP_SEQUENCER,
AppSection::Chain => self::chain::KEYMAP_CHAIN,
}
);
Ok(if self.entered {
handle_focused(self)?
|| handle_keymap(self, e, KEYMAP)?
|| handle_keymap(self, e, KEYMAP_FOCUS)?
} else {
handle_keymap(self, e, KEYMAP)?
|| handle_keymap(self, e, KEYMAP_FOCUS)?
|| handle_focused(self)?
})
}
let handle_focused = |state: &mut Self|match state.section {
AppSection::Arranger =>
handle_keymap(state, e, KEYMAP_ARRANGER),
AppSection::Sequencer =>
handle_keymap(state, e, KEYMAP_SEQUENCER),
AppSection::Chain =>
handle_keymap(state, e, KEYMAP_CHAIN),
};
Ok(if self.entered {
handle_focused(self)?
|| handle_keymap(self, e, KEYMAP)?
|| handle_keymap(self, e, KEYMAP_FOCUS)?
} else {
handle_keymap(self, e, KEYMAP)?
|| handle_keymap(self, e, KEYMAP_FOCUS)?
|| handle_focused(self)?
})
});
}
const KEYMAP_FOCUS: &'static [KeyBinding<App>] = keymap!(App {
[Tab, NONE, "focus_next", "focus next area", focus_next],
@ -142,151 +141,6 @@ const KEYMAP: &'static [KeyBinding<App>] = keymap!(App {
// [Char('s'), NONE, "stop_and_rewind", "Stop and rewind", stop_and_rewind],
});
const KEYMAP_ARRANGER: &'static [KeyBinding<App>] = keymap!(App {
[Up, NONE, "arranger_cursor_up", "move cursor up", |app: &mut App| Ok(
match app.arranger_mode {
false => {app.scene_cursor = app.scene_cursor.saturating_sub(1); true},
true => {app.track_cursor = app.track_cursor.saturating_sub(1); true},
}
)],
[Down, NONE, "arranger_cursor_down", "move cursor down", |app: &mut App| Ok(
match app.arranger_mode {
false => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); true},
true => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); true},
}
)],
[Left, NONE, "arranger_cursor_left", "move cursor left", |app: &mut App| Ok(
match app.arranger_mode {
false => {app.track_cursor = app.track_cursor.saturating_sub(1); true},
true => {app.scene_cursor = app.scene_cursor.saturating_sub(1); true},
}
)],
[Right, NONE, "arranger_cursor_right", "move cursor right", |app: &mut App| Ok(
match app.arranger_mode {
false => {app.track_cursor = app.tracks.len().min(app.track_cursor + 1); true},
true => {app.scene_cursor = app.scenes.len().min(app.scene_cursor + 1); true},
}
)],
[Enter, NONE, "arranger_activate", "activate item at cursor", |app: &mut App| Ok(
if app.scene_cursor == 0 {
false
} else {
let scene = &app.scenes[app.scene_cursor - 1];
if app.track_cursor == 0 {
for (i, track) in app.tracks.iter_mut().enumerate() {
track.sequence = scene.clips[i];
track.reset = true;
}
} else {
let track = &mut app.tracks[app.track_cursor - 1];
track.sequence = scene.clips[app.track_cursor - 1];
track.reset = true;
};
true
}
)],
[Char('.'), NONE, "arranger_increment", "set next clip at cursor", |app: &mut App| {
//if state.cursor.0 >= 1 && state.cursor.1 >= 1 {
//let scene_id = state.cursor.1 - 1;
//let clip_id = state.cursor.0 - 1;
//let scene = &mut state.scenes[scene_id];
//scene.clips[clip_id] = match scene.clips[clip_id] {
//None => Some(0),
//Some(i) => if i >= state.tracks[clip_id].sequencer.phrases.len().saturating_sub(1) {
//None
//} else {
//Some(i + 1)
//}
//};
//}
Ok(true)
}],
[Char(','), NONE, "arranger_decrement", "set previous clip at cursor", |app: &mut App| {
//if state.cursor.0 >= 1 && state.cursor.1 >= 1 {
//let scene_id = state.cursor.1 - 1;
//let clip_id = state.cursor.0 - 1;
//let scene = &mut state.scenes[scene_id];
//scene.clips[clip_id] = match scene.clips[clip_id] {
//None => Some(state.tracks[clip_id].sequencer.phrases.len().saturating_sub(1)),
//Some(i) => if i == 0 {
//None
//} else {
//Some(i - 1)
//}
//};
//}
Ok(true)
}],
[Char('`'), NONE, "arranger_mode_switch", "switch the display mode", |app: &mut App| {
app.arranger_mode = !app.seq_mode;
Ok(true)
}],
});
const KEYMAP_SEQUENCER: &'static [KeyBinding<App>] = keymap!(App {
[Up, NONE, "seq_cursor_up", "move cursor up", |app: &mut App| {
app.note_cursor = app.note_cursor.saturating_sub(1);
Ok(true)
}],
[Down, NONE, "seq_cursor_down", "move cursor up", |app: &mut App| {
app.note_cursor = app.note_cursor + 1;
Ok(true)
}],
[Left, NONE, "seq_cursor_left", "move cursor up", |app: &mut App| {
if app.entered {
app.time_cursor = app.time_cursor.saturating_sub(1);
} else {
app.time_start = app.time_start.saturating_sub(1);
}
Ok(true)
}],
[Right, NONE, "seq_cursor_right", "move cursor up", |app: &mut App| {
if app.entered {
app.time_cursor = app.time_cursor + 1;
} else {
app.time_start = app.time_start + 1;
}
Ok(true)
}],
[Char('`'), NONE, "seq_mode_switch", "switch the display mode", |app: &mut App| {
app.seq_mode = !app.seq_mode;
Ok(true)
}],
// [Char('a'), NONE, "note_add", "Add note", note_add],
// [Char('z'), NONE, "note_del", "Delete note", note_del],
// [CapsLock, NONE, "advance", "Toggle auto advance", nop],
// [Char('w'), NONE, "rest", "Advance by note duration", nop],
});
const KEYMAP_CHAIN: &'static [KeyBinding<App>] = keymap!(App {
[Up, NONE, "chain_cursor_up", "move cursor up", |_: &mut App| {
Ok(true)
}],
[Down, NONE, "chain_cursor_down", "move cursor down", |_: &mut App| {
Ok(true)
}],
[Left, NONE, "chain_cursor_left", "move cursor left", |app: &mut App| {
if let Some((_, track)) = app.track_mut() {
track.device = track.device.saturating_sub(1);
Ok(true)
} else {
Ok(false)
}
}],
[Right, NONE, "chain_cursor_right", "move cursor right", |app: &mut App| {
if let Some((_, track)) = app.track_mut() {
track.device = (track.device + 1).min(track.devices.len().saturating_sub(1));
Ok(true)
} else {
Ok(false)
}
}],
[Char('`'), NONE, "chain_mode_switch", "switch the display mode", |app: &mut App| {
app.chain_mode = !app.seq_mode;
Ok(true)
}],
});
fn focus_next (app: &mut App) -> Usually<bool> {
app.section.next();
Ok(true)