hypersig_desktop/
notification.rs1use notify_rust::NotificationResponse;
5use tokio::sync::oneshot;
6
7use crate::error::Error;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
11#[must_use = "a notification shows only when it is given to `Os::notify`"]
12pub struct Notification {
13 pub(crate) title: String,
14 pub(crate) body: Option<String>,
15}
16
17impl Notification {
18 pub fn new(title: impl Into<String>) -> Self {
20 Self {
21 title: title.into(),
22 body: None,
23 }
24 }
25
26 pub fn body(mut self, body: impl Into<String>) -> Self {
28 self.body = Some(body.into());
29 self
30 }
31
32 #[must_use]
34 pub fn title(&self) -> &str {
35 &self.title
36 }
37
38 #[must_use]
40 pub fn text(&self) -> Option<&str> {
41 self.body.as_deref()
42 }
43}
44
45#[derive(Debug)]
47pub struct Notified {
48 pub(crate) clicked: oneshot::Receiver<bool>,
49}
50
51impl Notified {
52 pub async fn clicked(self) -> bool {
60 self.clicked.await.unwrap_or(false)
61 }
62}
63
64pub(crate) fn show(notification: &Notification) -> Result<Notified, Error> {
67 let (clicked, heard) = oneshot::channel();
68 let mut system = notify_rust::Notification::new();
69 system.summary(¬ification.title);
70 if let Some(body) = ¬ification.body {
71 system.body(body);
72 }
73 #[cfg(target_os = "linux")]
76 system.action("default", "Open");
77 let handle = system.show().map_err(Error::notification)?;
78 std::thread::Builder::new()
79 .name("hypersig-desktop-notification".to_owned())
80 .spawn(move || {
81 let waited = handle.wait_for_response(|response: &NotificationResponse| {
84 let _ = clicked.send(!matches!(response, NotificationResponse::Closed(_)));
85 });
86 if let Err(error) = waited {
87 tracing::warn!(%error, "the notification's click was not heard");
88 }
89 })
90 .map_err(Error::notification)?;
91 Ok(Notified { clicked: heard })
92}
93
94#[cfg(target_os = "macos")]
99pub(crate) fn name_application(id: &str) {
100 if let Err(error) = notify_rust::set_application(id) {
101 tracing::debug!(%error, "notifications keep the default application, since no bundle has this identifier");
102 }
103}