🚧 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

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