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;
|
pub struct InputExtension;
|
||||||
|
|
||||||
@@ -8,9 +8,7 @@ impl Extension for InputExtension {
|
|||||||
std::thread::spawn(move || loop {
|
std::thread::spawn(move || loop {
|
||||||
if let Ok(e) = crossterm::event::read() {
|
if let Ok(e) = crossterm::event::read() {
|
||||||
for widget in screen.widgets.lock().unwrap().iter() {
|
for widget in screen.widgets.lock().unwrap().iter() {
|
||||||
if let Some(h) = widget.get::<Handler<crossterm::event::Event>>() {
|
widget.event(&e);
|
||||||
h.call(widget, &e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -58,3 +58,9 @@ impl<E: Event> Debug for Handler<E> {
|
|||||||
write!(f, "Handler({})", type_name::<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 std::{fmt::Debug, sync::Arc};
|
||||||
|
|
||||||
use crate::extensions::Extension;
|
use crate::extensions::Extension;
|
||||||
use crate::extensions::Handler;
|
|
||||||
use crate::{event, Screen};
|
use crate::{event, Screen};
|
||||||
|
|
||||||
pub struct TickExtension(pub u16);
|
pub struct TickExtension(pub u16);
|
||||||
@@ -16,9 +15,7 @@ impl Extension for TickExtension {
|
|||||||
let mut tick = 0;
|
let mut tick = 0;
|
||||||
loop {
|
loop {
|
||||||
for w in screen.widgets.lock().unwrap().iter() {
|
for w in screen.widgets.lock().unwrap().iter() {
|
||||||
if let Some(on_tick) = w.get::<Handler<TickEvent>>() {
|
w.event(&TickEvent(tick));
|
||||||
on_tick.call(&w, &TickEvent(tick))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
tick += 1;
|
tick += 1;
|
||||||
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
|
std::thread::sleep(std::time::Duration::from_millis(rate_dur));
|
||||||
|
|||||||
+23
-1
@@ -12,7 +12,11 @@ use std::{
|
|||||||
sync::{Arc, Mutex, MutexGuard},
|
sync::{Arc, Mutex, MutexGuard},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{render_scope::RenderScope, state::DependencyHandler};
|
use crate::{
|
||||||
|
prelude::{Event, Handler},
|
||||||
|
render_scope::RenderScope,
|
||||||
|
state::DependencyHandler,
|
||||||
|
};
|
||||||
|
|
||||||
/// A trait object for any renderable UI element.
|
/// A trait object for any renderable UI element.
|
||||||
pub type BoxedElement = Box<dyn Element + Send + Sync>;
|
pub type BoxedElement = Box<dyn Element + Send + Sync>;
|
||||||
@@ -37,6 +41,9 @@ pub trait Element: Send + Sync {
|
|||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
fn draw_child(&self, element: &Arc<Widget>) {}
|
fn draw_child(&self, element: &Arc<Widget>) {}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
fn event(&mut self, event: &dyn Event) {}
|
||||||
|
|
||||||
/// Returns a type-erased reference to this object.
|
/// Returns a type-erased reference to this object.
|
||||||
fn as_any(&self) -> &dyn Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
|
|
||||||
@@ -223,6 +230,21 @@ impl Widget {
|
|||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn event<E: Event + Clone + 'static>(self: &Arc<Self>, e: &E) {
|
||||||
|
if let Some(wrapper) = self.get::<Handler<E>>() {
|
||||||
|
wrapper.call(self, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
match &**self {
|
||||||
|
Widget::Dynamic(w) => {
|
||||||
|
w.get_elem().event(e);
|
||||||
|
}
|
||||||
|
Widget::Static(w) => {
|
||||||
|
w.get_elem().event(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticWidget {
|
impl StaticWidget {
|
||||||
|
|||||||
Reference in New Issue
Block a user