🚚 Simplifica Child[Component] y Typed[Component]

This commit is contained in:
Manuel Cillero 2025-01-05 09:32:49 +01:00
parent db13a6ab8b
commit ee84c219cc
12 changed files with 57 additions and 59 deletions

View file

@ -7,5 +7,5 @@ pub use definition::{ComponentBase, ComponentTrait};
mod children;
pub use children::Children;
pub use children::{ChildComponent, ChildOp};
pub use children::{TypedComponent, TypedOp};
pub use children::{Child, ChildOp};
pub use children::{Typed, TypedOp};

View file

@ -5,14 +5,14 @@ use crate::{fn_builder, UniqueId};
use std::sync::{Arc, RwLock, RwLockWriteGuard};
#[derive(Clone)]
pub struct ChildComponent(Arc<RwLock<dyn ComponentTrait>>);
pub struct Child(Arc<RwLock<dyn ComponentTrait>>);
impl ChildComponent {
impl Child {
pub fn with(component: impl ComponentTrait) -> Self {
ChildComponent(Arc::new(RwLock::new(component)))
Child(Arc::new(RwLock::new(component)))
}
// ChildComponent GETTERS.
// Child GETTERS.
pub fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
@ -22,13 +22,13 @@ impl ChildComponent {
self.0.write().unwrap()
}
// ChildComponent RENDER.
// Child RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// ChildComponent HELPERS.
// Child HELPERS.
fn type_id(&self) -> UniqueId {
self.0.read().unwrap().type_id()
@ -41,20 +41,20 @@ impl ChildComponent {
// *************************************************************************************************
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
pub struct Typed<C: ComponentTrait>(Arc<RwLock<C>>);
impl<C: ComponentTrait> Clone for TypedComponent<C> {
impl<C: ComponentTrait> Clone for Typed<C> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<C: ComponentTrait> TypedComponent<C> {
impl<C: ComponentTrait> Typed<C> {
pub fn with(component: C) -> Self {
TypedComponent(Arc::new(RwLock::new(component)))
Typed(Arc::new(RwLock::new(component)))
}
// TypedComponent GETTERS.
// Typed GETTERS.
pub fn id(&self) -> Option<String> {
self.0.read().unwrap().id()
@ -64,50 +64,50 @@ impl<C: ComponentTrait> TypedComponent<C> {
self.0.write().unwrap()
}
// TypedComponent RENDER.
// Typed RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// TypedComponent HELPERS.
// Typed HELPERS.
fn to_child(&self) -> ChildComponent {
ChildComponent(self.0.clone())
fn to_child(&self) -> Child {
Child(self.0.clone())
}
}
// *************************************************************************************************
pub enum ChildOp {
Add(ChildComponent),
InsertAfterId(&'static str, ChildComponent),
InsertBeforeId(&'static str, ChildComponent),
Prepend(ChildComponent),
Add(Child),
InsertAfterId(&'static str, Child),
InsertBeforeId(&'static str, Child),
Prepend(Child),
RemoveById(&'static str),
ReplaceById(&'static str, ChildComponent),
ReplaceById(&'static str, Child),
Reset,
}
pub enum TypedOp<C: ComponentTrait> {
Add(TypedComponent<C>),
InsertAfterId(&'static str, TypedComponent<C>),
InsertBeforeId(&'static str, TypedComponent<C>),
Prepend(TypedComponent<C>),
Add(Typed<C>),
InsertAfterId(&'static str, Typed<C>),
InsertBeforeId(&'static str, Typed<C>),
Prepend(Typed<C>),
RemoveById(&'static str),
ReplaceById(&'static str, TypedComponent<C>),
ReplaceById(&'static str, Typed<C>),
Reset,
}
#[derive(Clone, Default)]
pub struct Children(Vec<ChildComponent>);
pub struct Children(Vec<Child>);
impl Children {
pub fn new() -> Self {
Children::default()
}
pub fn with(child: ChildComponent) -> Self {
pub fn with(child: Child) -> Self {
Children::default().with_child(ChildOp::Add(child))
}
@ -148,13 +148,13 @@ impl Children {
}
#[inline]
pub fn add(&mut self, child: ChildComponent) -> &mut Self {
pub fn add(&mut self, child: Child) -> &mut Self {
self.0.push(child);
self
}
#[inline]
fn insert_after_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
fn insert_after_id(&mut self, id: &str, child: Child) -> &mut Self {
match self.0.iter().position(|c| c.child_id() == id) {
Some(index) => self.0.insert(index + 1, child),
_ => self.0.push(child),
@ -163,7 +163,7 @@ impl Children {
}
#[inline]
fn insert_before_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
fn insert_before_id(&mut self, id: &str, child: Child) -> &mut Self {
match self.0.iter().position(|c| c.child_id() == id) {
Some(index) => self.0.insert(index, child),
_ => self.0.insert(0, child),
@ -172,7 +172,7 @@ impl Children {
}
#[inline]
fn prepend(&mut self, child: ChildComponent) -> &mut Self {
fn prepend(&mut self, child: Child) -> &mut Self {
self.0.insert(0, child);
self
}
@ -186,7 +186,7 @@ impl Children {
}
#[inline]
fn replace_by_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
fn replace_by_id(&mut self, id: &str, child: Child) -> &mut Self {
for c in &mut self.0 {
if c.child_id() == id {
*c = child;
@ -212,17 +212,17 @@ impl Children {
self.0.is_empty()
}
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ChildComponent> {
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&Child> {
let id = id.into();
self.0.iter().find(|c| c.child_id() == id)
}
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ChildComponent> {
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &Child> {
let id = id.into();
self.0.iter().filter(move |&c| c.child_id() == id)
}
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &ChildComponent> {
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &Child> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}

View file

@ -1,4 +1,4 @@
use crate::core::component::{ChildComponent, ChildOp, Children};
use crate::core::component::{Child, ChildOp, Children};
use crate::core::theme::ThemeRef;
use crate::{fn_builder, AutoDefault, UniqueId};
@ -19,7 +19,7 @@ impl ChildrenInRegions {
ChildrenInRegions::default()
}
pub fn with(region_id: &'static str, child: ChildComponent) -> Self {
pub fn with(region_id: &'static str, child: Child) -> Self {
ChildrenInRegions::default().with_in_region(region_id, ChildOp::Add(child))
}
@ -54,7 +54,7 @@ pub enum InRegion {
}
impl InRegion {
pub fn add(&self, child: ChildComponent) -> &Self {
pub fn add(&self, child: Child) -> &Self {
match self {
InRegion::Content => {
COMMON_REGIONS

View file

@ -1,8 +1,8 @@
use crate::core::component::{ComponentTrait, Context, TypedComponent};
use crate::core::component::{ComponentTrait, Context, Typed};
use crate::fn_builder;
use crate::html::{html, Markup};
pub struct OptionComponent<C: ComponentTrait>(Option<TypedComponent<C>>);
pub struct OptionComponent<C: ComponentTrait>(Option<Typed<C>>);
impl<C: ComponentTrait> Default for OptionComponent<C> {
fn default() -> Self {
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
#[fn_builder]
pub fn with_value(mut self, component: Option<C>) -> Self {
if let Some(component) = component {
self.0 = Some(TypedComponent::with(component));
self.0 = Some(Typed::with(component));
} else {
self.0 = None;
}
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
// OptionComponent GETTERS.
pub fn get(&self) -> Option<TypedComponent<C>> {
pub fn get(&self) -> Option<Typed<C>> {
if let Some(value) = &self.0 {
return Some(value.clone());
}

View file

@ -5,7 +5,7 @@ pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::core::component::{AssetsOp, Context};
use crate::core::component::{ChildComponent, ChildOp, ComponentTrait};
use crate::core::component::{Child, ChildOp, ComponentTrait};
use crate::fn_builder;
use crate::html::{html, Markup, DOCTYPE};
use crate::html::{ClassesOp, OptionClasses, OptionId, OptionTranslated};
@ -102,10 +102,8 @@ impl Page {
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.context.alter_in_region(
"region-content",
ChildOp::Add(ChildComponent::with(component)),
);
self.context
.alter_in_region("region-content", ChildOp::Add(Child::with(component)));
self
}
@ -115,7 +113,7 @@ impl Page {
component: impl ComponentTrait,
) -> Self {
self.context
.alter_in_region(region, ChildOp::Add(ChildComponent::with(component)));
.alter_in_region(region, ChildOp::Add(Child::with(component)));
self
}