1use call::{room, ActiveCall};
2use client::User;
3use collections::HashMap;
4use gpui::{
5 actions,
6 elements::*,
7 geometry::{rect::RectF, vector::vec2f},
8 platform::{CursorStyle, MouseButton, WindowBounds, WindowKind, WindowOptions},
9 AppContext, Entity, View, ViewContext,
10};
11use settings::Settings;
12use std::sync::Arc;
13use workspace::JoinProject;
14
15actions!(project_shared_notification, [DismissProject]);
16
17pub fn init(cx: &mut AppContext) {
18 cx.add_action(ProjectSharedNotification::join);
19 cx.add_action(ProjectSharedNotification::dismiss);
20
21 let active_call = ActiveCall::global(cx);
22 let mut notification_windows = HashMap::default();
23 cx.subscribe(&active_call, move |_, event, cx| match event {
24 room::Event::RemoteProjectShared {
25 owner,
26 project_id,
27 worktree_root_names,
28 } => {
29 const PADDING: f32 = 16.;
30 let theme = &cx.global::<Settings>().theme.project_shared_notification;
31 let window_size = vec2f(theme.window_width, theme.window_height);
32
33 for screen in cx.platform().screens() {
34 let screen_bounds = screen.bounds();
35 let (window_id, _) = cx.add_window(
36 WindowOptions {
37 bounds: WindowBounds::Fixed(RectF::new(
38 screen_bounds.upper_right() - vec2f(PADDING + window_size.x(), PADDING),
39 window_size,
40 )),
41 titlebar: None,
42 center: false,
43 focus: false,
44 kind: WindowKind::PopUp,
45 is_movable: false,
46 screen: Some(screen),
47 },
48 |_| {
49 ProjectSharedNotification::new(
50 owner.clone(),
51 *project_id,
52 worktree_root_names.clone(),
53 )
54 },
55 );
56 notification_windows
57 .entry(*project_id)
58 .or_insert(Vec::new())
59 .push(window_id);
60 }
61 }
62 room::Event::RemoteProjectUnshared { project_id } => {
63 if let Some(window_ids) = notification_windows.remove(&project_id) {
64 for window_id in window_ids {
65 cx.update_window(window_id, |cx| cx.remove_window());
66 }
67 }
68 }
69 room::Event::Left => {
70 for (_, window_ids) in notification_windows.drain() {
71 for window_id in window_ids {
72 cx.update_window(window_id, |cx| cx.remove_window());
73 }
74 }
75 }
76 _ => {}
77 })
78 .detach();
79}
80
81pub struct ProjectSharedNotification {
82 project_id: u64,
83 worktree_root_names: Vec<String>,
84 owner: Arc<User>,
85}
86
87impl ProjectSharedNotification {
88 fn new(owner: Arc<User>, project_id: u64, worktree_root_names: Vec<String>) -> Self {
89 Self {
90 project_id,
91 worktree_root_names,
92 owner,
93 }
94 }
95
96 fn join(&mut self, _: &JoinProject, cx: &mut ViewContext<Self>) {
97 cx.remove_window();
98 cx.propagate_action();
99 }
100
101 fn dismiss(&mut self, _: &DismissProject, cx: &mut ViewContext<Self>) {
102 cx.remove_window();
103 }
104
105 fn render_owner(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
106 let theme = &cx.global::<Settings>().theme.project_shared_notification;
107 Flex::row()
108 .with_children(self.owner.avatar.clone().map(|avatar| {
109 Image::from_data(avatar)
110 .with_style(theme.owner_avatar)
111 .aligned()
112 }))
113 .with_child(
114 Flex::column()
115 .with_child(
116 Label::new(
117 self.owner.github_login.clone(),
118 theme.owner_username.text.clone(),
119 )
120 .contained()
121 .with_style(theme.owner_username.container),
122 )
123 .with_child(
124 Label::new(
125 format!(
126 "is sharing a project in Zed{}",
127 if self.worktree_root_names.is_empty() {
128 ""
129 } else {
130 ":"
131 }
132 ),
133 theme.message.text.clone(),
134 )
135 .contained()
136 .with_style(theme.message.container),
137 )
138 .with_children(if self.worktree_root_names.is_empty() {
139 None
140 } else {
141 Some(
142 Label::new(
143 self.worktree_root_names.join(", "),
144 theme.worktree_roots.text.clone(),
145 )
146 .contained()
147 .with_style(theme.worktree_roots.container),
148 )
149 })
150 .contained()
151 .with_style(theme.owner_metadata)
152 .aligned(),
153 )
154 .contained()
155 .with_style(theme.owner_container)
156 .flex(1., true)
157 .into_any()
158 }
159
160 fn render_buttons(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
161 enum Open {}
162 enum Dismiss {}
163
164 let project_id = self.project_id;
165 let owner_user_id = self.owner.id;
166
167 Flex::column()
168 .with_child(
169 MouseEventHandler::<Open, Self>::new(0, cx, |_, cx| {
170 let theme = &cx.global::<Settings>().theme.project_shared_notification;
171 Label::new("Open", theme.open_button.text.clone())
172 .aligned()
173 .contained()
174 .with_style(theme.open_button.container)
175 })
176 .with_cursor_style(CursorStyle::PointingHand)
177 .on_click(MouseButton::Left, move |_, _, cx| {
178 cx.dispatch_action(JoinProject {
179 project_id,
180 follow_user_id: owner_user_id,
181 });
182 })
183 .flex(1., true),
184 )
185 .with_child(
186 MouseEventHandler::<Dismiss, Self>::new(0, cx, |_, cx| {
187 let theme = &cx.global::<Settings>().theme.project_shared_notification;
188 Label::new("Dismiss", theme.dismiss_button.text.clone())
189 .aligned()
190 .contained()
191 .with_style(theme.dismiss_button.container)
192 })
193 .with_cursor_style(CursorStyle::PointingHand)
194 .on_click(MouseButton::Left, |_, _, cx| {
195 cx.dispatch_action(DismissProject);
196 })
197 .flex(1., true),
198 )
199 .constrained()
200 .with_width(
201 cx.global::<Settings>()
202 .theme
203 .project_shared_notification
204 .button_width,
205 )
206 .into_any()
207 }
208}
209
210impl Entity for ProjectSharedNotification {
211 type Event = ();
212}
213
214impl View for ProjectSharedNotification {
215 fn ui_name() -> &'static str {
216 "ProjectSharedNotification"
217 }
218
219 fn render(&mut self, cx: &mut ViewContext<Self>) -> gpui::AnyElement<Self> {
220 let background = cx
221 .global::<Settings>()
222 .theme
223 .project_shared_notification
224 .background;
225 Flex::row()
226 .with_child(self.render_owner(cx))
227 .with_child(self.render_buttons(cx))
228 .contained()
229 .with_background_color(background)
230 .expanded()
231 .into_any()
232 }
233}