add focus proxy for sequencers

This commit is contained in:
🪞👃🪞 2024-10-03 15:52:21 +03:00
parent 47cdf5869b
commit 4be2df1347
2 changed files with 71 additions and 17 deletions

View file

@ -48,7 +48,8 @@ impl ArrangerCli {
transport,
show_sequencer: Some(tek_core::Direction::Down),
arranger,
focus: 0
focus: 0,
sequencer_proxy: SequencerProxy(Default::default(), false)
})))?;
Ok(())
}
@ -56,13 +57,31 @@ impl ArrangerCli {
/// Root level object for standalone `tek_arranger`
struct ArrangerStandalone<E: Engine> {
/// Controls the JACK transport.
transport: Option<TransportToolbar<E>>,
transport: Option<TransportToolbar<E>>,
/// Contains all the sequencers.
arranger: Arranger<E>,
arranger: Arranger<E>,
/// This allows the sequencer view to be moved or hidden.
show_sequencer: Option<tek_core::Direction>,
///
focus: usize,
show_sequencer: Option<tek_core::Direction>,
/// Index of currently focused component
focus: usize,
/// Focus target that passes events down to sequencer
sequencer_proxy: SequencerProxy<E>,
}
impl<E: Engine> ArrangerStandalone<E> {
fn sequencer (&self) -> Option<&Sequencer<E>> {
if self.show_sequencer.is_some() {
self.arranger.sequencer()
} else {
None
}
}
fn sequencer_mut (&mut self) -> Option<&mut Sequencer<E>> {
if self.show_sequencer.is_some() {
self.arranger.sequencer_mut()
} else {
None
}
}
}
/// The standalone arranger consists of transport, clip grid, and sequencer.
impl Content for ArrangerStandalone<Tui> {
@ -94,6 +113,16 @@ impl Content for ArrangerStandalone<Tui> {
/// Handle top-level events in standalone arranger.
impl Handle<Tui> for ArrangerStandalone<Tui> {
fn handle (&mut self, from: &TuiInput) -> Perhaps<bool> {
let focus = self.focus;
let is_first_row = self.arranger.is_first_row();
let is_last_row = self.arranger.is_last_row();
let mut focused_handle = || {
if focus == 2 {
self.arranger.sequencer_mut().handle(from)
} else {
self.focused_mut().handle(from)
}
};
match from.event() {
key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport {
@ -109,38 +138,55 @@ impl Handle<Tui> for ArrangerStandalone<Tui> {
self.focus_prev();
},
key!(KeyCode::Down) => {
if self.focus == 0 {
if focus == 0 {
self.focus_next();
} else if self.focus == 1 && self.arranger.is_last_row() {
} else if focus == 1 && is_last_row {
self.focus_next();
} else {
return self.focused_mut().handle(from)
return focused_handle()
}
},
key!(KeyCode::Up) => {
if self.focus == 1 && self.arranger.is_first_row() {
if focus == 1 && is_first_row {
self.focus_prev();
} else {
return self.focused_mut().handle(from)
return focused_handle()
}
},
_ => return self.focused_mut().handle(from)
_ => return focused_handle()
}
Ok(Some(true))
}
}
/// Focusable items in standalone arranger.
impl Focus<2, Tui> for ArrangerStandalone<Tui> {
impl Focus<3, Tui> for ArrangerStandalone<Tui> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<Tui>;2] {
focusables!(self.transport, self.arranger)
fn focusable (&self) -> [&dyn Focusable<Tui>;3] {
focusables!(self.transport, self.arranger, self.sequencer_proxy)
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;2] {
focusables_mut!(self.transport, self.arranger)
fn focusable_mut (&mut self) -> [&mut dyn Focusable<Tui>;3] {
focusables_mut!(self.transport, self.arranger, self.sequencer_proxy)
}
}
#[derive(Default)]
struct SequencerProxy<E: Engine>(PhantomData<E>, bool);
impl Handle<Tui> for SequencerProxy<Tui> {
fn handle (&mut self, _: &TuiInput) -> Perhaps<bool> { unreachable!() }
}
impl Content for SequencerProxy<Tui> {
type Engine = Tui;
fn content (&self) -> impl Widget<Engine = Tui> { "" }
}
impl Focusable<Tui> for SequencerProxy<Tui> {
fn is_focused (&self) -> bool {
self.1
}
fn set_focused (&mut self, focus: bool) {
self.1 = focus
}
}

View file

@ -885,6 +885,14 @@ impl Content for Sequencer<Tui> {
.fg(Color::Rgb(70, 80, 50))))
}
}
impl Focusable<Tui> for Sequencer<Tui> {
fn is_focused (&self) -> bool {
self.focused
}
fn set_focused (&mut self, focused: bool) {
self.focused = focused
}
}
/// Colors of piano keys
const KEY_COLORS: [(Color, Color);6] = [