incoming_call_notification.rs

  1use crate::notification_window_options;
  2use crate::notifications::collab_notification::CollabNotification;
  3use call::{ActiveCall, IncomingCall};
  4use futures::StreamExt;
  5use gpui::{prelude::*, AppContext, WindowHandle};
  6use settings::Settings;
  7use std::sync::{Arc, Weak};
  8use theme::{SystemAppearance, ThemeSettings};
  9use ui::{prelude::*, Button, Label};
 10use util::ResultExt;
 11use workspace::AppState;
 12
 13pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
 14    let app_state = Arc::downgrade(app_state);
 15    let mut incoming_call = ActiveCall::global(cx).read(cx).incoming();
 16    cx.spawn(|mut cx| async move {
 17        let mut notification_windows: Vec<WindowHandle<IncomingCallNotification>> = Vec::new();
 18        while let Some(incoming_call) = incoming_call.next().await {
 19            for window in notification_windows.drain(..) {
 20                window
 21                    .update(&mut cx, |_, cx| {
 22                        cx.remove_window();
 23                    })
 24                    .log_err();
 25            }
 26
 27            if let Some(incoming_call) = incoming_call {
 28                let unique_screens = cx.update(|cx| cx.displays()).unwrap();
 29                let window_size = gpui::Size {
 30                    width: px(400.),
 31                    height: px(72.),
 32                };
 33
 34                for screen in unique_screens {
 35                    let options = notification_window_options(screen, window_size);
 36                    let window = cx
 37                        .open_window(options, |cx| {
 38                            SystemAppearance::init_for_window(cx);
 39
 40                            cx.new_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}
116
117impl Render for IncomingCallNotification {
118    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
119        // TODO: Is there a better place for us to initialize the font?
120        let (ui_font, ui_font_size) = {
121            let theme_settings = ThemeSettings::get_global(cx);
122            (
123                theme_settings.ui_font.family.clone(),
124                theme_settings.ui_font_size.clone(),
125            )
126        };
127
128        cx.set_rem_size(ui_font_size);
129
130        div().size_full().font(ui_font).child(
131            CollabNotification::new(
132                self.state.call.calling_user.avatar_uri.clone(),
133                Button::new("accept", "Accept").on_click({
134                    let state = self.state.clone();
135                    move |_, cx| state.respond(true, cx)
136                }),
137                Button::new("decline", "Decline").on_click({
138                    let state = self.state.clone();
139                    move |_, cx| state.respond(false, cx)
140                }),
141            )
142            .child(v_flex().overflow_hidden().child(Label::new(format!(
143                "{} is sharing a project in Zed",
144                self.state.call.calling_user.github_login
145            )))),
146        )
147    }
148}