BlueGit

ict rusty-teams

Files

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

ring_view.rs

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