Skip to main content

ferroui_xaml_interactions_custom/writeable_bitmap/
writeable_bitmap_trigger.rs

1//! Port of `WriteableBitmap/WriteableBitmapTrigger.cs`.
2
3use crate::WriteableBitmapRef;
4use ferroui_base::{
5    ferro_class, ferro_class_info, ferro_properties, instantiate, BoxedValue, FerroObjectImpl, FerroProperty, Ref,
6    StyledElementImpl, StyledProperty,
7};
8use ferroui_xaml_interactivity::{Interaction, StyledElementBehaviorImpl, StyledElementTrigger};
9use std::rc::Rc;
10
11/// A trigger that executes its actions when [`trigger`](Self::trigger) is called, passing a
12/// [`WriteableBitmap`](ferroui_base::media::imaging::WriteableBitmap) as parameter.
13#[repr(C)]
14pub struct WriteableBitmapTrigger {
15    base: StyledElementTrigger,
16}
17
18ferro_class!(WriteableBitmapTrigger: StyledElementTrigger);
19ferro_class_info!(WriteableBitmapTrigger {
20    new: WriteableBitmapTrigger::new,
21    markup: {
22        property_attributes: [
23            Bitmap: [ResolveByName],
24        ],
25    },
26});
27ferroui_base::ferro_impl_classes!(WriteableBitmapTrigger: FerroObjectImpl, StyledElementImpl, StyledElementBehaviorImpl);
28
29ferro_properties! {
30    impl WriteableBitmapTrigger {
31        /// Identifies the `Bitmap` property.
32        pub fn bitmap_property() -> StyledProperty<Option<WriteableBitmapRef>> {
33            FerroProperty::register::<WriteableBitmapTrigger, _>("Bitmap", None)
34        }
35    }
36}
37
38impl WriteableBitmapTrigger {
39    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
40    pub fn construct() -> Self {
41        Self { base: StyledElementTrigger::construct() }
42    }
43
44    /// Creates a trigger.
45    pub fn new() -> Ref<Self> {
46        instantiate(Self::construct())
47    }
48
49    /// Gets or sets the bitmap passed to actions. This is a property of the object model.
50    pub fn bitmap(&self) -> Option<WriteableBitmapRef> {
51        self.get_value(Self::bitmap_property())
52    }
53
54    pub fn set_bitmap(&self, value: Option<WriteableBitmapRef>) {
55        self.set_value(Self::bitmap_property(), value)
56    }
57
58    /// Manually invokes the trigger.
59    pub fn trigger(&self) {
60        if !self.is_enabled() {
61            return;
62        }
63
64        let sender = self.associated_object().map(|associated_object| Rc::new(associated_object) as BoxedValue);
65        let parameter = self.bitmap().map(|bitmap| Rc::new(bitmap) as BoxedValue);
66        Interaction::execute_actions(sender.as_ref(), self.actions().as_ref(), parameter.as_ref());
67    }
68}