Skip to main content

ferroui_xaml_interactions_custom/icon/
set_path_icon_data_action.rs

1//! Port of `Icon/SetPathIconDataAction.cs`.
2
3use ferroui_base::data::core::ValueTypes;
4use ferroui_base::media::Geometry;
5use ferroui_base::{
6    ferro_class, ferro_class_info, ferro_properties, instantiate, BoxedValue, ElementRef, FerroObjectImpl,
7    FerroProperty, Nullable, Ref, StyledElementImpl, StyledProperty,
8};
9use ferroui_controls::PathIcon;
10use ferroui_xaml_interactivity::helpers::member_lookup::object_of;
11use ferroui_xaml_interactivity::{StyledElementAction, StyledElementActionImpl};
12use std::rc::Rc;
13
14/// Changes the [`PathIcon::data`] of a target icon when executed.
15#[repr(C)]
16pub struct SetPathIconDataAction {
17    base: StyledElementAction,
18}
19
20ferro_class!(SetPathIconDataAction: StyledElementAction);
21ferro_class_info!(SetPathIconDataAction {
22    new: SetPathIconDataAction::new,
23    markup: {
24        property_attributes: [
25            PathIcon: [ResolveByName],
26        ],
27    },
28});
29ferroui_base::ferro_impl_classes!(SetPathIconDataAction: FerroObjectImpl, StyledElementImpl);
30
31ferro_properties! {
32    impl SetPathIconDataAction {
33        /// Identifies the `Data` property.
34        pub fn data_property() -> StyledProperty<Option<Ref<Geometry>>> {
35            FerroProperty::register::<SetPathIconDataAction, _>("Data", None)
36        }
37
38        /// Identifies the `PathIcon` property.
39        ///
40        /// Deviation (DEVIATIONS.md, Behaviors): the action does not own the icon, so the value is
41        /// an element reference.
42        pub fn path_icon_property() -> StyledProperty<Option<ElementRef<PathIcon>>> {
43            ValueTypes::register_element_ref::<PathIcon>();
44            FerroProperty::register::<SetPathIconDataAction, _>("PathIcon", None)
45        }
46    }
47}
48
49impl StyledElementActionImpl for SetPathIconDataAction {
50    fn execute(this: &Self, sender: Option<&BoxedValue>, _parameter: Option<&BoxedValue>) -> Option<BoxedValue> {
51        if !this.is_enabled() {
52            return Some(Rc::new(false));
53        }
54
55        let target =
56            this.path_icon().or_else(|| sender.and_then(object_of).and_then(|sender| sender.cast::<PathIcon>()));
57        let Some(target) = target else {
58            return Some(Rc::new(false));
59        };
60
61        target.set_data(this.data());
62        Some(Rc::new(true))
63    }
64}
65
66impl SetPathIconDataAction {
67    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
68    pub fn construct() -> Self {
69        Self { base: StyledElementAction::construct() }
70    }
71
72    /// Creates an action.
73    pub fn new() -> Ref<Self> {
74        instantiate(Self::construct())
75    }
76
77    /// Gets or sets the geometry to apply. This is a property of the object model.
78    pub fn data(&self) -> Option<Ref<Geometry>> {
79        self.get_value(Self::data_property())
80    }
81
82    pub fn set_data(&self, value: impl Into<Nullable<Geometry>>) {
83        self.set_value(Self::data_property(), value.into().0)
84    }
85
86    /// Gets or sets the target icon. This is a property of the object model. Markup resolves a
87    /// name given for it to the named element.
88    pub fn path_icon(&self) -> Option<Ref<PathIcon>> {
89        ElementRef::resolve(&self.get_value(Self::path_icon_property()))
90    }
91
92    pub fn set_path_icon(&self, value: impl Into<Nullable<PathIcon>>) {
93        self.set_value(Self::path_icon_property(), ElementRef::from_nullable(value.into().0))
94    }
95}