use super::key::Key;
use crossterm::event::{self, Event as CrosstermEvent, KeyEventKind, MouseEvent, MouseEventKind};
use std::{sync::mpsc, thread, time::Duration};
#[derive(Debug, Clone, Copy)]
pub struct EventConfig {
#[allow(dead_code)]
pub exit_key: Key,
pub tick_rate: Duration,
}
impl Default for EventConfig {
fn default() -> EventConfig {
EventConfig {
exit_key: Key::Ctrl('c'),
tick_rate: Duration::from_millis(250),
}
}
}
pub enum Event {
Input(Key),
Mouse(MouseEvent),
Tick,
}
pub struct Events {
rx: mpsc::Receiver<Event>,
_tx: mpsc::Sender<Event>,
}
impl Events {
pub fn new(tick_rate: u64) -> Events {
Events::with_config(EventConfig {
tick_rate: Duration::from_millis(tick_rate),
..Default::default()
})
}
pub fn with_config(config: EventConfig) -> Events {
let (tx, rx) = mpsc::channel();
let event_tx = tx.clone();
thread::spawn(move || {
loop {
if event::poll(config.tick_rate).unwrap() {
match event::read().unwrap() {
CrosstermEvent::Key(key) => {
if key.kind == KeyEventKind::Press {
let key = Key::from(key);
if event_tx.send(Event::Input(key)).is_err() {
break;
}
}
}
CrosstermEvent::Mouse(mouse) => {
if matches!(
mouse.kind,
MouseEventKind::Down(_) | MouseEventKind::ScrollUp | MouseEventKind::ScrollDown
) && event_tx.send(Event::Mouse(mouse)).is_err()
{
break;
}
}
_ => {}
}
}
if event_tx.send(Event::Tick).is_err() {
break;
}
}
});
Events { rx, _tx: tx }
}
pub fn next(&self) -> Result<Event, mpsc::RecvError> {
self.rx.recv()
}
}