Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] - Add support for custom audio file in Windows Toasts #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/custom_sound_sample.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use url::Url;

use winrt_toast_reborn::{Audio, Result, Text, Toast, ToastDuration, ToastManager};

fn main() -> Result<()> {
let manager = ToastManager::new(ToastManager::POWERSHELL_AUM_ID);

let mut toast = Toast::new();

toast
.tag("example")
.text1("Title")
.text2(Text::new("Body"))
.duration(ToastDuration::Long)
.audio(Audio::new_local(Url::from_file_path("path/to/sound/file").unwrap()).with_looping());

manager.show(&toast).expect("Failed to show toast");

Ok(())
}
12 changes: 12 additions & 0 deletions src/content/audio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hs;
use std::fmt::Debug;
use url::Url;
use windows::Data::Xml::Dom::XmlElement;

/// An enum representing the sounds available.
Expand All @@ -17,6 +18,8 @@ pub enum Sound {
SMS,
/// Enable looping sound. See [`LoopingSound`] for the available sounds.
Looping(LoopingSound),
/// Customize sound by providing the Url (path of the local file)
Custom(Url),
/// No sound.
None,
}
Expand All @@ -31,6 +34,7 @@ impl Sound {
Sound::SMS => "SMS",
Sound::Looping(s) => s.as_str(),
Sound::None => "",
_ => "",
}
}
}
Expand Down Expand Up @@ -106,6 +110,11 @@ impl Audio {
}
}

/// Creates a new audio element with custom sound URL.
pub fn new_local(url: url::Url) -> Self {
Self::new(Sound::Custom(url))
}

/// Set the audio to loop.
pub fn with_looping(mut self) -> Self {
self.loop_ = true;
Expand All @@ -131,6 +140,9 @@ impl Audio {
)),
)?;
}
Sound::Custom(url) => {
el.SetAttribute(&hs("src"), &hs(url))?;
}
_ => {
el.SetAttribute(
&hs("src"),
Expand Down