Element events #19
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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,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));
|
||||
|
||||
Reference in New Issue
Block a user