🚚 Define acción base "before_render_component"

This commit is contained in:
Manuel Cillero 2023-06-07 23:21:42 +02:00
parent d8812433f1
commit cb04a29388
12 changed files with 81 additions and 81 deletions

View file

@ -1,3 +1,5 @@
pub mod action;
pub mod component;
pub mod theme;

View file

@ -0,0 +1 @@
mod before_render_component;

View file

@ -0,0 +1,64 @@
#[macro_export]
macro_rules! action_before_render_component {
( $ACTION_HANDLE:ident for $Component:ty ) => {
$crate::paste! {
$crate::define_handle!($ACTION_HANDLE);
type Action = fn(&$Component, &mut RenderContext);
pub struct [< BeforeRender $Component >] {
action: Option<Action>,
weight: isize,
}
impl ActionTrait for [< BeforeRender $Component >] {
fn new() -> Self {
[< BeforeRender $Component >] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
$ACTION_HANDLE
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [< BeforeRender $Component >] {
#[allow(dead_code)]
pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action);
self
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn run(&self, component: &mut $Component, rcx: &mut RenderContext) {
if let Some(action) = self.action {
action(component, rcx)
}
}
}
#[inline(always)]
pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) {
run_actions($ACTION_HANDLE, |action|
action_ref::<[< BeforeRender $Component >]>(&**action)
.run(component, rcx)
);
}
}
};
}

View file

@ -2,7 +2,7 @@ use crate::prelude::*;
define_handle!(COMPONENT_BLOCK);
action_before_render_component!(ACTION_BEFORE_RENDER_BLOCK, Block);
action_before_render_component!(ACTION_BEFORE_RENDER_BLOCK for Block);
#[rustfmt::skip]
#[derive(Default)]

View file

@ -11,20 +11,18 @@ pub enum L10nOp {
Escaped(&'static str, &'static Locales),
}
impl Default for L10nOp {
fn default() -> Self {
L10nOp::None
}
}
#[derive(Default)]
pub struct L10n {
op: L10nOp,
args: HashMap<&'static str, String>,
}
impl Default for L10n {
fn default() -> Self {
L10n {
op: L10nOp::None,
args: HashMap::new(),
}
}
}
impl ComponentTrait for L10n {
fn new() -> Self {
L10n::default()

View file

@ -74,68 +74,3 @@ pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
component.as_mut_any().downcast_mut::<C>().unwrap()
}
#[macro_export]
macro_rules! action_before_render_component {
( $ACTION_HANDLE:ident, $Component:ty ) => {
$crate::paste! {
$crate::define_handle!($ACTION_HANDLE);
type Action = fn(&$Component, &mut RenderContext);
pub struct [< BeforeRender $Component >] {
action: Option<Action>,
weight: isize,
}
impl ActionTrait for [< BeforeRender $Component >] {
fn new() -> Self {
[< BeforeRender $Component >] {
action: None,
weight: 0,
}
}
fn handle(&self) -> Handle {
$ACTION_HANDLE
}
fn weight(&self) -> isize {
self.weight
}
fn as_ref_any(&self) -> &dyn AnyAction {
self
}
}
impl [< BeforeRender $Component >] {
#[allow(dead_code)]
pub fn with_action(mut self, action: Action) -> Self {
self.action = Some(action);
self
}
#[allow(dead_code)]
pub fn with_weight(mut self, weight: isize) -> Self {
self.weight = weight;
self
}
pub fn run(&self, component: &mut $Component, rcx: &mut RenderContext) {
if let Some(action) = self.action {
action(component, rcx)
}
}
}
#[inline(always)]
pub fn before_render_inline(component: &mut $Component, rcx: &mut RenderContext) {
run_actions($ACTION_HANDLE, |action|
action_ref::<[< BeforeRender $Component >]>(&**action)
.run(component, rcx)
);
}
}
};
}

View file

@ -78,7 +78,7 @@
//!
//! define_locale!(LOCALE_SAMPLE, "static/locales");
//! ```
//! Y utiliza el componente [L10n](crate::core::component::L10n) para incluir, en respuestas a las
//! Y utiliza el componente [L10n](crate::base::component::L10n) para incluir, en respuestas a las
//! peticiones web, textos y contenidos opcionalmente traducibles según el contexto de renderizado.
use crate::{args, config, trace, LazyStatic};