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