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