diff --git a/pagetop-minimal/src/component/block.rs b/pagetop-minimal/src/component/block.rs index 823bf2b3..7d19f588 100644 --- a/pagetop-minimal/src/component/block.rs +++ b/pagetop-minimal/src/component/block.rs @@ -12,7 +12,7 @@ pub struct Block { id : IdentifierValue, classes : Classes, title : AttributeValue, - content : PackComponents, + stuff : PackComponents, template : String, } @@ -94,12 +94,17 @@ impl Block { } pub fn with_component(mut self, component: impl ComponentTrait) -> Self { - self.content.alter(PackOp::Add, ComponentRef::to(component)); + self.stuff.alter(PackOp::Add, ComponentArc::new(component)); + self + } + + pub fn with_component_arc(mut self, arc: ComponentArc) -> Self { + self.stuff.alter(PackOp::Add, arc); self } pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { - self.content.alter(op, ComponentRef::to(component)); + self.stuff.alter(op, ComponentArc::new(component)); self } @@ -120,7 +125,7 @@ impl Block { } pub fn components(&self) -> &PackComponents { - &self.content + &self.stuff } pub fn template(&self) -> &str { diff --git a/pagetop-minimal/src/component/form_element/form.rs b/pagetop-minimal/src/component/form_element/form.rs index d3e2f2fb..358e162d 100644 --- a/pagetop-minimal/src/component/form_element/form.rs +++ b/pagetop-minimal/src/component/form_element/form.rs @@ -121,12 +121,17 @@ impl Form { } pub fn with_element(mut self, element: impl ComponentTrait) -> Self { - self.elements.alter(PackOp::Add, ComponentRef::to(element)); + self.elements.alter(PackOp::Add, ComponentArc::new(element)); + self + } + + pub fn with_element_arc(mut self, arc: ComponentArc) -> Self { + self.elements.alter(PackOp::Add, arc); self } pub fn alter_elements(&mut self, op: PackOp, element: impl ComponentTrait) -> &mut Self { - self.elements.alter(op, ComponentRef::to(element)); + self.elements.alter(op, ComponentArc::new(element)); self } diff --git a/pagetop-minimal/src/component/grid/column.rs b/pagetop-minimal/src/component/grid/column.rs index 151aa517..044260f1 100644 --- a/pagetop-minimal/src/component/grid/column.rs +++ b/pagetop-minimal/src/component/grid/column.rs @@ -44,7 +44,7 @@ pub struct Column { id : IdentifierValue, classes : Classes, size : ColumnSize, - content : PackComponents, + stuff : PackComponents, template : String, } @@ -136,12 +136,17 @@ impl Column { } pub fn with_component(mut self, component: impl ComponentTrait) -> Self { - self.content.alter(PackOp::Add, ComponentRef::to(component)); + self.stuff.alter(PackOp::Add, ComponentArc::new(component)); + self + } + + pub fn with_component_arc(mut self, arc: ComponentArc) -> Self { + self.stuff.alter(PackOp::Add, arc); self } pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { - self.content.alter(op, ComponentRef::to(component)); + self.stuff.alter(op, ComponentArc::new(component)); self } @@ -162,7 +167,7 @@ impl Column { } pub fn components(&self) -> &PackComponents { - &self.content + &self.stuff } pub fn template(&self) -> &str { diff --git a/pagetop-minimal/src/component/grid/row.rs b/pagetop-minimal/src/component/grid/row.rs index 91450629..923910dc 100644 --- a/pagetop-minimal/src/component/grid/row.rs +++ b/pagetop-minimal/src/component/grid/row.rs @@ -83,12 +83,12 @@ impl Row { } pub fn with_column(mut self, column: grid::Column) -> Self { - self.columns.alter(PackOp::Add, ComponentRef::to(column)); + self.columns.alter(PackOp::Add, ComponentArc::new(column)); self } pub fn alter_columns(&mut self, op: PackOp, column: grid::Column) -> &mut Self { - self.columns.alter(op, ComponentRef::to(column)); + self.columns.alter(op, ComponentArc::new(column)); self } diff --git a/pagetop-minimal/src/component/paragraph.rs b/pagetop-minimal/src/component/paragraph.rs index 09e85ddb..c6b3e508 100644 --- a/pagetop-minimal/src/component/paragraph.rs +++ b/pagetop-minimal/src/component/paragraph.rs @@ -20,7 +20,7 @@ pub struct Paragraph { renderable: Renderable, id : IdentifierValue, classes : Classes, - content : PackComponents, + stuff : PackComponents, display : ParagraphDisplay, template : String, } @@ -90,12 +90,17 @@ impl Paragraph { } pub fn with_component(mut self, component: impl ComponentTrait) -> Self { - self.content.alter(PackOp::Add, ComponentRef::to(component)); + self.stuff.alter(PackOp::Add, ComponentArc::new(component)); + self + } + + pub fn with_component_arc(mut self, arc: ComponentArc) -> Self { + self.stuff.alter(PackOp::Add, arc); self } pub fn alter_components(&mut self, op: PackOp, component: impl ComponentTrait) -> &mut Self { - self.content.alter(op, ComponentRef::to(component)); + self.stuff.alter(op, ComponentArc::new(component)); self } @@ -130,7 +135,7 @@ impl Paragraph { } pub fn components(&self) -> &PackComponents { - &self.content + &self.stuff } pub fn display(&self) -> &ParagraphDisplay { diff --git a/pagetop/src/core/component.rs b/pagetop/src/core/component.rs index a4573f1d..32d742c3 100644 --- a/pagetop/src/core/component.rs +++ b/pagetop/src/core/component.rs @@ -1,18 +1,21 @@ mod context; pub use context::{Context, ContextOp}; -pub type ContextualPath = fn(cx: &Context) -> &str; +pub type FnContextualPath = fn(cx: &Context) -> &str; mod definition; pub use definition::{component_mut, component_ref, ComponentBase, ComponentTrait}; +mod arc; +pub use arc::ComponentArc; + mod one; pub use one::OneComponent; mod pack; -pub use pack::{ComponentRef, PackComponents, PackOp}; +pub use pack::{PackComponents, PackOp}; mod renderable; -pub use renderable::{IsRenderable, Renderable}; +pub use renderable::{FnIsRenderable, Renderable}; pub mod html; pub mod l10n; @@ -23,7 +26,7 @@ macro_rules! actions_for_component { $crate::paste! { use $crate::prelude::*; - pub type [] = fn(component: &$Component, cx: &mut Context); + pub type [] = fn(component: &$Component, cx: &mut Context); // ************************************************************************************* // ACTION BEFORE PREPARE COMPONENT @@ -32,7 +35,7 @@ macro_rules! actions_for_component { $crate::new_handle!([] for Action); pub struct [] { - action: Option<[]>, + action: Option<[]>, weight: Weight, } @@ -55,7 +58,7 @@ macro_rules! actions_for_component { impl [] { #[allow(dead_code)] - pub fn with_action(mut self, action: []) -> Self { + pub fn with_action(mut self, action: []) -> Self { self.action = Some(action); self } @@ -91,7 +94,7 @@ macro_rules! actions_for_component { $crate::new_handle!([] for Action); pub struct [] { - action: Option<[]>, + action: Option<[]>, weight: Weight, } @@ -114,7 +117,7 @@ macro_rules! actions_for_component { impl [] { #[allow(dead_code)] - pub fn with_action(mut self, action: []) -> Self { + pub fn with_action(mut self, action: []) -> Self { self.action = Some(action); self } diff --git a/pagetop/src/core/component/arc.rs b/pagetop/src/core/component/arc.rs new file mode 100644 index 00000000..93e1193c --- /dev/null +++ b/pagetop/src/core/component/arc.rs @@ -0,0 +1,30 @@ +use crate::core::component::{ComponentTrait, Context}; +use crate::html::Markup; +use crate::{Handle, Weight}; + +use std::sync::{Arc, RwLock}; + +#[derive(Clone)] +pub struct ComponentArc(Arc>); + +impl ComponentArc { + pub fn new(component: impl ComponentTrait) -> Self { + ComponentArc(Arc::new(RwLock::new(component))) + } + + pub(crate) fn handle(&self) -> Handle { + self.0.read().unwrap().handle() + } + + pub(crate) fn id(&self) -> Option { + self.0.read().unwrap().id() + } + + pub(crate) fn weight(&self) -> Weight { + self.0.read().unwrap().weight() + } + + pub(crate) fn prepare(&self, cx: &mut Context) -> Markup { + self.0.write().unwrap().prepare(cx) + } +} diff --git a/pagetop/src/core/component/pack.rs b/pagetop/src/core/component/pack.rs index 0e95dd2c..e55799c9 100644 --- a/pagetop/src/core/component/pack.rs +++ b/pagetop/src/core/component/pack.rs @@ -1,33 +1,6 @@ -use crate::core::component::{ComponentTrait, Context}; +use crate::core::component::{ComponentArc, Context}; use crate::html::{html, Markup}; -use crate::{Handle, Weight}; - -use std::sync::{Arc, RwLock}; - -#[derive(Clone)] -pub struct ComponentRef(Arc>); - -impl ComponentRef { - pub fn to(component: impl ComponentTrait) -> Self { - ComponentRef(Arc::new(RwLock::new(component))) - } - - fn handle(&self) -> Handle { - self.0.read().unwrap().handle() - } - - fn id(&self) -> Option { - self.0.read().unwrap().id() - } - - fn weight(&self) -> Weight { - self.0.read().unwrap().weight() - } - - fn prepare(&self, cx: &mut Context) -> Markup { - self.0.write().unwrap().prepare(cx) - } -} +use crate::Handle; pub enum PackOp { Add, @@ -40,16 +13,16 @@ pub enum PackOp { } #[derive(Clone, Default)] -pub struct PackComponents(Vec); +pub struct PackComponents(Vec); impl PackComponents { pub fn new() -> Self { PackComponents::default() } - pub fn with(cref: ComponentRef) -> Self { + pub fn with(arc: ComponentArc) -> Self { let mut pack = PackComponents::new(); - pack.alter(PackOp::Add, cref); + pack.alter(PackOp::Add, arc); pack } @@ -63,22 +36,22 @@ impl PackComponents { // PackComponents BUILDER. - pub fn alter(&mut self, op: PackOp, cref: ComponentRef) -> &mut Self { + pub fn alter(&mut self, op: PackOp, arc: ComponentArc) -> &mut Self { match op { - PackOp::Add => self.0.push(cref), + PackOp::Add => self.0.push(arc), PackOp::AddAfterId(id) => { match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { - Some(index) => self.0.insert(index + 1, cref), - _ => self.0.push(cref), + Some(index) => self.0.insert(index + 1, arc), + _ => self.0.push(arc), } } PackOp::AddBeforeId(id) => { match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { - Some(index) => self.0.insert(index, cref), - _ => self.0.insert(0, cref), + Some(index) => self.0.insert(index, arc), + _ => self.0.insert(0, arc), } } - PackOp::AddFirst => self.0.insert(0, cref), + PackOp::AddFirst => self.0.insert(0, arc), PackOp::RemoveById(id) => { if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) { self.0.remove(index); @@ -87,7 +60,7 @@ impl PackComponents { PackOp::ReplaceById(id) => { for c in self.0.iter_mut() { if c.id().as_deref() == Some(id) { - *c = cref; + *c = arc; break; } } @@ -99,15 +72,15 @@ impl PackComponents { // PackComponents GETTERS. - pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentRef> { + pub fn get_by_id(&self, id: &'static str) -> Option<&ComponentArc> { self.0.iter().find(|&c| c.id().as_deref() == Some(id)) } - pub fn iter_by_id(&self, id: &'static str) -> impl Iterator { + pub fn iter_by_id(&self, id: &'static str) -> impl Iterator { self.0.iter().filter(|&c| c.id().as_deref() == Some(id)) } - pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator { + pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator { self.0.iter().filter(move |&c| c.handle() == handle) } diff --git a/pagetop/src/core/theme/regions.rs b/pagetop/src/core/theme/regions.rs index 0e1d7460..20aa99bd 100644 --- a/pagetop/src/core/theme/regions.rs +++ b/pagetop/src/core/theme/regions.rs @@ -1,4 +1,4 @@ -use crate::core::component::{ComponentRef, PackComponents, PackOp}; +use crate::core::component::{ComponentArc, PackComponents, PackOp}; use crate::core::theme::ThemeRef; use crate::{Handle, LazyStatic}; @@ -19,17 +19,17 @@ impl ComponentsRegions { ComponentsRegions::default() } - pub fn with(region: &'static str, cref: ComponentRef) -> Self { + pub fn with(region: &'static str, arc: ComponentArc) -> Self { let mut regions = ComponentsRegions::new(); - regions.add_in(region, cref); + regions.add_in(region, arc); regions } - pub fn add_in(&mut self, region: &'static str, cref: ComponentRef) { + pub fn add_in(&mut self, region: &'static str, arc: ComponentArc) { if let Some(region) = self.0.get_mut(region) { - region.alter(PackOp::Add, cref); + region.alter(PackOp::Add, arc); } else { - self.0.insert(region, PackComponents::with(cref)); + self.0.insert(region, PackComponents::with(arc)); } } @@ -48,17 +48,17 @@ pub enum Region { OfTheme(ThemeRef, &'static str), } -pub fn add_component_in(region: Region, cref: ComponentRef) { +pub fn add_component_in(region: Region, arc: ComponentArc) { match region { Region::Named(name) => { - COMMON_REGIONS.write().unwrap().add_in(name, cref); + COMMON_REGIONS.write().unwrap().add_in(name, arc); } Region::OfTheme(theme, region) => { let mut regions = THEME_REGIONS.write().unwrap(); if let Some(hm) = regions.get_mut(&theme.handle()) { - hm.add_in(region, cref); + hm.add_in(region, arc); } else { - regions.insert(theme.handle(), ComponentsRegions::with(region, cref)); + regions.insert(theme.handle(), ComponentsRegions::with(region, arc)); } } } diff --git a/pagetop/src/response/page.rs b/pagetop/src/response/page.rs index c2d9434c..86199984 100644 --- a/pagetop/src/response/page.rs +++ b/pagetop/src/response/page.rs @@ -2,7 +2,7 @@ mod action; pub use action::*; use crate::core::component::l10n::L10n; -use crate::core::component::{ComponentRef, ComponentTrait, Context, ContextOp, OneComponent}; +use crate::core::component::{ComponentArc, ComponentTrait, Context, ContextOp, OneComponent}; use crate::core::theme::ComponentsRegions; use crate::html::{html, Classes, ClassesOp, Favicon, Markup, DOCTYPE}; use crate::response::fatal_error::FatalError; @@ -90,7 +90,7 @@ impl Page { #[fn_builder] pub fn alter_in(&mut self, region: &'static str, component: impl ComponentTrait) -> &mut Self { - self.regions.add_in(region, ComponentRef::to(component)); + self.regions.add_in(region, ComponentArc::new(component)); self }