1use crate::notification_window_options;
2use call::{ActiveCall, IncomingCall};
3use futures::StreamExt;
4use gpui::{
5 div, px, red, AppContext, Div, Element, ParentElement, Render, RenderOnce, Styled, ViewContext,
6 VisualContext as _, WindowHandle,
7};
8use std::sync::{Arc, Weak};
9use ui::prelude::*;
10use ui::{h_stack, v_stack, Avatar, Button, Label};
11use util::ResultExt;
12use workspace::AppState;
13
14pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
15 let app_state = Arc::downgrade(app_state);
16 let mut incoming_call = ActiveCall::global(cx).read(cx).incoming();
17 cx.spawn(|mut cx| async move {
18 let mut notification_windows: Vec<WindowHandle<IncomingCallNotification>> = Vec::new();
19 while let Some(incoming_call) = incoming_call.next().await {
20 for window in notification_windows.drain(..) {
21 window
22 .update(&mut cx, |_, cx| {
23 // todo!()
24 cx.remove_window();
25 })
26 .log_err();
27 }
28
29 if let Some(incoming_call) = incoming_call {
30 let unique_screens = cx.update(|cx| cx.displays()).unwrap();
31 let window_size = gpui::Size {
32 width: px(380.),
33 height: px(64.),
34 };
35
36 for screen in unique_screens {
37 let options = notification_window_options(screen, window_size);
38 let window = cx
39 .open_window(options, |cx| {
40 cx.build_view(|_| {
41 IncomingCallNotification::new(
42 incoming_call.clone(),
43 app_state.clone(),
44 )
45 })
46 })
47 .unwrap();
48 notification_windows.push(window);
49 }
50 }
51 }
52 })
53 .detach();
54}
55
56#[derive(Clone, PartialEq)]
57struct RespondToCall {
58 accept: bool,
59}
60
61struct IncomingCallNotificationState {
62 call: IncomingCall,
63 app_state: Weak<AppState>,
64}
65
66pub struct IncomingCallNotification {
67 state: Arc<IncomingCallNotificationState>,
68}
69impl IncomingCallNotificationState {
70 pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
71 Self { call, app_state }
72 }
73
74 fn respond(&self, accept: bool, cx: &mut AppContext) {
75 let active_call = ActiveCall::global(cx);
76 if accept {
77 let join = active_call.update(cx, |active_call, cx| active_call.accept_incoming(cx));
78 let caller_user_id = self.call.calling_user.id;
79 let initial_project_id = self.call.initial_project.as_ref().map(|project| project.id);
80 let app_state = self.app_state.clone();
81 let cx: &mut AppContext = cx;
82 cx.spawn(|cx| async move {
83 join.await?;
84 if let Some(project_id) = initial_project_id {
85 cx.update(|cx| {
86 if let Some(app_state) = app_state.upgrade() {
87 workspace::join_remote_project(
88 project_id,
89 caller_user_id,
90 app_state,
91 cx,
92 )
93 .detach_and_log_err(cx);
94 }
95 })
96 .log_err();
97 }
98 anyhow::Ok(())
99 })
100 .detach_and_log_err(cx);
101 } else {
102 active_call.update(cx, |active_call, cx| {
103 active_call.decline_incoming(cx).log_err();
104 });
105 }
106 }
107}
108
109impl IncomingCallNotification {
110 pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
111 Self {
112 state: Arc::new(IncomingCallNotificationState::new(call, app_state)),
113 }
114 }
115 fn render_caller(&self, cx: &mut ViewContext<Self>) -> impl Element {
116 h_stack()
117 .child(Avatar::new(self.state.call.calling_user.avatar_uri.clone()))
118 .child(
119 v_stack()
120 .child(Label::new(format!(
121 "{} is sharing a project in Zed",
122 self.state.call.calling_user.github_login
123 )))
124 .child(self.render_buttons(cx)),
125 )
126 }
127
128 fn render_buttons(&self, cx: &mut ViewContext<Self>) -> impl Element {
129 h_stack()
130 .child(Button::new("accept", "Accept").render(cx).on_click({
131 let state = self.state.clone();
132 move |_, cx| state.respond(true, cx)
133 }))
134 .child(Button::new("decline", "Decline").render(cx).on_click({
135 let state = self.state.clone();
136 move |_, cx| state.respond(false, cx)
137 }))
138 }
139}
140
141impl Render for IncomingCallNotification {
142 type Element = Div;
143
144 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
145 div().bg(red()).flex_none().child(self.render_caller(cx))
146 }
147}