Skip to main content

ferroui_xaml_interactivity/triggers/
interactive_trigger_base.rs

1//! Port of `Triggers/InteractiveTriggerBase.cs`.
2
3use crate::{Interaction, StyledElementBehaviorImpl, StyledElementTriggerOfT};
4use ferroui_base::interactivity::{Interactive, RoutingStrategies};
5use ferroui_base::{ferro_class, ferro_properties, BoxedValue, FerroObjectImpl, FerroProperty, StyledElementImpl, StyledProperty};
6use std::rc::Rc;
7
8/// Base class for triggers that listen for routed events (a trigger attached to an
9/// [`Interactive`]).
10#[repr(C)]
11pub struct InteractiveTriggerBase {
12    base: StyledElementTriggerOfT,
13}
14
15ferro_class!(InteractiveTriggerBase: StyledElementTriggerOfT);
16ferroui_base::ferro_impl_classes!(InteractiveTriggerBase: FerroObjectImpl, StyledElementImpl, StyledElementBehaviorImpl);
17
18ferro_properties! {
19    impl InteractiveTriggerBase {
20        /// Identifies the `RoutingStrategies` property.
21        pub fn routing_strategies_property() -> StyledProperty<RoutingStrategies> {
22            FerroProperty::register::<InteractiveTriggerBase, _>("RoutingStrategies", RoutingStrategies::BUBBLE)
23        }
24    }
25}
26
27impl InteractiveTriggerBase {
28    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
29    pub fn construct() -> Self {
30        Self { base: StyledElementTriggerOfT::construct(<Interactive as ferroui_base::StaticType>::TYPE) }
31    }
32
33    /// Gets or sets the routing strategies used when subscribing to events.
34    pub fn routing_strategies(&self) -> RoutingStrategies {
35        self.get_value(Self::routing_strategies_property())
36    }
37
38    pub fn set_routing_strategies(&self, value: RoutingStrategies) {
39        self.set_value(Self::routing_strategies_property(), value)
40    }
41
42    /// Executes the actions associated with this trigger, with the event args as the parameter.
43    pub fn execute(&self, parameter: Option<&BoxedValue>) {
44        if !self.is_enabled() {
45            return;
46        }
47
48        let sender = self.associated_object().map(|associated_object| Rc::new(associated_object) as BoxedValue);
49        Interaction::execute_actions(sender.as_ref(), self.actions().as_ref(), parameter);
50    }
51}