mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wip: unified focus system
This commit is contained in:
parent
8b59658015
commit
3a7aa9e9a3
10 changed files with 194 additions and 123 deletions
|
|
@ -144,14 +144,14 @@ pub const KEYMAP_FOCUS: &'static [KeyBinding<App>] = keymap!(App {
|
|||
[Esc, NONE, "focus_exit", "unfocus", |app: &mut App|{
|
||||
app.entered = false;
|
||||
app.transport.entered = app.entered;
|
||||
app.arranger.entered = app.entered;
|
||||
//app.arranger.entered = app.entered;
|
||||
app.arranger.sequencer_mut().map(|s|s.entered = app.entered);
|
||||
Ok(true)
|
||||
}],
|
||||
[Enter, NONE, "focus_enter", "activate item at cursor", |app: &mut App|{
|
||||
app.entered = true;
|
||||
app.transport.entered = app.entered;
|
||||
app.arranger.entered = app.entered;
|
||||
//app.arranger.entered = app.entered;
|
||||
app.arranger.sequencer_mut().map(|s|s.entered = app.entered);
|
||||
Ok(true)
|
||||
}],
|
||||
|
|
@ -161,8 +161,8 @@ pub fn focus_next (app: &mut App) -> Usually<bool> {
|
|||
app.section.next();
|
||||
app.transport.focused = app.section == AppFocus::Transport;
|
||||
app.transport.entered = app.entered;
|
||||
app.arranger.focused = app.section == AppFocus::Arranger;
|
||||
app.arranger.entered = app.entered;
|
||||
//app.arranger.focused = app.section == AppFocus::Arranger;
|
||||
//app.arranger.entered = app.entered;
|
||||
app.arranger.sequencer_mut().map(|s|{
|
||||
s.focused = app.section == AppFocus::Sequencer;
|
||||
s.entered = app.entered;
|
||||
|
|
@ -174,8 +174,8 @@ pub fn focus_prev (app: &mut App) -> Usually<bool> {
|
|||
app.section.prev();
|
||||
app.transport.focused = app.section == AppFocus::Transport;
|
||||
app.transport.entered = app.entered;
|
||||
app.arranger.focused = app.section == AppFocus::Arranger;
|
||||
app.arranger.entered = app.entered;
|
||||
//app.arranger.focused = app.section == AppFocus::Arranger;
|
||||
//app.arranger.entered = app.entered;
|
||||
app.arranger.sequencer_mut().map(|s|{
|
||||
s.focused = app.section == AppFocus::Sequencer;
|
||||
s.entered = app.entered;
|
||||
|
|
|
|||
|
|
@ -73,36 +73,3 @@ pub enum AppEvent {
|
|||
// /// JACK notification
|
||||
// Jack(JackEvent)
|
||||
}
|
||||
|
||||
pub type KeyHandler<T> = &'static dyn Fn(&mut T)->Usually<bool>;
|
||||
|
||||
pub type KeyBinding<T> = (
|
||||
KeyCode, KeyModifiers, &'static str, &'static str, KeyHandler<T>
|
||||
);
|
||||
|
||||
pub type KeyMap<T> = [KeyBinding<T>];
|
||||
|
||||
pub fn handle_keymap <T> (
|
||||
state: &mut T, event: &AppEvent, keymap: &KeyMap<T>,
|
||||
) -> Usually<bool> {
|
||||
match event {
|
||||
AppEvent::Input(crossterm::event::Event::Key(event)) => {
|
||||
for (code, modifiers, _, _, command) in keymap.iter() {
|
||||
if *code == event.code && modifiers.bits() == event.modifiers.bits() {
|
||||
return command(state)
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Define a keymap
|
||||
#[macro_export] macro_rules! keymap {
|
||||
($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* $(,)? }) => {
|
||||
&[
|
||||
$((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),*
|
||||
] as &'static [KeyBinding<$T>]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
51
crates/tek_core/src/handle_focus.rs
Normal file
51
crates/tek_core/src/handle_focus.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use crate::*;
|
||||
|
||||
/// A collection of [Focusable] items.
|
||||
pub struct Focus {
|
||||
index: usize,
|
||||
items: Vec<Focusable>
|
||||
}
|
||||
|
||||
impl Focus {
|
||||
pub fn new (items: Vec<Focusable>) -> Self {
|
||||
Self {
|
||||
index: 0,
|
||||
items
|
||||
}
|
||||
}
|
||||
pub fn item_mut (&mut self) -> &mut impl Handle {
|
||||
&mut self.items[self.index]
|
||||
}
|
||||
/// Select next item.
|
||||
pub fn next (&mut self) -> Usually<bool> {
|
||||
self.index = (self.index + 1) % self.items.len();
|
||||
Ok(true)
|
||||
}
|
||||
/// Select previous item.
|
||||
pub fn prev (&mut self) -> Usually<bool> {
|
||||
self.index = match self.index {
|
||||
0 => self.items.len().saturating_sub(1),
|
||||
_ => self.index - 1
|
||||
};
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
handle!(Focus |self, e| Ok(
|
||||
handle_keymap(self, e, KEYMAP_FOCUS)? || self.item_mut().handle(e)?
|
||||
));
|
||||
|
||||
pub const KEYMAP_FOCUS: &'static [KeyBinding<Focus>] = keymap!(Focus {
|
||||
[Tab, NONE, "focus_next", "focus next item", Focus::next],
|
||||
[Tab, SHIFT, "focus_prev", "focus previous item", Focus::prev],
|
||||
});
|
||||
|
||||
/// A wrapper around items that can be focused.
|
||||
pub enum Focusable {
|
||||
/// A monolithic focus item.
|
||||
Mono(Arc<dyn Handle + Send + Sync>),
|
||||
/// A focus item that contains other focus items.
|
||||
Poly(Box<Focusable>),
|
||||
}
|
||||
|
||||
handle!(Focusable |self, e| { todo!("{e:?}"); Ok(false) });
|
||||
34
crates/tek_core/src/handle_keymap.rs
Normal file
34
crates/tek_core/src/handle_keymap.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use crate::*;
|
||||
|
||||
pub type KeyHandler<T> = &'static dyn Fn(&mut T)->Usually<bool>;
|
||||
|
||||
pub type KeyBinding<T> = (
|
||||
KeyCode, KeyModifiers, &'static str, &'static str, KeyHandler<T>
|
||||
);
|
||||
|
||||
pub type KeyMap<T> = [KeyBinding<T>];
|
||||
|
||||
pub fn handle_keymap <T> (
|
||||
state: &mut T, event: &AppEvent, keymap: &KeyMap<T>,
|
||||
) -> Usually<bool> {
|
||||
match event {
|
||||
AppEvent::Input(crossterm::event::Event::Key(event)) => {
|
||||
for (code, modifiers, _, _, command) in keymap.iter() {
|
||||
if *code == event.code && modifiers.bits() == event.modifiers.bits() {
|
||||
return command(state)
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => {}
|
||||
};
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
/// Define a keymap
|
||||
#[macro_export] macro_rules! keymap {
|
||||
($T:ty { $([$k:ident $(($char:literal))?, $m:ident, $n: literal, $d: literal, $f: expr]),* $(,)? }) => {
|
||||
&[
|
||||
$((KeyCode::$k $(($char))?, KeyModifiers::$m, $n, $d, &$f as KeyHandler<$T>)),*
|
||||
] as &'static [KeyBinding<$T>]
|
||||
}
|
||||
}
|
||||
|
|
@ -46,6 +46,8 @@ use crossterm::terminal::{
|
|||
submod! {
|
||||
exit
|
||||
handle
|
||||
handle_focus
|
||||
handle_keymap
|
||||
jack_core
|
||||
jack_device
|
||||
jack_event
|
||||
|
|
|
|||
|
|
@ -123,13 +123,19 @@ impl<'a> Render for Box<dyn Fn(&mut Buffer, Rect) -> Usually<Rect> + Send + Sync
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Render> Render for Arc<Mutex<T>> {
|
||||
impl<T: Render> Render for Arc<T> {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
self.as_ref().render(b, a)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Render> Render for Mutex<T> {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
self.lock().unwrap().render(b, a)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Render + Sync> Render for Arc<RwLock<T>> {
|
||||
impl<T: Render + Sync> Render for RwLock<T> {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
self.read().unwrap().render(b, a)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,10 +15,7 @@ pub struct Arranger {
|
|||
pub selected: ArrangerFocus,
|
||||
/// Display mode of arranger
|
||||
pub mode: ArrangerViewMode,
|
||||
|
||||
pub focused: bool,
|
||||
pub entered: bool,
|
||||
pub focus_sequencer: bool,
|
||||
/// Slot for modal dialog displayed on top of app.
|
||||
pub modal: Option<Box<dyn ExitableComponent>>,
|
||||
}
|
||||
|
||||
|
|
@ -30,9 +27,6 @@ impl Arranger {
|
|||
selected: ArrangerFocus::Clip(0, 0),
|
||||
scenes: vec![],
|
||||
tracks: vec![],
|
||||
entered: true,
|
||||
focused: true,
|
||||
focus_sequencer: false,
|
||||
modal: None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,22 @@ pub fn main () -> Usually<()> {
|
|||
}
|
||||
|
||||
struct ArrangerStandalone {
|
||||
arranger: Arranger,
|
||||
/// Contains all the sequencers.
|
||||
arranger: Arc<Arranger>,
|
||||
/// Controls the JACK transport.
|
||||
transport: Option<Arc<RwLock<TransportToolbar>>>,
|
||||
/// This allows the sequencer view to be moved or hidden.
|
||||
show_sequencer: Option<tek_core::Direction>,
|
||||
/// Proxies input events to the currently active sequencer.
|
||||
sequencer_proxy: SequencerProxy,
|
||||
///
|
||||
focus_order: Vec<FocusItem>,
|
||||
}
|
||||
|
||||
struct SequencerProxy;
|
||||
|
||||
impl Focus for SequencerProxy {}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct ArrangerCli {
|
||||
|
|
@ -28,17 +39,24 @@ pub struct ArrangerCli {
|
|||
#[arg(short, long, default_value_t = 8)] scenes: usize,
|
||||
}
|
||||
|
||||
impl ArrangerStandalone {
|
||||
impl<'a> ArrangerStandalone<'a> {
|
||||
pub fn from_args () -> Usually<Self> {
|
||||
let args = ArrangerCli::parse();
|
||||
let mut app = ArrangerStandalone {
|
||||
arranger: Arranger::new(""),
|
||||
transport: match args.transport {
|
||||
let arranger = Arranger::new("");
|
||||
let transport = match args.transport {
|
||||
Some(true) => Some(Arc::new(RwLock::new(TransportToolbar::new(None)))),
|
||||
_ => None
|
||||
},
|
||||
show_sequencer: Some(tek_core::Direction::Down),
|
||||
};
|
||||
let sequencer_proxy = SequencerProxy;
|
||||
let mut app = ArrangerStandalone {
|
||||
transport,
|
||||
show_sequencer: Some(tek_core::Direction::Down),
|
||||
arranger,
|
||||
sequencer_proxy,
|
||||
focus_order: vec![],
|
||||
};
|
||||
app.focus_order.push(FocusItem::Mono(&app.arranger));
|
||||
app.focus_order.push(FocusItem::Poly(&app.sequencer_proxy));
|
||||
if let Some(name) = args.name {
|
||||
*app.arranger.name.write().unwrap() = name.clone();
|
||||
}
|
||||
|
|
@ -60,7 +78,8 @@ impl ArrangerStandalone {
|
|||
}
|
||||
}
|
||||
|
||||
render!(ArrangerStandalone |self, buf, area| {
|
||||
impl Render for ArrangerStandalone {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
let mut layout = Split::down();
|
||||
if let Some(transport) = &self.transport {
|
||||
layout = layout.add_ref(transport);
|
||||
|
|
@ -80,9 +99,11 @@ render!(ArrangerStandalone |self, buf, area| {
|
|||
modal.render(buf, area)?;
|
||||
}
|
||||
Ok(result)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
handle!(ArrangerStandalone |self, e| {
|
||||
impl Handle for ArrangerStandalone {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
if let Some(modal) = self.arranger.modal.as_mut() {
|
||||
let result = modal.handle(e)?;
|
||||
if modal.exited() {
|
||||
|
|
@ -92,20 +113,22 @@ handle!(ArrangerStandalone |self, e| {
|
|||
} else {
|
||||
match e {
|
||||
AppEvent::Input(Event::Key(k)) => {
|
||||
if k.code == KeyCode::Tab {
|
||||
self.arranger.focus_sequencer = !self.arranger.focus_sequencer;
|
||||
Ok(true)
|
||||
} else if self.arranger.focus_sequencer {
|
||||
if let Some(sequencer) = self.arranger.sequencer_mut() {
|
||||
handle_keymap(sequencer, e, KEYMAP_SEQUENCER)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
} else {
|
||||
handle_keymap(&mut self.arranger, e, KEYMAP_ARRANGER)
|
||||
}
|
||||
//if k.code == KeyCode::Tab {
|
||||
//self.arranger.focus_sequencer = !self.arranger.focus_sequencer;
|
||||
//Ok(true)
|
||||
//} else if self.arranger.focus_sequencer {
|
||||
//if let Some(sequencer) = self.arranger.sequencer_mut() {
|
||||
//handle_keymap(sequencer, e, KEYMAP_SEQUENCER)
|
||||
//} else {
|
||||
//Ok(false)
|
||||
//}
|
||||
//} else {
|
||||
//handle_keymap(&mut self.arranger, e, KEYMAP_ARRANGER)
|
||||
//}
|
||||
},
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ pub fn draw (state: &Arranger, buf: &mut Buffer, mut area: Rect) -> Usually<Rect
|
|||
area.height = area.height.min((2 + state.tracks.len() * 2) as u16);
|
||||
let tracks = state.tracks.as_slice();
|
||||
Layered::new()
|
||||
.add(FillBg(Nord::bg_lo(state.focused, state.entered)))
|
||||
//.add(FillBg(Nord::bg_lo(state.focused, state.entered)))
|
||||
.add(Split::right()
|
||||
.add(TrackNameColumn(tracks, state.selected))
|
||||
.add(TrackMonitorColumn(tracks))
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ pub fn draw_compact_2 (state: &Arranger, buf: &mut Buffer, area: Rect) -> Usuall
|
|||
draw(state, buf, area, track_cols.as_slice(), scene_rows.as_slice())
|
||||
}
|
||||
|
||||
/// Draw arranger with number of rows per scene corresponding to duration of scene.
|
||||
/// Draw arranger with number of rows per scene proportional to duration of scene.
|
||||
pub fn draw_expanded (state: &Arranger, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
let track_cols = track_clip_name_lengths(state.tracks.as_slice());
|
||||
let scene_rows = scene_ppqs(state.tracks.as_slice(), state.scenes.as_slice());
|
||||
|
|
@ -30,14 +30,13 @@ pub fn draw (
|
|||
) -> Usually<Rect> {
|
||||
area.height = 2 + (rows[rows.len() - 1].1 / 96) as u16;
|
||||
let offset = 3 + scene_name_max_len(state.scenes.as_ref()) as u16;
|
||||
let Arranger { focus_sequencer, focused, entered, selected, .. } = *state;
|
||||
let tracks = state.tracks.as_ref();
|
||||
let scenes = state.scenes.as_ref();
|
||||
Layered::new()
|
||||
//.add_ref(&FillBg(Color::Rgb(30, 33, 36)))//COLOR_BG1))//bg_lo(state.focused, state.entered)))
|
||||
.add_ref(&ColumnSeparators(offset, cols))
|
||||
.add_ref(&RowSeparators(rows))
|
||||
.add_ref(&CursorFocus(focus_sequencer, focused, entered, selected, offset, cols, rows))
|
||||
.add_ref(&CursorFocus(state.selected, offset, cols, rows))
|
||||
.add_ref(&Split::down()
|
||||
.add_ref(&TracksHeader(offset, cols, tracks))
|
||||
.add_ref(&SceneRows(offset, cols, rows, tracks, scenes)))
|
||||
|
|
@ -81,12 +80,12 @@ impl<'a> Render for RowSeparators<'a> {
|
|||
}
|
||||
|
||||
struct CursorFocus<'a>(
|
||||
bool, bool, bool, ArrangerFocus, u16, &'a [(usize, usize)], &'a [(usize, usize)]
|
||||
ArrangerFocus, u16, &'a [(usize, usize)], &'a [(usize, usize)]
|
||||
);
|
||||
|
||||
impl<'a> Render for CursorFocus<'a> {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
let Self(focus_sequencer, focused, entered, selected, offset, cols, rows) = *self;
|
||||
let Self(selected, offset, cols, rows) = *self;
|
||||
let get_track_area = |t: usize| Rect {
|
||||
x: offset + area.x + cols[t].1 as u16 - 1,
|
||||
y: area.y,
|
||||
|
|
@ -109,11 +108,9 @@ impl<'a> Render for CursorFocus<'a> {
|
|||
let mut scene_area: Option<Rect> = None;
|
||||
let mut clip_area: Option<Rect> = None;
|
||||
let area = match selected {
|
||||
ArrangerFocus::Mix => if focused && entered && selected == ArrangerFocus::Mix {
|
||||
ArrangerFocus::Mix => {
|
||||
fill_bg(buf, area, COLOR_BG0);
|
||||
area
|
||||
} else {
|
||||
area
|
||||
},
|
||||
ArrangerFocus::Track(t) => {
|
||||
track_area = Some(get_track_area(t));
|
||||
|
|
@ -145,9 +142,6 @@ impl<'a> Render for CursorFocus<'a> {
|
|||
} else if let Some(scene_area) = scene_area {
|
||||
fill_bg(buf, scene_area, COLOR_BG0);
|
||||
}
|
||||
if !focus_sequencer {
|
||||
Corners(Style::default().green().not_dim()).draw(buf, area)?;
|
||||
}
|
||||
Ok(area)
|
||||
}
|
||||
}
|
||||
|
|
@ -212,7 +206,7 @@ impl<'a> Render for SceneRow<'a> {
|
|||
let playing = scene.is_playing(tracks);
|
||||
(if playing { "▶" } else { " " }).blit(buf, x, y, None)?;
|
||||
scene.name.read().unwrap().blit(buf, x + 1, y, Some(Style::default().white()))?;
|
||||
fill_bg(buf, Rect { x: x, y, width: offset.saturating_sub(1), height: 2 }, COLOR_BG1);
|
||||
fill_bg(buf, Rect { x: x, y, width: offset.saturating_sub(1), height: area.height }, COLOR_BG1);
|
||||
|
||||
for (track, (w, x)) in track_cols.iter().enumerate() {
|
||||
let x = *x as u16 + offset;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue