mirror of
https://github.com/tauri-apps/winrt-notification.git
synced 2026-01-31 00:45:19 +01:00
feat: add on_activated (#9)
* Expose Activated event
Allows users to set an ToastNotification.Activated callback. Currently requires the callback to be 'static; this likely can be fixed by some lifetime and boxing trickery at the cost of readability.
* Show callback functionality in example.
* Revert "Show callback functionality in example."
This reverts commit 62fc7a0bc6.
* Simplify callback in example.
May be a candidate for a new example instead of polluting the "simple" example.
* Change FnMut requirement to FnOnce
Loosens the requirements on `on_activated.` Turns out I had my Fn trait hierarchies wrong :(
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
* Change callback back to FnMut, don't send params to callback
* Whoops that's not how closures move
* Remove callback params from simple.rs
* cargo fmt
* Create on-activated.md
* Update examples/simple.rs
This commit is contained in:
5
.changes/on-activated.md
Normal file
5
.changes/on-activated.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri-winrt-notification": patch
|
||||
---
|
||||
|
||||
Add `Toast::on_activated` to add a handler to run when the toast is activated.
|
||||
@@ -3,6 +3,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{process::exit, thread::sleep, time::Duration as StdDuration};
|
||||
|
||||
use tauri_winrt_notification::{Duration, Sound, Toast};
|
||||
|
||||
fn main() {
|
||||
@@ -11,6 +13,15 @@ fn main() {
|
||||
.text1("(╯°□°)╯︵ ┻━┻")
|
||||
.sound(Some(Sound::SMS))
|
||||
.duration(Duration::Short)
|
||||
.on_activated(handle_notification_click)
|
||||
.show()
|
||||
.expect("unable to send notification");
|
||||
println!("Waiting 10 seconds for the notification to be clicked...");
|
||||
sleep(StdDuration::from_secs(10));
|
||||
println!("The notification wasn't clicked!");
|
||||
}
|
||||
|
||||
fn handle_notification_click() -> windows::core::Result<()> {
|
||||
println!("You've clicked me!");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -32,7 +32,10 @@
|
||||
/// https://softwareengineering.stackexchange.com/questions/222339/using-the-system-tray-notification-area-app-in-windows-7
|
||||
|
||||
/// For actions look at https://docs.microsoft.com/en-us/dotnet/api/microsoft.toolkit.uwp.notifications.toastactionscustom?view=win-comm-toolkit-dotnet-7.0
|
||||
use windows::{Data::Xml::Dom::XmlDocument, UI::Notifications::ToastNotificationManager};
|
||||
use windows::{
|
||||
core::IInspectable, Data::Xml::Dom::XmlDocument, Foundation::TypedEventHandler,
|
||||
UI::Notifications::ToastNotificationManager,
|
||||
};
|
||||
|
||||
use std::fmt::Display;
|
||||
use std::path::Path;
|
||||
@@ -52,6 +55,7 @@ pub struct Toast {
|
||||
audio: String,
|
||||
app_id: String,
|
||||
scenario: String,
|
||||
on_activated: Option<TypedEventHandler<ToastNotification, IInspectable>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -264,6 +268,7 @@ impl Toast {
|
||||
audio: String::new(),
|
||||
app_id: app_id.to_string(),
|
||||
scenario: String::new(),
|
||||
on_activated: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,6 +406,13 @@ impl Toast {
|
||||
self
|
||||
}
|
||||
|
||||
// HACK: f is static so that we know the function is valid to call.
|
||||
// this would be nice to remove at some point
|
||||
pub fn on_activated<F: FnMut() -> Result<()> + Send + 'static>(mut self, mut f: F) -> Self {
|
||||
self.on_activated = Some(TypedEventHandler::new(move |_, _| f()));
|
||||
self
|
||||
}
|
||||
|
||||
fn create_template(&self) -> windows::core::Result<ToastNotification> {
|
||||
//using this to get an instance of XmlDocument
|
||||
let toast_xml = XmlDocument::new()?;
|
||||
@@ -443,6 +455,9 @@ impl Toast {
|
||||
/// Display the toast on the screen
|
||||
pub fn show(&self) -> windows::core::Result<()> {
|
||||
let toast_template = self.create_template()?;
|
||||
if let Some(handler) = &self.on_activated {
|
||||
toast_template.Activated(handler)?;
|
||||
}
|
||||
|
||||
let toast_notifier =
|
||||
ToastNotificationManager::CreateToastNotifierWithId(&HSTRING::from(&self.app_id))?;
|
||||
|
||||
Reference in New Issue
Block a user