♻️ Rename smart pointers components for clarity

This commit is contained in:
Manuel Cillero 2024-03-04 22:21:40 +01:00
parent 9a5618ef4b
commit d80a594cf5
18 changed files with 151 additions and 155 deletions

View file

@ -8,7 +8,7 @@ pub struct Block {
renderable: Renderable, renderable: Renderable,
classes : OptionClasses, classes : OptionClasses,
title : OptionTranslated, title : OptionTranslated,
stuff : AnyComponents, stuff : MixedComponents,
} }
impl ComponentTrait for Block { impl ComponentTrait for Block {
@ -78,12 +78,12 @@ impl Block {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self { pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op); self.stuff.alter_value(op);
self self
} }
@ -94,7 +94,7 @@ impl Block {
&self.title &self.title
} }
pub fn components(&self) -> &AnyComponents { pub fn components(&self) -> &MixedComponents {
&self.stuff &self.stuff
} }
} }

View file

@ -7,7 +7,7 @@ pub struct Container {
weight : Weight, weight : Weight,
renderable : Renderable, renderable : Renderable,
classes : OptionClasses, classes : OptionClasses,
items : TypedComponents<flex::Item>, items : VectorComponents<flex::Item>,
direction : flex::Direction, direction : flex::Direction,
wrap_align : flex::WrapAlign, wrap_align : flex::WrapAlign,
content_justify: flex::ContentJustify, content_justify: flex::ContentJustify,
@ -84,12 +84,12 @@ impl Container {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_item(mut self, item: flex::Item) -> Self { pub fn add_item(mut self, item: flex::Item) -> Self {
self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_items(&mut self, op: ArcTypedOp<flex::Item>) -> &mut Self { pub fn alter_items(&mut self, op: TypedOp<flex::Item>) -> &mut Self {
self.items.alter_value(op); self.items.alter_value(op);
self self
} }
@ -126,7 +126,7 @@ impl Container {
// Container GETTERS. // Container GETTERS.
pub fn items(&self) -> &TypedComponents<flex::Item> { pub fn items(&self) -> &VectorComponents<flex::Item> {
&self.items &self.items
} }

View file

@ -12,7 +12,7 @@ pub struct Item {
item_size : flex::ItemSize, item_size : flex::ItemSize,
item_offset : flex::ItemOffset, item_offset : flex::ItemOffset,
item_align : flex::ItemAlign, item_align : flex::ItemAlign,
stuff : AnyComponents, stuff : MixedComponents,
} }
impl ComponentTrait for Item { impl ComponentTrait for Item {
@ -115,12 +115,12 @@ impl Item {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self { pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op); self.stuff.alter_value(op);
self self
} }
@ -147,7 +147,7 @@ impl Item {
&self.item_align &self.item_align
} }
pub fn components(&self) -> &AnyComponents { pub fn components(&self) -> &MixedComponents {
&self.stuff &self.stuff
} }
} }

View file

@ -17,7 +17,7 @@ pub struct Form {
action : OptionString, action : OptionString,
charset : OptionString, charset : OptionString,
method : FormMethod, method : FormMethod,
stuff : AnyComponents, stuff : MixedComponents,
} }
impl ComponentTrait for Form { impl ComponentTrait for Form {
@ -99,12 +99,12 @@ impl Form {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_element(mut self, element: impl ComponentTrait) -> Self { pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(element))); self.stuff.alter_value(OneOp::Add(OneComponent::with(element)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_elements(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_elements(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op); self.stuff.alter_value(op);
self self
} }
@ -123,7 +123,7 @@ impl Form {
&self.method &self.method
} }
pub fn elements(&self) -> &AnyComponents { pub fn elements(&self) -> &MixedComponents {
&self.stuff &self.stuff
} }
} }

View file

@ -2,8 +2,8 @@ use crate::prelude::*;
use super::Submenu; use super::Submenu;
type Content = ArcTypedComponent<Html>; type Content = TypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>; type SubmenuItems = TypedComponent<Submenu>;
#[derive(AutoDefault)] #[derive(AutoDefault)]
pub enum ElementType { pub enum ElementType {
@ -52,14 +52,14 @@ impl ComponentTrait for Element {
impl Element { impl Element {
pub fn html(content: Html) -> Self { pub fn html(content: Html) -> Self {
Element { Element {
element_type: ElementType::Html(Content::new(content)), element_type: ElementType::Html(Content::with(content)),
..Default::default() ..Default::default()
} }
} }
pub fn submenu(submenu: Submenu) -> Self { pub fn submenu(submenu: Submenu) -> Self {
Element { Element {
element_type: ElementType::Submenu(SubmenuItems::new(submenu)), element_type: ElementType::Submenu(SubmenuItems::with(submenu)),
..Default::default() ..Default::default()
} }
} }

View file

@ -8,7 +8,7 @@ pub struct Group {
id : OptionId, id : OptionId,
weight : Weight, weight : Weight,
renderable: Renderable, renderable: Renderable,
elements : TypedComponents<Element>, elements : VectorComponents<Element>,
} }
impl ComponentTrait for Group { impl ComponentTrait for Group {
@ -60,19 +60,19 @@ impl Group {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_element(mut self, element: Element) -> Self { pub fn add_element(mut self, element: Element) -> Self {
self.elements.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(element))); self.elements.alter_value(TypedOp::Add(TypedComponent::with(element)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_elements(&mut self, op: ArcTypedOp<Element>) -> &mut Self { pub fn alter_elements(&mut self, op: TypedOp<Element>) -> &mut Self {
self.elements.alter_value(op); self.elements.alter_value(op);
self self
} }
// Group GETTERS. // Group GETTERS.
pub fn elements(&self) -> &TypedComponents<Element> { pub fn elements(&self) -> &VectorComponents<Element> {
&self.elements &self.elements
} }
} }

View file

@ -3,9 +3,9 @@ use crate::prelude::*;
use super::{Megamenu, Submenu}; use super::{Megamenu, Submenu};
type Label = L10n; type Label = L10n;
type Content = ArcTypedComponent<Html>; type Content = TypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>; type SubmenuItems = TypedComponent<Submenu>;
type MegamenuGroups = ArcTypedComponent<Megamenu>; type MegamenuGroups = TypedComponent<Megamenu>;
#[derive(AutoDefault)] #[derive(AutoDefault)]
pub enum ItemType { pub enum ItemType {
@ -135,21 +135,21 @@ impl Item {
pub fn html(content: Html) -> Self { pub fn html(content: Html) -> Self {
Item { Item {
item_type: ItemType::Html(Content::new(content)), item_type: ItemType::Html(Content::with(content)),
..Default::default() ..Default::default()
} }
} }
pub fn submenu(label: L10n, submenu: Submenu) -> Self { pub fn submenu(label: L10n, submenu: Submenu) -> Self {
Item { Item {
item_type: ItemType::Submenu(label, SubmenuItems::new(submenu)), item_type: ItemType::Submenu(label, SubmenuItems::with(submenu)),
..Default::default() ..Default::default()
} }
} }
pub fn megamenu(label: L10n, megamenu: Megamenu) -> Self { pub fn megamenu(label: L10n, megamenu: Megamenu) -> Self {
Item { Item {
item_type: ItemType::Megamenu(label, MegamenuGroups::new(megamenu)), item_type: ItemType::Megamenu(label, MegamenuGroups::with(megamenu)),
..Default::default() ..Default::default()
} }
} }

View file

@ -8,7 +8,7 @@ pub struct Megamenu {
id : OptionId, id : OptionId,
weight : Weight, weight : Weight,
renderable: Renderable, renderable: Renderable,
groups : TypedComponents<Group>, groups : VectorComponents<Group>,
} }
impl ComponentTrait for Megamenu { impl ComponentTrait for Megamenu {
@ -60,19 +60,19 @@ impl Megamenu {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_group(mut self, group: Group) -> Self { pub fn add_group(mut self, group: Group) -> Self {
self.groups.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(group))); self.groups.alter_value(TypedOp::Add(TypedComponent::with(group)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_groups(&mut self, op: ArcTypedOp<Group>) -> &mut Self { pub fn alter_groups(&mut self, op: TypedOp<Group>) -> &mut Self {
self.groups.alter_value(op); self.groups.alter_value(op);
self self
} }
// Megamenu GETTERS. // Megamenu GETTERS.
pub fn groups(&self) -> &TypedComponents<Group> { pub fn groups(&self) -> &VectorComponents<Group> {
&self.groups &self.groups
} }
} }

View file

@ -8,7 +8,7 @@ pub struct Menu {
id : OptionId, id : OptionId,
weight : Weight, weight : Weight,
renderable: Renderable, renderable: Renderable,
items : TypedComponents<Item>, items : VectorComponents<Item>,
} }
impl ComponentTrait for Menu { impl ComponentTrait for Menu {
@ -88,19 +88,19 @@ impl Menu {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self { pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_items(&mut self, op: ArcTypedOp<Item>) -> &mut Self { pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_value(op); self.items.alter_value(op);
self self
} }
// Menu GETTERS. // Menu GETTERS.
pub fn items(&self) -> &TypedComponents<Item> { pub fn items(&self) -> &VectorComponents<Item> {
&self.items &self.items
} }
} }

View file

@ -9,7 +9,7 @@ pub struct Submenu {
weight : Weight, weight : Weight,
renderable: Renderable, renderable: Renderable,
title : OptionTranslated, title : OptionTranslated,
items : TypedComponents<Item>, items : VectorComponents<Item>,
} }
impl ComponentTrait for Submenu { impl ComponentTrait for Submenu {
@ -72,12 +72,12 @@ impl Submenu {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self { pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); self.items.alter_value(TypedOp::Add(TypedComponent::with(item)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_items(&mut self, op: ArcTypedOp<Item>) -> &mut Self { pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_value(op); self.items.alter_value(op);
self self
} }
@ -88,7 +88,7 @@ impl Submenu {
&self.title &self.title
} }
pub fn items(&self) -> &TypedComponents<Item> { pub fn items(&self) -> &VectorComponents<Item> {
&self.items &self.items
} }
} }

View file

@ -8,7 +8,7 @@ pub struct Paragraph {
renderable: Renderable, renderable: Renderable,
classes : OptionClasses, classes : OptionClasses,
font_size : FontSize, font_size : FontSize,
stuff : AnyComponents, stuff : MixedComponents,
} }
impl ComponentTrait for Paragraph { impl ComponentTrait for Paragraph {
@ -81,18 +81,18 @@ impl Paragraph {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self { pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self self
} }
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_translated(mut self, l10n: L10n) -> Self { pub fn add_translated(mut self, l10n: L10n) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(Translate::with(l10n)))); self.stuff.alter_value(OneOp::Add(OneComponent::with(Translate::with(l10n))));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op); self.stuff.alter_value(op);
self self
} }
@ -103,7 +103,7 @@ impl Paragraph {
&self.font_size &self.font_size
} }
pub fn components(&self) -> &AnyComponents { pub fn components(&self) -> &MixedComponents {
&self.stuff &self.stuff
} }
} }

View file

@ -18,7 +18,7 @@ pub struct Wrapper {
renderable : Renderable, renderable : Renderable,
classes : OptionClasses, classes : OptionClasses,
wrapper_type : WrapperType, wrapper_type : WrapperType,
stuff : AnyComponents, stuff : MixedComponents,
} }
impl ComponentTrait for Wrapper { impl ComponentTrait for Wrapper {
@ -126,12 +126,12 @@ impl Wrapper {
#[rustfmt::skip] #[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self { pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); self.stuff.alter_value(OneOp::Add(OneComponent::with(component)));
self self
} }
#[fn_builder] #[fn_builder]
pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_components(&mut self, op: OneOp) -> &mut Self {
self.stuff.alter_value(op); self.stuff.alter_value(op);
self self
} }
@ -142,7 +142,7 @@ impl Wrapper {
&self.wrapper_type &self.wrapper_type
} }
pub fn components(&self) -> &AnyComponents { pub fn components(&self) -> &MixedComponents {
&self.stuff &self.stuff
} }
} }

View file

@ -11,10 +11,10 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
mod classes; mod classes;
pub use classes::{ComponentClasses, ComponentClassesOp}; pub use classes::{ComponentClasses, ComponentClassesOp};
mod arc_any; mod arc_one;
pub use arc_any::AnyComponents; pub use arc_one::MixedComponents;
pub use arc_any::{ArcAnyComponent, ArcAnyOp}; pub use arc_one::{OneComponent, OneOp};
mod arc_typed; mod arc_typed;
pub use arc_typed::TypedComponents; pub use arc_typed::VectorComponents;
pub use arc_typed::{ArcTypedComponent, ArcTypedOp}; pub use arc_typed::{TypedComponent, TypedOp};

View file

@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
#[derive(Clone)] #[derive(Clone)]
pub struct ArcAnyComponent(Arc<RwLock<dyn ComponentTrait>>); pub struct OneComponent(Arc<RwLock<dyn ComponentTrait>>);
impl ArcAnyComponent { impl OneComponent {
pub fn new(component: impl ComponentTrait) -> Self { pub fn with(component: impl ComponentTrait) -> Self {
ArcAnyComponent(Arc::new(RwLock::new(component))) OneComponent(Arc::new(RwLock::new(component)))
} }
// ArcAnyComponent BUILDER. // OneComponent BUILDER.
pub fn set(&mut self, component: impl ComponentTrait) { pub fn set(&mut self, component: impl ComponentTrait) {
self.0 = Arc::new(RwLock::new(component)); self.0 = Arc::new(RwLock::new(component));
} }
// ArcAnyComponent GETTERS. // OneComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> { pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
self.0.read().unwrap() self.0.read().unwrap()
@ -28,13 +28,13 @@ impl ArcAnyComponent {
self.0.write().unwrap() self.0.write().unwrap()
} }
// ArcAnyComponent RENDER. // OneComponent 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)
} }
// ArcAnyComponent HELPERS. // OneComponent HELPERS.
fn type_id(&self) -> TypeId { fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id() self.0.read().unwrap().type_id()
@ -51,86 +51,86 @@ impl ArcAnyComponent {
// ************************************************************************************************* // *************************************************************************************************
pub enum ArcAnyOp { pub enum OneOp {
Add(ArcAnyComponent), Add(OneComponent),
AddAfterId(&'static str, ArcAnyComponent), AddAfterId(&'static str, OneComponent),
AddBeforeId(&'static str, ArcAnyComponent), AddBeforeId(&'static str, OneComponent),
Prepend(ArcAnyComponent), Prepend(OneComponent),
RemoveById(&'static str), RemoveById(&'static str),
ReplaceById(&'static str, ArcAnyComponent), ReplaceById(&'static str, OneComponent),
Reset, Reset,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct AnyComponents(Vec<ArcAnyComponent>); pub struct MixedComponents(Vec<OneComponent>);
impl AnyComponents { impl MixedComponents {
pub fn new(arc: ArcAnyComponent) -> Self { pub fn new(any: OneComponent) -> Self {
AnyComponents::default().with_value(ArcAnyOp::Add(arc)) MixedComponents::default().with_value(OneOp::Add(any))
} }
pub(crate) fn merge(mixes: &[Option<&AnyComponents>]) -> Self { pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
let mut opt = AnyComponents::default(); let mut opt = MixedComponents::default();
for m in mixes.iter().flatten() { for m in mixes.iter().flatten() {
opt.0.append(&mut m.0.clone()); opt.0.append(&mut m.0.clone());
} }
opt opt
} }
// AnyComponents BUILDER. // MixedComponents BUILDER.
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, op: ArcAnyOp) -> &mut Self { pub fn alter_value(&mut self, op: OneOp) -> &mut Self {
match op { match op {
ArcAnyOp::Add(arc) => self.0.push(arc), OneOp::Add(any) => self.0.push(any),
ArcAnyOp::AddAfterId(id, arc) => match self.0.iter().position(|c| c.id() == id) { OneOp::AddAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, arc), Some(index) => self.0.insert(index + 1, any),
_ => self.0.push(arc), _ => self.0.push(any),
}, },
ArcAnyOp::AddBeforeId(id, arc) => match self.0.iter().position(|c| c.id() == id) { OneOp::AddBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, arc), Some(index) => self.0.insert(index, any),
_ => self.0.insert(0, arc), _ => self.0.insert(0, any),
}, },
ArcAnyOp::Prepend(arc) => self.0.insert(0, arc), OneOp::Prepend(any) => self.0.insert(0, any),
ArcAnyOp::RemoveById(id) => { OneOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id() == id) { if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index); self.0.remove(index);
} }
} }
ArcAnyOp::ReplaceById(id, arc) => { OneOp::ReplaceById(id, any) => {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.id() == id { if c.id() == id {
*c = arc; *c = any;
break; break;
} }
} }
} }
ArcAnyOp::Reset => self.0.clear(), OneOp::Reset => self.0.clear(),
} }
self self
} }
// AnyComponents GETTERS. // MixedComponents GETTERS.
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0.is_empty() self.0.is_empty()
} }
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ArcAnyComponent> { pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent> {
let id = id.into(); let id = id.into();
self.0.iter().find(|c| c.id() == id) self.0.iter().find(|c| c.id() == id)
} }
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ArcAnyComponent> { pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &OneComponent> {
let id = id.into(); let id = id.into();
self.0.iter().filter(move |&c| c.id() == id) self.0.iter().filter(move |&c| c.id() == id)
} }
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &ArcAnyComponent> { pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &OneComponent> {
self.0.iter().filter(move |&c| c.type_id() == type_id) self.0.iter().filter(move |&c| c.type_id() == type_id)
} }
// AnyComponents RENDER. // MixedComponents RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();

View file

@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub struct ArcTypedComponent<C: ComponentTrait>(Arc<RwLock<C>>); pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
impl<C: ComponentTrait> Clone for ArcTypedComponent<C> { impl<C: ComponentTrait> Clone for TypedComponent<C> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone())
} }
} }
impl<C: ComponentTrait> ArcTypedComponent<C> { impl<C: ComponentTrait> TypedComponent<C> {
pub fn new(component: C) -> Self { pub fn with(component: C) -> Self {
ArcTypedComponent(Arc::new(RwLock::new(component))) TypedComponent(Arc::new(RwLock::new(component)))
} }
// ArcTypedComponent BUILDER. // TypedComponent BUILDER.
pub fn set(&mut self, component: C) { pub fn set(&mut self, component: C) {
self.0 = Arc::new(RwLock::new(component)); self.0 = Arc::new(RwLock::new(component));
} }
// ArcTypedComponent GETTERS. // TypedComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, C> { pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap() self.0.read().unwrap()
@ -33,13 +33,13 @@ impl<C: ComponentTrait> ArcTypedComponent<C> {
self.0.write().unwrap() self.0.write().unwrap()
} }
// ArcTypedComponent RENDER. // TypedComponent 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)
} }
// ArcTypedComponent HELPERS. // TypedComponent HELPERS.
fn type_id(&self) -> TypeId { fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id() self.0.read().unwrap().type_id()
@ -56,45 +56,45 @@ impl<C: ComponentTrait> ArcTypedComponent<C> {
// ************************************************************************************************* // *************************************************************************************************
pub enum ArcTypedOp<C: ComponentTrait> { pub enum TypedOp<C: ComponentTrait> {
Add(ArcTypedComponent<C>), Add(TypedComponent<C>),
AddAfterId(&'static str, ArcTypedComponent<C>), AddAfterId(&'static str, TypedComponent<C>),
AddBeforeId(&'static str, ArcTypedComponent<C>), AddBeforeId(&'static str, TypedComponent<C>),
Prepend(ArcTypedComponent<C>), Prepend(TypedComponent<C>),
RemoveById(&'static str), RemoveById(&'static str),
ReplaceById(&'static str, ArcTypedComponent<C>), ReplaceById(&'static str, TypedComponent<C>),
Reset, Reset,
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct TypedComponents<C: ComponentTrait>(Vec<ArcTypedComponent<C>>); pub struct VectorComponents<C: ComponentTrait>(Vec<TypedComponent<C>>);
impl<C: ComponentTrait + Default> TypedComponents<C> { impl<C: ComponentTrait + Default> VectorComponents<C> {
pub fn new(arc: ArcTypedComponent<C>) -> Self { pub fn new(one: TypedComponent<C>) -> Self {
TypedComponents::default().with_value(ArcTypedOp::Add(arc)) VectorComponents::default().with_value(TypedOp::Add(one))
} }
// TypedComponents BUILDER. // VectorComponents BUILDER.
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, op: ArcTypedOp<C>) -> &mut Self { pub fn alter_value(&mut self, op: TypedOp<C>) -> &mut Self {
match op { match op {
ArcTypedOp::Add(one) => self.0.push(one), TypedOp::Add(one) => self.0.push(one),
ArcTypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) { TypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, one), Some(index) => self.0.insert(index + 1, one),
_ => self.0.push(one), _ => self.0.push(one),
}, },
ArcTypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) { TypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, one), Some(index) => self.0.insert(index, one),
_ => self.0.insert(0, one), _ => self.0.insert(0, one),
}, },
ArcTypedOp::Prepend(one) => self.0.insert(0, one), TypedOp::Prepend(one) => self.0.insert(0, one),
ArcTypedOp::RemoveById(id) => { TypedOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id() == id) { if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index); self.0.remove(index);
} }
} }
ArcTypedOp::ReplaceById(id, one) => { TypedOp::ReplaceById(id, one) => {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.id() == id { if c.id() == id {
*c = one; *c = one;
@ -102,32 +102,32 @@ impl<C: ComponentTrait + Default> TypedComponents<C> {
} }
} }
} }
ArcTypedOp::Reset => self.0.clear(), TypedOp::Reset => self.0.clear(),
} }
self self
} }
// TypedComponents GETTERS. // VectorComponents GETTERS.
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0.is_empty() self.0.is_empty()
} }
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ArcTypedComponent<C>> { pub fn get_by_id(&self, id: impl Into<String>) -> Option<&TypedComponent<C>> {
let id = id.into(); let id = id.into();
self.0.iter().find(|&c| c.id() == id) self.0.iter().find(|&c| c.id() == id)
} }
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ArcTypedComponent<C>> { pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &TypedComponent<C>> {
let id = id.into(); let id = id.into();
self.0.iter().filter(move |&c| c.id() == id) self.0.iter().filter(move |&c| c.id() == id)
} }
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &ArcTypedComponent<C>> { pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &TypedComponent<C>> {
self.0.iter().filter(move |&c| c.type_id() == type_id) self.0.iter().filter(move |&c| c.type_id() == type_id)
} }
// TypedComponents RENDER. // VectorComponents RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone(); let mut components = self.0.clone();

View file

@ -1,4 +1,4 @@
use crate::core::component::{AnyComponents, ArcAnyComponent, ArcAnyOp}; use crate::core::component::{MixedComponents, OneComponent, OneOp};
use crate::core::theme::ThemeRef; use crate::core::theme::ThemeRef;
use crate::{AutoDefault, LazyStatic, TypeId}; use crate::{AutoDefault, LazyStatic, TypeId};
@ -12,29 +12,29 @@ static COMMON_REGIONS: LazyStatic<RwLock<ComponentsInRegions>> =
LazyStatic::new(|| RwLock::new(ComponentsInRegions::default())); LazyStatic::new(|| RwLock::new(ComponentsInRegions::default()));
#[derive(AutoDefault)] #[derive(AutoDefault)]
pub struct ComponentsInRegions(HashMap<&'static str, AnyComponents>); pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
impl ComponentsInRegions { impl ComponentsInRegions {
pub fn new(region: &'static str, arc: ArcAnyComponent) -> Self { pub fn new(region: &'static str, one: OneComponent) -> Self {
let mut regions = ComponentsInRegions::default(); let mut regions = ComponentsInRegions::default();
regions.add_component_in(region, arc); regions.add_in(region, one);
regions regions
} }
pub fn add_component_in(&mut self, region: &'static str, arc: ArcAnyComponent) { pub fn add_in(&mut self, region: &'static str, one: OneComponent) {
if let Some(region) = self.0.get_mut(region) { if let Some(region) = self.0.get_mut(region) {
region.alter_value(ArcAnyOp::Add(arc)); region.alter_value(OneOp::Add(one));
} else { } else {
self.0.insert(region, AnyComponents::new(arc)); self.0.insert(region, MixedComponents::new(one));
} }
} }
pub fn get_components(&self, theme: ThemeRef, region: &str) -> AnyComponents { pub fn get_components(&self, theme: ThemeRef, region: &str) -> MixedComponents {
let common = COMMON_REGIONS.read().unwrap(); let common = COMMON_REGIONS.read().unwrap();
if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) { if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) {
AnyComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)]) MixedComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)])
} else { } else {
AnyComponents::merge(&[common.0.get(region), self.0.get(region)]) MixedComponents::merge(&[common.0.get(region), self.0.get(region)])
} }
} }
} }
@ -46,23 +46,20 @@ pub enum InRegion {
} }
impl InRegion { impl InRegion {
pub fn add_component(&self, arc: ArcAnyComponent) -> &Self { pub fn add(&self, one: OneComponent) -> &Self {
match self { match self {
InRegion::Content => { InRegion::Content => {
COMMON_REGIONS COMMON_REGIONS.write().unwrap().add_in("content", one);
.write()
.unwrap()
.add_component_in("content", arc);
} }
InRegion::Named(name) => { InRegion::Named(name) => {
COMMON_REGIONS.write().unwrap().add_component_in(name, arc); COMMON_REGIONS.write().unwrap().add_in(name, one);
} }
InRegion::OfTheme(region, theme) => { InRegion::OfTheme(region, theme) => {
let mut regions = THEME_REGIONS.write().unwrap(); let mut regions = THEME_REGIONS.write().unwrap();
if let Some(r) = regions.get_mut(&theme.type_id()) { if let Some(r) = regions.get_mut(&theme.type_id()) {
r.add_component_in(region, arc); r.add_in(region, one);
} else { } else {
regions.insert(theme.type_id(), ComponentsInRegions::new(region, arc)); regions.insert(theme.type_id(), ComponentsInRegions::new(region, one));
} }
} }
} }

View file

@ -1,8 +1,8 @@
use crate::core::component::{ArcTypedComponent, ComponentTrait, Context}; use crate::core::component::{ComponentTrait, Context, TypedComponent};
use crate::fn_builder; use crate::fn_builder;
use crate::html::{html, Markup}; use crate::html::{html, Markup};
pub struct OptionComponent<C: ComponentTrait>(Option<ArcTypedComponent<C>>); pub struct OptionComponent<C: ComponentTrait>(Option<TypedComponent<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 alter_value(&mut self, component: Option<C>) -> &mut Self { pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
if let Some(component) = component { if let Some(component) = component {
self.0 = Some(ArcTypedComponent::new(component)); self.0 = Some(TypedComponent::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<ArcTypedComponent<C>> { pub fn get(&self) -> Option<TypedComponent<C>> {
if let Some(value) = &self.0 { if let Some(value) = &self.0 {
return Some(value.clone()); return Some(value.clone());
} }

View file

@ -4,7 +4,7 @@ pub use error::ErrorPage;
pub use actix_web::Result as ResultPage; pub use actix_web::Result as ResultPage;
use crate::base::action; use crate::base::action;
use crate::core::component::{AnyComponents, ArcAnyComponent, ComponentTrait}; use crate::core::component::{ComponentTrait, MixedComponents, OneComponent};
use crate::core::component::{Context, ContextOp}; use crate::core::component::{Context, ContextOp};
use crate::core::theme::ComponentsInRegions; use crate::core::theme::ComponentsInRegions;
use crate::fn_builder; use crate::fn_builder;
@ -107,7 +107,7 @@ impl Page {
#[fn_builder] #[fn_builder]
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self {
self.regions self.regions
.add_component_in("content", ArcAnyComponent::new(component)); .add_in("content", OneComponent::with(component));
self self
} }
@ -117,8 +117,7 @@ impl Page {
region: &'static str, region: &'static str,
component: impl ComponentTrait, component: impl ComponentTrait,
) -> &mut Self { ) -> &mut Self {
self.regions self.regions.add_in(region, OneComponent::with(component));
.add_component_in(region, ArcAnyComponent::new(component));
self self
} }
@ -166,7 +165,7 @@ impl Page {
&self.skip_to &self.skip_to
} }
pub fn components_in(&self, region: &str) -> AnyComponents { pub fn components_in(&self, region: &str) -> MixedComponents {
self.regions.get_components(self.context.theme(), region) self.regions.get_components(self.context.theme(), region)
} }