Allow clicking a titlebar avatar to initiate following

This commit is contained in:
Max Brunsfeld 2022-03-18 13:37:07 -07:00
parent d860ed25c1
commit e338da0271
3 changed files with 134 additions and 114 deletions

View file

@ -184,6 +184,12 @@ macro_rules! action {
Box::new(self.clone()) Box::new(self.clone())
} }
} }
impl From<$arg> for $name {
fn from(arg: $arg) -> Self {
Self(arg)
}
}
}; };
($name:ident) => { ($name:ident) => {

View file

@ -4272,8 +4272,8 @@ mod tests {
workspace_b workspace_b
.update(cx_b, |workspace, cx| { .update(cx_b, |workspace, cx| {
workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, cx); workspace.split_pane(workspace.active_pane().clone(), SplitDirection::Right, cx);
let leader_id = project_b.read(cx).collaborators().keys().next().unwrap(); let leader_id = *project_b.read(cx).collaborators().keys().next().unwrap();
workspace.follow(*leader_id, cx) workspace.follow(&leader_id.into(), cx).unwrap()
}) })
.await .await
.unwrap(); .unwrap();
@ -4291,8 +4291,7 @@ mod tests {
}); });
workspace_b workspace_b
.condition(cx_b, |workspace, cx| { .condition(cx_b, |workspace, cx| {
let active_item = workspace.active_item(cx).unwrap(); workspace.active_item(cx).unwrap().id() == editor_b1.id()
active_item.project_path(cx) == Some((worktree_id, "1.txt").into())
}) })
.await; .await;
} }

View file

@ -43,7 +43,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use theme::{Theme, ThemeRegistry}; use theme::{Theme, ThemeRegistry};
use util::{ResultExt, TryFutureExt}; use util::ResultExt;
type ProjectItemBuilders = HashMap< type ProjectItemBuilders = HashMap<
TypeId, TypeId,
@ -68,6 +68,7 @@ action!(Open, Arc<AppState>);
action!(OpenNew, Arc<AppState>); action!(OpenNew, Arc<AppState>);
action!(OpenPaths, OpenParams); action!(OpenPaths, OpenParams);
action!(ToggleShare); action!(ToggleShare);
action!(FollowCollaborator, PeerId);
action!(JoinProject, JoinProjectParams); action!(JoinProject, JoinProjectParams);
action!(Save); action!(Save);
action!(DebugElements); action!(DebugElements);
@ -88,6 +89,7 @@ pub fn init(client: &Arc<Client>, cx: &mut MutableAppContext) {
}); });
cx.add_action(Workspace::toggle_share); cx.add_action(Workspace::toggle_share);
cx.add_async_action(Workspace::follow);
cx.add_action( cx.add_action(
|workspace: &mut Workspace, _: &Save, cx: &mut ViewContext<Workspace>| { |workspace: &mut Workspace, _: &Save, cx: &mut ViewContext<Workspace>| {
workspace.save_active_item(cx).detach_and_log_err(cx); workspace.save_active_item(cx).detach_and_log_err(cx);
@ -1192,13 +1194,18 @@ impl Workspace {
} }
} }
pub fn follow(&mut self, leader_id: PeerId, cx: &mut ViewContext<Self>) -> Task<Result<()>> { pub fn follow(
if let Some(project_id) = self.project.read(cx).remote_id() { &mut self,
FollowCollaborator(leader_id): &FollowCollaborator,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
let leader_id = *leader_id;
let project_id = self.project.read(cx).remote_id()?;
let request = self.client.request(proto::Follow { let request = self.client.request(proto::Follow {
project_id, project_id,
leader_id: leader_id.0, leader_id: leader_id.0,
}); });
cx.spawn_weak(|this, mut cx| async move { Some(cx.spawn_weak(|this, mut cx| async move {
let mut response = request.await?; let mut response = request.await?;
if let Some(this) = this.upgrade(&cx) { if let Some(this) = this.upgrade(&cx) {
let mut item_tasks = Vec::new(); let mut item_tasks = Vec::new();
@ -1270,10 +1277,7 @@ impl Workspace {
}); });
} }
Ok(()) Ok(())
}) }))
} else {
Task::ready(Err(anyhow!("project is not remote")))
}
} }
fn update_followers( fn update_followers(
@ -1383,7 +1387,9 @@ impl Workspace {
Some(self.render_avatar( Some(self.render_avatar(
collaborator.user.avatar.clone()?, collaborator.user.avatar.clone()?,
collaborator.replica_id, collaborator.replica_id,
Some(collaborator.peer_id),
theme, theme,
cx,
)) ))
}) })
.collect() .collect()
@ -1397,7 +1403,7 @@ impl Workspace {
cx: &mut RenderContext<Self>, cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
if let Some(avatar) = user.and_then(|user| user.avatar.clone()) { if let Some(avatar) = user.and_then(|user| user.avatar.clone()) {
self.render_avatar(avatar, replica_id, theme) self.render_avatar(avatar, replica_id, None, theme, cx)
} else { } else {
MouseEventHandler::new::<Authenticate, _, _>(0, cx, |state, _| { MouseEventHandler::new::<Authenticate, _, _>(0, cx, |state, _| {
let style = if state.hovered { let style = if state.hovered {
@ -1421,16 +1427,15 @@ impl Workspace {
&self, &self,
avatar: Arc<ImageData>, avatar: Arc<ImageData>,
replica_id: ReplicaId, replica_id: ReplicaId,
peer_id: Option<PeerId>,
theme: &Theme, theme: &Theme,
cx: &mut RenderContext<Self>,
) -> ElementBox { ) -> ElementBox {
ConstrainedBox::new( let content = Stack::new()
Stack::new()
.with_child( .with_child(
ConstrainedBox::new(
Image::new(avatar) Image::new(avatar)
.with_style(theme.workspace.titlebar.avatar) .with_style(theme.workspace.titlebar.avatar)
.boxed(), .constrained()
)
.with_width(theme.workspace.titlebar.avatar_width) .with_width(theme.workspace.titlebar.avatar_width)
.aligned() .aligned()
.boxed(), .boxed(),
@ -1444,27 +1449,37 @@ impl Workspace {
.bottom() .bottom()
.boxed(), .boxed(),
) )
.boxed(), .constrained()
)
.with_width(theme.workspace.right_sidebar.width) .with_width(theme.workspace.right_sidebar.width)
.boxed();
if let Some(peer_id) = peer_id {
MouseEventHandler::new::<FollowCollaborator, _, _>(
replica_id.into(),
cx,
move |_, _| content,
)
.with_cursor_style(CursorStyle::PointingHand)
.on_click(move |cx| cx.dispatch_action(FollowCollaborator(peer_id)))
.boxed() .boxed()
} else {
content
}
} }
fn render_share_icon(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> Option<ElementBox> { fn render_share_icon(&self, theme: &Theme, cx: &mut RenderContext<Self>) -> Option<ElementBox> {
if self.project().read(cx).is_local() && self.client.user_id().is_some() { if self.project().read(cx).is_local() && self.client.user_id().is_some() {
enum Share {}
let color = if self.project().read(cx).is_shared() { let color = if self.project().read(cx).is_shared() {
theme.workspace.titlebar.share_icon_active_color theme.workspace.titlebar.share_icon_active_color
} else { } else {
theme.workspace.titlebar.share_icon_color theme.workspace.titlebar.share_icon_color
}; };
Some( Some(
MouseEventHandler::new::<Share, _, _>(0, cx, |_, _| { MouseEventHandler::new::<ToggleShare, _, _>(0, cx, |_, _| {
Align::new( Align::new(
ConstrainedBox::new( Svg::new("icons/broadcast-24.svg")
Svg::new("icons/broadcast-24.svg").with_color(color).boxed(), .with_color(color)
) .constrained()
.with_width(24.) .with_width(24.)
.boxed(), .boxed(),
) )