wayle_notification/
service.rs1use std::sync::Arc;
2
3use derive_more::Debug;
4use tokio::sync::broadcast;
5use tokio_util::sync::CancellationToken;
6use tracing::{instrument, warn};
7use wayle_core::Property;
8use zbus::Connection;
9
10use crate::{
11 builder::NotificationServiceBuilder, core::notification::Notification, error::Error,
12 events::NotificationEvent, persistence::NotificationStore, popup_timer::PopupTimerManager,
13 types::ClosedReason,
14};
15
16#[derive(Debug)]
18pub struct NotificationService {
19 #[debug(skip)]
20 pub(crate) cancellation_token: CancellationToken,
21 #[debug(skip)]
22 pub(crate) notif_tx: broadcast::Sender<NotificationEvent>,
23 #[debug(skip)]
24 pub(crate) store: Option<NotificationStore>,
25 #[debug(skip)]
26 pub(crate) connection: Connection,
27
28 pub notifications: Property<Vec<Arc<Notification>>>,
30 pub popups: Property<Vec<Arc<Notification>>>,
32 pub popup_duration: Property<u32>,
34 pub dnd: Property<bool>,
36 pub remove_expired: Property<bool>,
38 pub blocklist: Property<Vec<String>>,
40 #[debug(skip)]
41 pub(crate) popup_timers: Arc<PopupTimerManager>,
42}
43
44impl NotificationService {
45 #[instrument(name = "NotificationService::new", err)]
50 pub async fn new() -> Result<Arc<Self>, Error> {
51 Self::builder().build().await
52 }
53
54 pub fn builder() -> NotificationServiceBuilder {
56 NotificationServiceBuilder::new()
57 }
58
59 #[instrument(skip(self), err)]
64 pub async fn dismiss_all(&self) -> Result<(), Error> {
65 let notifications = self.notifications.get();
66
67 for notif in notifications.iter() {
68 if let Err(error) = self.notif_tx.send(NotificationEvent::Remove(
69 notif.id,
70 ClosedReason::DismissedByUser,
71 )) {
72 warn!(error = %error, id = notif.id, "cannot dismiss notification");
73 }
74 }
75
76 Ok(())
77 }
78
79 pub fn set_dnd(&self, dnd: bool) {
84 self.dnd.set(dnd)
85 }
86
87 pub fn set_popup_duration(&self, duration: u32) {
89 self.popup_duration.set(duration)
90 }
91
92 pub fn set_blocklist(&self, patterns: Vec<String>) {
94 self.blocklist.set(patterns)
95 }
96
97 pub fn dismiss_popup(&self, id: u32) {
101 self.popup_timers.cancel(id);
102
103 let mut list = self.popups.get();
104 list.retain(|popup| popup.id != id);
105 self.popups.set(list);
106 }
107
108 pub fn inhibit_popup(&self, id: u32) {
110 self.popup_timers.pause(id);
111 }
112
113 pub fn release_popup(&self, id: u32) {
115 self.popup_timers.resume(id);
116 }
117}
118
119impl Drop for NotificationService {
120 fn drop(&mut self) {
121 self.cancellation_token.cancel();
122 }
123}