working divs

This commit is contained in:
2025-07-05 04:40:05 -05:00
parent cec25c02d5
commit ec963707a1
8 changed files with 155 additions and 61 deletions
+9 -23
View File
@@ -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")
}
+1 -1
View File
@@ -15,7 +15,7 @@ impl Extension for TickExtension {
let mut tick = 0;
loop {
for w in &widgets {
if let Some(on_tick) = w.get::<Handler>() {
if let Some(on_tick) = w.get::<Handler<TickEvent>>() {
on_tick.call(&w, &TickEvent(tick))
}
}