First one-shot pass
This commit is contained in:
49
src/event.rs
Normal file
49
src/event.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use crossterm::event::{self, Event, KeyEvent};
|
||||
|
||||
pub enum AppEvent {
|
||||
Key(KeyEvent),
|
||||
Tick,
|
||||
Resize(#[allow(dead_code)] u16, #[allow(dead_code)] u16),
|
||||
}
|
||||
|
||||
pub struct EventHandler {
|
||||
rx: mpsc::Receiver<AppEvent>,
|
||||
_tx: mpsc::Sender<AppEvent>,
|
||||
}
|
||||
|
||||
impl EventHandler {
|
||||
pub fn new(tick_rate: Duration) -> Self {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let _tx = tx.clone();
|
||||
|
||||
thread::spawn(move || loop {
|
||||
if event::poll(tick_rate).unwrap_or(false) {
|
||||
match event::read() {
|
||||
Ok(Event::Key(key)) => {
|
||||
if tx.send(AppEvent::Key(key)).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Ok(Event::Resize(w, h)) => {
|
||||
if tx.send(AppEvent::Resize(w, h)).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if tx.send(AppEvent::Tick).is_err() {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Self { rx, _tx }
|
||||
}
|
||||
|
||||
pub fn next(&self) -> anyhow::Result<AppEvent> {
|
||||
Ok(self.rx.recv()?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user