wip: directional focus!

This commit is contained in:
🪞👃🪞 2024-10-09 14:49:00 +03:00
parent ec3eb40bb4
commit 225eda0d24
5 changed files with 118 additions and 59 deletions

View file

@ -2,6 +2,8 @@ use crate::*;
/// Root level object for standalone `tek_arranger`
pub struct Arranger<E: Engine> {
/// Index of currently focused component
pub focus: ArrangerFocus,
/// Controls the JACK transport.
pub transport: Option<Arc<RwLock<TransportToolbar<E>>>>,
/// Contains all the sequencers.
@ -12,10 +14,25 @@ pub struct Arranger<E: Engine> {
pub editor: PhraseEditor<E>,
/// This allows the sequencer view to be moved or hidden.
pub show_sequencer: Option<tek_core::Direction>,
/// Index of currently focused component
pub focus: usize,
/// Slot for modal dialog displayed on top of app.
pub modal: Option<Box<dyn ContentComponent<E>>>,
/// Focus cursor
pub focus_cursor: (usize, usize)
}
/// Sections in the arranger that may be focused
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum ArrangerFocus { Transport, Arrangement, PhrasePool, PhraseEditor }
/// Focus layout of arranger.
impl<E: Engine> FocusGrid<ArrangerFocus> for Arranger<E> {
fn cursor (&self) -> (usize, usize) { self.focus_cursor }
fn cursor_mut (&mut self) -> &mut (usize, usize) { &mut self.focus_cursor }
fn layout (&self) -> &[&[ArrangerFocus]] {
&[
&[ArrangerFocus::Transport],
&[ArrangerFocus::Arrangement],
&[ArrangerFocus::PhrasePool, ArrangerFocus::PhraseEditor],
]
}
}
/// Represents the tracks and scenes of the composition.
pub struct Arrangement<E: Engine> {

View file

@ -58,9 +58,10 @@ impl ArrangerCli {
)?
);
Tui::run(Arc::new(RwLock::new(Arranger {
focus_cursor: (0, 1),
transport: self.transport.then_some(transport),
show_sequencer: Some(tek_core::Direction::Down),
focus: 0,
focus: ArrangerFocus::Arrangement,
modal: None,
editor: PhraseEditor::new(),
arrangement,

View file

@ -25,66 +25,34 @@ impl Content for Arranger<Tui> {
})
}
}
/// Focusable items in standalone arranger.
impl Focus<3, Tui> for Arranger<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arrangement, self.editor)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arrangement, self.editor)
}
}
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for Arranger<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
if let Some(modal) = self.modal.as_mut() {
let result = modal.handle(from)?;
if from.is_done() {
self.modal = None;
if !match self.focused() {
ArrangerFocus::Transport => self.transport.handle(from)?,
ArrangerFocus::Arrangement => self.arrangement.handle(from)?,
ArrangerFocus::PhrasePool => self.phrases.handle(from)?,
ArrangerFocus::PhraseEditor => self.editor.handle(from)?
}.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(); },
// 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(); },
// Global play/pause binding
key!(KeyCode::Char(' ')) => match self.transport {
Some(ref mut transport) => { transport.write().unwrap().toggle_play()?; },
None => { return Ok(None) }
},
_ => {}
}
return Ok(result)
}
let focus = self.focus;
let is_first_row = self.arrangement.is_first_row();
let is_last_row = self.arrangement.is_last_row();
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {
transport.write().unwrap().toggle_play()?;
} else {
return Ok(None)
}
},
key!(KeyCode::Tab) => {
self.focus_next();
},
key!(KeyCode::BackTab) => {
self.focus_prev();
},
key!(KeyCode::Down) => {
if focus == 0 {
self.focus_next();
} else if focus == 1 && is_last_row {
self.focus_next();
} else {
return self.focused_mut().handle(from)
}
},
key!(KeyCode::Up) => {
if focus == 1 && is_first_row {
self.focus_prev();
} else {
return self.focused_mut().handle(from)
}
},
_ => return self.focused_mut().handle(from)
}
};
Ok(Some(true))
}
}