Skip to main content

ferroui_xaml_interactions/core/
invoke_command_action.rs

1//! Port of `Core/InvokeCommandAction.cs`.
2
3use ferroui_base::{ferro_class, ferro_class_info, instantiate, BoxedValue, FerroObjectImpl, Ref, StyledElementImpl};
4use ferroui_xaml_interactivity::{InvokeCommandActionBase, StyledElementActionImpl};
5use std::rc::Rc;
6
7/// Executes a specified command (`ICommand`) when invoked.
8#[repr(C)]
9pub struct InvokeCommandAction {
10    base: InvokeCommandActionBase,
11}
12
13ferro_class!(InvokeCommandAction: InvokeCommandActionBase);
14ferro_class_info!(InvokeCommandAction { new: InvokeCommandAction::new });
15ferroui_base::ferro_impl_classes!(InvokeCommandAction: FerroObjectImpl, StyledElementImpl);
16
17impl StyledElementActionImpl for InvokeCommandAction {
18    /// Executes the action.
19    ///
20    /// `sender` is the object that is passed to the action by the behavior. Generally this is the
21    /// associated object of the behavior or a target object. The value of `parameter` is
22    /// determined by the caller.
23    ///
24    /// Returns true if the command is successfully executed; else false.
25    fn execute(this: &Self, _sender: Option<&BoxedValue>, parameter: Option<&BoxedValue>) -> Option<BoxedValue> {
26        let command = this.command();
27        let Some(command) = command.filter(|_| this.is_enabled()) else {
28            return Some(Rc::new(false));
29        };
30
31        let resolved_parameter = this.resolve_parameter(parameter);
32
33        if !command.can_execute(resolved_parameter.as_ref()) {
34            return Some(Rc::new(false));
35        }
36
37        command.execute(resolved_parameter.as_ref());
38
39        Some(Rc::new(true))
40    }
41}
42
43impl InvokeCommandAction {
44    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
45    pub fn construct() -> Self {
46        Self { base: InvokeCommandActionBase::construct() }
47    }
48
49    /// Creates an action.
50    pub fn new() -> Ref<Self> {
51        instantiate(Self::construct())
52    }
53}