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