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