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