Retoca la llamada al renderizado de componentes

This commit is contained in:
Manuel Cillero 2022-07-20 17:56:51 +02:00
parent db053531d1
commit 3eb9cfbff8
4 changed files with 29 additions and 20 deletions

4
.gitignore vendored
View file

@ -2,4 +2,8 @@
**/log/*.log*
**/update.sh
Cargo.lock
website/website
.sitecopy
workdir

View file

@ -2,8 +2,7 @@ mod context;
pub use context::{InContext, InContextOp};
mod definition;
use definition::render_component;
pub use definition::{component_mut, component_ref, AnyComponent, ComponentTrait};
pub use definition::{component_mut, component_ref, AnyComponent, BaseComponent, ComponentTrait};
mod bundle;
pub use bundle::ComponentsBundle;

View file

@ -30,7 +30,7 @@ impl ComponentsBundle {
components.sort_by_key(|c| c.read().unwrap().weight());
html! {
@for c in components.iter() {
(" ")(super::render_component(&mut *c.write().unwrap(), context))(" ")
(" ")(c.write().unwrap().render(context))(" ")
}
}
}

View file

@ -4,7 +4,11 @@ use crate::util;
pub use std::any::Any as AnyComponent;
pub trait ComponentTrait: AnyComponent + Send + Sync {
pub trait BaseComponent {
fn render(&mut self, context: &mut InContext) -> Markup;
}
pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
fn new() -> Self
where
Self: Sized;
@ -41,6 +45,24 @@ pub trait ComponentTrait: AnyComponent + Send + Sync {
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
}
impl<C: ComponentTrait> BaseComponent for C {
fn render(&mut self, context: &mut InContext) -> Markup {
// Acciones del componente antes de renderizar.
self.before_render(context);
// Acciones del tema antes de renderizar el componente.
context.theme().before_render_component(self, context);
match self.is_renderable(context) {
true => match context.theme().render_component(self, context) {
Some(html) => html,
None => self.default_render(context),
},
false => html! {},
}
}
}
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
component.as_ref_any().downcast_ref::<C>().unwrap()
}
@ -49,22 +71,6 @@ pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
component.as_mut_any().downcast_mut::<C>().unwrap()
}
pub fn render_component(component: &mut dyn ComponentTrait, context: &mut InContext) -> Markup {
// Acciones del componente antes de renderizar.
component.before_render(context);
// Acciones del tema antes de renderizar el componente.
context.theme().before_render_component(component, context);
match component.is_renderable(context) {
true => match context.theme().render_component(component, context) {
Some(html) => html,
None => component.default_render(context),
},
false => html! {},
}
}
#[macro_export]
macro_rules! hook_before_render_component {
( $ACTION_HANDLER:ident = $handler:literal, $Component:ty ) => {