1use crossterm::event as crossterm_event;
6use std::{io::Result as CrosstermResult, time::Duration};
7
8#[derive(Debug, Clone)]
10pub enum Event {
11 Key(crossterm_event::KeyEvent),
13 Mouse(crossterm_event::MouseEvent),
15 Resize(u16, u16),
17 Timeout,
19}
20
21pub trait EventHandler {
23 fn handle(&mut self, event: &Event) -> bool;
27}
28
29pub struct EventSource {
31 tick_rate: Duration,
32}
33
34impl EventSource {
35 pub fn new(tick_rate: Duration) -> Self {
37 Self { tick_rate }
38 }
39
40 pub fn next(&self) -> CrosstermResult<Event> {
42 if crossterm_event::poll(self.tick_rate)? {
43 match crossterm_event::read()? {
44 crossterm_event::Event::Key(key) => Ok(Event::Key(key)),
45 crossterm_event::Event::Mouse(mouse) => Ok(Event::Mouse(mouse)),
46 crossterm_event::Event::Resize(width, height) => Ok(Event::Resize(width, height)),
47 _ => Ok(Event::Timeout),
48 }
49 }
50 else {
51 Ok(Event::Timeout)
52 }
53 }
54}