🚧 Working on actions system

This commit is contained in:
Manuel Cillero 2024-03-25 17:38:39 +01:00
parent 2ea0a1698e
commit e732244a2e
15 changed files with 165 additions and 153 deletions

View file

@ -52,67 +52,53 @@ impl ThemeTrait for Bootsier {
#[rustfmt::skip]
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
match component.type_id() {
t if t == TypeId::of::<Icon>() => {
if let Some(i) = component_as_mut::<Icon>(component) {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
}
}
t if t == TypeId::of::<Button>() => {
if let Some(b) = component_as_mut::<Button>(component) {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "btn");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "btn-primary",
StyleBase::Info => "btn-info",
StyleBase::Success => "btn-success",
StyleBase::Warning => "btn-warning",
StyleBase::Danger => "btn-danger",
StyleBase::Light => "btn-light",
StyleBase::Dark => "btn-dark",
StyleBase::Link => "btn-link",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
}
}
t if t == TypeId::of::<Heading>() => {
if let Some(h) = component_as_mut::<Heading>(component) {
h.alter_classes(
ClassesOp::Replace(h.size().to_string()),
match h.size() {
HeadingSize::ExtraLarge => "display-1",
HeadingSize::XxLarge => "display-2",
HeadingSize::XLarge => "display-3",
HeadingSize::Large => "display-4",
HeadingSize::Medium => "display-5",
_ => "",
},
);
}
}
t if t == TypeId::of::<Paragraph>() => {
if let Some(p) = component_as_mut::<Paragraph>(component) {
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
}
_ => {}
if let Some(i) = component.downcast_mut::<Icon>() {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
} else if let Some(b) = component.downcast_mut::<Button>() {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "btn");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "btn-primary",
StyleBase::Info => "btn-info",
StyleBase::Success => "btn-success",
StyleBase::Warning => "btn-warning",
StyleBase::Danger => "btn-danger",
StyleBase::Light => "btn-light",
StyleBase::Dark => "btn-dark",
StyleBase::Link => "btn-link",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
} else if let Some(h) = component.downcast_mut::<Heading>() {
h.alter_classes(
ClassesOp::Replace(h.size().to_string()),
match h.size() {
HeadingSize::ExtraLarge => "display-1",
HeadingSize::XxLarge => "display-2",
HeadingSize::XLarge => "display-3",
HeadingSize::Large => "display-4",
HeadingSize::Medium => "display-5",
_ => "",
},
);
} else if let Some(p) = component.downcast_mut::<Paragraph>() {
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
}
fn render_component(&self, component: &dyn ComponentTrait, cx: &mut Context) -> Option<Markup> {
match component.type_id() {
t if t == TypeId::of::<Layout>() => Some(
if component.downcast_ref::<Layout>().is_some() {
Some(
match cx.layout() {
"admin" => Container::new().add_item(
Flex::new()
@ -134,8 +120,9 @@ impl ThemeTrait for Bootsier {
),
}
.render(cx),
),
t if t == TypeId::of::<Error404>() => Some(html! {
)
} else if component.downcast_ref::<Error404>().is_some() {
Some(html! {
div class="jumbotron" {
div class="media" {
img
@ -165,8 +152,9 @@ impl ThemeTrait for Bootsier {
}
}
}
}),
_ => None,
})
} else {
None
}
}
}

View file

@ -41,57 +41,43 @@ impl ThemeTrait for Bulmix {
#[rustfmt::skip]
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
match component.type_id() {
t if t == TypeId::of::<Icon>() => {
if let Some(i) = component_as_mut::<Icon>(component) {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
if let Some(i) = component.downcast_mut::<Icon>() {
i.alter_classes(
ClassesOp::Replace(i.font_size().to_string()),
with_font(i.font_size()),
);
} else if let Some(b) = component.downcast_mut::<Button>() {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "button");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "is-primary",
StyleBase::Info => "is-info",
StyleBase::Success => "is-success",
StyleBase::Warning => "is-warning",
StyleBase::Danger => "is-danger",
StyleBase::Light => "is-light",
StyleBase::Dark => "is-dark",
StyleBase::Link => "is-text",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
} else if let Some(h) = component.downcast_mut::<Heading>() {
match h.size() {
HeadingSize::Subtitle => {
h.alter_classes(ClassesOp::Replace(h.size().to_string()), "subtitle")
}
}
t if t == TypeId::of::<Button>() => {
if let Some(b) = component_as_mut::<Button>(component) {
b.alter_classes(ClassesOp::Replace("button__tap".to_owned()), "button");
b.alter_classes(
ClassesOp::Replace(b.style().to_string()),
match b.style() {
StyleBase::Default => "is-primary",
StyleBase::Info => "is-info",
StyleBase::Success => "is-success",
StyleBase::Warning => "is-warning",
StyleBase::Danger => "is-danger",
StyleBase::Light => "is-light",
StyleBase::Dark => "is-dark",
StyleBase::Link => "is-text",
},
);
b.alter_classes(
ClassesOp::Replace(b.font_size().to_string()),
with_font(b.font_size()),
);
}
}
t if t == TypeId::of::<Heading>() => {
if let Some(h) = component_as_mut::<Heading>(component) {
match h.size() {
HeadingSize::Subtitle => {
h.alter_classes(ClassesOp::Replace(h.size().to_string()), "subtitle")
}
_ => h.alter_classes(ClassesOp::Add, "title"),
};
}
}
t if t == TypeId::of::<Paragraph>() => {
if let Some(p) = component_as_mut::<Paragraph>(component) {
p.alter_classes(ClassesOp::Add, "block");
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
}
_ => {}
_ => h.alter_classes(ClassesOp::Add, "title"),
};
} else if let Some(p) = component.downcast_mut::<Paragraph>() {
p.alter_classes(ClassesOp::Add, "block");
p.alter_classes(
ClassesOp::Replace(p.font_size().to_string()),
with_font(p.font_size()),
);
}
}
@ -100,17 +86,13 @@ impl ThemeTrait for Bulmix {
component: &dyn ComponentTrait,
_cx: &mut Context,
) -> Option<Markup> {
match component.type_id() {
t if t == TypeId::of::<Icon>() => {
if let Some(i) = component_as_ref::<Icon>(component) {
return match i.icon_name().get() {
None => None,
_ => Some(html! { span class="icon" { i class=[i.classes().get()] {} } }),
};
}
None
}
_ => None,
if let Some(i) = component.downcast_ref::<Icon>() {
return match i.icon_name().get() {
None => None,
_ => Some(html! { span class="icon" { i class=[i.classes().get()] {} } }),
};
} else {
None
}
}
}

View file

@ -47,7 +47,7 @@ impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
|action| (action_ref::<AfterPrepareComponent<C>>(&**action).f)(component, cx),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -47,7 +47,7 @@ impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub(crate) fn dispatch(component: &mut C, cx: &mut Context, referer_id: Option<String>) {
dispatch_actions(
(TypeId::of::<Self>(), Some(TypeId::of::<C>()), referer_id),
|action| (action_ref::<BeforePrepareComponent<C>>(&**action).f)(component, cx),
|action: &Self| (action.f)(component, cx),
);
}
}

View file

@ -25,8 +25,8 @@ impl AfterPrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action| {
(action_ref::<AfterPrepareBody>(&**action).f)(page)
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
}
}

View file

@ -25,8 +25,8 @@ impl BeforePrepareBody {
#[inline(always)]
pub(crate) fn dispatch(page: &mut Page) {
dispatch_actions((TypeId::of::<Self>(), None, None), |action| {
(action_ref::<BeforePrepareBody>(&**action).f)(page)
dispatch_actions((TypeId::of::<Self>(), None, None), |action: &Self| {
(action.f)(page)
});
}
}

View file

@ -6,6 +6,8 @@ use std::any::Any;
// Common definitions for core types.
pub trait AnyBase: Any {
fn type_name(&self) -> &'static str;
fn single_name(&self) -> &'static str;
fn as_any_ref(&self) -> &dyn Any;
@ -14,19 +16,55 @@ pub trait AnyBase: Any {
}
impl<T: Any> AnyBase for T {
fn single_name(&self) -> &'static str {
util::single_type_name::<Self>()
#[inline(always)]
fn type_name(&self) -> &'static str {
std::any::type_name::<T>()
}
#[inline(always)]
fn single_name(&self) -> &'static str {
util::single_type_name::<T>()
}
#[inline(always)]
fn as_any_ref(&self) -> &dyn Any {
self
}
#[inline(always)]
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
pub trait AnyTo: AnyBase {
#[inline]
fn is<T>(&self) -> bool
where
T: AnyBase,
{
self.as_any_ref().is::<T>()
}
#[inline]
fn downcast_ref<T>(&self) -> Option<&T>
where
T: AnyBase,
{
self.as_any_ref().downcast_ref()
}
#[inline]
fn downcast_mut<T>(&mut self) -> Option<&mut T>
where
T: AnyBase,
{
self.as_any_mut().downcast_mut()
}
}
impl<T: ?Sized + AnyBase> AnyTo for T {}
// API to define functions that alter the behavior of PageTop core.
pub mod action;

View file

@ -1,5 +1,5 @@
mod definition;
pub use definition::{action_ref, ActionTrait};
pub use definition::ActionTrait;
mod list;
pub use list::Action;

View file

@ -1,4 +1,4 @@
use crate::core::action::{Action, ActionsList};
use crate::core::action::{Action, ActionTrait, ActionsList};
use crate::{LazyStatic, TypeId};
use std::collections::HashMap;
@ -24,9 +24,10 @@ pub fn add_action(action: Action) {
}
}
pub fn dispatch_actions<B, F>(key_action: KeyAction, f: F)
pub fn dispatch_actions<A, B, F>(key_action: KeyAction, f: F)
where
F: FnMut(&Action) -> B,
A: ActionTrait,
F: FnMut(&A) -> B,
{
if let Some(list) = ACTIONS.read().unwrap().get(&key_action) {
list.iter_map(f)

View file

@ -14,7 +14,3 @@ pub trait ActionTrait: AnyBase + Send + Sync {
0
}
}
pub fn action_ref<A: 'static>(action: &dyn ActionTrait) -> &A {
action.as_any_ref().downcast_ref::<A>().unwrap()
}

View file

@ -1,4 +1,6 @@
use crate::core::action::ActionTrait;
use crate::core::AnyTo;
use crate::trace;
use crate::AutoDefault;
use std::sync::{Arc, RwLock};
@ -21,12 +23,25 @@ impl ActionsList {
list.sort_by_key(|a| a.weight());
}
pub fn iter_map<B, F>(&self, f: F)
pub fn iter_map<A, B, F>(&self, mut f: F)
where
Self: Sized,
F: FnMut(&Action) -> B,
A: ActionTrait,
F: FnMut(&A) -> B,
{
let _: Vec<_> = self.0.read().unwrap().iter().map(f).collect();
let _: Vec<_> = self
.0
.read()
.unwrap()
.iter()
.map(|a| {
if let Some(action) = (&**a).downcast_ref::<A>() {
f(action);
} else {
trace::error!("Failed to downcast action of type {}", (&**a).type_name());
}
})
.collect();
}
}

View file

@ -6,7 +6,7 @@ mod renderable;
pub use renderable::{FnIsRenderable, Renderable};
mod definition;
pub use definition::{component_as_mut, component_as_ref, ComponentBase, ComponentTrait};
pub use definition::{ComponentBase, ComponentTrait};
mod classes;
pub use classes::{ComponentClasses, ComponentClassesOp};

View file

@ -83,11 +83,3 @@ impl<C: ComponentTrait> ComponentBase for C {
}
}
}
pub fn component_as_ref<C: ComponentTrait>(component: &dyn ComponentTrait) -> Option<&C> {
component.as_any_ref().downcast_ref::<C>()
}
pub fn component_as_mut<C: ComponentTrait>(component: &mut dyn ComponentTrait) -> Option<&mut C> {
component.as_any_mut().downcast_mut::<C>()
}

View file

@ -93,7 +93,7 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
match component.type_id() {
t if t == TypeId::of::<Block>() => {
if let Some(b) = component_as_mut::<Block>(component) {
if let Some(b) = component.downcast_mut::<Block>() {
b.alter_title("New title");
}
},
@ -114,7 +114,7 @@ pub trait ThemeTrait: PackageTrait + Send + Sync {
match component.type_id() {
t if t == TypeId::of::<Block>() => {
if let Some(b) = component_as_mut::<Block>(component) {
if let Some(b) = component.downcast_mut::<Block>() {
b.alter_title("New title");
}
},

View file

@ -42,7 +42,7 @@ pub use crate::{db, db::*, migrations, new_migration};
pub use crate::service;
pub use crate::service::{HttpMessage, HttpRequest};
pub use crate::core::AnyBase;
pub use crate::core::{AnyBase, AnyTo};
pub use crate::core::action::*;
pub use crate::core::component::*;