Añade soporte para inyectar acciones en código

This commit is contained in:
Manuel Cillero 2025-07-21 08:58:09 +02:00
parent 861392430a
commit c89d4bb5fc
10 changed files with 316 additions and 4 deletions

43
src/core/action/list.rs Normal file
View file

@ -0,0 +1,43 @@
use crate::core::action::{ActionBox, ActionTrait};
use crate::core::AnyCast;
use crate::trace;
use crate::AutoDefault;
use std::sync::RwLock;
#[derive(AutoDefault)]
pub struct ActionsList(RwLock<Vec<ActionBox>>);
impl ActionsList {
pub fn new() -> Self {
ActionsList::default()
}
pub fn add(&mut self, action: ActionBox) {
let mut list = self.0.write().unwrap();
list.push(action);
list.sort_by_key(|a| a.weight());
}
pub fn iter_map<A, B, F>(&self, mut f: F)
where
Self: Sized,
A: ActionTrait,
F: FnMut(&A) -> B,
{
let _: Vec<_> = self
.0
.read()
.unwrap()
.iter()
.rev()
.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();
}
}