| 1 |
use crate::response::NotificationResponse; |
| 2 |
use crate::CloseReason; |
| 3 |
pub use crate::{error::*, notification::Notification, timeout::Timeout, urgency::Urgency}; |
| 4 |
|
| 5 |
use std::{ |
| 6 |
ops::{Deref, DerefMut}, |
| 7 |
path::Path, |
| 8 |
str::FromStr, |
| 9 |
sync::mpsc::{channel, Receiver}, |
| 10 |
}; |
| 11 |
|
| 12 |
use winrt_notification::{Toast, ToastDismissalReason}; |
| 13 |
|
| 14 |
pub(crate) fn show_notification(notification: &Notification) -> Result<NotificationHandle> { |
| 15 |
let (sender, receiver) = channel(); |
| 16 |
let activated_sender = sender.clone(); |
| 17 |
|
| 18 |
let mut toast = build_toast(notification); |
| 19 |
|
| 20 |
for action in notification.actions.chunks(2) { |
| 21 |
if let [identifier, label] = action { |
| 22 |
toast = toast.add_button(label, identifier); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
toast = toast |
| 27 |
.on_activated(move |action| { |
| 28 |
let response = match action { |
| 29 |
None => NotificationResponse::Default, |
| 30 |
Some(key) => NotificationResponse::Action(key), |
| 31 |
}; |
| 32 |
let _ = activated_sender.send(response); |
| 33 |
Ok(()) |
| 34 |
}) |
| 35 |
.on_dismissed(move |reason| { |
| 36 |
let _ = sender.send(NotificationResponse::Closed(reason.into())); |
| 37 |
Ok(()) |
| 38 |
}); |
| 39 |
|
| 40 |
toast |
| 41 |
.show() |
| 42 |
.map_err(|error| Error::from(ErrorKind::Msg(format!("{error:?}"))))?; |
| 43 |
|
| 44 |
Ok(NotificationHandle { |
| 45 |
notification: notification.clone(), |
| 46 |
events: receiver, |
| 47 |
}) |
| 48 |
} |
| 49 |
|
| 50 |
fn build_toast(notification: &Notification) -> Toast { |
| 51 |
let sound = match ¬ification.sound_name { |
| 52 |
Some(chosen_sound_name) => winrt_notification::Sound::from_str(chosen_sound_name).ok(), |
| 53 |
None => None, |
| 54 |
}; |
| 55 |
|
| 56 |
let duration = match notification.timeout { |
| 57 |
Timeout::Default => winrt_notification::Duration::Short, |
| 58 |
Timeout::Never => winrt_notification::Duration::Long, |
| 59 |
Timeout::Milliseconds(t) => { |
| 60 |
if t >= 25000 { |
| 61 |
winrt_notification::Duration::Long |
| 62 |
} else { |
| 63 |
winrt_notification::Duration::Short |
| 64 |
} |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
let scenario = match notification.urgency { |
| 72 |
Some(Urgency::Critical) => Some(winrt_notification::Scenario::Reminder), |
| 73 |
Some(Urgency::Low) | Some(Urgency::Normal) | None => None, |
| 74 |
}; |
| 75 |
|
| 76 |
let app_id = notification |
| 77 |
.app_id |
| 78 |
.as_deref() |
| 79 |
.unwrap_or(Toast::POWERSHELL_APP_ID); |
| 80 |
|
| 81 |
let mut toast = Toast::new(app_id) |
| 82 |
.title(¬ification.summary) |
| 83 |
.text1(notification.subtitle.as_ref().map_or("", AsRef::as_ref)) |
| 84 |
.text2(¬ification.body) |
| 85 |
.sound(sound) |
| 86 |
.duration(duration); |
| 87 |
|
| 88 |
|
| 89 |
if let Some(scenario) = scenario { |
| 90 |
toast = toast.scenario(scenario); |
| 91 |
} |
| 92 |
if let Some(image_path) = ¬ification.path_to_image { |
| 93 |
toast = toast.image(Path::new(&image_path), ""); |
| 94 |
} |
| 95 |
|
| 96 |
toast |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
#[derive(Debug)] |
| 104 |
pub struct NotificationHandle { |
| 105 |
notification: Notification, |
| 106 |
events: Receiver<NotificationResponse>, |
| 107 |
} |
| 108 |
|
| 109 |
impl NotificationHandle { |
| 110 |
|
| 111 |
pub fn wait_for_action<F>(self, invocation_closure: F) |
| 112 |
where |
| 113 |
F: FnOnce(&str), |
| 114 |
{ |
| 115 |
match self.events.recv() { |
| 116 |
Ok(NotificationResponse::Action(action)) => invocation_closure(&action), |
| 117 |
Ok(_) => invocation_closure("__closed"), |
| 118 |
Err(_error) => invocation_closure("__closed"), |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
pub fn wait_for_response(self, handler: impl crate::response::ResponseHandler) -> Result<()> { |
| 124 |
match self.events.recv() { |
| 125 |
Ok(response) => { |
| 126 |
handler.call(&response); |
| 127 |
Ok(()) |
| 128 |
} |
| 129 |
Err(error) => Err(Error::from(ErrorKind::Msg(format!( |
| 130 |
"failed to get notification response: {error}" |
| 131 |
)))), |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
pub fn on_close<A>(self, handler: impl CloseHandler<A>) { |
| 137 |
while let Ok(event) = self.events.recv() { |
| 138 |
if let NotificationResponse::Closed(reason) = event { |
| 139 |
handler.call(reason); |
| 140 |
break; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl Deref for NotificationHandle { |
| 147 |
type Target = Notification; |
| 148 |
|
| 149 |
fn deref(&self) -> &Notification { |
| 150 |
&self.notification |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl DerefMut for NotificationHandle { |
| 155 |
fn deref_mut(&mut self) -> &mut Notification { |
| 156 |
&mut self.notification |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl From<Option<ToastDismissalReason>> for CloseReason { |
| 161 |
fn from(reason: Option<ToastDismissalReason>) -> Self { |
| 162 |
match reason { |
| 163 |
Some(ToastDismissalReason::TimedOut) => CloseReason::Expired, |
| 164 |
Some(ToastDismissalReason::UserCanceled) => CloseReason::Dismissed, |
| 165 |
Some(ToastDismissalReason::ApplicationHidden) => CloseReason::CloseAction, |
| 166 |
Some(_) | None => CloseReason::Other(0), |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
pub trait CloseHandler<T> { |
| 173 |
|
| 174 |
fn call(&self, reason: CloseReason); |
| 175 |
} |
| 176 |
|
| 177 |
impl<F> CloseHandler<CloseReason> for F |
| 178 |
where |
| 179 |
F: Fn(CloseReason), |
| 180 |
{ |
| 181 |
fn call(&self, reason: CloseReason) { |
| 182 |
self(reason); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
impl<F> CloseHandler<()> for F |
| 187 |
where |
| 188 |
F: Fn(), |
| 189 |
{ |
| 190 |
fn call(&self, _: CloseReason) { |
| 191 |
self(); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
#[cfg(test)] |
| 196 |
mod tests { |
| 197 |
use super::*; |
| 198 |
|
| 199 |
fn handle_with(event: NotificationResponse) -> NotificationHandle { |
| 200 |
let (sender, receiver) = channel(); |
| 201 |
sender.send(event).unwrap(); |
| 202 |
NotificationHandle { |
| 203 |
notification: Notification::new(), |
| 204 |
events: receiver, |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
#[test] |
| 209 |
fn wait_for_action_returns_custom_action() { |
| 210 |
let handle = handle_with(NotificationResponse::Action("clicked_a".to_owned())); |
| 211 |
let mut actual = String::new(); |
| 212 |
|
| 213 |
handle.wait_for_action(|action| { |
| 214 |
actual = action.to_owned(); |
| 215 |
}); |
| 216 |
|
| 217 |
assert_eq!(actual, "clicked_a"); |
| 218 |
} |
| 219 |
|
| 220 |
#[test] |
| 221 |
fn wait_for_action_keeps_closed_compatibility_keyword() { |
| 222 |
let handle = handle_with(NotificationResponse::Closed(CloseReason::Dismissed)); |
| 223 |
let mut actual = String::new(); |
| 224 |
|
| 225 |
handle.wait_for_action(|action| { |
| 226 |
actual = action.to_owned(); |
| 227 |
}); |
| 228 |
|
| 229 |
assert_eq!(actual, "__closed"); |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn wait_for_response_preserves_close_reason() { |
| 234 |
use crate::response::{CloseReason as Reason, NotificationResponse}; |
| 235 |
|
| 236 |
let handle = handle_with(NotificationResponse::Closed(CloseReason::Expired)); |
| 237 |
let mut expired = false; |
| 238 |
|
| 239 |
handle |
| 240 |
.wait_for_response(|response: &NotificationResponse| { |
| 241 |
expired = matches!(response, NotificationResponse::Closed(Reason::Expired)); |
| 242 |
}) |
| 243 |
.unwrap(); |
| 244 |
|
| 245 |
assert!(expired); |
| 246 |
} |
| 247 |
} |