Revert using weight to order components

This commit is contained in:
Manuel Cillero 2024-04-15 21:42:53 +02:00
parent 7d4cf642ff
commit ece78b2b4a

View file

@ -1,121 +1,59 @@
use crate::core::component::{ComponentTrait, Context}; use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup}; use crate::html::{html, Markup};
use crate::{fn_builder, TypeId, Weight}; use crate::{fn_builder, TypeId};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::sync::{Arc, RwLock};
#[derive(Clone)] #[derive(Clone)]
pub struct AnyComponent { pub struct AnyComponent(Arc<RwLock<dyn ComponentTrait>>);
weight: Weight,
component: Arc<RwLock<dyn ComponentTrait>>,
}
impl AnyComponent { impl AnyComponent {
pub fn with(component: impl ComponentTrait) -> Self { pub fn with(component: impl ComponentTrait) -> Self {
AnyComponent { AnyComponent(Arc::new(RwLock::new(component)))
weight: Weight::default(),
component: Arc::new(RwLock::new(component)),
}
}
// AnyComponent BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
// AnyComponent GETTERS.
pub fn weight(&self) -> Weight {
self.weight
}
pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> {
self.component.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> {
self.component.write().unwrap()
} }
// AnyComponent RENDER. // AnyComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.component.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// AnyComponent HELPERS. // AnyComponent HELPERS.
fn type_id(&self) -> TypeId { fn type_id(&self) -> TypeId {
self.component.read().unwrap().type_id() self.0.read().unwrap().type_id()
} }
fn id(&self) -> String { fn id(&self) -> String {
self.component.read().unwrap().id().unwrap_or_default() self.0.read().unwrap().id().unwrap_or_default()
} }
} }
// ************************************************************************************************* // *************************************************************************************************
pub struct TypedComponent<C: ComponentTrait> { pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
weight: Weight,
component: Arc<RwLock<C>>,
}
impl<C: ComponentTrait> Clone for TypedComponent<C> { impl<C: ComponentTrait> Clone for TypedComponent<C> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self(self.0.clone())
weight: self.weight.clone(),
component: self.component.clone(),
}
} }
} }
impl<C: ComponentTrait> TypedComponent<C> { impl<C: ComponentTrait> TypedComponent<C> {
pub fn with(component: C) -> Self { pub fn with(component: C) -> Self {
TypedComponent { TypedComponent(Arc::new(RwLock::new(component)))
weight: Weight::default(),
component: Arc::new(RwLock::new(component)),
}
}
// TypedComponent BUILDER.
#[fn_builder]
pub fn alter_weight(&mut self, value: Weight) -> &mut Self {
self.weight = value;
self
}
// TypedComponent GETTERS.
pub fn weight(&self) -> Weight {
self.weight
}
pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.component.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
self.component.write().unwrap()
} }
// TypedComponent RENDER. // TypedComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
self.component.write().unwrap().render(cx) self.0.write().unwrap().render(cx)
} }
// TypedComponent HELPERS. // TypedComponent HELPERS.
fn to_any(&self) -> AnyComponent { fn to_any(&self) -> AnyComponent {
AnyComponent { AnyComponent(self.0.clone())
weight: self.weight.clone(),
component: self.component.clone(),
}
} }
} }
@ -266,10 +204,8 @@ impl MixedComponents {
// MixedComponents RENDER. // MixedComponents RENDER.
pub fn render(&self, cx: &mut Context) -> Markup { pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone();
components.sort_by_key(|c| c.weight());
html! { html! {
@for c in components.iter() { @for c in self.0.iter() {
(c.render(cx)) (c.render(cx))
} }
} }