Refactoring

This commit is contained in:
2026-01-20 03:08:37 +01:00
parent a81716cbd5
commit 06ec2ae70f
13 changed files with 678 additions and 263 deletions

View File

@@ -5,12 +5,12 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use crate::app::App;
use crate::commands::AppCommand;
use crate::link::LinkState;
use crate::model::PatternSpeed;
use crate::page::Page;
use crate::sequencer::{AudioCommand, SequencerSnapshot};
use crate::state::{AudioFocus, Focus, Modal, PatternField, PatternsViewLevel};
use crate::views::doc_view;
pub enum InputResult {
Continue,
@@ -25,10 +25,16 @@ pub struct InputContext<'a> {
pub audio_tx: &'a Sender<AudioCommand>,
}
pub fn handle_key(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
ctx.app.clear_status();
impl<'a> InputContext<'a> {
fn dispatch(&mut self, cmd: AppCommand) {
self.app.dispatch(cmd, self.link, self.snapshot);
}
}
if matches!(ctx.app.modal, Modal::None) {
pub fn handle_key(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
ctx.dispatch(AppCommand::ClearStatus);
if matches!(ctx.app.ui.modal, Modal::None) {
handle_normal_input(ctx, key)
} else {
handle_modal_input(ctx, key)
@@ -36,11 +42,11 @@ pub fn handle_key(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
}
fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
match &mut ctx.app.modal {
match &mut ctx.app.ui.modal {
Modal::ConfirmQuit { selected } => match key.code {
KeyCode::Char('y') | KeyCode::Char('Y') => return InputResult::Quit,
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::CloseModal);
}
KeyCode::Left | KeyCode::Right => {
*selected = !*selected;
@@ -49,7 +55,7 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
if *selected {
return InputResult::Quit;
} else {
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::CloseModal);
}
}
_ => {}
@@ -57,10 +63,10 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
Modal::SaveAs(path) => match key.code {
KeyCode::Enter => {
let save_path = PathBuf::from(path.as_str());
ctx.app.modal = Modal::None;
ctx.app.save(save_path);
ctx.dispatch(AppCommand::CloseModal);
ctx.dispatch(AppCommand::Save(save_path));
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
path.pop();
}
@@ -70,10 +76,10 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
Modal::LoadFrom(path) => match key.code {
KeyCode::Enter => {
let load_path = PathBuf::from(path.as_str());
ctx.app.modal = Modal::None;
ctx.app.load(load_path, ctx.link);
ctx.dispatch(AppCommand::CloseModal);
ctx.dispatch(AppCommand::Load(load_path));
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
path.pop();
}
@@ -88,10 +94,13 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
} else {
Some(name.clone())
};
ctx.app.project.banks[bank_idx].name = new_name;
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::RenameBank {
bank: bank_idx,
name: new_name,
});
ctx.dispatch(AppCommand::CloseModal);
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
name.pop();
}
@@ -110,10 +119,14 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
} else {
Some(name.clone())
};
ctx.app.project.banks[bank_idx].patterns[pattern_idx].name = new_name;
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::RenamePattern {
bank: bank_idx,
pattern: pattern_idx,
name: new_name,
});
ctx.dispatch(AppCommand::CloseModal);
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
name.pop();
}
@@ -127,36 +140,43 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
match field {
PatternField::Length => {
if let Ok(len) = input.parse::<usize>() {
ctx.app
ctx.dispatch(AppCommand::SetLength {
bank,
pattern,
length: len,
});
let new_len = ctx
.app
.project_state
.project
.pattern_at_mut(bank, pattern)
.set_length(len);
let new_len = ctx.app.project.pattern_at(bank, pattern).length;
if ctx.app.editor_ctx.step >= new_len {
ctx.app.editor_ctx.step = new_len - 1;
}
ctx.app.dirty_patterns.insert((bank, pattern));
ctx.app.status_message = Some(format!("Length set to {new_len}"));
.pattern_at(bank, pattern)
.length;
ctx.dispatch(AppCommand::SetStatus(format!("Length set to {new_len}")));
} else {
ctx.app.status_message = Some("Invalid length".to_string());
ctx.dispatch(AppCommand::SetStatus("Invalid length".to_string()));
}
}
PatternField::Speed => {
if let Some(speed) = PatternSpeed::from_label(input) {
ctx.app.project.pattern_at_mut(bank, pattern).speed = speed;
ctx.app.dirty_patterns.insert((bank, pattern));
ctx.app.status_message =
Some(format!("Speed set to {}", speed.label()));
ctx.dispatch(AppCommand::SetSpeed {
bank,
pattern,
speed,
});
ctx.dispatch(AppCommand::SetStatus(format!(
"Speed set to {}",
speed.label()
)));
} else {
ctx.app.status_message = Some(
ctx.dispatch(AppCommand::SetStatus(
"Invalid speed (try 1/8x, 1/4x, 1/2x, 1x, 2x, 4x, 8x)".to_string(),
);
));
}
}
}
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::CloseModal);
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
input.pop();
}
@@ -172,13 +192,13 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
let _ = ctx.audio_tx.send(AudioCommand::LoadSamples(index));
ctx.app.audio.config.sample_count += count;
ctx.app.audio.add_sample_path(sample_path);
ctx.app.status_message = Some(format!("Added {count} samples"));
ctx.dispatch(AppCommand::SetStatus(format!("Added {count} samples")));
} else {
ctx.app.status_message = Some("Path is not a directory".to_string());
ctx.dispatch(AppCommand::SetStatus("Path is not a directory".to_string()));
}
ctx.app.modal = Modal::None;
ctx.dispatch(AppCommand::CloseModal);
}
KeyCode::Esc => ctx.app.modal = Modal::None,
KeyCode::Esc => ctx.dispatch(AppCommand::CloseModal),
KeyCode::Backspace => {
path.pop();
}
@@ -193,23 +213,22 @@ fn handle_modal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
fn handle_normal_input(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
// Global navigation with Ctrl+arrows
if ctrl {
match key.code {
KeyCode::Left => {
ctx.app.page.left();
ctx.dispatch(AppCommand::PageLeft);
return InputResult::Continue;
}
KeyCode::Right => {
ctx.app.page.right();
ctx.dispatch(AppCommand::PageRight);
return InputResult::Continue;
}
KeyCode::Up => {
ctx.app.page.up();
ctx.dispatch(AppCommand::PageUp);
return InputResult::Continue;
}
KeyCode::Down => {
ctx.app.page.down();
ctx.dispatch(AppCommand::PageDown);
return InputResult::Continue;
}
_ => {}
@@ -228,47 +247,51 @@ fn handle_main_page(ctx: &mut InputContext, key: KeyEvent, ctrl: bool) -> InputR
match ctx.app.editor_ctx.focus {
Focus::Sequencer => match key.code {
KeyCode::Char('q') => {
ctx.app.modal = Modal::ConfirmQuit { selected: false };
ctx.dispatch(AppCommand::OpenModal(Modal::ConfirmQuit {
selected: false,
}));
}
KeyCode::Char(' ') => {
ctx.app.toggle_playing();
ctx.playing.store(ctx.app.playing, Ordering::Relaxed);
ctx.dispatch(AppCommand::TogglePlaying);
ctx.playing
.store(ctx.app.playback.playing, Ordering::Relaxed);
}
KeyCode::Tab => ctx.app.toggle_focus(ctx.link),
KeyCode::Left => ctx.app.prev_step(),
KeyCode::Right => ctx.app.next_step(),
KeyCode::Up => ctx.app.step_up(),
KeyCode::Down => ctx.app.step_down(),
KeyCode::Enter => ctx.app.toggle_step(),
KeyCode::Tab => ctx.dispatch(AppCommand::ToggleFocus),
KeyCode::Left => ctx.dispatch(AppCommand::PrevStep),
KeyCode::Right => ctx.dispatch(AppCommand::NextStep),
KeyCode::Up => ctx.dispatch(AppCommand::StepUp),
KeyCode::Down => ctx.dispatch(AppCommand::StepDown),
KeyCode::Enter => ctx.dispatch(AppCommand::ToggleStep),
KeyCode::Char('s') => {
let default = ctx
.app
.project_state
.file_path
.as_ref()
.map(|p| p.display().to_string())
.unwrap_or_else(|| "project.buboseq".to_string());
ctx.app.modal = Modal::SaveAs(default);
ctx.dispatch(AppCommand::OpenModal(Modal::SaveAs(default)));
}
KeyCode::Char('l') => {
ctx.app.modal = Modal::LoadFrom(String::new());
ctx.dispatch(AppCommand::OpenModal(Modal::LoadFrom(String::new())));
}
KeyCode::Char('+') | KeyCode::Char('=') => ctx.app.tempo_up(ctx.link),
KeyCode::Char('-') => ctx.app.tempo_down(ctx.link),
KeyCode::Char('<') | KeyCode::Char(',') => ctx.app.length_decrease(),
KeyCode::Char('>') | KeyCode::Char('.') => ctx.app.length_increase(),
KeyCode::Char('[') => ctx.app.speed_decrease(),
KeyCode::Char(']') => ctx.app.speed_increase(),
KeyCode::Char('L') => ctx.app.open_pattern_modal(PatternField::Length),
KeyCode::Char('S') => ctx.app.open_pattern_modal(PatternField::Speed),
KeyCode::Char('c') if ctrl => ctx.app.copy_step(),
KeyCode::Char('v') if ctrl => ctx.app.paste_step(ctx.link),
KeyCode::Char('+') | KeyCode::Char('=') => ctx.dispatch(AppCommand::TempoUp),
KeyCode::Char('-') => ctx.dispatch(AppCommand::TempoDown),
KeyCode::Char('<') | KeyCode::Char(',') => ctx.dispatch(AppCommand::LengthDecrease),
KeyCode::Char('>') | KeyCode::Char('.') => ctx.dispatch(AppCommand::LengthIncrease),
KeyCode::Char('[') => ctx.dispatch(AppCommand::SpeedDecrease),
KeyCode::Char(']') => ctx.dispatch(AppCommand::SpeedIncrease),
KeyCode::Char('L') => ctx.dispatch(AppCommand::OpenPatternModal(PatternField::Length)),
KeyCode::Char('S') => ctx.dispatch(AppCommand::OpenPatternModal(PatternField::Speed)),
KeyCode::Char('c') if ctrl => ctx.dispatch(AppCommand::CopyStep),
KeyCode::Char('v') if ctrl => ctx.dispatch(AppCommand::PasteStep),
_ => {}
},
Focus::Editor => match key.code {
KeyCode::Tab | KeyCode::Esc => ctx.app.toggle_focus(ctx.link),
KeyCode::Tab | KeyCode::Esc => ctx.dispatch(AppCommand::ToggleFocus),
KeyCode::Char('e') if ctrl => {
ctx.app.save_editor_to_step();
ctx.app.compile_current_step(ctx.link);
ctx.dispatch(AppCommand::SaveEditorToStep);
ctx.dispatch(AppCommand::CompileCurrentStep);
}
_ => {
ctx.app.editor_ctx.text.input(Event::Key(key));
@@ -280,61 +303,46 @@ fn handle_main_page(ctx: &mut InputContext, key: KeyEvent, ctrl: bool) -> InputR
fn handle_patterns_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
match key.code {
KeyCode::Left => ctx.app.patterns_cursor = (ctx.app.patterns_cursor + 15) % 16,
KeyCode::Right => ctx.app.patterns_cursor = (ctx.app.patterns_cursor + 1) % 16,
KeyCode::Up => ctx.app.patterns_cursor = (ctx.app.patterns_cursor + 12) % 16,
KeyCode::Down => ctx.app.patterns_cursor = (ctx.app.patterns_cursor + 4) % 16,
KeyCode::Esc | KeyCode::Backspace => match ctx.app.patterns_view_level {
PatternsViewLevel::Banks => ctx.app.page.down(),
PatternsViewLevel::Patterns { .. } => {
ctx.app.patterns_view_level = PatternsViewLevel::Banks;
ctx.app.patterns_cursor = 0;
}
},
KeyCode::Enter => match ctx.app.patterns_view_level {
PatternsViewLevel::Banks => {
let bank = ctx.app.patterns_cursor;
ctx.app.patterns_view_level = PatternsViewLevel::Patterns { bank };
ctx.app.patterns_cursor = 0;
}
PatternsViewLevel::Patterns { bank } => {
let pattern = ctx.app.patterns_cursor;
ctx.app.select_edit_bank(bank);
ctx.app.select_edit_pattern(pattern);
ctx.app.patterns_view_level = PatternsViewLevel::Banks;
ctx.app.patterns_cursor = 0;
ctx.app.page.down();
}
},
KeyCode::Left => ctx.dispatch(AppCommand::PatternsCursorLeft),
KeyCode::Right => ctx.dispatch(AppCommand::PatternsCursorRight),
KeyCode::Up => ctx.dispatch(AppCommand::PatternsCursorUp),
KeyCode::Down => ctx.dispatch(AppCommand::PatternsCursorDown),
KeyCode::Esc | KeyCode::Backspace => ctx.dispatch(AppCommand::PatternsBack),
KeyCode::Enter => ctx.dispatch(AppCommand::PatternsEnter),
KeyCode::Char(' ') => {
if let PatternsViewLevel::Patterns { bank } = ctx.app.patterns_view_level {
let pattern = ctx.app.patterns_cursor;
ctx.app.toggle_pattern_playback(bank, pattern, ctx.snapshot);
ctx.dispatch(AppCommand::TogglePatternPlayback { bank, pattern });
}
}
KeyCode::Char('q') => {
ctx.app.modal = Modal::ConfirmQuit { selected: false };
ctx.dispatch(AppCommand::OpenModal(Modal::ConfirmQuit {
selected: false,
}));
}
KeyCode::Char('r') => match ctx.app.patterns_view_level {
PatternsViewLevel::Banks => {
let bank = ctx.app.patterns_cursor;
let current_name = ctx.app.project.banks[bank].name.clone().unwrap_or_default();
ctx.app.modal = Modal::RenameBank {
bank,
name: current_name,
};
}
PatternsViewLevel::Patterns { bank } => {
let pattern = ctx.app.patterns_cursor;
let current_name = ctx.app.project.banks[bank].patterns[pattern]
let current_name = ctx.app.project_state.project.banks[bank]
.name
.clone()
.unwrap_or_default();
ctx.app.modal = Modal::RenamePattern {
ctx.dispatch(AppCommand::OpenModal(Modal::RenameBank {
bank,
name: current_name,
}));
}
PatternsViewLevel::Patterns { bank } => {
let pattern = ctx.app.patterns_cursor;
let current_name = ctx.app.project_state.project.banks[bank].patterns[pattern]
.name
.clone()
.unwrap_or_default();
ctx.dispatch(AppCommand::OpenModal(Modal::RenamePattern {
bank,
pattern,
name: current_name,
};
}));
}
},
_ => {}
@@ -345,7 +353,9 @@ fn handle_patterns_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
fn handle_audio_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
match key.code {
KeyCode::Char('q') => {
ctx.app.modal = Modal::ConfirmQuit { selected: false };
ctx.dispatch(AppCommand::OpenModal(Modal::ConfirmQuit {
selected: false,
}));
}
KeyCode::Up | KeyCode::Char('k') => ctx.app.audio.prev_focus(),
KeyCode::Down | KeyCode::Char('j') => ctx.app.audio.next_focus(),
@@ -364,14 +374,16 @@ fn handle_audio_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
AudioFocus::SamplePaths => {}
},
KeyCode::Char('R') => ctx.app.audio.trigger_restart(),
KeyCode::Char('A') => ctx.app.modal = Modal::AddSamplePath(String::new()),
KeyCode::Char('A') => {
ctx.dispatch(AppCommand::OpenModal(Modal::AddSamplePath(String::new())));
}
KeyCode::Char('D') => {
ctx.app.audio.refresh_devices();
let out_count = ctx.app.audio.output_devices.len();
let in_count = ctx.app.audio.input_devices.len();
ctx.app.status_message = Some(format!(
ctx.dispatch(AppCommand::SetStatus(format!(
"Found {out_count} output, {in_count} input devices"
));
)));
}
KeyCode::Char('h') => {
let _ = ctx.audio_tx.send(AudioCommand::Hush);
@@ -386,8 +398,9 @@ fn handle_audio_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
.send(AudioCommand::Evaluate("sin 440 * 0.3".into()));
}
KeyCode::Char(' ') => {
ctx.app.toggle_playing();
ctx.playing.store(ctx.app.playing, Ordering::Relaxed);
ctx.dispatch(AppCommand::TogglePlaying);
ctx.playing
.store(ctx.app.playback.playing, Ordering::Relaxed);
}
_ => {}
}
@@ -395,20 +408,15 @@ fn handle_audio_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
}
fn handle_doc_page(ctx: &mut InputContext, key: KeyEvent) -> InputResult {
let topic_count = doc_view::topic_count();
match key.code {
KeyCode::Char('j') | KeyCode::Down => {
ctx.app.doc_topic = (ctx.app.doc_topic + 1) % topic_count;
ctx.app.doc_scroll = 0;
}
KeyCode::Char('k') | KeyCode::Up => {
ctx.app.doc_topic = (ctx.app.doc_topic + topic_count - 1) % topic_count;
ctx.app.doc_scroll = 0;
}
KeyCode::PageDown => ctx.app.doc_scroll = ctx.app.doc_scroll.saturating_add(10),
KeyCode::PageUp => ctx.app.doc_scroll = ctx.app.doc_scroll.saturating_sub(10),
KeyCode::Char('j') | KeyCode::Down => ctx.dispatch(AppCommand::DocNextTopic),
KeyCode::Char('k') | KeyCode::Up => ctx.dispatch(AppCommand::DocPrevTopic),
KeyCode::PageDown => ctx.dispatch(AppCommand::DocScrollDown(10)),
KeyCode::PageUp => ctx.dispatch(AppCommand::DocScrollUp(10)),
KeyCode::Char('q') => {
ctx.app.modal = Modal::ConfirmQuit { selected: false };
ctx.dispatch(AppCommand::OpenModal(Modal::ConfirmQuit {
selected: false,
}));
}
_ => {}
}