From 397c671da0db3da13ef6f4627519eca0896b62f1 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 24 Sep 2021 10:30:09 -0700 Subject: [PATCH] Disconnect signals on dispose --- src/notification_list.rs | 22 ++++++++++++++-------- src/notification_popover.rs | 26 +++++++++++++++++--------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/notification_list.rs b/src/notification_list.rs index 9397ddad..2fbc4051 100644 --- a/src/notification_list.rs +++ b/src/notification_list.rs @@ -14,6 +14,7 @@ use crate::notifications::{Notification, NotificationId, Notifications}; pub struct NotificationListInner { listbox: DerefCell, notifications: DerefCell, + ids: RefCell>, rows: RefCell>, } @@ -43,8 +44,12 @@ impl ObjectImpl for NotificationListInner { self.listbox.set(listbox); } - fn dispose(&self, _obj: &NotificationList) { + fn dispose(&self, obj: &NotificationList) { self.listbox.unparent(); + + for i in obj.inner().ids.take().into_iter() { + obj.inner().notifications.disconnect(i); + } } } @@ -59,14 +64,15 @@ 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); - })); - notifications.connect_notification_closed(clone!(@weak obj => move |id| { - obj.remove_notification(id); - })); + *obj.inner().ids.borrow_mut() = vec![ + notifications.connect_notification_recieved(clone!(@weak obj => move |notification| { + obj.handle_notification(¬ification); + })), + notifications.connect_notification_closed(clone!(@weak obj => move |id| { + obj.remove_notification(id); + })), + ]; obj } diff --git a/src/notification_popover.rs b/src/notification_popover.rs index d2578743..7d7d550f 100644 --- a/src/notification_popover.rs +++ b/src/notification_popover.rs @@ -14,6 +14,7 @@ use crate::notifications::{Notification, NotificationId, Notifications}; pub struct NotificationPopoverInner { notification_widget: DerefCell, notifications: DerefCell, + ids: RefCell>, source: RefCell>, } @@ -54,6 +55,12 @@ impl ObjectImpl for NotificationPopoverInner { }); }; } + + fn dispose(&self, obj: &NotificationPopover) { + for i in obj.inner().ids.take().into_iter() { + obj.inner().notifications.disconnect(i); + } + } } impl WidgetImpl for NotificationPopoverInner {} @@ -74,16 +81,17 @@ impl NotificationPopover { obj.set_child(Some(¬ification_widget)); obj.inner().notification_widget.set(notification_widget); - // XXX disconnect? obj.inner().notifications.set(notifications.clone()); - notifications.connect_notification_recieved(clone!(@weak obj => move |notification| { - obj.handle_notification(¬ification); - })); - notifications.connect_notification_closed(clone!(@weak obj => move |id| { - if obj.id() == Some(id) { - obj.popdown(); - } - })); + *obj.inner().ids.borrow_mut() = vec![ + notifications.connect_notification_recieved(clone!(@weak obj => move |notification| { + obj.handle_notification(¬ification); + })), + notifications.connect_notification_closed(clone!(@weak obj => move |id| { + if obj.id() == Some(id) { + obj.popdown(); + } + })), + ]; obj }