cosmic-applets/src/notification_popover.rs

81 lines
2.2 KiB
Rust
Raw Normal View History

2021-09-03 12:21:12 -07:00
use cascade::cascade;
2021-09-09 08:27:04 -07:00
use gtk4::{
glib::{self, clone},
prelude::*,
subclass::prelude::*,
};
2021-09-03 12:21:12 -07:00
use crate::deref_cell::DerefCell;
2021-09-09 13:05:24 -07:00
use crate::notification_widget::NotificationWidget;
use crate::notifications::{Notification, Notifications};
2021-09-03 12:21:12 -07:00
#[derive(Default)]
pub struct NotificationPopoverInner {
2021-09-09 13:05:24 -07:00
notification_widget: DerefCell<NotificationWidget>,
notifications: DerefCell<Notifications>,
2021-09-03 12:21:12 -07:00
}
#[glib::object_subclass]
impl ObjectSubclass for NotificationPopoverInner {
const NAME: &'static str = "S76NotificationPopover";
type ParentType = gtk4::Popover;
type Type = NotificationPopover;
}
impl ObjectImpl for NotificationPopoverInner {
fn constructed(&self, obj: &NotificationPopover) {
2021-09-09 08:27:04 -07:00
obj.add_controller(&cascade! {
gtk4::GestureClick::new();
..connect_pressed(clone!(@weak obj => move |_, _, _, _| {
obj.popdown();
}));
});
2021-09-03 12:21:12 -07:00
cascade! {
obj;
..set_autohide(false);
..set_has_arrow(false);
..set_offset(0, 12);
};
}
}
impl WidgetImpl for NotificationPopoverInner {}
impl PopoverImpl for NotificationPopoverInner {}
glib::wrapper! {
pub struct NotificationPopover(ObjectSubclass<NotificationPopoverInner>)
@extends gtk4::Popover, gtk4::Widget;
}
impl NotificationPopover {
2021-09-09 13:05:24 -07:00
pub fn new(notifications: &Notifications) -> Self {
2021-09-03 12:21:12 -07:00
let obj = glib::Object::new::<Self>(&[]).unwrap();
2021-09-09 13:05:24 -07:00
2021-09-13 13:48:17 -07:00
let notification_widget = cascade! {
NotificationWidget::new(notifications);
};
obj.set_child(Some(&notification_widget));
obj.inner().notification_widget.set(notification_widget);
2021-09-09 13:05:24 -07:00
// XXX disconnect?
obj.inner().notifications.set(notifications.clone());
notifications.connect_notification_recieved(clone!(@weak obj => move |notification| {
obj.handle_notification(&notification);
}));
2021-09-03 12:21:12 -07:00
obj
}
fn inner(&self) -> &NotificationPopoverInner {
NotificationPopoverInner::from_instance(self)
}
2021-09-08 15:18:13 -07:00
2021-09-09 13:05:24 -07:00
fn handle_notification(&self, notification: &Notification) {
self.inner()
.notification_widget
.set_notification(notification);
self.popup();
2021-09-08 15:18:13 -07:00
}
2021-09-03 12:21:12 -07:00
}