Avoid duplicate notifications
Co-authored-by: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
d5c7a96f98
commit
9b29245590
2 changed files with 39 additions and 28 deletions
|
@ -132,16 +132,15 @@ impl ContactsPanel {
|
||||||
project::Event::ContactRequestedJoin(user) => {
|
project::Event::ContactRequestedJoin(user) => {
|
||||||
if let Some(workspace) = workspace.upgrade(cx) {
|
if let Some(workspace) = workspace.upgrade(cx) {
|
||||||
workspace.update(cx, |workspace, cx| {
|
workspace.update(cx, |workspace, cx| {
|
||||||
workspace.show_notification(
|
workspace.show_notification(user.id as usize, cx, |cx| {
|
||||||
cx.add_view(|cx| {
|
cx.add_view(|cx| {
|
||||||
JoinProjectNotification::new(
|
JoinProjectNotification::new(
|
||||||
project,
|
project,
|
||||||
user.clone(),
|
user.clone(),
|
||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
}),
|
})
|
||||||
cx,
|
})
|
||||||
)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,12 +160,11 @@ impl ContactsPanel {
|
||||||
{
|
{
|
||||||
workspace.update(cx, |workspace, cx| match event.kind {
|
workspace.update(cx, |workspace, cx| match event.kind {
|
||||||
ContactEventKind::Requested | ContactEventKind::Accepted => workspace
|
ContactEventKind::Requested | ContactEventKind::Accepted => workspace
|
||||||
.show_notification(
|
.show_notification(event.user.id as usize, cx, |cx| {
|
||||||
cx.add_view(|cx| {
|
cx.add_view(|cx| {
|
||||||
ContactNotification::new(event.clone(), user_store, cx)
|
ContactNotification::new(event.clone(), user_store, cx)
|
||||||
}),
|
})
|
||||||
cx,
|
}),
|
||||||
),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -716,7 +716,7 @@ pub struct Workspace {
|
||||||
panes: Vec<ViewHandle<Pane>>,
|
panes: Vec<ViewHandle<Pane>>,
|
||||||
active_pane: ViewHandle<Pane>,
|
active_pane: ViewHandle<Pane>,
|
||||||
status_bar: ViewHandle<StatusBar>,
|
status_bar: ViewHandle<StatusBar>,
|
||||||
notifications: Vec<Box<dyn NotificationHandle>>,
|
notifications: Vec<(TypeId, usize, Box<dyn NotificationHandle>)>,
|
||||||
project: ModelHandle<Project>,
|
project: ModelHandle<Project>,
|
||||||
leader_state: LeaderState,
|
leader_state: LeaderState,
|
||||||
follower_states_by_leader: FollowerStatesByLeader,
|
follower_states_by_leader: FollowerStatesByLeader,
|
||||||
|
@ -1008,28 +1008,41 @@ impl Workspace {
|
||||||
|
|
||||||
pub fn show_notification<V: Notification>(
|
pub fn show_notification<V: Notification>(
|
||||||
&mut self,
|
&mut self,
|
||||||
notification: ViewHandle<V>,
|
id: usize,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
|
build_notification: impl FnOnce(&mut ViewContext<Self>) -> ViewHandle<V>,
|
||||||
) {
|
) {
|
||||||
cx.subscribe(¬ification, |this, handle, event, cx| {
|
let type_id = TypeId::of::<V>();
|
||||||
if handle.read(cx).should_dismiss_notification_on_event(event) {
|
if self
|
||||||
this.dismiss_notification(handle.id(), cx);
|
.notifications
|
||||||
}
|
.iter()
|
||||||
})
|
.all(|(existing_type_id, existing_id, _)| {
|
||||||
.detach();
|
(*existing_type_id, *existing_id) != (type_id, id)
|
||||||
self.notifications.push(Box::new(notification));
|
})
|
||||||
cx.notify();
|
{
|
||||||
|
let notification = build_notification(cx);
|
||||||
|
cx.subscribe(¬ification, move |this, handle, event, cx| {
|
||||||
|
if handle.read(cx).should_dismiss_notification_on_event(event) {
|
||||||
|
this.dismiss_notification(type_id, id, cx);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
self.notifications
|
||||||
|
.push((type_id, id, Box::new(notification)));
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dismiss_notification(&mut self, id: usize, cx: &mut ViewContext<Self>) {
|
fn dismiss_notification(&mut self, type_id: TypeId, id: usize, cx: &mut ViewContext<Self>) {
|
||||||
self.notifications.retain(|handle| {
|
self.notifications
|
||||||
if handle.id() == id {
|
.retain(|(existing_type_id, existing_id, _)| {
|
||||||
cx.notify();
|
if (*existing_type_id, *existing_id) == (type_id, id) {
|
||||||
false
|
cx.notify();
|
||||||
} else {
|
false
|
||||||
true
|
} else {
|
||||||
}
|
true
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn items<'a>(
|
pub fn items<'a>(
|
||||||
|
@ -1724,7 +1737,7 @@ impl Workspace {
|
||||||
} else {
|
} else {
|
||||||
Some(
|
Some(
|
||||||
Flex::column()
|
Flex::column()
|
||||||
.with_children(self.notifications.iter().map(|notification| {
|
.with_children(self.notifications.iter().map(|(_, _, notification)| {
|
||||||
ChildView::new(notification.as_ref())
|
ChildView::new(notification.as_ref())
|
||||||
.contained()
|
.contained()
|
||||||
.with_style(theme.notification)
|
.with_style(theme.notification)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue