From db70daa238aed445c221f10392d3fc27da7ce542 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Thu, 9 Sep 2021 13:05:24 -0700 Subject: [PATCH] Initial code for listing notifications --- src/main.rs | 2 + src/notification_list.rs | 76 +++++++++++++++++++++++++++++++++++++ src/notification_popover.rs | 46 ++++++++++------------ src/notification_widget.rs | 76 +++++++++++++++++++++++++++++++++++++ src/notifications.rs | 7 +++- src/time_button.rs | 41 +++++++++----------- 6 files changed, 197 insertions(+), 51 deletions(-) create mode 100644 src/notification_list.rs create mode 100644 src/notification_widget.rs diff --git a/src/main.rs b/src/main.rs index c6be6dec..d8a97ef5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/notification_list.rs b/src/notification_list.rs new file mode 100644 index 00000000..20413e91 --- /dev/null +++ b/src/notification_list.rs @@ -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, + notifications: DerefCell, +} + +#[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::(); + } +} + +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) + @extends gtk4::Widget; +} + +impl NotificationList { + pub fn new(notifications: &Notifications) -> Self { + let obj = glib::Object::new::(&[]).unwrap(); + + // XXX disconnect? + obj.inner().notifications.set(notifications.clone()); + notifications.connect_notification_recieved(clone!(@weak obj => move |notification| { + obj.handle_notification(¬ification); + })); + + 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(¬ification_widget); + } +} diff --git a/src/notification_popover.rs b/src/notification_popover.rs index 8c53acf5..c8683d48 100644 --- a/src/notification_popover.rs +++ b/src/notification_popover.rs @@ -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, - body_label: DerefCell, + notification_widget: DerefCell, + notifications: DerefCell, } #[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(¬ification_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::(&[]).unwrap(); + + // XXX disconnect? + obj.inner().notifications.set(notifications.clone()); + notifications.connect_notification_recieved(clone!(@weak obj => move |notification| { + obj.handle_notification(¬ification); + })); + obj } @@ -78,8 +72,10 @@ impl NotificationPopover { NotificationPopoverInner::from_instance(self) } - pub fn set_notification(&self, notification: &Notification) { - self.inner().summary_label.set_label(¬ification.summary); - self.inner().body_label.set_label(¬ification.body); + fn handle_notification(&self, notification: &Notification) { + self.inner() + .notification_widget + .set_notification(notification); + self.popup(); } } diff --git a/src/notification_widget.rs b/src/notification_widget.rs new file mode 100644 index 00000000..978520e7 --- /dev/null +++ b/src/notification_widget.rs @@ -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, + summary_label: DerefCell, + body_label: DerefCell, +} + +#[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::(); + } +} + +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) + @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(¬ification.summary); + self.inner().body_label.set_label(¬ification.body); + } +} diff --git a/src/notifications.rs b/src/notifications.rs index 94ff4499..6e02628a 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -382,13 +382,16 @@ impl Notifications { self.inner().notifications.borrow().get(&id).cloned() } - pub fn connect_notification_recieved( + pub fn connect_notification_recieved) + 'static>( &self, cb: F, ) -> SignalHandlerId { self.connect_local("notification-received", false, move |values| { + let obj = values[0].get::().unwrap(); let id = values[1].get().unwrap(); - cb(id); + if let Some(notification) = obj.get(id) { + cb(notification); + } None }) .unwrap() diff --git a/src/time_button.rs b/src/time_button.rs index 0e33c182..9b671dda 100644 --- a/src/time_button.rs +++ b/src/time_button.rs @@ -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, label: DerefCell, notification_popover: DerefCell, + left_box: DerefCell, } #[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::(&[]).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(¬ification); - } - })); + let notification_list = NotificationList::new(app.notifications()); + obj.inner().left_box.prepend(¬ification_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(); - } }