Dismiss project shared notifications when a project was unshared

This commit is contained in:
Antonio Scandurra 2022-10-11 11:44:31 +02:00
parent 1d4bdfc4a1
commit 0a306808da
2 changed files with 43 additions and 4 deletions

View file

@ -18,6 +18,10 @@ pub enum Event {
project_id: u64, project_id: u64,
worktree_root_names: Vec<String>, worktree_root_names: Vec<String>,
}, },
RemoteProjectUnshared {
project_id: u64,
},
Left,
} }
pub struct Room { pub struct Room {
@ -148,6 +152,7 @@ impl Room {
} }
cx.notify(); cx.notify();
cx.emit(Event::Left);
self.status = RoomStatus::Offline; self.status = RoomStatus::Offline;
self.remote_participants.clear(); self.remote_participants.clear();
self.pending_participants.clear(); self.pending_participants.clear();
@ -223,15 +228,21 @@ impl Room {
let peer_id = PeerId(participant.peer_id); let peer_id = PeerId(participant.peer_id);
this.participant_user_ids.insert(participant.user_id); this.participant_user_ids.insert(participant.user_id);
let existing_projects = this let old_projects = this
.remote_participants .remote_participants
.get(&peer_id) .get(&peer_id)
.into_iter() .into_iter()
.flat_map(|existing| &existing.projects) .flat_map(|existing| &existing.projects)
.map(|project| project.id) .map(|project| project.id)
.collect::<HashSet<_>>(); .collect::<HashSet<_>>();
let new_projects = participant
.projects
.iter()
.map(|project| project.id)
.collect::<HashSet<_>>();
for project in &participant.projects { for project in &participant.projects {
if !existing_projects.contains(&project.id) { if !old_projects.contains(&project.id) {
cx.emit(Event::RemoteProjectShared { cx.emit(Event::RemoteProjectShared {
owner: user.clone(), owner: user.clone(),
project_id: project.id, project_id: project.id,
@ -240,6 +251,12 @@ impl Room {
} }
} }
for unshared_project_id in old_projects.difference(&new_projects) {
cx.emit(Event::RemoteProjectUnshared {
project_id: *unshared_project_id,
});
}
this.remote_participants.insert( this.remote_participants.insert(
peer_id, peer_id,
RemoteParticipant { RemoteParticipant {
@ -252,7 +269,16 @@ impl Room {
} }
this.remote_participants.retain(|_, participant| { this.remote_participants.retain(|_, participant| {
this.participant_user_ids.contains(&participant.user.id) if this.participant_user_ids.contains(&participant.user.id) {
true
} else {
for project in &participant.projects {
cx.emit(Event::RemoteProjectUnshared {
project_id: project.id,
});
}
false
}
}); });
cx.notify(); cx.notify();

View file

@ -1,5 +1,6 @@
use call::{room, ActiveCall}; use call::{room, ActiveCall};
use client::User; use client::User;
use collections::HashMap;
use gpui::{ use gpui::{
actions, actions,
elements::*, elements::*,
@ -18,6 +19,7 @@ pub fn init(cx: &mut MutableAppContext) {
cx.add_action(ProjectSharedNotification::dismiss); cx.add_action(ProjectSharedNotification::dismiss);
let active_call = ActiveCall::global(cx); let active_call = ActiveCall::global(cx);
let mut notification_windows = HashMap::default();
cx.subscribe(&active_call, move |_, event, cx| match event { cx.subscribe(&active_call, move |_, event, cx| match event {
room::Event::RemoteProjectShared { room::Event::RemoteProjectShared {
owner, owner,
@ -29,7 +31,7 @@ pub fn init(cx: &mut MutableAppContext) {
let theme = &cx.global::<Settings>().theme.project_shared_notification; let theme = &cx.global::<Settings>().theme.project_shared_notification;
let window_size = vec2f(theme.window_width, theme.window_height); let window_size = vec2f(theme.window_width, theme.window_height);
cx.add_window( let (window_id, _) = cx.add_window(
WindowOptions { WindowOptions {
bounds: WindowBounds::Fixed(RectF::new( bounds: WindowBounds::Fixed(RectF::new(
vec2f(screen_size.x() - window_size.x() - PADDING, PADDING), vec2f(screen_size.x() - window_size.x() - PADDING, PADDING),
@ -48,6 +50,17 @@ pub fn init(cx: &mut MutableAppContext) {
) )
}, },
); );
notification_windows.insert(*project_id, window_id);
}
room::Event::RemoteProjectUnshared { project_id } => {
if let Some(window_id) = notification_windows.remove(&project_id) {
cx.remove_window(window_id);
}
}
room::Event::Left => {
for (_, window_id) in notification_windows.drain() {
cx.remove_window(window_id);
}
} }
}) })
.detach(); .detach();