wip: trying focus traits and macros again

This commit is contained in:
🪞👃🪞 2024-08-31 23:19:42 +03:00
parent 9f358f8a21
commit 2106a7c044
4 changed files with 78 additions and 35 deletions

View file

@ -0,0 +1,70 @@
use crate::*;
pub trait Focus <const N: usize>: Render + Handle {
fn focus (&self) -> usize;
fn focus_mut (&mut self) -> &mut usize;
fn focusable (&self) -> [&dyn Focusable;N];
fn focusable_mut (&mut self) -> [&mut dyn Focusable;N];
fn focused (&self) -> &dyn Focusable {
let focus = self.focus();
self.focusable()[focus]
}
fn focused_mut (&mut self) -> &mut dyn Focusable {
let focus = self.focus();
self.focusable_mut()[focus]
}
fn focus_prev (&mut self) {
let focus = self.focus();
let focus = if focus > 0 { N - 1 } else { focus - 1 };
*self.focus_mut() = focus;
}
fn focus_next (&mut self) {
let focus = self.focus();
let focus = if focus < N - 1 { focus + 1 } else { 0 };
*self.focus_mut() = focus;
}
}
pub trait Focusable: Render + Handle {
fn is_focused (&self) -> bool;
fn set_focused (&mut self, focused: bool);
}
#[macro_export] macro_rules! focus {
($struct:ident ($focus:ident) : $count:expr => [
$($focusable:ident),*
]) => {
impl Focus<$count> for $struct {
fn focus (&self) -> usize {
self.$focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.$focus
}
fn focusable (&self) -> [&dyn Focusable;$count] {
[
$(&self.$focusable as &dyn Focusable,)*
]
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable;2] {
[
$(&mut self.$focusable as &mut dyn Focusable,)*
]
}
}
}
}
#[macro_export] macro_rules! focusable {
($struct:ident ($focused:ident)) => {
impl Focusable for $struct {
fn is_focused (&self) -> bool {
self.$focused
}
fn set_focused (&mut self, focused: bool) {
self.$focused = focused
}
}
}
}

View file

@ -45,6 +45,7 @@ use crossterm::terminal::{
submod! { submod! {
exit exit
focus
handle handle
handle_keymap handle_keymap
jack_core jack_core

View file

@ -1,22 +1,20 @@
//! Clip launcher and arrangement editor. //! Clip launcher and arrangement editor.
use crate::*; use crate::*;
use tek_core::Direction;
/// Represents the tracks and scenes of the composition. /// Represents the tracks and scenes of the composition.
pub struct Arranger { pub struct Arranger {
/// Name of arranger /// Name of arranger
pub name: Arc<RwLock<String>>, pub name: Arc<RwLock<String>>,
/// Collection of tracks. /// Collection of tracks.
pub tracks: Vec<Sequencer>, pub tracks: Vec<Sequencer>,
/// Collection of scenes. /// Collection of scenes.
pub scenes: Vec<Scene>, pub scenes: Vec<Scene>,
/// Currently selected element. /// Currently selected element.
pub selected: ArrangerFocus, pub selected: ArrangerFocus,
/// Display mode of arranger /// Display mode of arranger
pub mode: ArrangerViewMode, pub mode: ArrangerViewMode,
/// Slot for modal dialog displayed on top of app. /// Slot for modal dialog displayed on top of app.
pub modal: Option<Box<dyn ExitableComponent>>, pub modal: Option<Box<dyn ExitableComponent>>,
} }
impl Arranger { impl Arranger {

View file

@ -18,33 +18,7 @@ struct ArrangerStandalone {
focus: usize, focus: usize,
} }
impl ArrangerStandalone { focus!(ArrangerStandalone (focus) : 2 => [ transport, arranger ]);
fn focus_prev (&mut self) {
self.focus = if self.focus > 0 { 1 } else { self.focus - 1 };
}
fn focus_next (&mut self) {
self.focus = if self.focus < 1 { self.focus + 1 } else { 0 };
}
fn focused (&self) -> &dyn Render {
self.focusable()[self.focus]
}
fn focusable (&self) -> [&dyn Render;2] {
[
&self.transport as &dyn Render,
&self.arranger as &dyn Render,
]
}
fn focused_mut (&mut self) -> &mut dyn Handle {
let focus = self.focus;
self.focusable_mut()[focus]
}
fn focusable_mut (&mut self) -> [&mut dyn Handle;2] {
[
&mut self.transport as &mut dyn Handle,
&mut self.arranger as &mut dyn Handle
]
}
}
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]