use crate::app::App;
pub struct Action {
pub name: String,
pub is_enabled: Box<dyn Fn(&App) -> bool>,
pub execute: Box<dyn Fn(&mut App) -> ActionResult>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ActionResult {
#[allow(dead_code)]
NoChange,
StateUpdated,
DialogOpened,
#[allow(dead_code)]
Cancelled,
}
impl Action {
pub fn new(
name: impl Into<String>,
is_enabled: impl Fn(&App) -> bool + 'static,
execute: impl Fn(&mut App) -> ActionResult + 'static,
) -> Self {
Self {
name: name.into(),
is_enabled: Box::new(is_enabled),
execute: Box::new(execute),
}
}
#[allow(dead_code)]
pub fn always_enabled(
name: impl Into<String>,
execute: impl Fn(&mut App) -> ActionResult + 'static,
) -> Self {
Self::new(name, |_| true, execute)
}
pub fn enabled(&self, app: &App) -> bool {
(self.is_enabled)(app)
}
pub fn run(&self, app: &mut App) -> ActionResult {
(self.execute)(app)
}
pub fn name(&self) -> &str {
&self.name
}
}
pub type ActionList = Vec<Action>;