Skip to main content

ferroui_xaml_interactions_custom/render_target/
render_render_target_bitmap_action.rs

1//! Port of `RenderTarget/RenderRenderTargetBitmapAction.cs`.
2
3use crate::IRenderTargetBitmapRenderHost;
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/// Action that invokes [`IRenderTargetBitmapRenderHost::render`] on the specified target.
12#[repr(C)]
13pub struct RenderRenderTargetBitmapAction {
14    base: StyledElementAction,
15}
16
17ferro_class!(RenderRenderTargetBitmapAction: StyledElementAction);
18ferro_class_info!(RenderRenderTargetBitmapAction {
19    new: RenderRenderTargetBitmapAction::new,
20    markup: {
21        property_attributes: [
22            Target: [ResolveByName],
23        ],
24    },
25});
26ferroui_base::ferro_impl_classes!(RenderRenderTargetBitmapAction: FerroObjectImpl, StyledElementImpl);
27
28ferro_properties! {
29    impl RenderRenderTargetBitmapAction {
30        /// Identifies the `Target` property.
31        pub fn target_property() -> StyledProperty<Option<Rc<dyn IRenderTargetBitmapRenderHost>>> {
32            FerroProperty::register::<RenderRenderTargetBitmapAction, _>("Target", None)
33        }
34    }
35}
36
37impl StyledElementActionImpl for RenderRenderTargetBitmapAction {
38    fn execute(this: &Self, _sender: Option<&BoxedValue>, _parameter: Option<&BoxedValue>) -> Option<BoxedValue> {
39        if !this.is_enabled() {
40            return Some(Rc::new(false));
41        }
42
43        if let Some(target) = this.target() {
44            target.render();
45        }
46
47        Some(Rc::new(true))
48    }
49}
50
51impl RenderRenderTargetBitmapAction {
52    /// Creates the class data; see [`ferroui_base::FerroObject::construct`].
53    pub fn construct() -> Self {
54        Self { base: StyledElementAction::construct() }
55    }
56
57    /// Creates an action.
58    pub fn new() -> Ref<Self> {
59        instantiate(Self::construct())
60    }
61
62    /// Gets or sets the render host. This is a property of the object model; markup resolves a
63    /// name given for it to the named host.
64    pub fn target(&self) -> Option<Rc<dyn IRenderTargetBitmapRenderHost>> {
65        self.get_value(Self::target_property())
66    }
67
68    pub fn set_target(&self, value: Option<Rc<dyn IRenderTargetBitmapRenderHost>>) {
69        self.set_value(Self::target_property(), value)
70    }
71}