use std::sync::Arc; use crossterm::event::KeyEvent; use crate::{ events::{Close, Event, EventManager}, extensions::Extension, Screen, }; impl Event for KeyEvent { fn as_any(self: Box) -> Box { self } } pub trait KeyPressEventHandler { fn on_keypress(&mut self, events: &Arc, event: Box); } pub struct KeyPressExtension; impl Extension for KeyPressExtension { fn init(&mut self, _: &mut Screen, events: &Arc) { crossterm::terminal::enable_raw_mode().unwrap(); crate::utils::clear().unwrap(); crate::utils::hide_cursor().unwrap(); events.on(|_, _: Box| { crossterm::terminal::disable_raw_mode().unwrap(); crate::utils::show_cursor().unwrap(); crate::utils::clear().unwrap(); }); } fn tick_start(&mut self, _: &mut Screen, _: &Arc) {} fn tick_end(&mut self, _: &mut Screen, events: &Arc) { if crossterm::event::poll(std::time::Duration::from_millis(13)).unwrap() { match crossterm::event::read().unwrap() { crossterm::event::Event::Key(e) => { events.dispatch(e); } _ => {} } } } }