✨ Define OneComponent con tipado restringido
This commit is contained in:
parent
dd443ca375
commit
23a6f36f62
12 changed files with 68 additions and 90 deletions
|
|
@ -4,11 +4,8 @@ pub use context::{ContextOp, RenderContext};
|
|||
mod definition;
|
||||
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
|
||||
|
||||
mod default;
|
||||
pub(crate) use default::DefaultComponent;
|
||||
|
||||
mod arc;
|
||||
pub use arc::ComponentArc;
|
||||
mod one;
|
||||
pub use one::OneComponent;
|
||||
|
||||
mod bundle;
|
||||
pub use bundle::ComponentsBundle;
|
||||
|
|
|
|||
|
|
@ -1,45 +0,0 @@
|
|||
use crate::core::component::{ComponentTrait, DefaultComponent, RenderContext};
|
||||
use crate::html::{html, Markup};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ComponentArc(Arc<RwLock<dyn ComponentTrait>>);
|
||||
|
||||
impl Default for ComponentArc {
|
||||
fn default() -> Self {
|
||||
ComponentArc(Arc::new(RwLock::new(DefaultComponent)))
|
||||
}
|
||||
}
|
||||
|
||||
impl ComponentArc {
|
||||
pub fn new() -> Self {
|
||||
ComponentArc::default()
|
||||
}
|
||||
|
||||
pub fn new_with(component: impl ComponentTrait) -> Self {
|
||||
ComponentArc(Arc::new(RwLock::new(component)))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, component: impl ComponentTrait) {
|
||||
self.0 = Arc::new(RwLock::new(component));
|
||||
}
|
||||
|
||||
pub fn weight(&self) -> isize {
|
||||
self.0.read().unwrap().weight()
|
||||
}
|
||||
|
||||
// ComponentArc RENDER.
|
||||
|
||||
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
||||
self.0.write().unwrap().render(rcx)
|
||||
}
|
||||
|
||||
pub fn optional_render(&self, rcx: &mut RenderContext) -> Option<Markup> {
|
||||
let render = self.0.write().unwrap().render(rcx).into_string();
|
||||
if !render.trim().is_empty() {
|
||||
return Some(html! { (render) });
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,10 @@
|
|||
use crate::core::component::{ComponentArc, ComponentTrait, RenderContext};
|
||||
use crate::core::component::{ComponentTrait, RenderContext};
|
||||
use crate::html::{html, Markup};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct ComponentsBundle(Vec<ComponentArc>);
|
||||
pub struct ComponentsBundle(Vec<Arc<RwLock<dyn ComponentTrait>>>);
|
||||
|
||||
impl ComponentsBundle {
|
||||
pub fn new() -> Self {
|
||||
|
|
@ -16,7 +18,7 @@ impl ComponentsBundle {
|
|||
}
|
||||
|
||||
pub fn add(&mut self, component: impl ComponentTrait) {
|
||||
self.0.push(ComponentArc::new_with(component));
|
||||
self.0.push(Arc::new(RwLock::new(component)));
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
|
|
@ -25,10 +27,10 @@ impl ComponentsBundle {
|
|||
|
||||
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
||||
let mut components = self.0.clone();
|
||||
components.sort_by_key(|c| c.weight());
|
||||
components.sort_by_key(|c| c.read().unwrap().weight());
|
||||
html! {
|
||||
@for c in components.iter() {
|
||||
(" ")(c.render(rcx))(" ")
|
||||
(" ")(c.write().unwrap().render(rcx))(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +0,0 @@
|
|||
use crate::core::component::{AnyComponent, ComponentTrait};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DefaultComponent;
|
||||
|
||||
impl ComponentTrait for DefaultComponent {
|
||||
fn new() -> Self {
|
||||
DefaultComponent::default()
|
||||
}
|
||||
|
||||
fn as_ref_any(&self) -> &dyn AnyComponent {
|
||||
self
|
||||
}
|
||||
|
||||
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
|
||||
self
|
||||
}
|
||||
}
|
||||
40
pagetop/src/core/component/one.rs
Normal file
40
pagetop/src/core/component/one.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
use crate::core::component::{ComponentTrait, RenderContext};
|
||||
use crate::html::{html, Markup};
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct OneComponent<T: ComponentTrait + Default>(Option<Arc<RwLock<T>>>);
|
||||
|
||||
impl<T: ComponentTrait + Default> OneComponent<T> {
|
||||
pub fn new() -> Self {
|
||||
OneComponent::<T>::default()
|
||||
}
|
||||
|
||||
pub fn new_with(component: T) -> Self {
|
||||
OneComponent(Some(Arc::new(RwLock::new(component))))
|
||||
}
|
||||
|
||||
pub fn set(&mut self, component: T) {
|
||||
self.0 = Some(Arc::new(RwLock::new(component)));
|
||||
}
|
||||
|
||||
// OneComponent RENDER.
|
||||
|
||||
pub fn render(&self, rcx: &mut RenderContext) -> Markup {
|
||||
if let Some(component) = &self.0 {
|
||||
return component.write().unwrap().render(rcx);
|
||||
}
|
||||
html! {}
|
||||
}
|
||||
|
||||
pub fn optional_render(&self, rcx: &mut RenderContext) -> Option<Markup> {
|
||||
if let Some(component) = &self.0 {
|
||||
let render = component.write().unwrap().render(rcx).into_string();
|
||||
if !render.trim().is_empty() {
|
||||
return Some(html! { (render) });
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -32,8 +32,8 @@ pub enum TextDirection {
|
|||
RightToLeft,
|
||||
}
|
||||
|
||||
pub type PageTitle = ComponentArc;
|
||||
pub type PageDescription = ComponentArc;
|
||||
type PageTitle = OneComponent<L10n>;
|
||||
type PageDescription = OneComponent<L10n>;
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub struct Page {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue