Skip to main content

ferroui_xaml_interactions_custom/writeable_bitmap/
writeable_bitmap_render_action.rs

1//! Port of `WriteableBitmap/WriteableBitmapRenderAction.cs`.
2
3use crate::{IWriteableBitmapRenderer, 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::{StyledElementAction, StyledElementActionImpl};
9use std::rc::Rc;
10
11/// Invokes an [`IWriteableBitmapRenderer`] to render into a bitmap.
12#[repr(C)]
13pub struct WriteableBitmapRenderAction {
14    base: StyledElementAction,
15}
16
17ferro_class!(WriteableBitmapRenderAction: StyledElementAction);
18ferro_class_info!(WriteableBitmapRenderAction {
19    new: WriteableBitmapRenderAction::new,
20    markup: {
21        property_attributes: [
22            Bitmap: [ResolveByName],
23        ],
24    },
25});
26ferroui_base::ferro_impl_classes!(WriteableBitmapRenderAction: FerroObjectImpl, StyledElementImpl);
27
28ferro_properties! {
29    impl WriteableBitmapRenderAction {
30        /// Identifies the `Renderer` property.
31        pub fn renderer_property() -> StyledProperty<Option<Rc<dyn IWriteableBitmapRenderer>>> {
32            FerroProperty::register::<WriteableBitmapRenderAction, _>("Renderer", None)
33        }
34
35        /// Identifies the `Bitmap` property.
36        pub fn bitmap_property() -> StyledProperty<Option<WriteableBitmapRef>> {
37            FerroProperty::register::<WriteableBitmapRenderAction, _>("Bitmap", None)
38        }
39    }
40}
41
42impl StyledElementActionImpl for WriteableBitmapRenderAction {
43    fn execute(this: &Self, _sender: Option<&BoxedValue>, _parameter: Option<&BoxedValue>) -> Option<BoxedValue> {
44        if !this.is_enabled() {
45            return Some(Rc::new(false));
46        }
47
48        let (Some(renderer), Some(bitmap)) = (this.renderer(), this.bitmap()) else {
49            return Some(Rc::new(false));
50        };
51
52        renderer.render(&bitmap);
53        Some(Rc::new(true))
54    }
55}
56
57impl WriteableBitmapRenderAction {
58    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
59    pub fn construct() -> Self {
60        Self { base: StyledElementAction::construct() }
61    }
62
63    /// Creates an action.
64    pub fn new() -> Ref<Self> {
65        instantiate(Self::construct())
66    }
67
68    /// Gets or sets the renderer used when executing the action. This is a property of the object
69    /// model.
70    pub fn renderer(&self) -> Option<Rc<dyn IWriteableBitmapRenderer>> {
71        self.get_value(Self::renderer_property())
72    }
73
74    pub fn set_renderer(&self, value: Option<Rc<dyn IWriteableBitmapRenderer>>) {
75        self.set_value(Self::renderer_property(), value)
76    }
77
78    /// Gets or sets the target bitmap. This is a property of the object model.
79    pub fn bitmap(&self) -> Option<WriteableBitmapRef> {
80        self.get_value(Self::bitmap_property())
81    }
82
83    pub fn set_bitmap(&self, value: Option<WriteableBitmapRef>) {
84        self.set_value(Self::bitmap_property(), value)
85    }
86}