Skip to main content

ferroui_xaml_interactivity/templates/
action_collection_template.rs

1//! Port of `Templates/ActionCollectionTemplate.cs`.
2
3use crate::ActionCollection;
4use ferroui_base::styling::ITemplate;
5use ferroui_base::{ferro_markup_type, BoxedValue};
6use ferroui_markup_xaml::templates::TemplateContent;
7use std::cell::RefCell;
8use std::rc::Rc;
9
10/// A template for creating a collection of [`IAction`](crate::IAction) objects.
11///
12/// As the value of a style setter of the `Actions` of a
13/// [`StyledElementTrigger`](crate::StyledElementTrigger) it builds a new collection for each
14/// trigger the style applies to.
15#[derive(Default)]
16pub struct ActionCollectionTemplate {
17    content: RefCell<Option<BoxedValue>>,
18}
19
20impl ActionCollectionTemplate {
21    /// Creates an empty template.
22    pub fn new() -> Rc<Self> {
23        Rc::new(Self::default())
24    }
25
26    /// Gets or sets the content of the template: deferred content built on each
27    /// [`build`](Self::build).
28    pub fn content(&self) -> Option<BoxedValue> {
29        self.content.borrow().clone()
30    }
31
32    pub fn set_content(&self, value: Option<BoxedValue>) {
33        *self.content.borrow_mut() = value;
34    }
35
36    /// Builds the collection of actions from the template.
37    ///
38    /// # Panics
39    /// Panics if the content is not template content, or builds something else than a
40    /// collection of actions.
41    pub fn build(&self) -> Option<ActionCollection> {
42        TemplateContent::load_as::<ActionCollection>(self.content().as_ref()).map(|result| result.result().clone())
43    }
44}
45
46impl ITemplate for ActionCollectionTemplate {
47    /// Builds the collection of actions from the template, as the value of the property (an
48    /// optional collection).
49    fn build(&self) -> BoxedValue {
50        Rc::new(ActionCollectionTemplate::build(self))
51    }
52}
53
54impl PartialEq for ActionCollectionTemplate {
55    fn eq(&self, other: &Self) -> bool {
56        std::ptr::eq(self, other)
57    }
58}
59
60ferro_markup_type!(class ActionCollectionTemplate {
61    this: Rc<ActionCollectionTemplate>,
62    handles: [ActionCollectionTemplate, Rc<ActionCollectionTemplate>, Option<Rc<ActionCollectionTemplate>>],
63    interfaces: [Rc<dyn ITemplate>],
64    constructors: [() => ActionCollectionTemplate::new],
65    content: Content,
66    properties: [
67        Content: Option<BoxedValue> {
68            get: ActionCollectionTemplate::content,
69            set: ActionCollectionTemplate::set_content
70        } [TemplateContent(TemplateResultType = type(ActionCollection))],
71    ],
72});