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 deref_cell;
mod mpris; mod mpris;
mod mpris_player; mod mpris_player;
mod notification_list;
mod notification_popover; mod notification_popover;
mod notification_widget;
mod notifications; mod notifications;
mod popover_container; mod popover_container;
mod status_area; 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 cascade::cascade;
use gtk4::{ use gtk4::{
glib::{self, clone}, glib::{self, clone},
pango,
prelude::*, prelude::*,
subclass::prelude::*, subclass::prelude::*,
}; };
use crate::deref_cell::DerefCell; use crate::deref_cell::DerefCell;
use crate::notifications::Notification; use crate::notification_widget::NotificationWidget;
use crate::notifications::{Notification, Notifications};
#[derive(Default)] #[derive(Default)]
pub struct NotificationPopoverInner { pub struct NotificationPopoverInner {
summary_label: DerefCell<gtk4::Label>, notification_widget: DerefCell<NotificationWidget>,
body_label: DerefCell<gtk4::Label>, notifications: DerefCell<Notifications>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -31,16 +31,8 @@ impl ObjectImpl for NotificationPopoverInner {
})); }));
}); });
let summary_label = cascade! { let notification_widget = cascade! {
gtk4::Label::new(None); NotificationWidget::new();
..set_attributes(Some(&cascade! {
pango::AttrList::new();
..insert(pango::Attribute::new_weight(pango::Weight::Bold));
}));
};
let body_label = cascade! {
gtk4::Label::new(None);
}; };
cascade! { cascade! {
@ -48,15 +40,10 @@ impl ObjectImpl for NotificationPopoverInner {
..set_autohide(false); ..set_autohide(false);
..set_has_arrow(false); ..set_has_arrow(false);
..set_offset(0, 12); ..set_offset(0, 12);
..set_child(Some(&cascade! { ..set_child(Some(&notification_widget));
gtk4::Box::new(gtk4::Orientation::Vertical, 0);
..append(&summary_label);
..append(&body_label);
}));
}; };
self.summary_label.set(summary_label); self.notification_widget.set(notification_widget);
self.body_label.set(body_label);
} }
} }
@ -69,8 +56,15 @@ glib::wrapper! {
} }
impl NotificationPopover { impl NotificationPopover {
pub fn new() -> Self { pub fn new(notifications: &Notifications) -> Self {
let obj = glib::Object::new::<Self>(&[]).unwrap(); 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 obj
} }
@ -78,8 +72,10 @@ impl NotificationPopover {
NotificationPopoverInner::from_instance(self) NotificationPopoverInner::from_instance(self)
} }
pub fn set_notification(&self, notification: &Notification) { fn handle_notification(&self, notification: &Notification) {
self.inner().summary_label.set_label(&notification.summary); self.inner()
self.inner().body_label.set_label(&notification.body); .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() 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, &self,
cb: F, cb: F,
) -> SignalHandlerId { ) -> SignalHandlerId {
self.connect_local("notification-received", false, move |values| { self.connect_local("notification-received", false, move |values| {
let obj = values[0].get::<Self>().unwrap();
let id = values[1].get().unwrap(); let id = values[1].get().unwrap();
cb(id); if let Some(notification) = obj.get(id) {
cb(notification);
}
None None
}) })
.unwrap() .unwrap()

View file

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