Refactored component
This commit is contained in:
@@ -5,23 +5,14 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
component::EventHandler,
|
||||
engine::{Command, CommandExecutor},
|
||||
render::DrawContext,
|
||||
state::{use_effect, HookDependency},
|
||||
View, ViewWrapper,
|
||||
View,
|
||||
};
|
||||
|
||||
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>,
|
||||
}
|
||||
use super::{scope::Scope, Component, ComponentImpl};
|
||||
|
||||
pub struct Context {
|
||||
component: Mutex<Component>,
|
||||
@@ -189,47 +180,3 @@ impl Context {
|
||||
self.execute(crate::engine::commands::Stop)
|
||||
}
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
children: Mutex::new(Vec::new()),
|
||||
executor,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn child<F: ComponentImpl + 'static>(
|
||||
self: &Arc<Self>,
|
||||
child: F,
|
||||
view_wrapper: Option<ViewWrapper>,
|
||||
) {
|
||||
let ctx = Context::new(child, self.executor.clone());
|
||||
|
||||
ctx.refresh();
|
||||
|
||||
self.children.lock().unwrap().push((ctx, view_wrapper));
|
||||
}
|
||||
|
||||
pub fn view(self: &Arc<Self>, view: View) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
pub mod context;
|
||||
pub mod scope;
|
||||
|
||||
use std::{
|
||||
any::Any,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::View;
|
||||
|
||||
use context::Context;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::{engine::CommandExecutor, View, ViewWrapper};
|
||||
|
||||
use super::{context::Context, ComponentImpl};
|
||||
|
||||
pub struct Scope {
|
||||
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||
executor: Arc<dyn CommandExecutor>,
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
children: Mutex::new(Vec::new()),
|
||||
executor,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn child<F: ComponentImpl + 'static>(
|
||||
self: &Arc<Self>,
|
||||
child: F,
|
||||
view_wrapper: Option<ViewWrapper>,
|
||||
) {
|
||||
let ctx = Context::new(child, self.executor.clone());
|
||||
|
||||
ctx.refresh();
|
||||
|
||||
self.children.lock().unwrap().push((ctx, view_wrapper));
|
||||
}
|
||||
|
||||
pub fn view(self: &Arc<Self>, view: View) {
|
||||
let ctx = Context::new(view, self.executor.clone());
|
||||
|
||||
ctx.refresh();
|
||||
|
||||
self.children.lock().unwrap().push((ctx, None));
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,8 @@ use std::{io::stdout, sync::Arc, time::Instant};
|
||||
|
||||
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||
|
||||
use crate::{
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
use crate::component::{context::Context, ComponentImpl};
|
||||
use crate::{render::Area, DrawContext, View};
|
||||
|
||||
use super::Engine;
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ use std::{
|
||||
|
||||
use crossterm::{cursor::MoveTo, execute, terminal::Clear};
|
||||
|
||||
use crate::component::{context::Context, ComponentImpl};
|
||||
use crate::{
|
||||
engine::{commands, CommandExecutor},
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
|
||||
+2
-5
@@ -7,11 +7,8 @@ pub use console::*;
|
||||
|
||||
use std::{any::Any, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
prelude::{ComponentImpl, Context},
|
||||
render::Area,
|
||||
DrawContext, View,
|
||||
};
|
||||
use crate::component::{context::Context, ComponentImpl};
|
||||
use crate::{render::Area, DrawContext, View};
|
||||
|
||||
pub trait Engine<Output = ()> {
|
||||
fn run<C: ComponentImpl + 'static>(&self, component: C) -> crate::Result<Output>;
|
||||
|
||||
+2
-6
@@ -1,11 +1,7 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::{
|
||||
component::{Context, Scope},
|
||||
render::Point,
|
||||
state::HookDependency,
|
||||
View,
|
||||
};
|
||||
use crate::component::{context::Context, scope::Scope};
|
||||
use crate::{render::Point, state::HookDependency, View};
|
||||
|
||||
pub trait ToRsx {
|
||||
fn to_rsx(&self) -> Rsx;
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ pub mod render;
|
||||
pub mod state;
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::component::*;
|
||||
pub use crate::component::{context::*, scope::*, *};
|
||||
pub use crate::engine::*;
|
||||
pub use crate::frontend::*;
|
||||
pub use crate::render::*;
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ use std::{
|
||||
sync::{Arc, Mutex, MutexGuard},
|
||||
};
|
||||
|
||||
use crate::prelude::Context;
|
||||
use crate::component::context::Context;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct HookEffect(Arc<Mutex<dyn FnMut() + Send + Sync>>);
|
||||
|
||||
Reference in New Issue
Block a user