Component params
This commit is contained in:
+25
-6
@@ -11,9 +11,13 @@ use crate::{
|
||||
View, ViewWrapper,
|
||||
};
|
||||
|
||||
pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>;
|
||||
pub type Component = Arc<dyn ComponentImpl>;
|
||||
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sync>>;
|
||||
|
||||
pub trait ComponentImpl: Send + Sync {
|
||||
fn call(&self, cx: &Arc<Context>) -> View;
|
||||
}
|
||||
|
||||
pub struct Scope {
|
||||
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||
executor: Arc<dyn CommandExecutor>,
|
||||
@@ -28,7 +32,7 @@ pub struct Context {
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(
|
||||
pub fn new<F: ComponentImpl + 'static>(
|
||||
component: F,
|
||||
executor: Arc<dyn CommandExecutor>,
|
||||
) -> Arc<Self> {
|
||||
@@ -48,7 +52,7 @@ impl Context {
|
||||
move || {
|
||||
s.event_handlers.lock().unwrap().clear();
|
||||
let c = s.component.lock().unwrap().clone();
|
||||
*s.view.lock().unwrap() = (c)(&s);
|
||||
*s.view.lock().unwrap() = c.call(&s);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -62,7 +66,7 @@ impl Context {
|
||||
move || {
|
||||
s.event_handlers.lock().unwrap().clear();
|
||||
let c = s.component.lock().unwrap().clone();
|
||||
*s.view.lock().unwrap() = (c)(&s);
|
||||
*s.view.lock().unwrap() = c.call(&s);
|
||||
*done.lock().unwrap() = true;
|
||||
}
|
||||
});
|
||||
@@ -194,7 +198,7 @@ impl Scope {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
pub fn child<F: ComponentImpl + 'static>(
|
||||
self: &Arc<Self>,
|
||||
child: F,
|
||||
view_wrapper: Option<ViewWrapper>,
|
||||
@@ -207,10 +211,25 @@ impl Scope {
|
||||
}
|
||||
|
||||
pub fn view(self: &Arc<Self>, view: View) {
|
||||
let ctx = Context::new(move |_| view.clone(), self.executor.clone());
|
||||
let ctx = Context::new(view, self.executor.clone());
|
||||
|
||||
ctx.refresh();
|
||||
|
||||
self.children.lock().unwrap().push((ctx, None));
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentImpl for View {
|
||||
fn call(&self, _: &Arc<Context>) -> View {
|
||||
self.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> ComponentImpl for F
|
||||
where
|
||||
F: Fn(&Arc<Context>) -> View + Send + Sync,
|
||||
{
|
||||
fn call(&self, ctx: &Arc<Context>) -> View {
|
||||
self(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@ use std::{io::stdout, sync::Arc, time::Instant};
|
||||
|
||||
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||
|
||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
||||
use crate::{
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
|
||||
use super::Engine;
|
||||
|
||||
@@ -24,10 +28,7 @@ impl<T: Engine> Benchmark<T> {
|
||||
}
|
||||
|
||||
impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> crate::Result<BenchmarkResult> {
|
||||
fn run<F: ComponentImpl + 'static>(&self, component: F) -> crate::Result<BenchmarkResult> {
|
||||
let mut times: Vec<u128> = Vec::new();
|
||||
let cx = self.init(component);
|
||||
|
||||
@@ -59,10 +60,7 @@ impl<T: Engine> Engine<BenchmarkResult> for Benchmark<T> {
|
||||
})
|
||||
}
|
||||
|
||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> Arc<Context> {
|
||||
fn init<F: ComponentImpl + 'static>(&self, component: F) -> Arc<Context> {
|
||||
self.0.init(component)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||
|
||||
use crate::{
|
||||
engine::{commands, CommandExecutor},
|
||||
prelude::Context,
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
@@ -79,10 +79,7 @@ impl Engine for Console {
|
||||
));
|
||||
}
|
||||
|
||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> Arc<Context> {
|
||||
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context> {
|
||||
let cx = Context::new(component, self.executor.clone());
|
||||
cx.refresh();
|
||||
|
||||
@@ -102,10 +99,7 @@ impl Engine for Console {
|
||||
self.executor.clone()
|
||||
}
|
||||
|
||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> crate::Result<()> {
|
||||
fn run<F: ComponentImpl + 'static>(&self, component: F) -> crate::Result<()> {
|
||||
let cx = self.init(component);
|
||||
|
||||
while self.executor.is_running() {
|
||||
|
||||
+7
-10
@@ -7,18 +7,15 @@ pub use console::*;
|
||||
|
||||
use std::{any::Any, sync::Arc};
|
||||
|
||||
use crate::{prelude::Context, render::Area, DrawContext, View};
|
||||
use crate::{
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
|
||||
pub trait Engine<Output = ()> {
|
||||
fn run<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> crate::Result<Output>;
|
||||
|
||||
fn init<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||
&self,
|
||||
component: F,
|
||||
) -> Arc<Context>;
|
||||
fn run<C: ComponentImpl + 'static>(&self, component: C) -> crate::Result<Output>;
|
||||
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context>;
|
||||
fn render(&self, cx: &Arc<Context>);
|
||||
fn render_delay(&self) {
|
||||
std::thread::sleep(std::time::Duration::from_millis(16))
|
||||
|
||||
Reference in New Issue
Block a user