diff --git a/src/demos/mod.rs b/src/demos/mod.rs index 104f97d..baa05a6 100644 --- a/src/demos/mod.rs +++ b/src/demos/mod.rs @@ -12,6 +12,9 @@ pub fn app() -> Rsx { }); rsx! { + @Handler::new(|_, _: &crossterm::event::Event| { + panic!("idk lol"); + }); FlexRow { FlexCol { @Transform::new().padding(2, 2); diff --git a/src/extensions/input_handling.rs b/src/extensions/input_handling.rs new file mode 100644 index 0000000..76a9792 --- /dev/null +++ b/src/extensions/input_handling.rs @@ -0,0 +1,24 @@ +use crate::{extensions::Extension, prelude::Handler}; + +pub struct InputExtension; + +impl Extension for InputExtension { + fn init(&mut self, screen: std::sync::Arc) { + crossterm::terminal::enable_raw_mode().unwrap(); + std::thread::spawn(move || loop { + if let Ok(e) = crossterm::event::read() { + for widget in screen.widgets.lock().unwrap().iter() { + if let Some(h) = widget.get::>() { + h.call(widget, &e); + } + } + } + }); + } +} + +impl crate::extensions::Event for crossterm::event::Event { + fn as_any(&self) -> &dyn std::any::Any { + self + } +} diff --git a/src/extensions/mod.rs b/src/extensions/mod.rs index ea6607e..b9bbb15 100644 --- a/src/extensions/mod.rs +++ b/src/extensions/mod.rs @@ -1,8 +1,10 @@ pub mod id; +pub mod input_handling; pub mod tick; pub mod velocity; pub use id::*; +pub use input_handling::*; pub use tick::*; pub use velocity::*; diff --git a/src/main.rs b/src/main.rs index 1e23f4f..66c252a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use osui::prelude::*; fn main() -> std::io::Result<()> { let screen = Screen::new(); + screen.extension(InputExtension); demos::app().draw(&screen);