Skip to main content

ferroui_xaml_interactivity/ferro_object/
trigger.rs

1//! Port of `FerroObject/Trigger.cs`.
2
3use crate::{ActionCollection, Behavior, BehaviorImpl, IBehavior, ITrigger};
4use ferroui_base::{ferro_class, ferro_class_info, ferro_properties, DirectProperty, FerroObject, FerroObjectImpl, FerroProperty, Ref};
5use std::cell::OnceCell;
6
7/// A base class for behaviors, implementing the basic plumbing of [`ITrigger`].
8///
9/// The class is abstract. Its content (markup's children of a trigger) is its
10/// [`actions`](Self::actions).
11#[repr(C)]
12pub struct Trigger {
13    base: Behavior,
14    actions: OnceCell<ActionCollection>,
15}
16
17ferro_class!(Trigger: Behavior);
18ferro_class_info!(Trigger {
19    markup: {
20        content: Actions,
21    },
22});
23ferroui_base::ferro_impl_classes!(Trigger: FerroObjectImpl, BehaviorImpl);
24
25ferro_properties! {
26    impl Trigger {
27        /// Identifies the `Actions` property.
28        pub fn actions_property() -> DirectProperty<Trigger, ActionCollection> {
29            FerroProperty::register_direct::<Trigger, _>("Actions", |t| t.actions(), None, ActionCollection::new())
30        }
31    }
32}
33
34impl Trigger {
35    /// The static constructor: the class implements the trigger contract.
36    fn static_constructor() {
37        <dyn ITrigger>::register::<Trigger>();
38    }
39
40    /// Creates the class data; see [`FerroObject::construct`].
41    pub fn construct() -> Self {
42        Self { base: Behavior::construct(), actions: OnceCell::new() }
43    }
44
45    /// Gets the collection of actions associated with the behavior. This is a property of the
46    /// object model.
47    pub fn actions(&self) -> ActionCollection {
48        self.actions.get_or_init(ActionCollection::new).clone()
49    }
50}
51
52impl IBehavior for Trigger {
53    fn associated_object(&self) -> Option<Ref<FerroObject>> {
54        Behavior::associated_object(self)
55    }
56
57    fn attach(&self, associated_object: Option<Ref<FerroObject>>) {
58        Behavior::attach(self, associated_object)
59    }
60
61    fn detach(&self) {
62        Behavior::detach(self)
63    }
64}
65
66impl ITrigger for Trigger {
67    fn actions(&self) -> Option<ActionCollection> {
68        Some(Trigger::actions(self))
69    }
70}