Skip to main content

notifications/
notification.rs

1use crate::private_prelude::*;
2
3#[derive(Serialize, Deserialize)]
4pub(crate) struct Notification {
5    pub(crate) id: u32,
6    pub(crate) app_name: String,
7    pub(crate) icon: Option<String>,
8    pub(crate) summary: String,
9    pub(crate) body: String,
10    pub(crate) actions: Vec<Arc<Action>>,
11    pub(crate) urgency: Urgency,
12    pub(crate) timeout: i32,
13}
14
15/// A handle to a notification.
16#[derive(Clone)]
17pub struct NotificationHandle {
18    pub(crate) inner: Arc<Notification>,
19    pub(crate) service: NotificationService,
20}
21
22impl NotificationHandle {
23    /// Returns the unique non-zero identifer of the notification.
24    pub fn id(&self) -> u32 {
25        self.inner.id
26    }
27
28    /// Returns the name of the application that sent the notification. Can be blank.
29    pub fn app_name(&self) -> String {
30        self.inner.app_name.clone()
31    }
32
33    /// Returns the optional icon of the notification.
34    ///
35    /// It is either file path or icon name.
36    // TODO: make it enum
37    pub fn icon(&self) -> Option<String> {
38        self.inner.icon.clone()
39    }
40
41    /// Returns the summary text briefly describing the notification.
42    pub fn summary(&self) -> String {
43        self.inner.summary.clone()
44    }
45
46    /// Returns optional detailed body text. Can be empty.
47    pub fn body(&self) -> String {
48        self.inner.body.clone()
49    }
50
51    /// Returns vector of action handles. Can be empty.
52    pub fn actions(&self) -> Vec<ActionHandle> {
53        self.inner
54            .actions
55            .iter()
56            .map(|a| ActionHandle {
57                inner: a.clone(),
58                service: self.service.clone(),
59            })
60            .collect()
61    }
62
63    /// Returns the urgency level of the notification.
64    pub fn urgency(&self) -> Urgency {
65        self.inner.urgency
66    }
67
68    /// Returns the expire timeout of the notification.
69    pub fn timeout(&self) -> i32 {
70        self.inner.timeout
71    }
72
73    /// Dismisses this notification.
74    ///
75    /// # Errors
76    /// It is a shortcut to [`NotificationService::dismiss_notification`] and therefore returns the
77    /// same errors.
78    pub async fn dismiss(&self) -> Result<()> {
79        self.service.dismiss_notification(self.id()).await?;
80        Ok(())
81    }
82
83    /// Invokes a callback when this notification is closed.
84    ///
85    /// The following arguments are passed to the callback:
86    /// 3. reason - The reason why the notification was closed
87    pub fn on_closed<F>(&self, callback: F) -> usize
88    where
89        F: Fn(&CloseReason) + Send + Sync + 'static,
90    {
91        let this_id = self.id();
92        self.service
93            .inner
94            .on_notification_closed
95            .connect(move |(id, reason)| {
96                if *id == this_id {
97                    callback(reason)
98                }
99            })
100    }
101}