♻️ Rename smart pointers components for clarity
This commit is contained in:
parent
9a5618ef4b
commit
d80a594cf5
18 changed files with 151 additions and 155 deletions
|
|
@ -8,7 +8,7 @@ pub struct Block {
|
|||
renderable: Renderable,
|
||||
classes : OptionClasses,
|
||||
title : OptionTranslated,
|
||||
stuff : AnyComponents,
|
||||
stuff : MixedComponents,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Block {
|
||||
|
|
@ -78,12 +78,12 @@ impl Block {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -94,7 +94,7 @@ impl Block {
|
|||
&self.title
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &AnyComponents {
|
||||
pub fn components(&self) -> &MixedComponents {
|
||||
&self.stuff
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ pub struct Container {
|
|||
weight : Weight,
|
||||
renderable : Renderable,
|
||||
classes : OptionClasses,
|
||||
items : TypedComponents<flex::Item>,
|
||||
items : VectorComponents<flex::Item>,
|
||||
direction : flex::Direction,
|
||||
wrap_align : flex::WrapAlign,
|
||||
content_justify: flex::ContentJustify,
|
||||
|
|
@ -84,12 +84,12 @@ impl Container {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -126,7 +126,7 @@ impl Container {
|
|||
|
||||
// Container GETTERS.
|
||||
|
||||
pub fn items(&self) -> &TypedComponents<flex::Item> {
|
||||
pub fn items(&self) -> &VectorComponents<flex::Item> {
|
||||
&self.items
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ pub struct Item {
|
|||
item_size : flex::ItemSize,
|
||||
item_offset : flex::ItemOffset,
|
||||
item_align : flex::ItemAlign,
|
||||
stuff : AnyComponents,
|
||||
stuff : MixedComponents,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Item {
|
||||
|
|
@ -115,12 +115,12 @@ impl Item {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -147,7 +147,7 @@ impl Item {
|
|||
&self.item_align
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &AnyComponents {
|
||||
pub fn components(&self) -> &MixedComponents {
|
||||
&self.stuff
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ pub struct Form {
|
|||
action : OptionString,
|
||||
charset : OptionString,
|
||||
method : FormMethod,
|
||||
stuff : AnyComponents,
|
||||
stuff : MixedComponents,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Form {
|
||||
|
|
@ -99,12 +99,12 @@ impl Form {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -123,7 +123,7 @@ impl Form {
|
|||
&self.method
|
||||
}
|
||||
|
||||
pub fn elements(&self) -> &AnyComponents {
|
||||
pub fn elements(&self) -> &MixedComponents {
|
||||
&self.stuff
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@ use crate::prelude::*;
|
|||
|
||||
use super::Submenu;
|
||||
|
||||
type Content = ArcTypedComponent<Html>;
|
||||
type SubmenuItems = ArcTypedComponent<Submenu>;
|
||||
type Content = TypedComponent<Html>;
|
||||
type SubmenuItems = TypedComponent<Submenu>;
|
||||
|
||||
#[derive(AutoDefault)]
|
||||
pub enum ElementType {
|
||||
|
|
@ -52,14 +52,14 @@ impl ComponentTrait for Element {
|
|||
impl Element {
|
||||
pub fn html(content: Html) -> Self {
|
||||
Element {
|
||||
element_type: ElementType::Html(Content::new(content)),
|
||||
element_type: ElementType::Html(Content::with(content)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn submenu(submenu: Submenu) -> Self {
|
||||
Element {
|
||||
element_type: ElementType::Submenu(SubmenuItems::new(submenu)),
|
||||
element_type: ElementType::Submenu(SubmenuItems::with(submenu)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ pub struct Group {
|
|||
id : OptionId,
|
||||
weight : Weight,
|
||||
renderable: Renderable,
|
||||
elements : TypedComponents<Element>,
|
||||
elements : VectorComponents<Element>,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Group {
|
||||
|
|
@ -60,19 +60,19 @@ impl Group {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
// Group GETTERS.
|
||||
|
||||
pub fn elements(&self) -> &TypedComponents<Element> {
|
||||
pub fn elements(&self) -> &VectorComponents<Element> {
|
||||
&self.elements
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ use crate::prelude::*;
|
|||
use super::{Megamenu, Submenu};
|
||||
|
||||
type Label = L10n;
|
||||
type Content = ArcTypedComponent<Html>;
|
||||
type SubmenuItems = ArcTypedComponent<Submenu>;
|
||||
type MegamenuGroups = ArcTypedComponent<Megamenu>;
|
||||
type Content = TypedComponent<Html>;
|
||||
type SubmenuItems = TypedComponent<Submenu>;
|
||||
type MegamenuGroups = TypedComponent<Megamenu>;
|
||||
|
||||
#[derive(AutoDefault)]
|
||||
pub enum ItemType {
|
||||
|
|
@ -135,21 +135,21 @@ impl Item {
|
|||
|
||||
pub fn html(content: Html) -> Self {
|
||||
Item {
|
||||
item_type: ItemType::Html(Content::new(content)),
|
||||
item_type: ItemType::Html(Content::with(content)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn submenu(label: L10n, submenu: Submenu) -> Self {
|
||||
Item {
|
||||
item_type: ItemType::Submenu(label, SubmenuItems::new(submenu)),
|
||||
item_type: ItemType::Submenu(label, SubmenuItems::with(submenu)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn megamenu(label: L10n, megamenu: Megamenu) -> Self {
|
||||
Item {
|
||||
item_type: ItemType::Megamenu(label, MegamenuGroups::new(megamenu)),
|
||||
item_type: ItemType::Megamenu(label, MegamenuGroups::with(megamenu)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ pub struct Megamenu {
|
|||
id : OptionId,
|
||||
weight : Weight,
|
||||
renderable: Renderable,
|
||||
groups : TypedComponents<Group>,
|
||||
groups : VectorComponents<Group>,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Megamenu {
|
||||
|
|
@ -60,19 +60,19 @@ impl Megamenu {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
// Megamenu GETTERS.
|
||||
|
||||
pub fn groups(&self) -> &TypedComponents<Group> {
|
||||
pub fn groups(&self) -> &VectorComponents<Group> {
|
||||
&self.groups
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ pub struct Menu {
|
|||
id : OptionId,
|
||||
weight : Weight,
|
||||
renderable: Renderable,
|
||||
items : TypedComponents<Item>,
|
||||
items : VectorComponents<Item>,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Menu {
|
||||
|
|
@ -88,19 +88,19 @@ impl Menu {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
// Menu GETTERS.
|
||||
|
||||
pub fn items(&self) -> &TypedComponents<Item> {
|
||||
pub fn items(&self) -> &VectorComponents<Item> {
|
||||
&self.items
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ pub struct Submenu {
|
|||
weight : Weight,
|
||||
renderable: Renderable,
|
||||
title : OptionTranslated,
|
||||
items : TypedComponents<Item>,
|
||||
items : VectorComponents<Item>,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Submenu {
|
||||
|
|
@ -72,12 +72,12 @@ impl Submenu {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -88,7 +88,7 @@ impl Submenu {
|
|||
&self.title
|
||||
}
|
||||
|
||||
pub fn items(&self) -> &TypedComponents<Item> {
|
||||
pub fn items(&self) -> &VectorComponents<Item> {
|
||||
&self.items
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ pub struct Paragraph {
|
|||
renderable: Renderable,
|
||||
classes : OptionClasses,
|
||||
font_size : FontSize,
|
||||
stuff : AnyComponents,
|
||||
stuff : MixedComponents,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Paragraph {
|
||||
|
|
@ -81,18 +81,18 @@ impl Paragraph {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ impl Paragraph {
|
|||
&self.font_size
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &AnyComponents {
|
||||
pub fn components(&self) -> &MixedComponents {
|
||||
&self.stuff
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ pub struct Wrapper {
|
|||
renderable : Renderable,
|
||||
classes : OptionClasses,
|
||||
wrapper_type : WrapperType,
|
||||
stuff : AnyComponents,
|
||||
stuff : MixedComponents,
|
||||
}
|
||||
|
||||
impl ComponentTrait for Wrapper {
|
||||
|
|
@ -126,12 +126,12 @@ impl Wrapper {
|
|||
|
||||
#[rustfmt::skip]
|
||||
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
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ impl Wrapper {
|
|||
&self.wrapper_type
|
||||
}
|
||||
|
||||
pub fn components(&self) -> &AnyComponents {
|
||||
pub fn components(&self) -> &MixedComponents {
|
||||
&self.stuff
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
|
|||
mod classes;
|
||||
pub use classes::{ComponentClasses, ComponentClassesOp};
|
||||
|
||||
mod arc_any;
|
||||
pub use arc_any::AnyComponents;
|
||||
pub use arc_any::{ArcAnyComponent, ArcAnyOp};
|
||||
mod arc_one;
|
||||
pub use arc_one::MixedComponents;
|
||||
pub use arc_one::{OneComponent, OneOp};
|
||||
|
||||
mod arc_typed;
|
||||
pub use arc_typed::TypedComponents;
|
||||
pub use arc_typed::{ArcTypedComponent, ArcTypedOp};
|
||||
pub use arc_typed::VectorComponents;
|
||||
pub use arc_typed::{TypedComponent, TypedOp};
|
||||
|
|
|
|||
|
|
@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight};
|
|||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ArcAnyComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||
pub struct OneComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||
|
||||
impl ArcAnyComponent {
|
||||
pub fn new(component: impl ComponentTrait) -> Self {
|
||||
ArcAnyComponent(Arc::new(RwLock::new(component)))
|
||||
impl OneComponent {
|
||||
pub fn with(component: impl ComponentTrait) -> Self {
|
||||
OneComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
// ArcAnyComponent BUILDER.
|
||||
// OneComponent BUILDER.
|
||||
|
||||
pub fn set(&mut self, component: impl ComponentTrait) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
// ArcAnyComponent GETTERS.
|
||||
// OneComponent GETTERS.
|
||||
|
||||
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
|
||||
self.0.read().unwrap()
|
||||
|
|
@ -28,13 +28,13 @@ impl ArcAnyComponent {
|
|||
self.0.write().unwrap()
|
||||
}
|
||||
|
||||
// ArcAnyComponent RENDER.
|
||||
// OneComponent RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().render(cx)
|
||||
}
|
||||
|
||||
// ArcAnyComponent HELPERS.
|
||||
// OneComponent HELPERS.
|
||||
|
||||
fn type_id(&self) -> TypeId {
|
||||
self.0.read().unwrap().type_id()
|
||||
|
|
@ -51,86 +51,86 @@ impl ArcAnyComponent {
|
|||
|
||||
// *************************************************************************************************
|
||||
|
||||
pub enum ArcAnyOp {
|
||||
Add(ArcAnyComponent),
|
||||
AddAfterId(&'static str, ArcAnyComponent),
|
||||
AddBeforeId(&'static str, ArcAnyComponent),
|
||||
Prepend(ArcAnyComponent),
|
||||
pub enum OneOp {
|
||||
Add(OneComponent),
|
||||
AddAfterId(&'static str, OneComponent),
|
||||
AddBeforeId(&'static str, OneComponent),
|
||||
Prepend(OneComponent),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, ArcAnyComponent),
|
||||
ReplaceById(&'static str, OneComponent),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct AnyComponents(Vec<ArcAnyComponent>);
|
||||
pub struct MixedComponents(Vec<OneComponent>);
|
||||
|
||||
impl AnyComponents {
|
||||
pub fn new(arc: ArcAnyComponent) -> Self {
|
||||
AnyComponents::default().with_value(ArcAnyOp::Add(arc))
|
||||
impl MixedComponents {
|
||||
pub fn new(any: OneComponent) -> Self {
|
||||
MixedComponents::default().with_value(OneOp::Add(any))
|
||||
}
|
||||
|
||||
pub(crate) fn merge(mixes: &[Option<&AnyComponents>]) -> Self {
|
||||
let mut opt = AnyComponents::default();
|
||||
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
|
||||
let mut opt = MixedComponents::default();
|
||||
for m in mixes.iter().flatten() {
|
||||
opt.0.append(&mut m.0.clone());
|
||||
}
|
||||
opt
|
||||
}
|
||||
|
||||
// AnyComponents BUILDER.
|
||||
// MixedComponents 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 {
|
||||
ArcAnyOp::Add(arc) => self.0.push(arc),
|
||||
ArcAnyOp::AddAfterId(id, arc) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index + 1, arc),
|
||||
_ => self.0.push(arc),
|
||||
OneOp::Add(any) => self.0.push(any),
|
||||
OneOp::AddAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index + 1, any),
|
||||
_ => self.0.push(any),
|
||||
},
|
||||
ArcAnyOp::AddBeforeId(id, arc) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index, arc),
|
||||
_ => self.0.insert(0, arc),
|
||||
OneOp::AddBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index, any),
|
||||
_ => self.0.insert(0, any),
|
||||
},
|
||||
ArcAnyOp::Prepend(arc) => self.0.insert(0, arc),
|
||||
ArcAnyOp::RemoveById(id) => {
|
||||
OneOp::Prepend(any) => self.0.insert(0, any),
|
||||
OneOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
ArcAnyOp::ReplaceById(id, arc) => {
|
||||
OneOp::ReplaceById(id, any) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id() == id {
|
||||
*c = arc;
|
||||
*c = any;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ArcAnyOp::Reset => self.0.clear(),
|
||||
OneOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// AnyComponents GETTERS.
|
||||
// MixedComponents GETTERS.
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
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();
|
||||
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();
|
||||
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)
|
||||
}
|
||||
|
||||
// AnyComponents RENDER.
|
||||
// MixedComponents RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
|
|
@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight};
|
|||
|
||||
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 {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ComponentTrait> ArcTypedComponent<C> {
|
||||
pub fn new(component: C) -> Self {
|
||||
ArcTypedComponent(Arc::new(RwLock::new(component)))
|
||||
impl<C: ComponentTrait> TypedComponent<C> {
|
||||
pub fn with(component: C) -> Self {
|
||||
TypedComponent(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
// ArcTypedComponent BUILDER.
|
||||
// TypedComponent BUILDER.
|
||||
|
||||
pub fn set(&mut self, component: C) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
// ArcTypedComponent GETTERS.
|
||||
// TypedComponent GETTERS.
|
||||
|
||||
pub fn get(&self) -> RwLockReadGuard<'_, C> {
|
||||
self.0.read().unwrap()
|
||||
|
|
@ -33,13 +33,13 @@ impl<C: ComponentTrait> ArcTypedComponent<C> {
|
|||
self.0.write().unwrap()
|
||||
}
|
||||
|
||||
// ArcTypedComponent RENDER.
|
||||
// TypedComponent RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
self.0.write().unwrap().render(cx)
|
||||
}
|
||||
|
||||
// ArcTypedComponent HELPERS.
|
||||
// TypedComponent HELPERS.
|
||||
|
||||
fn type_id(&self) -> TypeId {
|
||||
self.0.read().unwrap().type_id()
|
||||
|
|
@ -56,45 +56,45 @@ impl<C: ComponentTrait> ArcTypedComponent<C> {
|
|||
|
||||
// *************************************************************************************************
|
||||
|
||||
pub enum ArcTypedOp<C: ComponentTrait> {
|
||||
Add(ArcTypedComponent<C>),
|
||||
AddAfterId(&'static str, ArcTypedComponent<C>),
|
||||
AddBeforeId(&'static str, ArcTypedComponent<C>),
|
||||
Prepend(ArcTypedComponent<C>),
|
||||
pub enum TypedOp<C: ComponentTrait> {
|
||||
Add(TypedComponent<C>),
|
||||
AddAfterId(&'static str, TypedComponent<C>),
|
||||
AddBeforeId(&'static str, TypedComponent<C>),
|
||||
Prepend(TypedComponent<C>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, ArcTypedComponent<C>),
|
||||
ReplaceById(&'static str, TypedComponent<C>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[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> {
|
||||
pub fn new(arc: ArcTypedComponent<C>) -> Self {
|
||||
TypedComponents::default().with_value(ArcTypedOp::Add(arc))
|
||||
impl<C: ComponentTrait + Default> VectorComponents<C> {
|
||||
pub fn new(one: TypedComponent<C>) -> Self {
|
||||
VectorComponents::default().with_value(TypedOp::Add(one))
|
||||
}
|
||||
|
||||
// TypedComponents BUILDER.
|
||||
// VectorComponents 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 {
|
||||
ArcTypedOp::Add(one) => self.0.push(one),
|
||||
ArcTypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
|
||||
TypedOp::Add(one) => self.0.push(one),
|
||||
TypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
|
||||
Some(index) => self.0.insert(index + 1, 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),
|
||||
_ => self.0.insert(0, one),
|
||||
},
|
||||
ArcTypedOp::Prepend(one) => self.0.insert(0, one),
|
||||
ArcTypedOp::RemoveById(id) => {
|
||||
TypedOp::Prepend(one) => self.0.insert(0, one),
|
||||
TypedOp::RemoveById(id) => {
|
||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
||||
self.0.remove(index);
|
||||
}
|
||||
}
|
||||
ArcTypedOp::ReplaceById(id, one) => {
|
||||
TypedOp::ReplaceById(id, one) => {
|
||||
for c in self.0.iter_mut() {
|
||||
if c.id() == id {
|
||||
*c = one;
|
||||
|
|
@ -102,32 +102,32 @@ impl<C: ComponentTrait + Default> TypedComponents<C> {
|
|||
}
|
||||
}
|
||||
}
|
||||
ArcTypedOp::Reset => self.0.clear(),
|
||||
TypedOp::Reset => self.0.clear(),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
// TypedComponents GETTERS.
|
||||
// VectorComponents GETTERS.
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
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();
|
||||
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();
|
||||
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)
|
||||
}
|
||||
|
||||
// TypedComponents RENDER.
|
||||
// VectorComponents RENDER.
|
||||
|
||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
|
|
|
|||
|
|
@ -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::{AutoDefault, LazyStatic, TypeId};
|
||||
|
||||
|
|
@ -12,29 +12,29 @@ static COMMON_REGIONS: LazyStatic<RwLock<ComponentsInRegions>> =
|
|||
LazyStatic::new(|| RwLock::new(ComponentsInRegions::default()));
|
||||
|
||||
#[derive(AutoDefault)]
|
||||
pub struct ComponentsInRegions(HashMap<&'static str, AnyComponents>);
|
||||
pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
|
||||
|
||||
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();
|
||||
regions.add_component_in(region, arc);
|
||||
regions.add_in(region, one);
|
||||
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) {
|
||||
region.alter_value(ArcAnyOp::Add(arc));
|
||||
region.alter_value(OneOp::Add(one));
|
||||
} 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();
|
||||
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 {
|
||||
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 {
|
||||
pub fn add_component(&self, arc: ArcAnyComponent) -> &Self {
|
||||
pub fn add(&self, one: OneComponent) -> &Self {
|
||||
match self {
|
||||
InRegion::Content => {
|
||||
COMMON_REGIONS
|
||||
.write()
|
||||
.unwrap()
|
||||
.add_component_in("content", arc);
|
||||
COMMON_REGIONS.write().unwrap().add_in("content", one);
|
||||
}
|
||||
InRegion::Named(name) => {
|
||||
COMMON_REGIONS.write().unwrap().add_component_in(name, arc);
|
||||
COMMON_REGIONS.write().unwrap().add_in(name, one);
|
||||
}
|
||||
InRegion::OfTheme(region, theme) => {
|
||||
let mut regions = THEME_REGIONS.write().unwrap();
|
||||
if let Some(r) = regions.get_mut(&theme.type_id()) {
|
||||
r.add_component_in(region, arc);
|
||||
r.add_in(region, one);
|
||||
} else {
|
||||
regions.insert(theme.type_id(), ComponentsInRegions::new(region, arc));
|
||||
regions.insert(theme.type_id(), ComponentsInRegions::new(region, one));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use crate::core::component::{ArcTypedComponent, ComponentTrait, Context};
|
||||
use crate::core::component::{ComponentTrait, Context, TypedComponent};
|
||||
use crate::fn_builder;
|
||||
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> {
|
||||
fn default() -> Self {
|
||||
|
|
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
|
|||
#[fn_builder]
|
||||
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
|
||||
if let Some(component) = component {
|
||||
self.0 = Some(ArcTypedComponent::new(component));
|
||||
self.0 = Some(TypedComponent::with(component));
|
||||
} else {
|
||||
self.0 = None;
|
||||
}
|
||||
|
|
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
|
|||
|
||||
// OptionComponent GETTERS.
|
||||
|
||||
pub fn get(&self) -> Option<ArcTypedComponent<C>> {
|
||||
pub fn get(&self) -> Option<TypedComponent<C>> {
|
||||
if let Some(value) = &self.0 {
|
||||
return Some(value.clone());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ pub use error::ErrorPage;
|
|||
pub use actix_web::Result as ResultPage;
|
||||
|
||||
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::theme::ComponentsInRegions;
|
||||
use crate::fn_builder;
|
||||
|
|
@ -107,7 +107,7 @@ impl Page {
|
|||
#[fn_builder]
|
||||
pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self {
|
||||
self.regions
|
||||
.add_component_in("content", ArcAnyComponent::new(component));
|
||||
.add_in("content", OneComponent::with(component));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -117,8 +117,7 @@ impl Page {
|
|||
region: &'static str,
|
||||
component: impl ComponentTrait,
|
||||
) -> &mut Self {
|
||||
self.regions
|
||||
.add_component_in(region, ArcAnyComponent::new(component));
|
||||
self.regions.add_in(region, OneComponent::with(component));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -166,7 +165,7 @@ impl Page {
|
|||
&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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue