🚧 Normaliza el uso de los bundles de componentes

This commit is contained in:
Manuel Cillero 2023-06-11 18:58:49 +02:00
parent b2a7c71c1f
commit 7a6cb4dbfa
9 changed files with 26 additions and 42 deletions

View file

@ -254,14 +254,12 @@ impl MegaMenu {
self self
} }
#[fn_builder] pub fn with_item(mut self, item: MegaMenuItem) -> Self {
pub fn alter_item(&mut self, item: MegaMenuItem) -> &mut Self { self.items.alter_bundle(BundleOp::Add, item);
self.items.add(item);
self self
} }
#[fn_builder] pub fn alter_items(&mut self, op: BundleOp, item: MegaMenuItem) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, item: MegaMenuItem) -> &mut Self {
self.items.alter_bundle(op, item); self.items.alter_bundle(op, item);
self self
} }

View file

@ -158,14 +158,12 @@ impl Container {
self self
} }
#[fn_builder] pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { self.components.alter_bundle(BundleOp::Add, component);
self.components.add(component);
self self
} }
#[fn_builder] pub fn alter_components(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component); self.components.alter_bundle(op, component);
self self
} }

View file

@ -124,14 +124,12 @@ impl Form {
self self
} }
#[fn_builder] pub fn with_element(mut self, element: impl ComponentTrait) -> Self {
pub fn alter_element(&mut self, element: impl ComponentTrait) -> &mut Self { self.elements.alter_bundle(BundleOp::Add, element);
self.elements.add(element);
self self
} }
#[fn_builder] pub fn alter_elements(&mut self, op: BundleOp, element: impl ComponentTrait) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, element: impl ComponentTrait) -> &mut Self {
self.elements.alter_bundle(op, element); self.elements.alter_bundle(op, element);
self self
} }

View file

@ -139,14 +139,12 @@ impl Column {
self self
} }
#[fn_builder] pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { self.components.alter_bundle(BundleOp::Add, component);
self.components.add(component);
self self
} }
#[fn_builder] pub fn alter_components(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component); self.components.alter_bundle(op, component);
self self
} }

View file

@ -86,14 +86,12 @@ impl Row {
self self
} }
#[fn_builder] pub fn with_column(mut self, column: grid::Column) -> Self {
pub fn alter_column(&mut self, column: grid::Column) -> &mut Self { self.columns.alter_bundle(BundleOp::Add, column);
self.columns.add(column);
self self
} }
#[fn_builder] pub fn alter_columns(&mut self, op: BundleOp, column: grid::Column) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, column: grid::Column) -> &mut Self {
self.columns.alter_bundle(op, column); self.columns.alter_bundle(op, column);
self self
} }

View file

@ -97,14 +97,12 @@ impl Paragraph {
self self
} }
#[fn_builder] pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { self.components.alter_bundle(BundleOp::Add, component);
self.components.add(component);
self self
} }
#[fn_builder] pub fn alter_components(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component); self.components.alter_bundle(op, component);
self self
} }

View file

@ -97,14 +97,12 @@ impl Block {
self self
} }
#[fn_builder] pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { self.components.alter_bundle(BundleOp::Add, component);
self.components.add(component);
self self
} }
#[fn_builder] pub fn alter_components(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
self.components.alter_bundle(op, component); self.components.alter_bundle(op, component);
self self
} }

View file

@ -1,4 +1,5 @@
use crate::core::component::{ComponentTrait, RenderContext}; use crate::core::component::{ComponentTrait, RenderContext};
use crate::fn_builder;
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -23,7 +24,7 @@ impl ComponentsBundle {
pub fn new_with(component: impl ComponentTrait) -> Self { pub fn new_with(component: impl ComponentTrait) -> Self {
let mut bundle = ComponentsBundle::new(); let mut bundle = ComponentsBundle::new();
bundle.add(component); bundle.alter_bundle(BundleOp::Add, component);
bundle bundle
} }
@ -43,10 +44,7 @@ impl ComponentsBundle {
// ComponentsBundle BUILDER. // ComponentsBundle BUILDER.
pub fn add(&mut self, component: impl ComponentTrait) -> &mut Self { #[fn_builder]
self.alter_bundle(BundleOp::Add, component)
}
pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self { pub fn alter_bundle(&mut self, op: BundleOp, component: impl ComponentTrait) -> &mut Self {
let arc = Arc::new(RwLock::new(component)); let arc = Arc::new(RwLock::new(component));
match op { match op {

View file

@ -1,4 +1,4 @@
use crate::core::component::{ComponentTrait, ComponentsBundle}; use crate::core::component::{BundleOp, ComponentTrait, ComponentsBundle};
use crate::LazyStatic; use crate::LazyStatic;
use std::collections::HashMap; use std::collections::HashMap;
@ -17,7 +17,7 @@ impl ComponentsRegions {
pub fn add_to(&mut self, region: &'static str, component: impl ComponentTrait) { pub fn add_to(&mut self, region: &'static str, component: impl ComponentTrait) {
if let Some(region) = self.0.get_mut(region) { if let Some(region) = self.0.get_mut(region) {
region.add(component); region.alter_bundle(BundleOp::Add, component);
} else { } else {
self.0.insert(region, ComponentsBundle::new_with(component)); self.0.insert(region, ComponentsBundle::new_with(component));
} }