🚧 Working on actions

This commit is contained in:
Manuel Cillero 2024-03-27 07:12:42 +01:00
parent e732244a2e
commit d99a5aa586
23 changed files with 480 additions and 323 deletions

View file

@ -1,3 +1,9 @@
pub mod component;
use crate::prelude::*;
pub type FnActionWithComponent<C> = fn(component: &mut C, cx: &mut Context);
pub mod page;
pub mod theme;
pub mod component;

View file

@ -1,7 +1,3 @@
use crate::prelude::*;
pub type FnActionComponent<C> = fn(component: &mut C, cx: &mut Context);
mod before_prepare_component;
pub use before_prepare_component::*;

View file

@ -1,15 +1,15 @@
use crate::prelude::*;
use super::FnActionComponent;
use crate::base::action::FnActionWithComponent;
pub struct AfterPrepareComponent<C: ComponentTrait> {
f: FnActionComponent<C>,
pub struct AfterPrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
referer_type_id: Option<TypeId>,
referer_id: OptionId,
weight: Weight,
}
impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
impl<C: ComponentTrait> ActionTrait for AfterPrepare<C> {
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
@ -23,9 +23,9 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
}
}
impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn new(f: FnActionComponent<C>) -> Self {
AfterPrepareComponent {
impl<C: ComponentTrait> AfterPrepare<C> {
pub fn new(f: FnActionWithComponent<C>) -> Self {
AfterPrepare {
f,
referer_type_id: Some(TypeId::of::<C>()),
referer_id: OptionId::default(),
@ -44,10 +44,25 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx),
);
}
#[inline(always)]
pub(crate) fn dispatch_by_id(component: &mut C, cx: &mut Context) {
if component.id().is_some() {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
None,
Some(TypeId::of::<C>()),
component.id(),
),
|action: &Self| (action.f)(component, cx),
);
}
}
}

View file

@ -1,15 +1,15 @@
use crate::prelude::*;
use super::FnActionComponent;
use crate::base::action::FnActionWithComponent;
pub struct BeforePrepareComponent<C: ComponentTrait> {
f: FnActionComponent<C>,
pub struct BeforePrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
referer_type_id: Option<TypeId>,
referer_id: OptionId,
weight: Weight,
}
impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
impl<C: ComponentTrait> ActionTrait for BeforePrepare<C> {
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
@ -23,9 +23,9 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
}
}
impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn new(f: FnActionComponent<C>) -> Self {
BeforePrepareComponent {
impl<C: ComponentTrait> BeforePrepare<C> {
pub fn new(f: FnActionWithComponent<C>) -> Self {
BeforePrepare {
f,
referer_type_id: Some(TypeId::of::<C>()),
referer_id: OptionId::default(),
@ -44,10 +44,25 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
ActionKey::new(TypeId::of::<Self>(), None, Some(TypeId::of::<C>()), None),
|action: &Self| (action.f)(component, cx),
);
}
#[inline(always)]
pub(crate) fn dispatch_by_id(component: &mut C, cx: &mut Context) {
if component.id().is_some() {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
None,
Some(TypeId::of::<C>()),
component.id(),
),
|action: &Self| (action.f)(component, cx),
);
}
}
}

View file

@ -1,7 +1,3 @@
use crate::prelude::*;
pub type FnActionPage = fn(page: &mut Page);
mod before_prepare_body;
pub use before_prepare_body::*;

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use super::FnActionPage;
pub type FnAfterPrepareBody = fn(page: &mut Page);
pub struct AfterPrepareBody {
f: FnActionPage,
f: FnAfterPrepareBody,
weight: Weight,
}
@ -14,7 +14,7 @@ impl ActionTrait for AfterPrepareBody {
}
impl AfterPrepareBody {
pub fn new(f: FnActionPage) -> Self {
pub fn new(f: FnAfterPrepareBody) -> Self {
AfterPrepareBody { f, weight: 0 }
}
@ -25,8 +25,9 @@ impl AfterPrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use super::FnActionPage;
pub type FnBeforePrepareBody = fn(page: &mut Page);
pub struct BeforePrepareBody {
f: FnActionPage,
f: FnBeforePrepareBody,
weight: Weight,
}
@ -14,7 +14,7 @@ impl ActionTrait for BeforePrepareBody {
}
impl BeforePrepareBody {
pub fn new(f: FnActionPage) -> Self {
pub fn new(f: FnBeforePrepareBody) -> Self {
BeforePrepareBody { f, weight: 0 }
}
@ -25,8 +25,9 @@ impl BeforePrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
dispatch_actions(
ActionKey::new(TypeId::of::<Self>(), None, None, None),
|action: &Self| (action.f)(page),
);
}
}

8
src/base/action/theme.rs Normal file
View file

@ -0,0 +1,8 @@
mod before_prepare_component;
pub use before_prepare_component::*;
mod after_prepare_component;
pub use after_prepare_component::*;
mod render_component;
pub use render_component::*;

View file

@ -0,0 +1,42 @@
use crate::prelude::*;
use crate::base::action::FnActionWithComponent;
pub struct AfterPrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for AfterPrepare<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> AfterPrepare<C> {
pub fn new(theme: ThemeRef, f: FnActionWithComponent<C>) -> Self {
AfterPrepare {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -0,0 +1,42 @@
use crate::prelude::*;
use crate::base::action::FnActionWithComponent;
pub struct BeforePrepare<C: ComponentTrait> {
f: FnActionWithComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for BeforePrepare<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> BeforePrepare<C> {
pub fn new(theme: ThemeRef, f: FnActionWithComponent<C>) -> Self {
BeforePrepare {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &mut C, cx: &mut Context) {
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -0,0 +1,48 @@
use crate::prelude::*;
pub type FnRenderComponent<C> = fn(component: &C, cx: &mut Context) -> Option<Markup>;
pub struct RenderComponent<C: ComponentTrait> {
f: FnRenderComponent<C>,
theme_type_id: Option<TypeId>,
referer_type_id: Option<TypeId>,
}
impl<C: ComponentTrait> ActionTrait for RenderComponent<C> {
fn theme_type_id(&self) -> Option<TypeId> {
self.theme_type_id
}
fn referer_type_id(&self) -> Option<TypeId> {
self.referer_type_id
}
}
impl<C: ComponentTrait> RenderComponent<C> {
pub fn new(theme: ThemeRef, f: FnRenderComponent<C>) -> Self {
RenderComponent {
f,
theme_type_id: Some(theme.type_id()),
referer_type_id: Some(TypeId::of::<C>()),
}
}
#[inline(always)]
pub(crate) fn dispatch(component: &C, cx: &mut Context) -> Option<Markup> {
let mut render_component: Option<Markup> = None;
dispatch_actions(
ActionKey::new(
TypeId::of::<Self>(),
Some(cx.theme().type_id()),
Some(TypeId::of::<C>()),
None,
),
|action: &Self| {
if render_component.is_none() {
render_component = (action.f)(component, cx);
}
},
);
render_component
}
}