mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
wrapper impls for Render and Handle
This commit is contained in:
parent
3a7aa9e9a3
commit
777904cb35
4 changed files with 118 additions and 120 deletions
|
|
@ -28,7 +28,7 @@ pub fn input_thread (
|
|||
}
|
||||
|
||||
/// Trait for things that handle input events.
|
||||
pub trait Handle {
|
||||
pub trait Handle: Send + Sync {
|
||||
/// Handle an input event.
|
||||
/// Returns Ok(true) if the device handled the event.
|
||||
/// This is the mechanism which allows nesting of components;.
|
||||
|
|
@ -37,6 +37,45 @@ pub trait Handle {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for &mut T {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
(*self).handle(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Option<T> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
match self {
|
||||
Some(handle) => handle.handle(e),
|
||||
None => Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Mutex<T> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.lock().unwrap().handle(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Arc<Mutex<T>> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.lock().unwrap().handle(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for RwLock<T> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.write().unwrap().handle(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Arc<RwLock<T>> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.write().unwrap().handle(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement the `Handle` trait.
|
||||
#[macro_export] macro_rules! handle {
|
||||
($T:ty) => {
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
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) });
|
||||
|
|
@ -46,7 +46,6 @@ use crossterm::terminal::{
|
|||
submod! {
|
||||
exit
|
||||
handle
|
||||
handle_focus
|
||||
handle_keymap
|
||||
jack_core
|
||||
jack_device
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue