working divs
This commit is contained in:
+9
-23
@@ -3,7 +3,7 @@ pub mod tick;
|
||||
pub mod velocity;
|
||||
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
any::Any,
|
||||
fmt::Debug,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
@@ -23,12 +23,9 @@ pub trait Event: Send + Sync {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Handler(
|
||||
Arc<Mutex<dyn FnMut(&Arc<Widget>, &dyn Event) + Send + Sync>>,
|
||||
TypeId,
|
||||
);
|
||||
pub struct Handler<E: Event>(Arc<Mutex<dyn FnMut(&Arc<Widget>, &E) + Send + Sync>>);
|
||||
|
||||
impl Component for Handler {
|
||||
impl<E: Event + 'static> Component for Handler<E> {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
@@ -38,28 +35,17 @@ impl Component for Handler {
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler {
|
||||
pub fn new<E: Event + 'static, F: FnMut(&Arc<Widget>, &E) + Send + Sync + 'static>(
|
||||
mut f: F,
|
||||
) -> Handler {
|
||||
Handler(
|
||||
Arc::new(Mutex::new(move |w: &Arc<Widget>, e: &dyn Event| {
|
||||
if let Some(e) = e.as_any().downcast_ref::<E>() {
|
||||
f(w, e)
|
||||
}
|
||||
})),
|
||||
TypeId::of::<E>(),
|
||||
)
|
||||
impl<E: Event + 'static> Handler<E> {
|
||||
pub fn new<F: FnMut(&Arc<Widget>, &E) + Send + Sync + 'static>(f: F) -> Handler<E> {
|
||||
Handler(Arc::new(Mutex::new(f)))
|
||||
}
|
||||
|
||||
pub fn call<E: Event>(&self, w: &Arc<Widget>, e: &E) {
|
||||
if e.as_any().type_id() == self.1 {
|
||||
(self.0.lock().unwrap())(w, e)
|
||||
}
|
||||
pub fn call(&self, w: &Arc<Widget>, e: &E) {
|
||||
(self.0.lock().unwrap())(w, e)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for Handler {
|
||||
impl<T: Event> Debug for Handler<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Handler")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user