From 004d627b716ae474ec3055e9e2531d0df7409d9c Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Wed, 6 Mar 2024 13:04:41 -0500 Subject: [PATCH] feat(notifications): add clear all button to notifications --- cosmic-applet-notifications/src/main.rs | 30 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cosmic-applet-notifications/src/main.rs b/cosmic-applet-notifications/src/main.rs index 3b2d4108..e73ab230 100644 --- a/cosmic-applet-notifications/src/main.rs +++ b/cosmic-applet-notifications/src/main.rs @@ -93,7 +93,7 @@ enum Message { Config(NotificationsConfig), DbusEvent(subscriptions::dbus::Output), Dismissed(u32), - ClearAll(String), + ClearAll(Option), CardsToggled(String, bool), Token(TokenUpdate), OpenSettings, @@ -277,7 +277,7 @@ impl cosmic::Application for Notifications { self.cards.retain(|c| !c.1.is_empty()); } }, - Message::ClearAll(app_name) => { + Message::ClearAll(Some(app_name)) => { if let Some(pos) = self .cards .iter_mut() @@ -297,6 +297,20 @@ impl cosmic::Application for Notifications { } } } + Message::ClearAll(None) => { + for n in self.cards.drain(..).map(|n| n.1).flatten() { + if let Some(tx) = &self.dbus_sender { + let tx = tx.clone(); + tokio::spawn(async move { + if let Err(err) = + tx.send(subscriptions::dbus::Input::Dismiss(n.id)).await + { + tracing::error!("{:?}", err); + } + }); + } + } + } Message::CardsToggled(name, expanded) => { let id = if let Some((id, _, n_expanded, ..)) = self .cards @@ -381,7 +395,15 @@ impl cosmic::Application for Notifications { .spacing(12) } else { let mut notifs: Vec> = Vec::with_capacity(self.cards.len()); - + notifs.push( + container( + cosmic::widget::button::text(fl!("clear-all")) + .on_press(Message::ClearAll(None)), + ) + .width(Length::Fill) + .align_x(Horizontal::Right) + .into(), + ); for c in self.cards.iter().rev() { if c.1.is_empty() { continue; @@ -499,7 +521,7 @@ impl cosmic::Application for Notifications { c.0.clone(), &self.timeline, notif_elems, - Message::ClearAll(name.clone()), + Message::ClearAll(Some(name.clone())), move |_, e| Message::CardsToggled(name.clone(), e), &c.3, &c.4,