♻️ Rename types for Mixed and Typed components
This commit is contained in:
parent
57e71a9399
commit
c65ac74a6f
20 changed files with 151 additions and 154 deletions
|
|
@ -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};
|
||||
|
|
|
|||
|
|
@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight};
|
|||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct OneComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||
pub struct AnyComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||
|
||||
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<OneComponent>);
|
||||
pub struct MixedComponents(Vec<AnyComponent>);
|
||||
|
||||
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<String>) -> Option<&OneComponent> {
|
||||
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&AnyComponent> {
|
||||
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 = &OneComponent> {
|
||||
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &AnyComponent> {
|
||||
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 = &OneComponent> {
|
||||
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &AnyComponent> {
|
||||
self.0.iter().filter(move |&c| c.type_id() == type_id)
|
||||
}
|
||||
|
||||
|
|
@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight};
|
|||
|
||||
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
|
||||
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||
pub struct OneComponent<C: ComponentTrait>(Arc<RwLock<C>>);
|
||||
|
||||
impl<C: ComponentTrait> Clone for TypedComponent<C> {
|
||||
impl<C: ComponentTrait> Clone for OneComponent<C> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ComponentTrait> TypedComponent<C> {
|
||||
impl<C: ComponentTrait> OneComponent<C> {
|
||||
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<C: ComponentTrait> TypedComponent<C> {
|
|||
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<C: ComponentTrait> TypedComponent<C> {
|
|||
// *************************************************************************************************
|
||||
|
||||
pub enum TypedOp<C: ComponentTrait> {
|
||||
Add(TypedComponent<C>),
|
||||
AddAfterId(&'static str, TypedComponent<C>),
|
||||
AddBeforeId(&'static str, TypedComponent<C>),
|
||||
Prepend(TypedComponent<C>),
|
||||
Add(OneComponent<C>),
|
||||
InsertAfterId(&'static str, OneComponent<C>),
|
||||
InsertBeforeId(&'static str, OneComponent<C>),
|
||||
Prepend(OneComponent<C>),
|
||||
RemoveById(&'static str),
|
||||
ReplaceById(&'static str, TypedComponent<C>),
|
||||
ReplaceById(&'static str, OneComponent<C>),
|
||||
Reset,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct VectorComponents<C: ComponentTrait>(Vec<TypedComponent<C>>);
|
||||
pub struct TypedComponents<C: ComponentTrait>(Vec<OneComponent<C>>);
|
||||
|
||||
impl<C: ComponentTrait + Default> VectorComponents<C> {
|
||||
pub fn new(one: TypedComponent<C>) -> Self {
|
||||
VectorComponents::default().with_value(TypedOp::Add(one))
|
||||
impl<C: ComponentTrait + Default> TypedComponents<C> {
|
||||
pub fn new(one: OneComponent<C>) -> Self {
|
||||
TypedComponents::default().with_value(TypedOp::Add(one))
|
||||
}
|
||||
|
||||
// VectorComponents BUILDER.
|
||||
// TypedComponents BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn alter_value(&mut self, op: TypedOp<C>) -> &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<C: ComponentTrait + Default> VectorComponents<C> {
|
|||
self
|
||||
}
|
||||
|
||||
// VectorComponents GETTERS.
|
||||
// TypedComponents GETTERS.
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&TypedComponent<C>> {
|
||||
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent<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 = &TypedComponent<C>> {
|
||||
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &OneComponent<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 = &TypedComponent<C>> {
|
||||
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &OneComponent<C>> {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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<RwLock<ComponentsInRegions>> =
|
|||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue