Initial code for listing notifications

This commit is contained in:
Ian Douglas Scott 2021-09-09 13:05:24 -07:00
parent 14dc25985a
commit db70daa238
6 changed files with 197 additions and 51 deletions

View file

@ -4,7 +4,9 @@ mod application;
mod deref_cell;
mod mpris;
mod mpris_player;
mod notification_list;
mod notification_popover;
mod notification_widget;
mod notifications;
mod popover_container;
mod status_area;

76
src/notification_list.rs Normal file
View file

@ -0,0 +1,76 @@
use cascade::cascade;
use gtk4::{
glib::{self, clone},
prelude::*,
subclass::prelude::*,
};
use crate::deref_cell::DerefCell;
use crate::notification_widget::NotificationWidget;
use crate::notifications::{Notification, Notifications};
#[derive(Default)]
pub struct NotificationListInner {
listbox: DerefCell<gtk4::ListBox>,
notifications: DerefCell<Notifications>,
}
#[glib::object_subclass]
impl ObjectSubclass for NotificationListInner {
const NAME: &'static str = "S76NotificationList";
type ParentType = gtk4::Widget;
type Type = NotificationList;
fn class_init(klass: &mut Self::Class) {
klass.set_layout_manager_type::<gtk4::BinLayout>();
}
}
impl ObjectImpl for NotificationListInner {
fn constructed(&self, obj: &NotificationList) {
let listbox = cascade! {
gtk4::ListBox::new();
..set_parent(obj);
};
self.listbox.set(listbox);
}
fn dispose(&self, _obj: &NotificationList) {
self.listbox.unparent();
}
}
impl WidgetImpl for NotificationListInner {}
glib::wrapper! {
pub struct NotificationList(ObjectSubclass<NotificationListInner>)
@extends gtk4::Widget;
}
impl NotificationList {
pub fn new(notifications: &Notifications) -> Self {
let obj = glib::Object::new::<Self>(&[]).unwrap();
// XXX disconnect?
obj.inner().notifications.set(notifications.clone());
notifications.connect_notification_recieved(clone!(@weak obj => move |notification| {
obj.handle_notification(&notification);
}));
obj
}
fn inner(&self) -> &NotificationListInner {
NotificationListInner::from_instance(self)
}
fn handle_notification(&self, notification: &Notification) {
let notification_widget = cascade! {
NotificationWidget::new();
..set_notification(notification);
};
self.inner().listbox.prepend(&notification_widget);
}
}

View file

@ -1,18 +1,18 @@
use cascade::cascade;
use gtk4::{
glib::{self, clone},
pango,
prelude::*,
subclass::prelude::*,
};
use crate::deref_cell::DerefCell;
use crate::notifications::Notification;
use crate::notification_widget::NotificationWidget;
use crate::notifications::{Notification, Notifications};
#[derive(Default)]
pub struct NotificationPopoverInner {
summary_label: DerefCell<gtk4::Label>,
body_label: DerefCell<gtk4::Label>,
notification_widget: DerefCell<NotificationWidget>,
notifications: DerefCell<Notifications>,
}
#[glib::object_subclass]
@ -31,16 +31,8 @@ impl ObjectImpl for NotificationPopoverInner {
}));
});
let summary_label = cascade! {
gtk4::Label::new(None);
..set_attributes(Some(&cascade! {
pango::AttrList::new();
..insert(pango::Attribute::new_weight(pango::Weight::Bold));
}));
};
let body_label = cascade! {
gtk4::Label::new(None);
let notification_widget = cascade! {
NotificationWidget::new();
};
cascade! {
@ -48,15 +40,10 @@ impl ObjectImpl for NotificationPopoverInner {
..set_autohide(false);
..set_has_arrow(false);
..set_offset(0, 12);
..set_child(Some(&cascade! {
gtk4::Box::new(gtk4::Orientation::Vertical, 0);
..append(&summary_label);
..append(&body_label);
}));
..set_child(Some(&notification_widget));
};
self.summary_label.set(summary_label);
self.body_label.set(body_label);
self.notification_widget.set(notification_widget);
}
}
@ -69,8 +56,15 @@ glib::wrapper! {
}
impl NotificationPopover {
pub fn new() -> Self {
pub fn new(notifications: &Notifications) -> Self {
let obj = glib::Object::new::<Self>(&[]).unwrap();
// XXX disconnect?
obj.inner().notifications.set(notifications.clone());
notifications.connect_notification_recieved(clone!(@weak obj => move |notification| {
obj.handle_notification(&notification);
}));
obj
}
@ -78,8 +72,10 @@ impl NotificationPopover {
NotificationPopoverInner::from_instance(self)
}
pub fn set_notification(&self, notification: &Notification) {
self.inner().summary_label.set_label(&notification.summary);
self.inner().body_label.set_label(&notification.body);
fn handle_notification(&self, notification: &Notification) {
self.inner()
.notification_widget
.set_notification(notification);
self.popup();
}
}

View file

@ -0,0 +1,76 @@
use cascade::cascade;
use gtk4::{glib, pango, prelude::*, subclass::prelude::*};
use crate::deref_cell::DerefCell;
use crate::notifications::Notification;
#[derive(Default)]
pub struct NotificationWidgetInner {
box_: DerefCell<gtk4::Box>,
summary_label: DerefCell<gtk4::Label>,
body_label: DerefCell<gtk4::Label>,
}
#[glib::object_subclass]
impl ObjectSubclass for NotificationWidgetInner {
const NAME: &'static str = "S76NotificationWidget";
type ParentType = gtk4::Widget;
type Type = NotificationWidget;
fn class_init(klass: &mut Self::Class) {
klass.set_layout_manager_type::<gtk4::BinLayout>();
}
}
impl ObjectImpl for NotificationWidgetInner {
fn constructed(&self, obj: &NotificationWidget) {
let summary_label = cascade! {
gtk4::Label::new(None);
..set_attributes(Some(&cascade! {
pango::AttrList::new();
..insert(pango::Attribute::new_weight(pango::Weight::Bold));
}));
};
let body_label = cascade! {
gtk4::Label::new(None);
};
let box_ = cascade! {
gtk4::Box::new(gtk4::Orientation::Vertical, 0);
..set_parent(obj);
..append(&summary_label);
..append(&body_label);
};
self.box_.set(box_);
self.summary_label.set(summary_label);
self.body_label.set(body_label);
}
fn dispose(&self, _obj: &NotificationWidget) {
self.box_.unparent();
}
}
impl WidgetImpl for NotificationWidgetInner {}
glib::wrapper! {
pub struct NotificationWidget(ObjectSubclass<NotificationWidgetInner>)
@extends gtk4::Widget;
}
impl NotificationWidget {
pub fn new() -> Self {
glib::Object::new(&[]).unwrap()
}
fn inner(&self) -> &NotificationWidgetInner {
NotificationWidgetInner::from_instance(self)
}
pub fn set_notification(&self, notification: &Notification) {
self.inner().summary_label.set_label(&notification.summary);
self.inner().body_label.set_label(&notification.body);
}
}

View file

@ -382,13 +382,16 @@ impl Notifications {
self.inner().notifications.borrow().get(&id).cloned()
}
pub fn connect_notification_recieved<F: Fn(NotificationId) + 'static>(
pub fn connect_notification_recieved<F: Fn(Rc<Notification>) + 'static>(
&self,
cb: F,
) -> SignalHandlerId {
self.connect_local("notification-received", false, move |values| {
let obj = values[0].get::<Self>().unwrap();
let id = values[1].get().unwrap();
cb(id);
if let Some(notification) = obj.get(id) {
cb(notification);
}
None
})
.unwrap()

View file

@ -9,8 +9,8 @@ use gtk4::{
use crate::application::PanelApp;
use crate::deref_cell::DerefCell;
use crate::mpris::MprisControls;
use crate::notification_list::NotificationList;
use crate::notification_popover::NotificationPopover;
use crate::notifications::Notification;
use crate::popover_container::PopoverContainer;
#[derive(Default)]
@ -19,6 +19,7 @@ pub struct TimeButtonInner {
button: DerefCell<gtk4::ToggleButton>,
label: DerefCell<gtk4::Label>,
notification_popover: DerefCell<NotificationPopover>,
left_box: DerefCell<gtk4::Box>,
}
#[glib::object_subclass]
@ -52,27 +53,27 @@ impl ObjectImpl for TimeButtonInner {
..set_child(Some(&label));
};
let left_box = cascade! {
gtk4::Box::new(gtk4::Orientation::Vertical, 0);
..append(&MprisControls::new());
};
cascade! {
PopoverContainer::new(&button);
..set_parent(obj);
..popover().set_child(Some(&cascade! {
gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
..append(&MprisControls::new());
..append(&left_box);
..append(&calendar);
}));
..popover().connect_show(clone!(@strong obj => move |_| obj.opening()));
..popover().bind_property("visible", &button, "active").flags(glib::BindingFlags::BIDIRECTIONAL).build();
};
let notification_popover = cascade! {
NotificationPopover::new();
..set_parent(obj);
};
self.calendar.set(calendar);
self.button.set(button);
self.label.set(label);
self.notification_popover.set(notification_popover);
self.left_box.set(left_box);
// TODO: better way to do this?
glib::timeout_add_seconds_local(
@ -102,13 +103,14 @@ impl TimeButton {
pub fn new(app: &PanelApp) -> Self {
let obj = glib::Object::new::<Self>(&[]).unwrap();
let notifications = app.notifications().clone();
app.notifications()
.connect_notification_recieved(clone!(@weak obj => move |id| {
if let Some(notification) = notifications.get(id) {
obj.handle_notification(&notification);
}
}));
let notification_list = NotificationList::new(app.notifications());
obj.inner().left_box.prepend(&notification_list);
let notification_popover = cascade! {
NotificationPopover::new(app.notifications());
..set_parent(&obj);
};
obj.inner().notification_popover.set(notification_popover);
obj
}
@ -131,13 +133,4 @@ impl TimeButton {
.set_label(&time.format("%b %-d %-I:%M %p").to_string());
// time.format("%B %-d %Y")
}
fn handle_notification(&self, notification: &Notification) {
println!("{:?}", notification);
self.inner()
.notification_popover
.set_notification(notification);
self.inner().notification_popover.popup();
}
}