winrt_toast_reborn/content/
audio.rs

1use crate::hs;
2use std::fmt::Debug;
3use windows::Data::Xml::Dom::XmlElement;
4
5/// An enum representing the sounds available.
6#[derive(Debug, Clone)]
7pub enum Sound {
8    /// The default system sound.
9    Default,
10    /// A sound typically used for instant messages.
11    IM,
12    /// A sound typically used for incoming mail.
13    Mail,
14    /// A sound typically used for reminders.
15    Reminder,
16    /// A sound typically used for incoming SMS messages.
17    SMS,
18    /// Enable looping sound. See [`LoopingSound`] for the available sounds.
19    Looping(LoopingSound),
20    /// No sound.
21    None,
22}
23
24impl Sound {
25    fn as_str(&self) -> &'static str {
26        match self {
27            Sound::Default => "Default",
28            Sound::IM => "IM",
29            Sound::Mail => "Mail",
30            Sound::Reminder => "Reminder",
31            Sound::SMS => "SMS",
32            Sound::Looping(s) => s.as_str(),
33            Sound::None => "",
34        }
35    }
36}
37
38/// An enum representing the looping sounds available.
39#[allow(missing_docs)]
40#[derive(Debug, Clone)]
41pub enum LoopingSound {
42    Alarm,
43    Alarm2,
44    Alarm3,
45    Alarm4,
46    Alarm5,
47    Alarm6,
48    Alarm7,
49    Alarm8,
50    Alarm9,
51    Alarm10,
52    Call,
53    Call2,
54    Call3,
55    Call4,
56    Call5,
57    Call6,
58    Call7,
59    Call8,
60    Call9,
61    Call10,
62}
63
64impl LoopingSound {
65    fn as_str(&self) -> &'static str {
66        match self {
67            LoopingSound::Alarm => "Alarm",
68            LoopingSound::Alarm2 => "Alarm2",
69            LoopingSound::Alarm3 => "Alarm3",
70            LoopingSound::Alarm4 => "Alarm4",
71            LoopingSound::Alarm5 => "Alarm5",
72            LoopingSound::Alarm6 => "Alarm6",
73            LoopingSound::Alarm7 => "Alarm7",
74            LoopingSound::Alarm8 => "Alarm8",
75            LoopingSound::Alarm9 => "Alarm9",
76            LoopingSound::Alarm10 => "Alarm10",
77            LoopingSound::Call => "Call",
78            LoopingSound::Call2 => "Call2",
79            LoopingSound::Call3 => "Call3",
80            LoopingSound::Call4 => "Call4",
81            LoopingSound::Call5 => "Call5",
82            LoopingSound::Call6 => "Call6",
83            LoopingSound::Call7 => "Call7",
84            LoopingSound::Call8 => "Call8",
85            LoopingSound::Call9 => "Call9",
86            LoopingSound::Call10 => "Call10",
87        }
88    }
89}
90
91/// Represents an audio element in a toast.
92#[derive(Debug, Clone)]
93pub struct Audio {
94    src: Sound,
95    loop_: bool,
96    silent: bool,
97}
98
99impl Audio {
100    /// Create a new audio element.
101    pub fn new(src: Sound) -> Self {
102        Self {
103            src,
104            loop_: false,
105            silent: false,
106        }
107    }
108
109    /// Set the audio to loop.
110    pub fn with_looping(mut self) -> Self {
111        self.loop_ = true;
112        self
113    }
114
115    /// Set the audio to be silent.
116    pub fn with_silent(mut self) -> Self {
117        self.silent = true;
118        self
119    }
120
121    pub(crate) fn write_to_element(&self, el: &XmlElement) -> crate::Result<()> {
122        let mut silent = self.silent;
123        match &self.src {
124            Sound::None => silent = true,
125            Sound::Looping(s) => {
126                el.SetAttribute(
127                    &hs("src"),
128                    &hs(format!(
129                        "ms-winsoundevent:Notification.Looping.{}",
130                        s.as_str(),
131                    )),
132                )?;
133            }
134            _ => {
135                el.SetAttribute(
136                    &hs("src"),
137                    &hs(format!(
138                        "ms-winsoundevent:Notification.{}",
139                        self.src.as_str(),
140                    )),
141                )?;
142            }
143        }
144        el.SetAttribute(&hs("loop"), &hs(if self.loop_ { "true" } else { "false" }))?;
145        el.SetAttribute(&hs("silent"), &hs(if silent { "true" } else { "false" }))?;
146
147        Ok(())
148    }
149}