winrt_toast/content/
action.rs

1use windows::Data::Xml::Dom::XmlElement;
2
3use crate::hs;
4
5/// Specifies a button shown in a toast.
6#[derive(Debug, Clone)]
7pub struct Action {
8    content: String,
9    arguments: String,
10    r#type: String,
11    activation_type: Option<ActivationType>,
12    placement: Option<ActionPlacement>,
13}
14
15impl Action {
16    /// Create a new action.
17    ///
18    /// `arguments`: An argument string that can be passed to the associated app
19    /// to provide specifics about the action that it should execute
20    /// in response to the user action.
21    /// 
22    /// `typ`: An argument string that can be passed to the associated app to 
23    /// provide specifics about the action that it should execute in response to the user action.
24    pub fn new(
25        content: impl Into<String>,
26        arguments: impl Into<String>,
27        typ: impl Into<String>,
28    ) -> Self {
29        Self {
30            content: content.into(),
31            arguments: arguments.into(),
32            r#type: typ.into(),
33            activation_type: None,
34            placement: None,
35        }
36    }
37
38    /// The activation type of the action.
39    pub fn with_activation_type(mut self, activation_type: ActivationType) -> Self {
40        self.activation_type = Some(activation_type);
41        self
42    }
43
44    /// The placement of the action.
45    pub fn with_placement(mut self, placement: ActionPlacement) -> Self {
46        self.placement = Some(placement);
47        self
48    }
49
50    pub(crate) fn write_to_element(&self, el: &XmlElement) -> crate::Result<()> {
51        el.SetAttribute(&hs("content"), &hs(&self.content))?;
52        el.SetAttribute(&hs("arguments"), &hs(&self.arguments))?;
53        el.SetAttribute(&hs("type"), &hs(&self.r#type))?;
54        if let Some(activation_type) = self.activation_type {
55            el.SetAttribute(&hs("activationType"), &hs(activation_type.as_str()))?;
56        }
57        if let Some(placement) = self.placement {
58            el.SetAttribute(&hs("placement"), &hs(placement.as_str()))?;
59        }
60
61        Ok(())
62    }
63}
64
65/// The type of activation that will be used when the user interacts with a specific action
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub enum ActivationType {
68    /// Default value. Your foreground app is launched.
69    Foreground,
70    /// Your corresponding background task is triggered, and you can execute code in the background without interrupting the user.
71    Background,
72    /// Launch a different app using protocol activation.
73    Protocol,
74}
75
76impl ActivationType {
77    fn as_str(&self) -> &'static str {
78        match self {
79            ActivationType::Foreground => "foreground",
80            ActivationType::Background => "background",
81            ActivationType::Protocol => "protocol",
82        }
83    }
84}
85
86/// The location of the action
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub enum ActionPlacement {
89    /// The action becomes a context menu action added to the toast notification's
90    /// context menu rather than a traditional toast button.
91    ContextMenu,
92}
93
94impl ActionPlacement {
95    fn as_str(&self) -> &'static str {
96        match self {
97            ActionPlacement::ContextMenu => "contextMenu",
98        }
99    }
100}