| 1 | use calling::AcceptMode; |
| 2 | use gpui_kit::assets::IconName; |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::center::NotificationCenter; |
| 8 | use super::layout::{AVATAR_GAP, TOAST_PADDING}; |
| 9 | use crate::app_state::AppState; |
| 10 | use crate::call::RingEntry; |
| 11 | use crate::theme; |
| 12 | use crate::views::avatar::person_avatar; |
| 13 | use crate::views::call_view::pulsing; |
| 14 | use crate::views::widgets::icon; |
| 15 | |
| 16 | pub const RING_AVATAR: f32 = 56.; |
| 17 | const RING_BORDER: f32 = 2.; |
| 18 | const RING_WIDTH: f32 = 3.; |
| 19 | const RING_GAP: f32 = 3.; |
| 20 | const TOAST_RADIUS: f32 = 12.; |
| 21 | const BUTTON_HEIGHT: f32 = 32.; |
| 22 | pub const ENDS_CURRENT_CALL: &str = "Accept ends current call"; |
| 23 | |
| 24 | pub struct RingView { |
| 25 | app: Entity<AppState>, |
| 26 | ring_id: u64, |
| 27 | _subscription: Subscription, |
| 28 | } |
| 29 | |
| 30 | impl RingView { |
| 31 | pub fn new(center: Entity<NotificationCenter>, ring_id: u64, cx: &mut Context<Self>) -> Self { |
| 32 | let app = center.read(cx).app().clone(); |
| 33 | let subscription = cx.observe(&app, |_, _, cx| cx.notify()); |
| 34 | RingView { |
| 35 | app, |
| 36 | ring_id, |
| 37 | _subscription: subscription, |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn answer_button(id: &'static str, label: &str, glyph: IconName, fill: Hsla) -> Stateful<gpui_kit::Div> { |
| 43 | h_flex() |
| 44 | .id(id) |
| 45 | .h(px(BUTTON_HEIGHT)) |
| 46 | .px(px(10.)) |
| 47 | .gap(px(5.)) |
| 48 | .flex_none() |
| 49 | .items_center() |
| 50 | .justify_center() |
| 51 | .rounded_full() |
| 52 | .bg(fill) |
| 53 | .text_color(theme::white()) |
| 54 | .text_size(px(13.)) |
| 55 | .font_weight(FontWeight::SEMIBOLD) |
| 56 | .cursor_pointer() |
| 57 | .hover(|button| button.opacity(0.85)) |
| 58 | .child(icon(glyph, 16., theme::white())) |
| 59 | .child(label.to_owned()) |
| 60 | } |
| 61 | |
| 62 | fn caller_avatar(state: &AppState, entry: &RingEntry) -> AnyElement { |
| 63 | let avatar = person_avatar( |
| 64 | &state.directory, |
| 65 | entry.caller_user_id().as_deref(), |
| 66 | entry.caller_name(), |
| 67 | RING_AVATAR, |
| 68 | ); |
| 69 | let ringed = div() |
| 70 | .p(px(RING_GAP)) |
| 71 | .rounded_full() |
| 72 | .border(px(RING_WIDTH)) |
| 73 | .border_color(theme::green()) |
| 74 | .child(avatar); |
| 75 | pulsing(ringed, "ring-avatar-pulse") |
| 76 | } |
| 77 | |
| 78 | impl Render for RingView { |
| 79 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 80 | let state = self.app.read(cx); |
| 81 | let Some(entry) = state.rings.get(self.ring_id).cloned() else { |
| 82 | return div().size_full().bg(theme::surface_raised()).into_any_element(); |
| 83 | }; |
| 84 | window.set_window_title(&format!("{}: {}", entry.caller_name(), entry.subtitle())); |
| 85 | let ends_current = state.call.is_some(); |
| 86 | let avatar = caller_avatar(state, &entry); |
| 87 | let ring_id = self.ring_id; |
| 88 | let decline_app = self.app.clone(); |
| 89 | let accept_app = self.app.clone(); |
| 90 | let decline = answer_button("ring-decline", "Decline", IconName::PhoneOff, theme::red()).on_click( |
| 91 | move |_, _, cx| { |
| 92 | cx.stop_propagation(); |
| 93 | decline_app.update(cx, |state, cx| state.decline_ring(ring_id, cx)); |
| 94 | }, |
| 95 | ); |
| 96 | let accept = answer_button("ring-accept", entry.accept_label(), IconName::Phone, theme::green()).on_click( |
| 97 | move |_, _, cx| { |
| 98 | cx.stop_propagation(); |
| 99 | accept_app.update(cx, |state, cx| state.accept_ring(ring_id, AcceptMode::Audio, cx)); |
| 100 | }, |
| 101 | ); |
| 102 | let video_app = self.app.clone(); |
| 103 | let accept_video = entry.ring.video.then(|| { |
| 104 | answer_button("ring-accept-video", "Video", IconName::Video, theme::green()).on_click(move |_, _, cx| { |
| 105 | cx.stop_propagation(); |
| 106 | video_app.update(cx, |state, cx| state.accept_ring(ring_id, AcceptMode::Video, cx)); |
| 107 | }) |
| 108 | }); |
| 109 | div() |
| 110 | .id(("ring", ring_id)) |
| 111 | .size_full() |
| 112 | .rounded(px(TOAST_RADIUS)) |
| 113 | .bg(theme::surface_raised()) |
| 114 | .border(px(RING_BORDER)) |
| 115 | .border_color(theme::green()) |
| 116 | .overflow_hidden() |
| 117 | .child( |
| 118 | div().size_full().p(px(TOAST_PADDING)).child( |
| 119 | h_flex() |
| 120 | .size_full() |
| 121 | .gap(px(AVATAR_GAP)) |
| 122 | .items_start() |
| 123 | .child(avatar) |
| 124 | .child( |
| 125 | v_flex() |
| 126 | .flex_1() |
| 127 | .min_w_0() |
| 128 | .gap(px(2.)) |
| 129 | .child( |
| 130 | div() |
| 131 | .truncate() |
| 132 | .text_size(px(15.)) |
| 133 | .font_weight(FontWeight::SEMIBOLD) |
| 134 | .text_color(theme::text()) |
| 135 | .child(entry.caller_name().to_owned()), |
| 136 | ) |
| 137 | .child( |
| 138 | div() |
| 139 | .text_size(px(12.5)) |
| 140 | .text_color(theme::text_muted()) |
| 141 | .child(entry.subtitle()), |
| 142 | ) |
| 143 | .when(ends_current, |column| { |
| 144 | column.child( |
| 145 | div() |
| 146 | .text_size(px(12.)) |
| 147 | .text_color(theme::amber()) |
| 148 | .child(ENDS_CURRENT_CALL), |
| 149 | ) |
| 150 | }) |
| 151 | .child(h_flex().mt(px(8.)).gap(px(6.)).child(decline).child(accept).children(accept_video)), |
| 152 | ), |
| 153 | ), |
| 154 | ) |
| 155 | .into_any_element() |
| 156 | } |
| 157 | } |