View
This commit is contained in:
+5
-14
@@ -24,24 +24,13 @@ fn main() {
|
||||
});
|
||||
|
||||
loop {
|
||||
println!("{:?}", cx.get_nodes());
|
||||
cx.get_view()(&mut DrawContext {});
|
||||
std::thread::sleep(std::time::Duration::from_millis(16));
|
||||
}
|
||||
}
|
||||
|
||||
fn app(cx: &Arc<Context>) -> Vec<Node> {
|
||||
fn app(cx: &Arc<Context>) -> View {
|
||||
let count = use_state(0);
|
||||
let count2 = use_state(0);
|
||||
|
||||
use_effect(
|
||||
{
|
||||
let count2 = count2.clone();
|
||||
move || {
|
||||
*count2.get() += 1;
|
||||
}
|
||||
},
|
||||
&[&count],
|
||||
);
|
||||
|
||||
cx.on_event({
|
||||
let count = count.clone();
|
||||
@@ -52,5 +41,7 @@ fn app(cx: &Arc<Context>) -> Vec<Node> {
|
||||
}
|
||||
});
|
||||
|
||||
vec![Node::String(Arc::new(move || format!("{count} {count2}")))]
|
||||
Arc::new(move |_| {
|
||||
println!("Count: {count}");
|
||||
})
|
||||
}
|
||||
|
||||
+8
-10
@@ -4,7 +4,7 @@ use std::{
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::Node;
|
||||
use crate::View;
|
||||
|
||||
pub trait Event {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
@@ -16,33 +16,31 @@ impl<'a> dyn Event + 'a {
|
||||
}
|
||||
}
|
||||
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> Vec<Node> + Send + Sync>;
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>;
|
||||
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Event) + Send + Sync>>;
|
||||
|
||||
pub struct Context {
|
||||
component: Mutex<Component>,
|
||||
nodes: Mutex<Vec<Node>>,
|
||||
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||
view: Mutex<View>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new<F: Fn(&Arc<Context>) -> Vec<Node> + Send + Sync + 'static>(
|
||||
component: F,
|
||||
) -> Arc<Self> {
|
||||
pub fn new<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(component: F) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
component: Mutex::new(Arc::new(component)),
|
||||
nodes: Mutex::new(Vec::new()),
|
||||
event_handlers: Mutex::new(HashMap::new()),
|
||||
view: Mutex::new(Arc::new(|_| {})),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn refresh(self: &Arc<Context>) {
|
||||
let c = self.component.lock().unwrap().clone();
|
||||
*self.nodes.lock().unwrap() = (c)(self);
|
||||
*self.view.lock().unwrap() = (c)(self);
|
||||
}
|
||||
|
||||
pub fn get_nodes(self: &Arc<Context>) -> Vec<Node> {
|
||||
self.nodes.lock().unwrap().clone()
|
||||
pub fn get_view(self: &Arc<Context>) -> View {
|
||||
self.view.lock().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Context>, &T) + Send + Sync + 'static>(
|
||||
|
||||
+3
-18
@@ -6,24 +6,9 @@ pub mod state;
|
||||
pub mod prelude {
|
||||
pub use crate::component::*;
|
||||
pub use crate::state::*;
|
||||
pub use crate::Node;
|
||||
pub use crate::{DrawContext, View};
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Node {
|
||||
String(Arc<dyn Fn() -> String + Send + Sync>),
|
||||
Component(component::Component),
|
||||
}
|
||||
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
|
||||
|
||||
impl std::fmt::Debug for Node {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
match self {
|
||||
Self::Component(_) => format!("Component"),
|
||||
Self::String(v) => format!("String({})", v()),
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
pub struct DrawContext {}
|
||||
|
||||
Reference in New Issue
Block a user