mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
wip: slowly putting it back together
This commit is contained in:
parent
7fbb40fad6
commit
461c60d6b3
18 changed files with 788 additions and 774 deletions
|
|
@ -1,114 +1,46 @@
|
|||
use crate::*;
|
||||
|
||||
/// Spawn thread that listens for user input
|
||||
pub fn input_thread (
|
||||
exited: &Arc<AtomicBool>,
|
||||
device: &Arc<RwLock<impl Handle + Send + Sync + 'static>>
|
||||
) -> JoinHandle<()> {
|
||||
let poll = Duration::from_millis(100);
|
||||
let exited = exited.clone();
|
||||
let device = device.clone();
|
||||
spawn(move || loop {
|
||||
// Exit if flag is set
|
||||
if exited.fetch_and(true, Ordering::Relaxed) {
|
||||
break
|
||||
}
|
||||
// Listen for events and send them to the main thread
|
||||
if ::crossterm::event::poll(poll).is_ok() {
|
||||
let event = ::crossterm::event::read().unwrap();
|
||||
if let Event::Key(KeyEvent {
|
||||
code: KeyCode::Char('c'), modifiers: KeyModifiers::CONTROL, ..
|
||||
}) = event {
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
} else if let Err(e) = device.write().unwrap().handle(&AppEvent::Input(event)) {
|
||||
panic!("{e}")
|
||||
}
|
||||
}
|
||||
})
|
||||
/// Handle input
|
||||
pub trait Handle<T, U>: Send + Sync {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U>;
|
||||
}
|
||||
|
||||
/// Trait for things that handle input events.
|
||||
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;.
|
||||
fn handle (&mut self, _e: &AppEvent) -> Usually<bool> {
|
||||
Ok(false)
|
||||
impl<H, T, U> Handle<T, U> for &mut H where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
(*self).handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
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<H, T, U> Handle<T, U> for Option<H> where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
if let Some(ref mut handle) = self {
|
||||
handle.handle(context)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Mutex<T> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.lock().unwrap().handle(e)
|
||||
impl<H, T, U> Handle<T, U> for Mutex<H> where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
self.lock().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Arc<Mutex<T>> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.lock().unwrap().handle(e)
|
||||
impl<H, T, U> Handle<T, U> for Arc<Mutex<H>> where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
self.lock().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for RwLock<T> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.write().unwrap().handle(e)
|
||||
impl<H, T, U> Handle<T, U> for RwLock<H> where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Handle> Handle for Arc<RwLock<T>> {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
self.write().unwrap().handle(e)
|
||||
impl<H, T, U> Handle<T, U> for Arc<RwLock<H>> where H: Handle<T, U> {
|
||||
fn handle (&mut self, context: &mut T) -> Perhaps<U> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement the `Handle` trait.
|
||||
#[macro_export] macro_rules! handle {
|
||||
($T:ty) => {
|
||||
impl Handle for $T {}
|
||||
};
|
||||
($T:ty |$self:ident, $e:ident|$block:expr) => {
|
||||
impl Handle for $T {
|
||||
fn handle (&mut $self, $e: &AppEvent) -> Usually<bool> {
|
||||
$block
|
||||
}
|
||||
}
|
||||
};
|
||||
($T:ty = $handle:path) => {
|
||||
impl Handle for $T {
|
||||
fn handle (&mut self, e: &AppEvent) -> Usually<bool> {
|
||||
$handle(self, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AppEvent {
|
||||
/// Terminal input
|
||||
Input(::crossterm::event::Event),
|
||||
/// Update values but not the whole form.
|
||||
Update,
|
||||
/// Update the whole form.
|
||||
Redraw,
|
||||
/// Device gains focus
|
||||
Focus,
|
||||
/// Device loses focus
|
||||
Blur,
|
||||
// /// JACK notification
|
||||
// Jack(JackEvent)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue