Dismiss contact request notification if request is cancelled

This commit is contained in:
Nathan Sobo 2022-05-10 18:50:18 -06:00
parent 3bca1c29e2
commit fe89de8b11
5 changed files with 100 additions and 45 deletions

View file

@ -604,13 +604,20 @@ impl<T: Item> WeakItemHandle for WeakViewHandle<T> {
}
}
pub trait Notification: View {}
pub trait Notification: View {
fn should_dismiss_notification_on_event(&self, event: &<Self as Entity>::Event) -> bool;
}
pub trait NotificationHandle {
fn id(&self) -> usize;
fn to_any(&self) -> AnyViewHandle;
}
impl<T: Notification> NotificationHandle for ViewHandle<T> {
fn id(&self) -> usize {
self.id()
}
fn to_any(&self) -> AnyViewHandle {
self.into()
}
@ -996,10 +1003,27 @@ impl Workspace {
notification: ViewHandle<V>,
cx: &mut ViewContext<Self>,
) {
cx.subscribe(&notification, |this, handle, event, cx| {
if handle.read(cx).should_dismiss_notification_on_event(event) {
this.dismiss_notification(handle.id(), cx);
}
})
.detach();
self.notifications.push(Box::new(notification));
cx.notify();
}
fn dismiss_notification(&mut self, id: usize, cx: &mut ViewContext<Self>) {
self.notifications.retain(|handle| {
if handle.id() == id {
cx.notify();
false
} else {
true
}
});
}
pub fn items<'a>(
&'a self,
cx: &'a AppContext,