Element events #19

This commit is contained in:
2025-08-02 16:26:38 -05:00
parent e0ae338e15
commit ec3576e6db
4 changed files with 32 additions and 9 deletions
+2 -4
View File
@@ -1,4 +1,4 @@
use crate::{extensions::Extension, prelude::Handler};
use crate::extensions::Extension;
pub struct InputExtension;
@@ -8,9 +8,7 @@ impl Extension for InputExtension {
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::<Handler<crossterm::event::Event>>() {
h.call(widget, &e);
}
widget.event(&e);
}
}
});
+6
View File
@@ -58,3 +58,9 @@ impl<E: Event> Debug for Handler<E> {
write!(f, "Handler({})", type_name::<E>())
}
}
impl<'a> dyn Event + 'a {
pub fn get<T: Event + 'static>(&self) -> Option<&T> {
self.as_any().downcast_ref()
}
}
+1 -4
View File
@@ -1,7 +1,6 @@
use std::{fmt::Debug, sync::Arc};
use crate::extensions::Extension;
use crate::extensions::Handler;
use crate::{event, Screen};
pub struct TickExtension(pub u16);
@@ -16,9 +15,7 @@ impl Extension for TickExtension {
let mut tick = 0;
loop {
for w in screen.widgets.lock().unwrap().iter() {
if let Some(on_tick) = w.get::<Handler<TickEvent>>() {
on_tick.call(&w, &TickEvent(tick))
}
w.event(&TickEvent(tick));
}
tick += 1;
std::thread::sleep(std::time::Duration::from_millis(rate_dur));