| 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 super::platform; |
| 10 | use crate::app_state::AppState; |
| 11 | use crate::call::RingEntry; |
| 12 | use crate::theme; |
| 13 | use crate::views::avatar::person_avatar; |
| 14 | use crate::views::call_view::pulsing; |
| 15 | use crate::views::widgets::icon; |
| 16 | |
| 17 | pub const RING_AVATAR: f32 = 56.; |
| 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(super) fn toast_radius(window: &Window) -> Pixels { |
| 25 | px(card_radius(platform::window_transparency_available(window))) |
| 26 | } |
| 27 | |
| 28 | fn card_radius(window_transparent: bool) -> f32 { |
| 29 | if window_transparent { TOAST_RADIUS } else { 0. } |
| 30 | } |
| 31 | |
| 32 | pub struct RingView { |
| 33 | app: Entity<AppState>, |
| 34 | ring_id: u64, |
| 35 | _subscription: Subscription, |
| 36 | } |
| 37 | |
| 38 | impl RingView { |
| 39 | pub fn new(center: Entity<NotificationCenter>, ring_id: u64, cx: &mut Context<Self>) -> Self { |
| 40 | let app = center.read(cx).app().clone(); |
| 41 | let subscription = cx.observe(&app, |_, _, cx| cx.notify()); |
| 42 | RingView { |
| 43 | app, |
| 44 | ring_id, |
| 45 | _subscription: subscription, |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub(super) fn answer_button( |
| 51 | id: &'static str, |
| 52 | label: &str, |
| 53 | glyph: IconName, |
| 54 | fill: Hsla, |
| 55 | ) -> Stateful<gpui_kit::Div> { |
| 56 | h_flex() |
| 57 | .id(id) |
| 58 | .h(px(BUTTON_HEIGHT)) |
| 59 | .px(px(10.)) |
| 60 | .gap(px(5.)) |
| 61 | .flex_none() |
| 62 | .items_center() |
| 63 | .justify_center() |
| 64 | .rounded_full() |
| 65 | .bg(fill) |
| 66 | .text_color(theme::white()) |
| 67 | .text_size(px(13.)) |
| 68 | .font_weight(FontWeight::SEMIBOLD) |
| 69 | .cursor_pointer() |
| 70 | .hover(|button| button.opacity(0.85)) |
| 71 | .child(icon(glyph, 16., theme::white())) |
| 72 | .child(label.to_owned()) |
| 73 | } |
| 74 | |
| 75 | fn caller_avatar(state: &AppState, entry: &RingEntry) -> AnyElement { |
| 76 | let avatar = person_avatar( |
| 77 | &state.directory, |
| 78 | entry.caller_user_id().as_deref(), |
| 79 | entry.caller_name(), |
| 80 | RING_AVATAR, |
| 81 | ); |
| 82 | let ringed = div() |
| 83 | .p(px(RING_GAP)) |
| 84 | .rounded_full() |
| 85 | .border(px(RING_WIDTH)) |
| 86 | .border_color(theme::green()) |
| 87 | .child(avatar); |
| 88 | pulsing(ringed, "ring-avatar-pulse") |
| 89 | } |
| 90 | |
| 91 | impl Render for RingView { |
| 92 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 93 | let state = self.app.read(cx); |
| 94 | let Some(entry) = state.rings.get(self.ring_id).cloned() else { |
| 95 | return div().size_full().bg(theme::popup()).into_any_element(); |
| 96 | }; |
| 97 | window.set_window_title(&format!("{}: {}", entry.caller_name(), entry.subtitle())); |
| 98 | let ends_current = state.call.is_some(); |
| 99 | let avatar = caller_avatar(state, &entry); |
| 100 | let ring_id = self.ring_id; |
| 101 | let decline_app = self.app.clone(); |
| 102 | let accept_app = self.app.clone(); |
| 103 | let decline = answer_button("ring-decline", "Decline", IconName::PhoneOff, theme::red()) |
| 104 | .on_click(move |_, _, cx| { |
| 105 | cx.stop_propagation(); |
| 106 | decline_app.update(cx, |state, cx| state.decline_ring(ring_id, cx)); |
| 107 | }); |
| 108 | let accept = answer_button( |
| 109 | "ring-accept", |
| 110 | entry.accept_label(), |
| 111 | IconName::Phone, |
| 112 | theme::green(), |
| 113 | ) |
| 114 | .on_click(move |_, _, cx| { |
| 115 | cx.stop_propagation(); |
| 116 | accept_app.update(cx, |state, cx| { |
| 117 | state.accept_ring(ring_id, AcceptMode::Audio, cx) |
| 118 | }); |
| 119 | }); |
| 120 | let video_app = self.app.clone(); |
| 121 | let accept_video = entry.ring.video.then(|| { |
| 122 | answer_button( |
| 123 | "ring-accept-video", |
| 124 | "Video", |
| 125 | IconName::Video, |
| 126 | theme::green(), |
| 127 | ) |
| 128 | .on_click(move |_, _, cx| { |
| 129 | cx.stop_propagation(); |
| 130 | video_app.update(cx, |state, cx| { |
| 131 | state.accept_ring(ring_id, AcceptMode::Video, cx) |
| 132 | }); |
| 133 | }) |
| 134 | }); |
| 135 | div() |
| 136 | .id(("ring", ring_id)) |
| 137 | .size_full() |
| 138 | .rounded(toast_radius(window)) |
| 139 | .bg(theme::popup()) |
| 140 | .overflow_hidden() |
| 141 | .child( |
| 142 | div().size_full().p(px(TOAST_PADDING)).child( |
| 143 | h_flex() |
| 144 | .size_full() |
| 145 | .gap(px(AVATAR_GAP)) |
| 146 | .items_start() |
| 147 | .child(avatar) |
| 148 | .child( |
| 149 | v_flex() |
| 150 | .flex_1() |
| 151 | .min_w_0() |
| 152 | .gap(px(2.)) |
| 153 | .child( |
| 154 | div() |
| 155 | .truncate() |
| 156 | .text_size(px(15.)) |
| 157 | .font_weight(FontWeight::SEMIBOLD) |
| 158 | .text_color(theme::text()) |
| 159 | .child(entry.caller_name().to_owned()), |
| 160 | ) |
| 161 | .child( |
| 162 | div() |
| 163 | .text_size(px(12.5)) |
| 164 | .text_color(theme::text_muted()) |
| 165 | .child(entry.subtitle()), |
| 166 | ) |
| 167 | .when(ends_current, |column| { |
| 168 | column.child( |
| 169 | div() |
| 170 | .text_size(px(12.)) |
| 171 | .text_color(theme::amber()) |
| 172 | .child(ENDS_CURRENT_CALL), |
| 173 | ) |
| 174 | }) |
| 175 | .child( |
| 176 | h_flex() |
| 177 | .mt(px(8.)) |
| 178 | .gap(px(6.)) |
| 179 | .child(decline) |
| 180 | .child(accept) |
| 181 | .children(accept_video), |
| 182 | ), |
| 183 | ), |
| 184 | ), |
| 185 | ) |
| 186 | .into_any_element() |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | #[cfg(test)] |
| 191 | mod tests { |
| 192 | use super::{TOAST_RADIUS, card_radius}; |
| 193 | |
| 194 | #[test] |
| 195 | fn toast_cards_turn_square_without_window_transparency() { |
| 196 | assert_eq!(card_radius(true), TOAST_RADIUS); |
| 197 | assert_eq!(card_radius(false), 0.); |
| 198 | } |
| 199 | } |