diff --git a/src/component.rs b/src/component/context.rs similarity index 79% rename from src/component.rs rename to src/component/context.rs index 9928804..0a1a75d 100644 --- a/src/component.rs +++ b/src/component/context.rs @@ -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; -pub type EventHandler = Arc, &dyn Any) + Send + Sync>>; - -pub trait ComponentImpl: Send + Sync { - fn call(&self, cx: &Arc) -> View; -} - -pub struct Scope { - pub children: Mutex, Option)>>, - executor: Arc, -} +use super::{scope::Scope, Component, ComponentImpl}; pub struct Context { component: Mutex, @@ -189,47 +180,3 @@ impl Context { self.execute(crate::engine::commands::Stop) } } - -impl Scope { - pub fn new(executor: Arc) -> Arc { - Arc::new(Self { - children: Mutex::new(Vec::new()), - executor, - }) - } - - pub fn child( - self: &Arc, - child: F, - view_wrapper: Option, - ) { - let ctx = Context::new(child, self.executor.clone()); - - ctx.refresh(); - - self.children.lock().unwrap().push((ctx, view_wrapper)); - } - - pub fn view(self: &Arc, 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) -> View { - self.clone() - } -} - -impl ComponentImpl for F -where - F: Fn(&Arc) -> View + Send + Sync, -{ - fn call(&self, ctx: &Arc) -> View { - self(ctx) - } -} diff --git a/src/component/mod.rs b/src/component/mod.rs new file mode 100644 index 0000000..f2f83a9 --- /dev/null +++ b/src/component/mod.rs @@ -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; +pub type EventHandler = Arc, &dyn Any) + Send + Sync>>; + +pub trait ComponentImpl: Send + Sync { + fn call(&self, cx: &Arc) -> View; +} + +impl ComponentImpl for View { + fn call(&self, _: &Arc) -> View { + self.clone() + } +} + +impl ComponentImpl for F +where + F: Fn(&Arc) -> View + Send + Sync, +{ + fn call(&self, ctx: &Arc) -> View { + self(ctx) + } +} diff --git a/src/component/scope.rs b/src/component/scope.rs new file mode 100644 index 0000000..6cfe6e5 --- /dev/null +++ b/src/component/scope.rs @@ -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, Option)>>, + executor: Arc, +} + +impl Scope { + pub fn new(executor: Arc) -> Arc { + Arc::new(Self { + children: Mutex::new(Vec::new()), + executor, + }) + } + + pub fn child( + self: &Arc, + child: F, + view_wrapper: Option, + ) { + let ctx = Context::new(child, self.executor.clone()); + + ctx.refresh(); + + self.children.lock().unwrap().push((ctx, view_wrapper)); + } + + pub fn view(self: &Arc, view: View) { + let ctx = Context::new(view, self.executor.clone()); + + ctx.refresh(); + + self.children.lock().unwrap().push((ctx, None)); + } +} diff --git a/src/engine/benchmark.rs b/src/engine/benchmark.rs index a371bf5..1843050 100644 --- a/src/engine/benchmark.rs +++ b/src/engine/benchmark.rs @@ -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; diff --git a/src/engine/console.rs b/src/engine/console.rs index 7f82d54..6aa29ce 100644 --- a/src/engine/console.rs +++ b/src/engine/console.rs @@ -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, }; diff --git a/src/engine/mod.rs b/src/engine/mod.rs index c0f5525..b55f187 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -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 { fn run(&self, component: C) -> crate::Result; diff --git a/src/frontend.rs b/src/frontend.rs index 1bb3fa5..2689f75 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 4b3d469..93448c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::*; diff --git a/src/state.rs b/src/state.rs index 339c4b2..fae81c6 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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>);