Define OneComponent con tipado restringido

This commit is contained in:
Manuel Cillero 2023-05-28 09:08:29 +02:00
parent dd443ca375
commit 23a6f36f62
12 changed files with 68 additions and 90 deletions

View file

@ -2,15 +2,17 @@ use pagetop::prelude::*;
define_handle!(COMPONENT_MEGAMENUITEM);
type Label = OneComponent<L10n>;
#[derive(Default)]
pub enum MegaMenuItemType {
#[default]
Void,
Label(ComponentArc),
Link(ComponentArc, String),
LinkBlank(ComponentArc, String),
Label(Label),
Link(Label, String),
LinkBlank(Label, String),
Html(Markup),
Submenu(ComponentArc, MegaMenu),
Submenu(Label, MegaMenu),
Separator,
}
@ -85,21 +87,21 @@ impl ComponentTrait for MegaMenuItem {
impl MegaMenuItem {
pub fn label(label: L10n) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Label(ComponentArc::new_with(label)),
item_type: MegaMenuItemType::Label(OneComponent::new_with(label)),
..Default::default()
}
}
pub fn link(label: L10n, path: &str) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Link(ComponentArc::new_with(label), path.to_owned()),
item_type: MegaMenuItemType::Link(OneComponent::new_with(label), path.to_owned()),
..Default::default()
}
}
pub fn link_blank(label: L10n, path: &str) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::LinkBlank(ComponentArc::new_with(label), path.to_owned()),
item_type: MegaMenuItemType::LinkBlank(OneComponent::new_with(label), path.to_owned()),
..Default::default()
}
}
@ -113,7 +115,7 @@ impl MegaMenuItem {
pub fn submenu(label: L10n, menu: MegaMenu) -> Self {
MegaMenuItem {
item_type: MegaMenuItemType::Submenu(ComponentArc::new_with(label), menu),
item_type: MegaMenuItemType::Submenu(OneComponent::new_with(label), menu),
..Default::default()
}
}

View file

@ -12,7 +12,7 @@ pub use heading::{Heading, HeadingDisplay, HeadingType, COMPONENT_HEADING};
mod paragraph;
pub use paragraph::{Paragraph, ParagraphDisplay, COMPONENT_PARAGRAPH};
mod anchor;
pub use anchor::{Anchor, AnchorIcon, AnchorTarget, AnchorType, COMPONENT_ANCHOR};
pub use anchor::{Anchor, AnchorTarget, AnchorType, COMPONENT_ANCHOR};
mod block;
pub use block::{Block, COMPONENT_BLOCK};
mod image;

View file

@ -22,8 +22,8 @@ pub enum AnchorTarget {
Context(String),
}
pub type AnchorIcon = ComponentArc;
pub type AnchorHtml = ComponentArc;
type AnchorIcon = OneComponent<Icon>;
type AnchorHtml = OneComponent<L10n>;
#[rustfmt::skip]
#[derive(Default)]

View file

@ -10,7 +10,7 @@ pub enum ButtonType {
Reset,
}
pub type ButtonValue = ComponentArc;
type ButtonValue = OneComponent<L10n>;
#[rustfmt::skip]
#[derive(Default)]

View file

@ -13,8 +13,8 @@ pub enum InputType {
Url,
}
pub type InputLabel = ComponentArc;
pub type InputHelpText = ComponentArc;
type InputLabel = OneComponent<L10n>;
type InputHelpText = OneComponent<L10n>;
#[rustfmt::skip]
#[derive(Default)]

View file

@ -25,7 +25,7 @@ pub enum HeadingDisplay {
Subtitle,
}
pub type HeadingText = ComponentArc;
type HeadingText = OneComponent<L10n>;
#[rustfmt::skip]
#[derive(Default)]

View file

@ -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;

View file

@ -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
}
}

View file

@ -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))(" ")
}
}
}

View file

@ -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
}
}

View 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
}
}

View file

@ -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 {