Working event handling

This commit is contained in:
2025-11-23 10:37:20 +01:00
parent 22d02e7d1a
commit 8740bbee89
3 changed files with 69 additions and 6 deletions
+20 -2
View File
@@ -2,13 +2,24 @@ use std::sync::Arc;
use osui::prelude::*; use osui::prelude::*;
pub struct KeyPress;
impl Event for KeyPress {
fn as_any(&self) -> &dyn std::any::Any {
self
}
}
fn main() { fn main() {
let mut cx = Context::new(app); let cx = Context::new(app);
cx.refresh(); cx.refresh();
std::thread::spawn({ std::thread::spawn({
let cx = cx.clone(); let cx = cx.clone();
move || {} move || loop {
crossterm::event::read().unwrap();
cx.event(&KeyPress);
}
}); });
loop { loop {
@@ -20,5 +31,12 @@ fn main() {
fn app(cx: &Arc<Context>) -> Vec<Node> { fn app(cx: &Arc<Context>) -> Vec<Node> {
let count = use_state(0); let count = use_state(0);
cx.on_event({
let count = count.clone();
move |_cx, _event: &KeyPress| {
*count.get() += 1;
}
});
vec![Node::String(Arc::new(move || format!("{count}")))] vec![Node::String(Arc::new(move || format!("{count}")))]
} }
+48 -3
View File
@@ -1,19 +1,38 @@
use std::sync::{Arc, Mutex}; use std::{
any::{Any, TypeId},
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::Node; use crate::Node;
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node>>; pub trait Event {
fn as_any(&self) -> &dyn Any;
}
impl<'a> dyn Event + 'a {
pub fn get<T: Event + 'static>(&self) -> Option<&T> {
self.as_any().downcast_ref()
}
}
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node> + Send + Sync>;
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Event) + Send + Sync>>;
pub struct Context { pub struct Context {
component: Mutex<Component>, component: Mutex<Component>,
nodes: Mutex<Vec<Node>>, nodes: Mutex<Vec<Node>>,
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
} }
impl Context { impl Context {
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + 'static>(component: F) -> Arc<Self> { pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + Send + Sync + 'static>(
component: F,
) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
component: Mutex::new(Arc::new(component)), component: Mutex::new(Arc::new(component)),
nodes: Mutex::new(Vec::new()), nodes: Mutex::new(Vec::new()),
event_handlers: Mutex::new(HashMap::new()),
}) })
} }
@@ -25,4 +44,30 @@ impl Context {
pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> { pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> {
self.nodes.lock().unwrap().clone() self.nodes.lock().unwrap().clone()
} }
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Context>, &T) + Send + Sync + 'static>(
self: &Arc<Context>,
handler: F,
) {
self.event_handlers
.lock()
.unwrap()
.entry(TypeId::of::<T>())
.or_insert_with(|| Vec::new())
.push(Arc::new(Mutex::new(
move |ctx: &Arc<Context>, event: &dyn Event| {
if let Some(e) = event.get::<T>() {
(handler)(ctx, e);
}
},
)));
}
pub fn event<E: Event + 'static>(self: &Arc<Context>, event: &E) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
for i in v {
(i.lock().unwrap())(&self.clone(), event);
}
}
}
} }
+1 -1
View File
@@ -11,7 +11,7 @@ pub mod prelude {
#[derive(Clone)] #[derive(Clone)]
pub enum Node { pub enum Node {
String(Arc<dyn Fn() -> String>), String(Arc<dyn Fn() -> String + Send + Sync>),
Component(component::Component), Component(component::Component),
} }