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.remove_window(window_id);
66 }
67 }
68 }
69 room::Event::Left => {
70 for (_, window_ids) in notification_windows.drain() {
71 for window_id in window_ids {
72 cx.remove_window(window_id);
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>) -> Element<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 .boxed()
113 }))
114 .with_child(
115 Flex::column()
116 .with_child(
117 Label::new(
118 self.owner.github_login.clone(),
119 theme.owner_username.text.clone(),
120 )
121 .contained()
122 .with_style(theme.owner_username.container)
123 .boxed(),
124 )
125 .with_child(
126 Label::new(
127 format!(
128 "is sharing a project in Zed{}",
129 if self.worktree_root_names.is_empty() {
130 ""
131 } else {
132 ":"
133 }
134 ),
135 theme.message.text.clone(),
136 )
137 .contained()
138 .with_style(theme.message.container)
139 .boxed(),
140 )
141 .with_children(if self.worktree_root_names.is_empty() {
142 None
143 } else {
144 Some(
145 Label::new(
146 self.worktree_root_names.join(", "),
147 theme.worktree_roots.text.clone(),
148 )
149 .contained()
150 .with_style(theme.worktree_roots.container)
151 .boxed(),
152 )
153 })
154 .contained()
155 .with_style(theme.owner_metadata)
156 .aligned()
157 .boxed(),
158 )
159 .contained()
160 .with_style(theme.owner_container)
161 .flex(1., true)
162 .boxed()
163 }
164
165 fn render_buttons(&self, cx: &mut ViewContext<Self>) -> Element<Self> {
166 enum Open {}
167 enum Dismiss {}
168
169 let project_id = self.project_id;
170 let owner_user_id = self.owner.id;
171
172 Flex::column()
173 .with_child(
174 MouseEventHandler::<Open, Self>::new(0, cx, |_, cx| {
175 let theme = &cx.global::<Settings>().theme.project_shared_notification;
176 Label::new("Open", theme.open_button.text.clone())
177 .aligned()
178 .contained()
179 .with_style(theme.open_button.container)
180 .boxed()
181 })
182 .with_cursor_style(CursorStyle::PointingHand)
183 .on_click(MouseButton::Left, move |_, _, cx| {
184 cx.dispatch_action(JoinProject {
185 project_id,
186 follow_user_id: owner_user_id,
187 });
188 })
189 .flex(1., true)
190 .boxed(),
191 )
192 .with_child(
193 MouseEventHandler::<Dismiss, Self>::new(0, cx, |_, cx| {
194 let theme = &cx.global::<Settings>().theme.project_shared_notification;
195 Label::new("Dismiss", theme.dismiss_button.text.clone())
196 .aligned()
197 .contained()
198 .with_style(theme.dismiss_button.container)
199 .boxed()
200 })
201 .with_cursor_style(CursorStyle::PointingHand)
202 .on_click(MouseButton::Left, |_, _, cx| {
203 cx.dispatch_action(DismissProject);
204 })
205 .flex(1., true)
206 .boxed(),
207 )
208 .constrained()
209 .with_width(
210 cx.global::<Settings>()
211 .theme
212 .project_shared_notification
213 .button_width,
214 )
215 .boxed()
216 }
217}
218
219impl Entity for ProjectSharedNotification {
220 type Event = ();
221}
222
223impl View for ProjectSharedNotification {
224 fn ui_name() -> &'static str {
225 "ProjectSharedNotification"
226 }
227
228 fn render(&mut self, cx: &mut ViewContext<Self>) -> gpui::Element<Self> {
229 let background = cx
230 .global::<Settings>()
231 .theme
232 .project_shared_notification
233 .background;
234 Flex::row()
235 .with_child(self.render_owner(cx))
236 .with_child(self.render_buttons(cx))
237 .contained()
238 .with_background_color(background)
239 .expanded()
240 .boxed()
241 }
242}