what is up with the Focus trait again

This commit is contained in:
🪞👃🪞 2024-09-05 23:37:25 +03:00
parent e7f2284e5e
commit deaa66d6e2

View file

@ -7,9 +7,9 @@ pub fn main () -> Usually<()> {
Ok(()) Ok(())
} }
struct ArrangerStandalone { struct ArrangerStandalone<E: Engine> {
/// Contains all the sequencers. /// Contains all the sequencers.
arranger: Arranger, arranger: Arranger<E>,
/// Controls the JACK transport. /// Controls the JACK transport.
transport: Option<TransportToolbar>, transport: Option<TransportToolbar>,
/// This allows the sequencer view to be moved or hidden. /// This allows the sequencer view to be moved or hidden.
@ -18,8 +18,6 @@ struct ArrangerStandalone {
focus: usize, focus: usize,
} }
focus!(ArrangerStandalone (focus) : 2 => [ transport, arranger ]);
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
pub struct ArrangerCli { pub struct ArrangerCli {
@ -40,7 +38,7 @@ pub struct ArrangerCli {
scenes: usize, scenes: usize,
} }
impl ArrangerStandalone { impl<E: Engine> ArrangerStandalone<E> {
pub fn from_args () -> Usually<Self> { pub fn from_args () -> Usually<Self> {
let args = ArrangerCli::parse(); let args = ArrangerCli::parse();
let mut arranger = Arranger::new(""); let mut arranger = Arranger::new("");
@ -75,7 +73,7 @@ impl ArrangerStandalone {
} }
} }
impl Render<Tui> for ArrangerStandalone { impl Render<Tui> for ArrangerStandalone<Tui> {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> { fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
let area = to.area(); let area = to.area();
let sequencer = self.arranger.sequencer(); let sequencer = self.arranger.sequencer();
@ -94,55 +92,73 @@ impl Render<Tui> for ArrangerStandalone {
} }
} }
impl Handle<Tui> for ArrangerStandalone { impl Handle<Tui> for ArrangerStandalone<Tui> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> { fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
match e { match from.event() {
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Char(' '), .. })) => { key!(KeyCode::Char(' ')) => {
if let Some(ref mut transport) = self.transport { if let Some(ref mut transport) = self.transport {
transport.toggle_play()?; transport.toggle_play()?;
Ok(true) Ok(Some(true))
} else { } else {
Ok(false) Ok(None)
} }
}, },
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Tab, .. })) => { key!(KeyCode::Tab) => {
self.focus_next(); self.arranger.focus_next();
Ok(true) Ok(Some(true))
}, },
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::BackTab, .. })) => { key!(KeyCode::BackTab) => {
self.focus_prev(); self.arranger.focus_prev();
Ok(true) Ok(Some(true))
}, },
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Down, .. })) => { key!(KeyCode::Down) => {
if self.focus == 0 || ( if self.focus == 0 || (
self.focus == 1 && self.arranger.is_last_row() self.focus == 1 && self.arranger.is_last_row()
) { ) {
self.focus_next(); self.arranger.focus_next();
Ok(true) Ok(Some(true))
} else { } else {
self.focused_mut().handle(e) self.arranger.focused_mut().handle(from)
} }
}, },
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Up, .. })) => { key!(KeyCode::Up) => {
if self.focus == 1 && self.arranger.is_first_row() { if self.focus == 1 && self.arranger.is_first_row() {
self.focus_prev(); self.arranger.focus_prev();
Ok(true) Ok(Some(true))
} else { } else {
self.focused_mut().handle(e) self.arranger.focused_mut().handle(from)
} }
}, },
_ => self.focused_mut().handle(e) _ => self.arranger.focused_mut().handle(from)
} }
} }
} }
struct SequencerProxy(Arc<RwLock<Arranger>>); struct SequencerProxy<E: Engine>(Arc<RwLock<Arranger<E>>>);
impl Handle for SequencerProxy { impl Handle<Tui> for SequencerProxy<Tui> {
fn handle (&mut self, e: &AppEvent) -> Usually<bool> { fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
match self.0.write().unwrap().sequencer_mut() { self.0.write().unwrap().handle(from)
Some(sequencer) => sequencer.handle(e), }
None => Ok(false) }
}
impl Focus<2, E: Engine> for ArrangerStandalone<E> {
fn focus (&self) -> usize {
self.focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.focus
}
fn focusable (&self) -> [&dyn Focusable<E>;2] {
[
&self.transport as &dyn Focusable<E>,
&self.arranger as &dyn Focusable<E>,
]
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<E>;2] {
[
&mut self.transport as &mut dyn Focusable<E>,
&mut self.arranger as &mut dyn Focusable<E>,
]
} }
} }