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
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue