♻️ Rename smart pointers components for clarity
This commit is contained in:
parent
9a5618ef4b
commit
d80a594cf5
18 changed files with 151 additions and 155 deletions
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue