From 9575796f9ec164aeed22e7a4beb8ab88837746b2 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 21 Mar 2022 17:10:23 +0100 Subject: [PATCH] Allow unfollowing of leaders by clicking on their avatar --- crates/server/src/rpc.rs | 2 +- crates/workspace/src/workspace.rs | 35 ++++++++++++++++++------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/server/src/rpc.rs b/crates/server/src/rpc.rs index 044f42368207870bdeaf89d56e87eda49969be35..fe56cea2e299c763fc29b9605ebd5c1015728a3a 100644 --- a/crates/server/src/rpc.rs +++ b/crates/server/src/rpc.rs @@ -4281,7 +4281,7 @@ mod tests { workspace_b .update(cx_b, |workspace, cx| { let leader_id = *project_b.read(cx).collaborators().keys().next().unwrap(); - workspace.follow(&leader_id.into(), cx).unwrap() + workspace.toggle_follow(&leader_id.into(), cx).unwrap() }) .await .unwrap(); diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 5b3a21f7e893ad42a7bf64c9db75ce7835e79c65..760ffa8c1a8d84a954e3e9ac597e7af68fc12b15 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -69,7 +69,7 @@ action!(Open, Arc); action!(OpenNew, Arc); action!(OpenPaths, OpenParams); action!(ToggleShare); -action!(FollowCollaborator, PeerId); +action!(ToggleFollow, PeerId); action!(Unfollow); action!(JoinProject, JoinProjectParams); action!(Save); @@ -91,7 +91,7 @@ pub fn init(client: &Arc, cx: &mut MutableAppContext) { }); cx.add_action(Workspace::toggle_share); - cx.add_async_action(Workspace::follow); + cx.add_async_action(Workspace::toggle_follow); cx.add_action( |workspace: &mut Workspace, _: &Unfollow, cx: &mut ViewContext| { let pane = workspace.active_pane().clone(); @@ -1211,15 +1211,21 @@ impl Workspace { } } - pub fn follow( + pub fn toggle_follow( &mut self, - FollowCollaborator(leader_id): &FollowCollaborator, + ToggleFollow(leader_id): &ToggleFollow, cx: &mut ViewContext, ) -> Option>> { let leader_id = *leader_id; let pane = self.active_pane().clone(); - self.unfollow(&pane, cx); + if let Some(prev_leader_id) = self.unfollow(&pane, cx) { + if leader_id == prev_leader_id { + cx.notify(); + return None; + } + } + self.follower_states_by_leader .entry(leader_id) .or_default() @@ -1255,7 +1261,11 @@ impl Workspace { })) } - pub fn unfollow(&mut self, pane: &ViewHandle, cx: &mut ViewContext) -> Option<()> { + pub fn unfollow( + &mut self, + pane: &ViewHandle, + cx: &mut ViewContext, + ) -> Option { for (leader_id, states_by_pane) in &mut self.follower_states_by_leader { if let Some(state) = states_by_pane.remove(&pane) { for (_, item) in state.items_by_leader_view_id { @@ -1276,6 +1286,7 @@ impl Workspace { } cx.notify(); + return Some(*leader_id); } } None @@ -1437,14 +1448,10 @@ impl Workspace { .boxed(); if let Some(peer_id) = peer_id { - MouseEventHandler::new::( - replica_id.into(), - cx, - move |_, _| content, - ) - .with_cursor_style(CursorStyle::PointingHand) - .on_click(move |cx| cx.dispatch_action(FollowCollaborator(peer_id))) - .boxed() + MouseEventHandler::new::(replica_id.into(), cx, move |_, _| content) + .with_cursor_style(CursorStyle::PointingHand) + .on_click(move |cx| cx.dispatch_action(ToggleFollow(peer_id))) + .boxed() } else { content }