Skip to main content

ferroui_xaml_interactivity/templates/
notification_template.rs

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