cosmic-applets/src/notification_widget.rs

118 lines
3.7 KiB
Rust
Raw Normal View History

2021-09-09 13:05:24 -07:00
use cascade::cascade;
2021-09-13 13:48:17 -07:00
use gtk4::{
glib::{self, clone},
pango,
prelude::*,
subclass::prelude::*,
};
use std::cell::Cell;
2021-09-09 13:05:24 -07:00
use crate::deref_cell::DerefCell;
2021-09-13 13:48:17 -07:00
use crate::notifications::{Notification, NotificationId, Notifications};
2021-09-09 13:05:24 -07:00
#[derive(Default)]
pub struct NotificationWidgetInner {
box_: DerefCell<gtk4::Box>,
summary_label: DerefCell<gtk4::Label>,
body_label: DerefCell<gtk4::Label>,
2021-09-13 13:48:17 -07:00
notifications: DerefCell<Notifications>,
id: Cell<Option<NotificationId>>,
2021-09-09 13:05:24 -07:00
}
#[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);
2021-09-23 18:34:08 -07:00
..set_width_chars(20);
..set_max_width_chars(20);
2021-09-09 13:05:24 -07:00
..set_attributes(Some(&cascade! {
pango::AttrList::new();
2022-01-21 16:38:58 -05:00
..insert(pango::AttrInt::new_weight(pango::Weight::Bold));
2021-09-09 13:05:24 -07:00
}));
};
let body_label = cascade! {
gtk4::Label::new(None);
2021-09-23 18:34:08 -07:00
..set_width_chars(20);
..set_max_width_chars(20);
2021-09-09 13:05:24 -07:00
};
let box_ = cascade! {
2021-09-14 12:23:22 -07:00
gtk4::Box::new(gtk4::Orientation::Horizontal, 6);
2021-09-09 13:05:24 -07:00
..set_parent(obj);
2021-09-13 13:48:17 -07:00
..append(&cascade! {
gtk4::Box::new(gtk4::Orientation::Vertical, 0);
..append(&summary_label);
..append(&body_label);
});
2021-09-14 12:23:22 -07:00
..append(&cascade! {
gtk4::Button::new();
..style_context().add_provider(&cascade! {
gtk4::CssProvider::new();
..load_from_data(b"button { min-width: 0; min-height: 0; padding: 4px 4px; }");
}, gtk4::STYLE_PROVIDER_PRIORITY_APPLICATION);
..style_context().add_class("flat");
..set_valign(gtk4::Align::Start);
..set_child(Some(&cascade! {
2022-01-21 16:38:58 -05:00
gtk4::Image::from_icon_name("window-close-symbolic");
2021-09-14 12:23:22 -07:00
..set_pixel_size(8);
}));
..connect_clicked(clone!(@weak obj => move |_| {
if let Some(id) = obj.id() {
2021-09-14 12:23:22 -07:00
obj.inner().notifications.dismiss(id);
}
}));
});
2021-09-09 13:05:24 -07:00
};
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 {
2021-09-13 13:48:17 -07:00
pub fn new(notifications: &Notifications) -> Self {
let obj = glib::Object::new::<Self>(&[]).unwrap();
obj.inner().notifications.set(notifications.clone());
obj
2021-09-09 13:05:24 -07:00
}
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);
2021-09-13 13:48:17 -07:00
self.inner().id.set(Some(notification.id));
2021-09-09 13:05:24 -07:00
}
pub fn id(&self) -> Option<NotificationId> {
self.inner().id.get()
}
2021-09-09 13:05:24 -07:00
}