From 17df83e61136d91f3b8f8ba036c1fc6974ec4ec6 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Wed, 8 Sep 2021 15:18:13 -0700 Subject: [PATCH] Store notifications --- src/notification_popover.rs | 10 ++++---- src/notifications.rs | 48 ++++++++++++++++++++++++------------- src/window.rs | 10 ++++++++ 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/notification_popover.rs b/src/notification_popover.rs index 8b46319d..0f28b3fb 100644 --- a/src/notification_popover.rs +++ b/src/notification_popover.rs @@ -1,9 +1,5 @@ use cascade::cascade; -use gtk4::{ - glib::{self, clone}, - prelude::*, - subclass::prelude::*, -}; +use gtk4::{glib, prelude::*, subclass::prelude::*}; use crate::deref_cell::DerefCell; @@ -54,4 +50,8 @@ impl NotificationPopover { fn inner(&self) -> &NotificationPopoverInner { NotificationPopoverInner::from_instance(self) } + + pub fn set_body(&self, body: &str) { + self.inner().label.set_label(body); + } } diff --git a/src/notifications.rs b/src/notifications.rs index ab9befc8..a55c1a8e 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -5,7 +5,15 @@ use gtk4::{ subclass::prelude::*, }; use once_cell::sync::Lazy; -use std::{borrow::Cow, cell::Cell, collections::HashMap, fmt, num::NonZeroU32, time::Duration}; +use std::{ + borrow::Cow, + cell::{Cell, RefCell}, + collections::HashMap, + fmt, + num::NonZeroU32, + rc::Rc, + time::Duration, +}; static NOTIFICATIONS_XML: &str = " @@ -53,6 +61,7 @@ static NOTIFICATIONS_XML: &str = " #[derive(Default)] pub struct NotificationsInner { next_id: Cell, + notifications: RefCell>>, } #[glib::object_subclass] @@ -180,7 +189,7 @@ impl glib::FromVariant for Hints { } #[repr(transparent)] -#[derive(Debug, Clone, Copy, Hash, glib::GBoxed)] +#[derive(Debug, Clone, Copy, Hash, glib::GBoxed, PartialEq, Eq)] #[gboxed(type_name = "S76NotificationId")] pub struct NotificationId(NonZeroU32); @@ -202,7 +211,9 @@ impl NotificationId { } } -struct Notification { +#[derive(Debug)] +#[allow(dead_code)] +pub struct Notification { id: NotificationId, app_name: String, app_icon: String, // decode? @@ -252,21 +263,20 @@ impl Notifications { ) -> NotificationId { let id = replaces_id.unwrap_or_else(|| self.next_id()); - println!( - "{:?}", - ( - id, - app_name, - app_icon, - summary, - body, - actions, - hints, - expire_timeout - ) - ); + let notification = Rc::new(Notification { + id, + app_name, + app_icon, + summary, + body, + actions, + hints, + }); - // TODO + self.inner() + .notifications + .borrow_mut() + .insert(id, notification); if expire_timeout != 0 { let expire_timeout = if expire_timeout < 0 { @@ -360,6 +370,10 @@ impl Notifications { fn name_lost(&self, _connection: Option, _name: &str) {} + pub fn get(&self, id: NotificationId) -> Option> { + self.inner().notifications.borrow().get(&id).cloned() + } + pub fn connect_notification_recieved( &self, cb: F, diff --git a/src/window.rs b/src/window.rs index fa99bdb0..25f03867 100644 --- a/src/window.rs +++ b/src/window.rs @@ -132,6 +132,16 @@ impl PanelWindow { app.add_window(&obj); + let notifications = app.notifications().clone(); + app.notifications() + .connect_notification_recieved(clone!(@weak obj => move |id| { + let notification = notifications.get(id); + println!( + "{:?}", + notification + ); + })); + obj }