Store notifications

This commit is contained in:
Ian Douglas Scott 2021-09-08 15:18:13 -07:00
parent 2396ebbaa9
commit 17df83e611
3 changed files with 46 additions and 22 deletions

View file

@ -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);
}
}

View file

@ -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 = "
<node name='/org/freedesktop/Notifications'>
@ -53,6 +61,7 @@ static NOTIFICATIONS_XML: &str = "
#[derive(Default)]
pub struct NotificationsInner {
next_id: Cell<NotificationId>,
notifications: RefCell<HashMap<NotificationId, Rc<Notification>>>,
}
#[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<gio::DBusConnection>, _name: &str) {}
pub fn get(&self, id: NotificationId) -> Option<Rc<Notification>> {
self.inner().notifications.borrow().get(&id).cloned()
}
pub fn connect_notification_recieved<F: Fn(NotificationId) + 'static>(
&self,
cb: F,

View file

@ -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
}