🚚 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

@ -142,7 +142,7 @@ impl Container {
} }
pub fn with_child(mut self, child: impl ComponentTrait) -> Self { pub fn with_child(mut self, child: impl ComponentTrait) -> Self {
self.children.add(ChildComponent::with(child)); self.children.add(Child::with(child));
self self
} }

View file

@ -73,7 +73,7 @@ impl Grid {
} }
pub fn with_item(mut self, item: grid::Item) -> Self { pub fn with_item(mut self, item: grid::Item) -> Self {
self.items.add(ChildComponent::with(item)); self.items.add(Child::with(item));
self self
} }

View file

@ -85,7 +85,7 @@ impl Item {
} }
pub fn with_child(mut self, child: impl ComponentTrait) -> Self { pub fn with_child(mut self, child: impl ComponentTrait) -> Self {
self.children.add(ChildComponent::with(child)); self.children.add(Child::with(child));
self self
} }

View file

@ -9,7 +9,7 @@ pub enum NavbarType {
#[default] #[default]
Default, Default,
Basic, Basic,
Offcanvas(TypedComponent<Offcanvas>), Offcanvas(Typed<Offcanvas>),
} }
#[rustfmt::skip] #[rustfmt::skip]
@ -52,7 +52,7 @@ impl ComponentTrait for Navbar {
let (output, id_content) = if let NavbarType::Offcanvas(oc) = self.navbar_type() { let (output, id_content) = if let NavbarType::Offcanvas(oc) = self.navbar_type() {
( (
oc.writable() oc.writable()
.alter_children(ChildOp::Prepend(ChildComponent::with(Html::with(elements)))) .alter_children(ChildOp::Prepend(Child::with(Html::with(elements))))
.render(cx), .render(cx),
cx.required_id::<Offcanvas>(oc.id()), cx.required_id::<Offcanvas>(oc.id()),
) )

View file

@ -53,7 +53,7 @@ impl Nav {
} }
pub fn with_item(mut self, item: navbar::Item) -> Self { pub fn with_item(mut self, item: navbar::Item) -> Self {
self.items.add(ChildComponent::with(item)); self.items.add(Child::with(item));
self self
} }

View file

@ -142,7 +142,7 @@ impl Offcanvas {
} }
pub fn with_child(mut self, child: impl ComponentTrait) -> Self { pub fn with_child(mut self, child: impl ComponentTrait) -> Self {
self.children.add(ChildComponent::with(child)); self.children.add(Child::with(child));
self self
} }

View file

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

View file

@ -5,14 +5,14 @@ use crate::{fn_builder, UniqueId};
use std::sync::{Arc, RwLock, RwLockWriteGuard}; use std::sync::{Arc, RwLock, RwLockWriteGuard};
#[derive(Clone)] #[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 { 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> { pub fn id(&self) -> Option<String> {
self.0.read().unwrap().id() self.0.read().unwrap().id()
@ -22,13 +22,13 @@ impl ChildComponent {
self.0.write().unwrap() self.0.write().unwrap()
} }
// ChildComponent RENDER. // Child RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// ChildComponent HELPERS. // Child HELPERS.
fn type_id(&self) -> UniqueId { fn type_id(&self) -> UniqueId {
self.0.read().unwrap().type_id() 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 { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone())
} }
} }
impl<C: ComponentTrait> TypedComponent<C> { impl<C: ComponentTrait> Typed<C> {
pub fn with(component: C) -> Self { 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> { pub fn id(&self) -> Option<String> {
self.0.read().unwrap().id() self.0.read().unwrap().id()
@ -64,50 +64,50 @@ impl<C: ComponentTrait> TypedComponent<C> {
self.0.write().unwrap() self.0.write().unwrap()
} }
// TypedComponent RENDER. // Typed RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// TypedComponent HELPERS. // Typed HELPERS.
fn to_child(&self) -> ChildComponent { fn to_child(&self) -> Child {
ChildComponent(self.0.clone()) Child(self.0.clone())
} }
} }
// ************************************************************************************************* // *************************************************************************************************
pub enum ChildOp { pub enum ChildOp {
Add(ChildComponent), Add(Child),
InsertAfterId(&'static str, ChildComponent), InsertAfterId(&'static str, Child),
InsertBeforeId(&'static str, ChildComponent), InsertBeforeId(&'static str, Child),
Prepend(ChildComponent), Prepend(Child),
RemoveById(&'static str), RemoveById(&'static str),
ReplaceById(&'static str, ChildComponent), ReplaceById(&'static str, Child),
Reset, Reset,
} }
pub enum TypedOp<C: ComponentTrait> { pub enum TypedOp<C: ComponentTrait> {
Add(TypedComponent<C>), Add(Typed<C>),
InsertAfterId(&'static str, TypedComponent<C>), InsertAfterId(&'static str, Typed<C>),
InsertBeforeId(&'static str, TypedComponent<C>), InsertBeforeId(&'static str, Typed<C>),
Prepend(TypedComponent<C>), Prepend(Typed<C>),
RemoveById(&'static str), RemoveById(&'static str),
ReplaceById(&'static str, TypedComponent<C>), ReplaceById(&'static str, Typed<C>),
Reset, Reset,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Children(Vec<ChildComponent>); pub struct Children(Vec<Child>);
impl Children { impl Children {
pub fn new() -> Self { pub fn new() -> Self {
Children::default() Children::default()
} }
pub fn with(child: ChildComponent) -> Self { pub fn with(child: Child) -> Self {
Children::default().with_child(ChildOp::Add(child)) Children::default().with_child(ChildOp::Add(child))
} }
@ -148,13 +148,13 @@ impl Children {
} }
#[inline] #[inline]
pub fn add(&mut self, child: ChildComponent) -> &mut Self { pub fn add(&mut self, child: Child) -> &mut Self {
self.0.push(child); self.0.push(child);
self self
} }
#[inline] #[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) { match self.0.iter().position(|c| c.child_id() == id) {
Some(index) => self.0.insert(index + 1, child), Some(index) => self.0.insert(index + 1, child),
_ => self.0.push(child), _ => self.0.push(child),
@ -163,7 +163,7 @@ impl Children {
} }
#[inline] #[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) { match self.0.iter().position(|c| c.child_id() == id) {
Some(index) => self.0.insert(index, child), Some(index) => self.0.insert(index, child),
_ => self.0.insert(0, child), _ => self.0.insert(0, child),
@ -172,7 +172,7 @@ impl Children {
} }
#[inline] #[inline]
fn prepend(&mut self, child: ChildComponent) -> &mut Self { fn prepend(&mut self, child: Child) -> &mut Self {
self.0.insert(0, child); self.0.insert(0, child);
self self
} }
@ -186,7 +186,7 @@ impl Children {
} }
#[inline] #[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 { for c in &mut self.0 {
if c.child_id() == id { if c.child_id() == id {
*c = child; *c = child;
@ -212,17 +212,17 @@ impl Children {
self.0.is_empty() 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(); let id = id.into();
self.0.iter().find(|c| c.child_id() == id) 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(); let id = id.into();
self.0.iter().filter(move |&c| c.child_id() == id) 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) 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::core::theme::ThemeRef;
use crate::{fn_builder, AutoDefault, UniqueId}; use crate::{fn_builder, AutoDefault, UniqueId};
@ -19,7 +19,7 @@ impl ChildrenInRegions {
ChildrenInRegions::default() 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)) ChildrenInRegions::default().with_in_region(region_id, ChildOp::Add(child))
} }
@ -54,7 +54,7 @@ pub enum InRegion {
} }
impl InRegion { impl InRegion {
pub fn add(&self, child: ChildComponent) -> &Self { pub fn add(&self, child: Child) -> &Self {
match self { match self {
InRegion::Content => { InRegion::Content => {
COMMON_REGIONS 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::fn_builder;
use crate::html::{html, Markup}; 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> { impl<C: ComponentTrait> Default for OptionComponent<C> {
fn default() -> Self { fn default() -> Self {
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
#[fn_builder] #[fn_builder]
pub fn with_value(mut self, component: Option<C>) -> Self { pub fn with_value(mut self, component: Option<C>) -> Self {
if let Some(component) = component { if let Some(component) = component {
self.0 = Some(TypedComponent::with(component)); self.0 = Some(Typed::with(component));
} else { } else {
self.0 = None; self.0 = None;
} }
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
// OptionComponent GETTERS. // OptionComponent GETTERS.
pub fn get(&self) -> Option<TypedComponent<C>> { pub fn get(&self) -> Option<Typed<C>> {
if let Some(value) = &self.0 { if let Some(value) = &self.0 {
return Some(value.clone()); return Some(value.clone());
} }

View file

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

View file

@ -27,7 +27,7 @@ impl PackageTrait for PageTopWebSite {
} }
fn init(&self) { fn init(&self) {
let nav = Navbar::new().with_nav(TypedOp::Add(TypedComponent::with( let nav = Navbar::new().with_nav(TypedOp::Add(Typed::with(
navbar::Nav::new() navbar::Nav::new()
.with_item(navbar::Item::link( .with_item(navbar::Item::link(
L10n::t("menu_home", &LOCALES_WEBSITE), L10n::t("menu_home", &LOCALES_WEBSITE),
@ -53,7 +53,7 @@ impl PackageTrait for PageTopWebSite {
)), )),
))); )));
InRegion::Content.add(ChildComponent::with(nav)); InRegion::Content.add(Child::with(nav));
/* /*
let branding = Branding::new() let branding = Branding::new()