From c65ac74a6ffaefd6cfff09db4a68e8349900219b Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 7 Mar 2024 21:27:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20types=20for=20Mix?= =?UTF-8?q?ed=20and=20Typed=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/pagetop-admin/src/summary.rs | 10 ++-- src/base/component/block.rs | 10 ++-- src/base/component/flex/container.rs | 6 +-- src/base/component/flex/item.rs | 14 +++-- src/base/component/form/form_main.rs | 10 ++-- src/base/component/menu/element.rs | 4 +- src/base/component/menu/group.rs | 6 +-- src/base/component/menu/item.rs | 6 +-- src/base/component/menu/megamenu.rs | 6 +-- src/base/component/menu/menu_main.rs | 6 +-- src/base/component/menu/submenu.rs | 6 +-- src/base/component/paragraph.rs | 20 +++---- src/base/component/wrapper.rs | 20 +++---- src/base/package/welcome.rs | 22 ++++---- src/core/component.rs | 8 ++- .../component/{arc_one.rs => arc_mixed.rs} | 54 +++++++++---------- src/core/component/arc_typed.rs | 50 ++++++++--------- src/core/theme/regions.rs | 32 ++++++----- src/html/opt_component.rs | 8 +-- src/response/page.rs | 7 ++- 20 files changed, 151 insertions(+), 154 deletions(-) rename src/core/component/{arc_one.rs => arc_mixed.rs} (69%) diff --git a/packages/pagetop-admin/src/summary.rs b/packages/pagetop-admin/src/summary.rs index 882a003a..70dd5088 100644 --- a/packages/pagetop-admin/src/summary.rs +++ b/packages/pagetop-admin/src/summary.rs @@ -156,13 +156,9 @@ pub async fn summary(request: HttpRequest) -> ResultPage { .with_component_in("top-menu", side_menu) .with_component( flex::Container::new() - .add_item(flex::Item::new().add_component(Html::with(html! { - p { "Columna 1"} - }))) - .add_item(flex::Item::new().add_component(top_menu)) - .add_item(flex::Item::new().add_component(Html::with(html! { - p { "Columna 3"} - }))), + .add_item(flex::Item::with(Html::with(html! { p { "Columna 1"} }))) + .add_item(flex::Item::with(top_menu)) + .add_item(flex::Item::with(Html::with(html! { p { "Columna 3"} }))), ) .with_template("admin") .render() diff --git a/src/base/component/block.rs b/src/base/component/block.rs index bc0d4359..6067c18f 100644 --- a/src/base/component/block.rs +++ b/src/base/component/block.rs @@ -8,7 +8,7 @@ pub struct Block { renderable: Renderable, classes : OptionClasses, title : OptionTranslated, - stuff : MixedComponents, + mixed : MixedComponents, } impl ComponentTrait for Block { @@ -78,13 +78,13 @@ impl Block { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: OneOp) -> &mut Self { - self.stuff.alter_value(op); + pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { + self.mixed.alter_value(op); self } @@ -95,6 +95,6 @@ impl Block { } pub fn components(&self) -> &MixedComponents { - &self.stuff + &self.mixed } } diff --git a/src/base/component/flex/container.rs b/src/base/component/flex/container.rs index 727f7186..6518a067 100644 --- a/src/base/component/flex/container.rs +++ b/src/base/component/flex/container.rs @@ -7,7 +7,7 @@ pub struct Container { weight : Weight, renderable : Renderable, classes : OptionClasses, - items : VectorComponents, + items : TypedComponents, direction : flex::Direction, wrap_align : flex::WrapAlign, content_justify: flex::ContentJustify, @@ -84,7 +84,7 @@ impl Container { #[rustfmt::skip] pub fn add_item(mut self, item: flex::Item) -> Self { - self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); + self.items.alter_value(TypedOp::Add(OneComponent::with(item))); self } @@ -126,7 +126,7 @@ impl Container { // Container GETTERS. - pub fn items(&self) -> &VectorComponents { + pub fn items(&self) -> &TypedComponents { &self.items } diff --git a/src/base/component/flex/item.rs b/src/base/component/flex/item.rs index 2996769f..d4b4e165 100644 --- a/src/base/component/flex/item.rs +++ b/src/base/component/flex/item.rs @@ -12,7 +12,7 @@ pub struct Item { item_size : flex::ItemSize, item_offset : flex::ItemOffset, item_align : flex::ItemAlign, - stuff : MixedComponents, + mixed : MixedComponents, } impl ComponentTrait for Item { @@ -63,6 +63,10 @@ impl ComponentTrait for Item { } impl Item { + pub fn with(component: impl ComponentTrait) -> Self { + Item::default().add_component(component) + } + // Item BUILDER. #[fn_builder] @@ -115,13 +119,13 @@ impl Item { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: OneOp) -> &mut Self { - self.stuff.alter_value(op); + pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { + self.mixed.alter_value(op); self } @@ -148,6 +152,6 @@ impl Item { } pub fn components(&self) -> &MixedComponents { - &self.stuff + &self.mixed } } diff --git a/src/base/component/form/form_main.rs b/src/base/component/form/form_main.rs index edab5f66..af8beb8f 100644 --- a/src/base/component/form/form_main.rs +++ b/src/base/component/form/form_main.rs @@ -17,7 +17,7 @@ pub struct Form { action : OptionString, charset : OptionString, method : FormMethod, - stuff : MixedComponents, + mixed : MixedComponents, } impl ComponentTrait for Form { @@ -99,13 +99,13 @@ impl Form { #[rustfmt::skip] pub fn add_element(mut self, element: impl ComponentTrait) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(element))); + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(element))); self } #[fn_builder] - pub fn alter_elements(&mut self, op: OneOp) -> &mut Self { - self.stuff.alter_value(op); + pub fn alter_elements(&mut self, op: MixedOp) -> &mut Self { + self.mixed.alter_value(op); self } @@ -124,6 +124,6 @@ impl Form { } pub fn elements(&self) -> &MixedComponents { - &self.stuff + &self.mixed } } diff --git a/src/base/component/menu/element.rs b/src/base/component/menu/element.rs index a6a7d731..3f887733 100644 --- a/src/base/component/menu/element.rs +++ b/src/base/component/menu/element.rs @@ -2,8 +2,8 @@ use crate::prelude::*; use super::Submenu; -type Content = TypedComponent; -type SubmenuItems = TypedComponent; +type Content = OneComponent; +type SubmenuItems = OneComponent; #[derive(AutoDefault)] pub enum ElementType { diff --git a/src/base/component/menu/group.rs b/src/base/component/menu/group.rs index 8298629f..d083d602 100644 --- a/src/base/component/menu/group.rs +++ b/src/base/component/menu/group.rs @@ -8,7 +8,7 @@ pub struct Group { id : OptionId, weight : Weight, renderable: Renderable, - elements : VectorComponents, + elements : TypedComponents, } impl ComponentTrait for Group { @@ -60,7 +60,7 @@ impl Group { #[rustfmt::skip] pub fn add_element(mut self, element: Element) -> Self { - self.elements.alter_value(TypedOp::Add(TypedComponent::with(element))); + self.elements.alter_value(TypedOp::Add(OneComponent::with(element))); self } @@ -72,7 +72,7 @@ impl Group { // Group GETTERS. - pub fn elements(&self) -> &VectorComponents { + pub fn elements(&self) -> &TypedComponents { &self.elements } } diff --git a/src/base/component/menu/item.rs b/src/base/component/menu/item.rs index bf11bf9a..ccd39875 100644 --- a/src/base/component/menu/item.rs +++ b/src/base/component/menu/item.rs @@ -3,9 +3,9 @@ use crate::prelude::*; use super::{Megamenu, Submenu}; type Label = L10n; -type Content = TypedComponent; -type SubmenuItems = TypedComponent; -type MegamenuGroups = TypedComponent; +type Content = OneComponent; +type SubmenuItems = OneComponent; +type MegamenuGroups = OneComponent; #[derive(AutoDefault)] pub enum ItemType { diff --git a/src/base/component/menu/megamenu.rs b/src/base/component/menu/megamenu.rs index 8a186bbb..43962953 100644 --- a/src/base/component/menu/megamenu.rs +++ b/src/base/component/menu/megamenu.rs @@ -8,7 +8,7 @@ pub struct Megamenu { id : OptionId, weight : Weight, renderable: Renderable, - groups : VectorComponents, + groups : TypedComponents, } impl ComponentTrait for Megamenu { @@ -60,7 +60,7 @@ impl Megamenu { #[rustfmt::skip] pub fn add_group(mut self, group: Group) -> Self { - self.groups.alter_value(TypedOp::Add(TypedComponent::with(group))); + self.groups.alter_value(TypedOp::Add(OneComponent::with(group))); self } @@ -72,7 +72,7 @@ impl Megamenu { // Megamenu GETTERS. - pub fn groups(&self) -> &VectorComponents { + pub fn groups(&self) -> &TypedComponents { &self.groups } } diff --git a/src/base/component/menu/menu_main.rs b/src/base/component/menu/menu_main.rs index 7faa9b19..03883595 100644 --- a/src/base/component/menu/menu_main.rs +++ b/src/base/component/menu/menu_main.rs @@ -8,7 +8,7 @@ pub struct Menu { id : OptionId, weight : Weight, renderable: Renderable, - items : VectorComponents, + items : TypedComponents, } impl ComponentTrait for Menu { @@ -88,7 +88,7 @@ impl Menu { #[rustfmt::skip] pub fn add_item(mut self, item: Item) -> Self { - self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); + self.items.alter_value(TypedOp::Add(OneComponent::with(item))); self } @@ -100,7 +100,7 @@ impl Menu { // Menu GETTERS. - pub fn items(&self) -> &VectorComponents { + pub fn items(&self) -> &TypedComponents { &self.items } } diff --git a/src/base/component/menu/submenu.rs b/src/base/component/menu/submenu.rs index 2e9bdfcd..268d7746 100644 --- a/src/base/component/menu/submenu.rs +++ b/src/base/component/menu/submenu.rs @@ -9,7 +9,7 @@ pub struct Submenu { weight : Weight, renderable: Renderable, title : OptionTranslated, - items : VectorComponents, + items : TypedComponents, } impl ComponentTrait for Submenu { @@ -72,7 +72,7 @@ impl Submenu { #[rustfmt::skip] pub fn add_item(mut self, item: Item) -> Self { - self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); + self.items.alter_value(TypedOp::Add(OneComponent::with(item))); self } @@ -88,7 +88,7 @@ impl Submenu { &self.title } - pub fn items(&self) -> &VectorComponents { + pub fn items(&self) -> &TypedComponents { &self.items } } diff --git a/src/base/component/paragraph.rs b/src/base/component/paragraph.rs index 5ef28c23..322257f6 100644 --- a/src/base/component/paragraph.rs +++ b/src/base/component/paragraph.rs @@ -8,7 +8,7 @@ pub struct Paragraph { renderable: Renderable, classes : OptionClasses, font_size : FontSize, - stuff : MixedComponents, + mixed : MixedComponents, } impl ComponentTrait for Paragraph { @@ -49,8 +49,8 @@ impl Paragraph { Paragraph::default().add_component(component) } - pub fn translated(l10n: L10n) -> Self { - Paragraph::default().add_translated(l10n) + pub fn fluent(l10n: L10n) -> Self { + Paragraph::default().add_component(Fluent::with(l10n)) } // Paragraph BUILDER. @@ -81,19 +81,13 @@ impl Paragraph { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); - self - } - - #[rustfmt::skip] - pub fn add_translated(mut self, l10n: L10n) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(Translate::with(l10n)))); + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: OneOp) -> &mut Self { - self.stuff.alter_value(op); + pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { + self.mixed.alter_value(op); self } @@ -104,6 +98,6 @@ impl Paragraph { } pub fn components(&self) -> &MixedComponents { - &self.stuff + &self.mixed } } diff --git a/src/base/component/wrapper.rs b/src/base/component/wrapper.rs index 9bfefa06..86c5d07a 100644 --- a/src/base/component/wrapper.rs +++ b/src/base/component/wrapper.rs @@ -13,12 +13,12 @@ pub enum WrapperType { #[rustfmt::skip] #[derive(AutoDefault, ComponentClasses)] pub struct Wrapper { - id : OptionId, - weight : Weight, - renderable : Renderable, - classes : OptionClasses, - wrapper_type : WrapperType, - stuff : MixedComponents, + id : OptionId, + weight : Weight, + renderable : Renderable, + classes : OptionClasses, + wrapper_type: WrapperType, + mixed : MixedComponents, } impl ComponentTrait for Wrapper { @@ -126,13 +126,13 @@ impl Wrapper { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); + self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: OneOp) -> &mut Self { - self.stuff.alter_value(op); + pub fn alter_components(&mut self, op: MixedOp) -> &mut Self { + self.mixed.alter_value(op); self } @@ -143,6 +143,6 @@ impl Wrapper { } pub fn components(&self) -> &MixedComponents { - &self.stuff + &self.mixed } } diff --git a/src/base/package/welcome.rs b/src/base/package/welcome.rs index df9a30ca..7bb7f470 100644 --- a/src/base/package/welcome.rs +++ b/src/base/package/welcome.rs @@ -61,7 +61,7 @@ fn hello_world() -> Wrapper { Heading::h1(L10n::l("welcome_title")).with_size(HeadingSize::Medium), ) .add_component( - Paragraph::translated(L10n::l("welcome_intro").with_arg( + Paragraph::fluent(L10n::l("welcome_intro").with_arg( "app", format!( "{}", @@ -70,7 +70,7 @@ fn hello_world() -> Wrapper { )) .with_font_size(FontSize::Medium), ) - .add_component(Paragraph::translated(L10n::l("welcome_powered").with_arg( + .add_component(Paragraph::fluent(L10n::l("welcome_powered").with_arg( "pagetop", format!( "{}", @@ -119,10 +119,8 @@ fn welcome() -> Wrapper { )) .with_size(HeadingSize::Subtitle), ) - .add_component( - Paragraph::translated(L10n::l("welcome_text1")).with_font_size(FontSize::Medium), - ) - .add_component(Paragraph::translated(L10n::l("welcome_text2"))) + .add_component(Paragraph::fluent(L10n::l("welcome_text1")).with_font_size(FontSize::Medium)) + .add_component(Paragraph::fluent(L10n::l("welcome_text2"))) } fn about_pagetop() -> Wrapper { @@ -142,11 +140,11 @@ fn about_pagetop() -> Wrapper { .with_classes(ClassesOp::Add, "pagetop-col-text") .add_component(Heading::h2(L10n::l("welcome_pagetop_title"))) .add_component( - Paragraph::translated(L10n::l("welcome_pagetop_text1")) + Paragraph::fluent(L10n::l("welcome_pagetop_text1")) .with_font_size(FontSize::Medium), ) - .add_component(Paragraph::translated(L10n::l("welcome_pagetop_text2"))) - .add_component(Paragraph::translated(L10n::l("welcome_pagetop_text3"))), + .add_component(Paragraph::fluent(L10n::l("welcome_pagetop_text2"))) + .add_component(Paragraph::fluent(L10n::l("welcome_pagetop_text3"))), ), ) } @@ -163,7 +161,7 @@ fn promo_pagetop() -> Wrapper { .with_size(flex::ItemSize::Percent50) .add_component(Heading::h2(L10n::l("welcome_promo_title"))) .add_component( - Paragraph::translated(L10n::l("welcome_promo_text1").with_arg( + Paragraph::fluent(L10n::l("welcome_promo_text1").with_arg( "pagetop", format!( "{}", @@ -199,10 +197,10 @@ fn reporting_issues() -> Wrapper { .with_size(flex::ItemSize::Percent50) .add_component(Heading::h2(L10n::l("welcome_issues_title"))) .add_component( - Paragraph::translated(L10n::l("welcome_issues_text1")) + Paragraph::fluent(L10n::l("welcome_issues_text1")) .with_font_size(FontSize::Medium), ) - .add_component(Paragraph::translated( + .add_component(Paragraph::fluent( L10n::l("welcome_issues_text2").with_arg( "app", format!( diff --git a/src/core/component.rs b/src/core/component.rs index 637199f7..cf88dfbc 100644 --- a/src/core/component.rs +++ b/src/core/component.rs @@ -11,10 +11,8 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen mod classes; pub use classes::{ComponentClasses, ComponentClassesOp}; -mod arc_one; -pub use arc_one::MixedComponents; -pub use arc_one::{OneComponent, OneOp}; +mod arc_mixed; +pub use arc_mixed::{AnyComponent, MixedComponents, MixedOp}; mod arc_typed; -pub use arc_typed::VectorComponents; -pub use arc_typed::{TypedComponent, TypedOp}; +pub use arc_typed::{OneComponent, TypedComponents, TypedOp}; diff --git a/src/core/component/arc_one.rs b/src/core/component/arc_mixed.rs similarity index 69% rename from src/core/component/arc_one.rs rename to src/core/component/arc_mixed.rs index 0c078b32..1cfd4ce6 100644 --- a/src/core/component/arc_one.rs +++ b/src/core/component/arc_mixed.rs @@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; #[derive(Clone)] -pub struct OneComponent(Arc>); +pub struct AnyComponent(Arc>); -impl OneComponent { +impl AnyComponent { pub fn with(component: impl ComponentTrait) -> Self { - OneComponent(Arc::new(RwLock::new(component))) + AnyComponent(Arc::new(RwLock::new(component))) } - // OneComponent BUILDER. + // AnyComponent BUILDER. pub fn set(&mut self, component: impl ComponentTrait) { self.0 = Arc::new(RwLock::new(component)); } - // OneComponent GETTERS. + // AnyComponent GETTERS. pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> { self.0.read().unwrap() @@ -28,13 +28,13 @@ impl OneComponent { self.0.write().unwrap() } - // OneComponent RENDER. + // AnyComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } - // OneComponent HELPERS. + // AnyComponent HELPERS. fn type_id(&self) -> TypeId { self.0.read().unwrap().type_id() @@ -51,22 +51,22 @@ impl OneComponent { // ************************************************************************************************* -pub enum OneOp { - Add(OneComponent), - AddAfterId(&'static str, OneComponent), - AddBeforeId(&'static str, OneComponent), - Prepend(OneComponent), +pub enum MixedOp { + Add(AnyComponent), + InsertAfterId(&'static str, AnyComponent), + InsertBeforeId(&'static str, AnyComponent), + Prepend(AnyComponent), RemoveById(&'static str), - ReplaceById(&'static str, OneComponent), + ReplaceById(&'static str, AnyComponent), Reset, } #[derive(Clone, Default)] -pub struct MixedComponents(Vec); +pub struct MixedComponents(Vec); impl MixedComponents { - pub fn new(any: OneComponent) -> Self { - MixedComponents::default().with_value(OneOp::Add(any)) + pub fn new(any: AnyComponent) -> Self { + MixedComponents::default().with_value(MixedOp::Add(any)) } pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self { @@ -80,24 +80,24 @@ impl MixedComponents { // MixedComponents BUILDER. #[fn_builder] - pub fn alter_value(&mut self, op: OneOp) -> &mut Self { + pub fn alter_value(&mut self, op: MixedOp) -> &mut Self { match op { - OneOp::Add(any) => self.0.push(any), - OneOp::AddAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) { + MixedOp::Add(any) => self.0.push(any), + MixedOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index + 1, any), _ => self.0.push(any), }, - OneOp::AddBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) { + MixedOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index, any), _ => self.0.insert(0, any), }, - OneOp::Prepend(any) => self.0.insert(0, any), - OneOp::RemoveById(id) => { + MixedOp::Prepend(any) => self.0.insert(0, any), + MixedOp::RemoveById(id) => { if let Some(index) = self.0.iter().position(|c| c.id() == id) { self.0.remove(index); } } - OneOp::ReplaceById(id, any) => { + MixedOp::ReplaceById(id, any) => { for c in self.0.iter_mut() { if c.id() == id { *c = any; @@ -105,7 +105,7 @@ impl MixedComponents { } } } - OneOp::Reset => self.0.clear(), + MixedOp::Reset => self.0.clear(), } self } @@ -116,17 +116,17 @@ impl MixedComponents { self.0.is_empty() } - pub fn get_by_id(&self, id: impl Into) -> Option<&OneComponent> { + pub fn get_by_id(&self, id: impl Into) -> Option<&AnyComponent> { let id = id.into(); self.0.iter().find(|c| c.id() == id) } - pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { + pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { let id = id.into(); self.0.iter().filter(move |&c| c.id() == id) } - pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator { + pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator { self.0.iter().filter(move |&c| c.type_id() == type_id) } diff --git a/src/core/component/arc_typed.rs b/src/core/component/arc_typed.rs index df772730..1b91ad5d 100644 --- a/src/core/component/arc_typed.rs +++ b/src/core/component/arc_typed.rs @@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; -pub struct TypedComponent(Arc>); +pub struct OneComponent(Arc>); -impl Clone for TypedComponent { +impl Clone for OneComponent { fn clone(&self) -> Self { Self(self.0.clone()) } } -impl TypedComponent { +impl OneComponent { pub fn with(component: C) -> Self { - TypedComponent(Arc::new(RwLock::new(component))) + OneComponent(Arc::new(RwLock::new(component))) } - // TypedComponent BUILDER. + // OneComponent BUILDER. pub fn set(&mut self, component: C) { self.0 = Arc::new(RwLock::new(component)); } - // TypedComponent GETTERS. + // OneComponent GETTERS. pub fn get(&self) -> RwLockReadGuard<'_, C> { self.0.read().unwrap() @@ -33,13 +33,13 @@ impl TypedComponent { self.0.write().unwrap() } - // TypedComponent RENDER. + // OneComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } - // TypedComponent HELPERS. + // OneComponent HELPERS. fn type_id(&self) -> TypeId { self.0.read().unwrap().type_id() @@ -57,34 +57,34 @@ impl TypedComponent { // ************************************************************************************************* pub enum TypedOp { - Add(TypedComponent), - AddAfterId(&'static str, TypedComponent), - AddBeforeId(&'static str, TypedComponent), - Prepend(TypedComponent), + Add(OneComponent), + InsertAfterId(&'static str, OneComponent), + InsertBeforeId(&'static str, OneComponent), + Prepend(OneComponent), RemoveById(&'static str), - ReplaceById(&'static str, TypedComponent), + ReplaceById(&'static str, OneComponent), Reset, } #[derive(Clone, Default)] -pub struct VectorComponents(Vec>); +pub struct TypedComponents(Vec>); -impl VectorComponents { - pub fn new(one: TypedComponent) -> Self { - VectorComponents::default().with_value(TypedOp::Add(one)) +impl TypedComponents { + pub fn new(one: OneComponent) -> Self { + TypedComponents::default().with_value(TypedOp::Add(one)) } - // VectorComponents BUILDER. + // TypedComponents BUILDER. #[fn_builder] pub fn alter_value(&mut self, op: TypedOp) -> &mut Self { match op { TypedOp::Add(one) => self.0.push(one), - TypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) { + TypedOp::InsertAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index + 1, one), _ => self.0.push(one), }, - TypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) { + TypedOp::InsertBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index, one), _ => self.0.insert(0, one), }, @@ -107,27 +107,27 @@ impl VectorComponents { self } - // VectorComponents GETTERS. + // TypedComponents GETTERS. pub fn is_empty(&self) -> bool { self.0.is_empty() } - pub fn get_by_id(&self, id: impl Into) -> Option<&TypedComponent> { + pub fn get_by_id(&self, id: impl Into) -> Option<&OneComponent> { let id = id.into(); self.0.iter().find(|&c| c.id() == id) } - pub fn iter_by_id(&self, id: impl Into) -> impl Iterator> { + pub fn iter_by_id(&self, id: impl Into) -> impl Iterator> { let id = id.into(); self.0.iter().filter(move |&c| c.id() == id) } - pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator> { + pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator> { self.0.iter().filter(move |&c| c.type_id() == type_id) } - // VectorComponents RENDER. + // TypedComponents RENDER. pub fn render(&self, cx: &mut Context) -> Markup { let mut components = self.0.clone(); diff --git a/src/core/theme/regions.rs b/src/core/theme/regions.rs index 92fdaafc..618727a0 100644 --- a/src/core/theme/regions.rs +++ b/src/core/theme/regions.rs @@ -1,4 +1,4 @@ -use crate::core::component::{MixedComponents, OneComponent, OneOp}; +use crate::core::component::{AnyComponent, MixedComponents, MixedOp}; use crate::core::theme::ThemeRef; use crate::{AutoDefault, LazyStatic, TypeId}; @@ -15,17 +15,25 @@ static COMMON_REGIONS: LazyStatic> = pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>); impl ComponentsInRegions { - pub fn new(region: &'static str, one: OneComponent) -> Self { + pub fn new(region: &'static str, any: AnyComponent) -> Self { let mut regions = ComponentsInRegions::default(); - regions.add_in(region, one); + regions.add_in(region, any); regions } - pub fn add_in(&mut self, region: &'static str, one: OneComponent) { - if let Some(region) = self.0.get_mut(region) { - region.alter_value(OneOp::Add(one)); + pub fn add(&mut self, any: AnyComponent) { + if let Some(region) = self.0.get_mut("content") { + region.alter_value(MixedOp::Add(any)); } else { - self.0.insert(region, MixedComponents::new(one)); + self.0.insert("content", MixedComponents::new(any)); + } + } + + pub fn add_in(&mut self, region: &'static str, any: AnyComponent) { + if let Some(region) = self.0.get_mut(region) { + region.alter_value(MixedOp::Add(any)); + } else { + self.0.insert(region, MixedComponents::new(any)); } } @@ -46,20 +54,20 @@ pub enum InRegion { } impl InRegion { - pub fn add(&self, one: OneComponent) -> &Self { + pub fn add(&self, any: AnyComponent) -> &Self { match self { InRegion::Content => { - COMMON_REGIONS.write().unwrap().add_in("content", one); + COMMON_REGIONS.write().unwrap().add(any); } InRegion::Named(name) => { - COMMON_REGIONS.write().unwrap().add_in(name, one); + COMMON_REGIONS.write().unwrap().add_in(name, any); } InRegion::OfTheme(region, theme) => { let mut regions = THEME_REGIONS.write().unwrap(); if let Some(r) = regions.get_mut(&theme.type_id()) { - r.add_in(region, one); + r.add_in(region, any); } else { - regions.insert(theme.type_id(), ComponentsInRegions::new(region, one)); + regions.insert(theme.type_id(), ComponentsInRegions::new(region, any)); } } } diff --git a/src/html/opt_component.rs b/src/html/opt_component.rs index 9bb4523c..ef5a6c83 100644 --- a/src/html/opt_component.rs +++ b/src/html/opt_component.rs @@ -1,8 +1,8 @@ -use crate::core::component::{ComponentTrait, Context, TypedComponent}; +use crate::core::component::{ComponentTrait, Context, OneComponent}; use crate::fn_builder; use crate::html::{html, Markup}; -pub struct OptionComponent(Option>); +pub struct OptionComponent(Option>); impl Default for OptionComponent { fn default() -> Self { @@ -20,7 +20,7 @@ impl OptionComponent { #[fn_builder] pub fn alter_value(&mut self, component: Option) -> &mut Self { if let Some(component) = component { - self.0 = Some(TypedComponent::with(component)); + self.0 = Some(OneComponent::with(component)); } else { self.0 = None; } @@ -29,7 +29,7 @@ impl OptionComponent { // OptionComponent GETTERS. - pub fn get(&self) -> Option> { + pub fn get(&self) -> Option> { if let Some(value) = &self.0 { return Some(value.clone()); } diff --git a/src/response/page.rs b/src/response/page.rs index 6bfd8c0c..1c1dc8f6 100644 --- a/src/response/page.rs +++ b/src/response/page.rs @@ -4,7 +4,7 @@ pub use error::ErrorPage; pub use actix_web::Result as ResultPage; use crate::base::action; -use crate::core::component::{ComponentTrait, MixedComponents, OneComponent}; +use crate::core::component::{AnyComponent, ComponentTrait, MixedComponents}; use crate::core::component::{Context, ContextOp}; use crate::core::theme::ComponentsInRegions; use crate::fn_builder; @@ -106,8 +106,7 @@ impl Page { #[fn_builder] pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { - self.regions - .add_in("content", OneComponent::with(component)); + self.regions.add(AnyComponent::with(component)); self } @@ -117,7 +116,7 @@ impl Page { region: &'static str, component: impl ComponentTrait, ) -> &mut Self { - self.regions.add_in(region, OneComponent::with(component)); + self.regions.add_in(region, AnyComponent::with(component)); self }