diff --git a/crates/tek_core/src/focus.rs b/crates/tek_core/src/focus.rs index bcc93170..c52f6a25 100644 --- a/crates/tek_core/src/focus.rs +++ b/crates/tek_core/src/focus.rs @@ -1,6 +1,6 @@ use crate::*; -pub trait FocusGrid { +pub trait FocusGrid { fn layout (&self) -> &[&[T]]; fn cursor (&self) -> (usize, usize); fn cursor_mut (&mut self) -> &mut (usize, usize); @@ -39,6 +39,7 @@ pub trait FocusGrid { *self.cursor_mut() = (next_x, y); } fn focus_next (&mut self) { + let current = *self.focused(); let (x, y) = self.cursor(); if x < self.layout()[y].len().saturating_sub(1) { self.focus_right(); @@ -46,8 +47,12 @@ pub trait FocusGrid { self.focus_down(); self.cursor_mut().0 = 0; } + if *self.focused() == current { // FIXME: prevent infinite loop + self.focus_next() + } } fn focus_prev (&mut self) { + let current = *self.focused(); let (x, _) = self.cursor(); if x > 0 { self.focus_left(); @@ -57,6 +62,9 @@ pub trait FocusGrid { let next_x = self.layout()[y].len().saturating_sub(1); self.cursor_mut().0 = next_x; } + if *self.focused() == current { // FIXME: prevent infinite loop + self.focus_prev() + } } } diff --git a/crates/tek_sequencer/src/arranger_tui.rs b/crates/tek_sequencer/src/arranger_tui.rs index 84aa1257..8431acad 100644 --- a/crates/tek_sequencer/src/arranger_tui.rs +++ b/crates/tek_sequencer/src/arranger_tui.rs @@ -28,6 +28,15 @@ impl Content for Arranger { /// Handle top-level events in standalone arranger. impl Handle for Arranger { fn handle (&mut self, from: &TuiInput) -> Perhaps { + let update_focus = |arranger: &mut Self| { + let focused = *arranger.focused(); + arranger.transport.as_ref().map(|transport|{ + transport.write().unwrap().focused = focused == ArrangerFocus::Transport + }); + arranger.arrangement.focused = focused == ArrangerFocus::Arrangement; + arranger.phrases.write().unwrap().focused = focused == ArrangerFocus::PhrasePool; + arranger.editor.focused = focused == ArrangerFocus::PhraseEditor; + }; if !match self.focused() { ArrangerFocus::Transport => self.transport.handle(from)?, ArrangerFocus::Arrangement => self.arrangement.handle(from)?, @@ -36,15 +45,15 @@ impl Handle for Arranger { }.unwrap_or(false) { match from.event() { // Tab navigation - key!(KeyCode::Tab) => { self.focus_next(); }, - key!(Shift-KeyCode::Tab) => { self.focus_prev(); }, - key!(KeyCode::BackTab) => { self.focus_prev(); }, - key!(Shift-KeyCode::BackTab) => { self.focus_prev(); }, + key!(KeyCode::Tab) => { self.focus_next(); update_focus(self); }, + key!(Shift-KeyCode::Tab) => { self.focus_prev(); update_focus(self); }, + key!(KeyCode::BackTab) => { self.focus_prev(); update_focus(self); }, + key!(Shift-KeyCode::BackTab) => { self.focus_prev(); update_focus(self); }, // Directional navigation - key!(KeyCode::Up) => { self.focus_up(); }, - key!(KeyCode::Down) => { self.focus_down(); }, - key!(KeyCode::Left) => { self.focus_left(); }, - key!(KeyCode::Right) => { self.focus_right(); }, + key!(KeyCode::Up) => { self.focus_up(); update_focus(self); }, + key!(KeyCode::Down) => { self.focus_down(); update_focus(self); }, + key!(KeyCode::Left) => { self.focus_left(); update_focus(self); }, + key!(KeyCode::Right) => { self.focus_right(); update_focus(self); }, // Global play/pause binding key!(KeyCode::Char(' ')) => match self.transport { Some(ref mut transport) => { transport.write().unwrap().toggle_play()?; }, @@ -562,19 +571,13 @@ impl Handle for ArrangerRenameModal { match from.event() { TuiEvent::Input(Event::Key(k)) => { match k.code { - KeyCode::Esc => { - self.exit(); - }, + KeyCode::Esc => { self.exit(); }, KeyCode::Enter => { *self.result.write().unwrap() = self.value.clone(); self.exit(); }, - KeyCode::Left => { - self.cursor = self.cursor.saturating_sub(1); - }, - KeyCode::Right => { - self.cursor = self.value.len().min(self.cursor + 1) - }, + KeyCode::Left => { self.cursor = self.cursor.saturating_sub(1); }, + KeyCode::Right => { self.cursor = self.value.len().min(self.cursor + 1) }, KeyCode::Backspace => { let last = self.value.len().saturating_sub(1); self.value = format!("{}{}", diff --git a/crates/tek_sequencer/src/transport_tui.rs b/crates/tek_sequencer/src/transport_tui.rs index 096b5092..03b7e5f4 100644 --- a/crates/tek_sequencer/src/transport_tui.rs +++ b/crates/tek_sequencer/src/transport_tui.rs @@ -35,7 +35,10 @@ impl Handle for TransportToolbar { key!(KeyCode::Left) => { self.focus.prev(); }, key!(KeyCode::Right) => { self.focus.next(); }, _ => match self.focus { - TransportToolbarFocus::PlayPause => self.toggle_play().map(|_|())?, + TransportToolbarFocus::PlayPause => match from.event() { + key!(KeyCode::Enter) => self.toggle_play().map(|_|())?, + _ => return Ok(None) + }, TransportToolbarFocus::Bpm => self.handle_bpm(from).map(|_|())?, TransportToolbarFocus::Quant => self.handle_quant(from).map(|_|())?, TransportToolbarFocus::Sync => self.handle_sync(from).map(|_|())?,