BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / notify / ring_view.rs

ring_view.rs

ring_view.rs 6.9 KiB Download Raw
1use calling::AcceptMode;
2use gpui_kit::assets::IconName;
3use gpui_kit::component::{h_flex, v_flex};
4use gpui_kit::prelude::FluentBuilder as _;
5use gpui_kit::*;
6
7use super::center::NotificationCenter;
8use super::layout::{AVATAR_GAP, TOAST_PADDING};
9use super::platform;
10use crate::app_state::AppState;
11use crate::call::RingEntry;
12use crate::theme;
13use crate::views::avatar::person_avatar;
14use crate::views::call_view::pulsing;
15use crate::views::widgets::icon;
16
17pub const RING_AVATAR: f32 = 56.;
18const RING_WIDTH: f32 = 3.;
19const RING_GAP: f32 = 3.;
20const TOAST_RADIUS: f32 = 10.;
21const BUTTON_HEIGHT: f32 = 32.;
22pub const ENDS_CURRENT_CALL: &str = "Accept ends current call";
23
24pub(super) fn toast_radius(window: &Window) -> Pixels {
25 px(card_radius(platform::window_transparency_available(window)))
26}
27
28fn card_radius(window_transparent: bool) -> f32 {
29 if window_transparent { TOAST_RADIUS } else { 0. }
30}
31
32pub struct RingView {
33 app: Entity<AppState>,
34 center: Entity<NotificationCenter>,
35 ring_id: u64,
36 _subscription: Subscription,
37}
38
39impl 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
52pub(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
78fn 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
94impl 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_center = self.center.clone();
105 let accept_center = self.center.clone();
106 let decline = answer_button(
107 "ring-decline",
108 "Decline",
109 IconName::PhoneOff,
110 theme::red(),
111 theme::white(),
112 )
113 .on_click(move |_, _, cx| {
114 cx.stop_propagation();
115 decline_center.update(cx, |center, cx| center.decline_ring(ring_id, cx));
116 });
117 let accept = answer_button(
118 "ring-accept",
119 entry.accept_label(),
120 IconName::Phone,
121 theme::green(),
122 theme::white(),
123 )
124 .on_click(move |_, _, cx| {
125 cx.stop_propagation();
126 accept_center.update(cx, |center, cx| {
127 center.accept_ring(ring_id, AcceptMode::Audio, cx)
128 });
129 });
130 let video_center = self.center.clone();
131 let accept_video = entry.ring.video.then(|| {
132 answer_button(
133 "ring-accept-video",
134 "Video",
135 IconName::Video,
136 theme::green(),
137 theme::white(),
138 )
139 .on_click(move |_, _, cx| {
140 cx.stop_propagation();
141 video_center.update(cx, |center, cx| {
142 center.accept_ring(ring_id, AcceptMode::Video, cx)
143 });
144 })
145 });
146 div()
147 .id(("ring", ring_id))
148 .size_full()
149 .rounded(toast_radius(window))
150 .bg(theme::popup())
151 .overflow_hidden()
152 .child(
153 div().size_full().p(px(TOAST_PADDING)).child(
154 h_flex()
155 .size_full()
156 .gap(px(AVATAR_GAP))
157 .items_start()
158 .child(avatar)
159 .child(
160 v_flex()
161 .flex_1()
162 .min_w_0()
163 .gap(px(2.))
164 .child(
165 div()
166 .truncate()
167 .text_size(px(15.))
168 .font_weight(FontWeight::SEMIBOLD)
169 .text_color(theme::text())
170 .child(entry.caller_name().to_owned()),
171 )
172 .child(
173 div()
174 .text_size(px(12.5))
175 .text_color(theme::text_muted())
176 .child(entry.subtitle()),
177 )
178 .when(ends_current, |column| {
179 column.child(
180 div()
181 .text_size(px(12.))
182 .text_color(theme::amber())
183 .child(ENDS_CURRENT_CALL),
184 )
185 })
186 .child(
187 h_flex()
188 .mt(px(8.))
189 .gap(px(6.))
190 .child(decline)
191 .child(accept)
192 .children(accept_video),
193 ),
194 ),
195 ),
196 )
197 .into_any_element()
198 }
199}
200
201#[cfg(test)]
202mod tests {
203 use super::{TOAST_RADIUS, card_radius};
204
205 #[test]
206 fn toast_cards_turn_square_without_window_transparency() {
207 assert_eq!(card_radius(true), TOAST_RADIUS);
208 assert_eq!(card_radius(false), 0.);
209 }
210}