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::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                            cx.new_view(|_| {
 39                                IncomingCallNotification::new(
 40                                    incoming_call.clone(),
 41                                    app_state.clone(),
 42                                )
 43                            })
 44                        })
 45                        .unwrap();
 46                    notification_windows.push(window);
 47                }
 48            }
 49        }
 50    })
 51    .detach();
 52}
 53
 54#[derive(Clone, PartialEq)]
 55struct RespondToCall {
 56    accept: bool,
 57}
 58
 59struct IncomingCallNotificationState {
 60    call: IncomingCall,
 61    app_state: Weak<AppState>,
 62}
 63
 64pub struct IncomingCallNotification {
 65    state: Arc<IncomingCallNotificationState>,
 66}
 67impl IncomingCallNotificationState {
 68    pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
 69        Self { call, app_state }
 70    }
 71
 72    fn respond(&self, accept: bool, cx: &mut AppContext) {
 73        let active_call = ActiveCall::global(cx);
 74        if accept {
 75            let join = active_call.update(cx, |active_call, cx| active_call.accept_incoming(cx));
 76            let caller_user_id = self.call.calling_user.id;
 77            let initial_project_id = self.call.initial_project.as_ref().map(|project| project.id);
 78            let app_state = self.app_state.clone();
 79            let cx: &mut AppContext = cx;
 80            cx.spawn(|cx| async move {
 81                join.await?;
 82                if let Some(project_id) = initial_project_id {
 83                    cx.update(|cx| {
 84                        if let Some(app_state) = app_state.upgrade() {
 85                            workspace::join_remote_project(
 86                                project_id,
 87                                caller_user_id,
 88                                app_state,
 89                                cx,
 90                            )
 91                            .detach_and_log_err(cx);
 92                        }
 93                    })
 94                    .log_err();
 95                }
 96                anyhow::Ok(())
 97            })
 98            .detach_and_log_err(cx);
 99        } else {
100            active_call.update(cx, |active_call, cx| {
101                active_call.decline_incoming(cx).log_err();
102            });
103        }
104    }
105}
106
107impl IncomingCallNotification {
108    pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
109        Self {
110            state: Arc::new(IncomingCallNotificationState::new(call, app_state)),
111        }
112    }
113}
114
115impl Render for IncomingCallNotification {
116    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
117        // TODO: Is there a better place for us to initialize the font?
118        let (ui_font, ui_font_size) = {
119            let theme_settings = ThemeSettings::get_global(cx);
120            (
121                theme_settings.ui_font.family.clone(),
122                theme_settings.ui_font_size.clone(),
123            )
124        };
125
126        cx.set_rem_size(ui_font_size);
127
128        div().size_full().font(ui_font).child(
129            CollabNotification::new(
130                self.state.call.calling_user.avatar_uri.clone(),
131                Button::new("accept", "Accept").on_click({
132                    let state = self.state.clone();
133                    move |_, cx| state.respond(true, cx)
134                }),
135                Button::new("decline", "Decline").on_click({
136                    let state = self.state.clone();
137                    move |_, cx| state.respond(false, cx)
138                }),
139            )
140            .child(v_flex().overflow_hidden().child(Label::new(format!(
141                "{} is sharing a project in Zed",
142                self.state.call.calling_user.github_login
143            )))),
144        )
145    }
146}