BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / call_view.rs

call_view.rs

call_view.rs 9.7 KiB Download Raw
1use std::time::{Duration, Instant};
2
3use calling::DeviceEntry;
4use gpui_kit::assets::IconName;
5use gpui_kit::component::{
6 button::{Button, ButtonVariants as _},
7 h_flex,
8 menu::{DropdownMenu as _, PopupMenu, PopupMenuItem},
9 tooltip::Tooltip,
10 v_flex,
11};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14
15use super::avatar::person_avatar;
16use super::widgets::icon;
17use crate::app_state::AppState;
18use crate::call::CallModel;
19use crate::theme;
20
21const HEADER_HEIGHT: f32 = 60.;
22const TILE_WIDTH: f32 = 260.;
23const TILE_HEIGHT: f32 = 210.;
24const AVATAR_SIZE: f32 = 96.;
25const RING_WIDTH: f32 = 3.;
26const RING_GAP: f32 = 4.;
27const CONTROL_SIZE: f32 = 44.;
28const CONTROL_ICON: f32 = 20.;
29const MENU_WIDTH: f32 = 280.;
30const PULSE_PERIOD: Duration = Duration::from_millis(1400);
31const DISABLED_OPACITY: f32 = 0.4;
32pub const ECHO_NAME: &str = "Teams echo";
33pub const NO_MICROPHONE_HINT: &str = "No microphone found";
34
35pub fn status_line(model: &CallModel, now: Instant) -> (String, Hsla) {
36 if model.is_connecting() {
37 ("Calling...".to_owned(), theme::text_muted())
38 } else if model.is_reconnecting() {
39 ("Reconnecting...".to_owned(), theme::amber())
40 } else {
41 (model.timer_text(now), theme::text_muted())
42 }
43}
44
45pub fn pulsing(element: impl IntoElement + 'static, id: &'static str) -> AnyElement {
46 div()
47 .child(element)
48 .with_animation(
49 id,
50 Animation::new(PULSE_PERIOD).repeat(),
51 |element, delta| {
52 let wave = 1. - (delta * 2. - 1.).abs();
53 element.opacity(0.45 + 0.55 * wave)
54 },
55 )
56 .into_any_element()
57}
58
59fn speaking_ring(avatar: AnyElement, speaking: bool) -> Div {
60 div()
61 .p(px(RING_GAP))
62 .rounded_full()
63 .border(px(RING_WIDTH))
64 .border_color(if speaking {
65 theme::green()
66 } else {
67 transparent_black()
68 })
69 .child(avatar)
70}
71
72fn tile(avatar: AnyElement, name: &str, speaking: bool, muted: bool, caption: Option<AnyElement>) -> Div {
73 let badge = div()
74 .absolute()
75 .top(px(10.))
76 .right(px(10.))
77 .size(px(24.))
78 .flex()
79 .items_center()
80 .justify_center()
81 .rounded_full()
82 .bg(theme::red())
83 .child(icon(IconName::MicOff, 13., theme::white()));
84 v_flex()
85 .relative()
86 .w(px(TILE_WIDTH))
87 .h(px(TILE_HEIGHT))
88 .items_center()
89 .justify_center()
90 .gap(px(12.))
91 .rounded(px(12.))
92 .bg(theme::surface())
93 .border_1()
94 .border_color(theme::border())
95 .child(speaking_ring(avatar, speaking))
96 .child(
97 v_flex()
98 .items_center()
99 .gap(px(2.))
100 .child(
101 div()
102 .text_size(px(14.))
103 .font_weight(FontWeight::SEMIBOLD)
104 .child(name.to_owned()),
105 )
106 .children(caption),
107 )
108 .when(muted, |tile| tile.child(badge))
109}
110
111fn round_control(id: &'static str, glyph: IconName, background: Hsla, foreground: Hsla) -> Stateful<Div> {
112 div()
113 .id(id)
114 .size(px(CONTROL_SIZE))
115 .flex()
116 .flex_none()
117 .items_center()
118 .justify_center()
119 .rounded_full()
120 .bg(background)
121 .cursor_pointer()
122 .hover(|button| button.opacity(0.85))
123 .child(icon(glyph, CONTROL_ICON, foreground))
124}
125
126pub fn mute_button(app: &Entity<AppState>, model: &CallModel, id: &'static str, size: f32) -> Stateful<Div> {
127 let (glyph, background, foreground) = if model.muted {
128 (IconName::MicOff, theme::red(), theme::white())
129 } else {
130 (IconName::Mic, theme::surface_raised(), theme::text())
131 };
132 let button = round_control(id, glyph, background, foreground).size(px(size));
133 if !model.can_unmute() {
134 return button
135 .opacity(DISABLED_OPACITY)
136 .cursor_default()
137 .tooltip(|window, cx| Tooltip::new(NO_MICROPHONE_HINT).build(window, cx));
138 }
139 let app = app.clone();
140 button.on_click(move |_, _, cx| {
141 cx.stop_propagation();
142 app.update(cx, |state, cx| state.toggle_call_mute(cx));
143 })
144}
145
146pub fn leave_button(app: &Entity<AppState>, model: &CallModel, id: &'static str, compact: bool) -> Stateful<Div> {
147 let label = if model.is_connecting() { "Cancel" } else { "Leave" };
148 let app = app.clone();
149 h_flex()
150 .id(id)
151 .h(px(if compact { 28. } else { CONTROL_SIZE }))
152 .px(px(if compact { 10. } else { 20. }))
153 .gap(px(8.))
154 .flex_none()
155 .items_center()
156 .justify_center()
157 .rounded_full()
158 .bg(theme::red())
159 .text_color(theme::white())
160 .text_size(px(if compact { 12. } else { 14. }))
161 .font_weight(FontWeight::SEMIBOLD)
162 .cursor_pointer()
163 .hover(|button| button.opacity(0.85))
164 .child(icon(IconName::PhoneOff, if compact { 14. } else { 18. }, theme::white()))
165 .child(label)
166 .on_click(move |_, _, cx| {
167 cx.stop_propagation();
168 app.update(cx, |state, cx| state.leave_call(cx));
169 })
170}
171
172fn device_items(
173 entries: Vec<DeviceEntry>,
174 selected: &calling::DeviceChoice,
175 pick: impl Fn(calling::DeviceChoice, &mut App) + Clone + 'static,
176) -> Vec<PopupMenuItem> {
177 entries
178 .into_iter()
179 .map(|entry| {
180 let pick = pick.clone();
181 let choice = entry.choice.clone();
182 PopupMenuItem::new(entry.label)
183 .checked(&entry.choice == selected)
184 .on_click(move |_, _, cx| pick(choice.clone(), cx))
185 })
186 .collect()
187}
188
189fn devices_menu(popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
190 let input_app = app.clone();
191 let output_app = app.clone();
192 let inputs = device_items(model.devices.input_entries(), &model.input, move |choice, cx| {
193 input_app.update(cx, |state, cx| state.select_call_input(choice, cx));
194 });
195 let outputs = device_items(model.devices.output_entries(), &model.output, move |choice, cx| {
196 output_app.update(cx, |state, cx| state.select_call_output(choice, cx));
197 });
198 let mut popup = popup
199 .min_w(px(MENU_WIDTH))
200 .item(PopupMenuItem::label("Microphone"));
201 if model.listen_only {
202 popup = popup.item(PopupMenuItem::new(NO_MICROPHONE_HINT).disabled(true));
203 } else {
204 for item in inputs {
205 popup = popup.item(item);
206 }
207 }
208 popup = popup.separator().item(PopupMenuItem::label("Speaker"));
209 for item in outputs {
210 popup = popup.item(item);
211 }
212 popup
213}
214
215fn devices_button(app: &Entity<AppState>, model: &CallModel) -> impl IntoElement {
216 let app = app.clone();
217 let model = model.clone();
218 Button::new("call-devices")
219 .ghost()
220 .p_0()
221 .size(px(CONTROL_SIZE))
222 .rounded_full()
223 .child(round_control(
224 "call-devices-face",
225 IconName::Volume2,
226 theme::surface_raised(),
227 theme::text(),
228 ))
229 .dropdown_menu_with_anchor(Anchor::BottomLeft, move |popup, _, _| {
230 devices_menu(popup, &app, &model)
231 })
232}
233
234pub fn render_call_view(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> {
235 let call = state.call.as_ref()?;
236 let model = &call.model;
237 let now = Instant::now();
238 let (status, status_color) = status_line(model, now);
239 let me = state.directory.me.as_ref();
240 let header = h_flex()
241 .w_full()
242 .h(px(HEADER_HEIGHT))
243 .flex_none()
244 .px(px(20.))
245 .gap(px(12.))
246 .items_center()
247 .border_b_1()
248 .border_color(theme::border())
249 .child(icon(IconName::Phone, 18., theme::accent_text()))
250 .child(
251 div()
252 .text_size(px(16.))
253 .font_weight(FontWeight::SEMIBOLD)
254 .child(model.title.clone()),
255 )
256 .child(div().text_size(px(13.)).text_color(status_color).child(status));
257
258 let echo_avatar = person_avatar(&state.directory, None, ECHO_NAME, AVATAR_SIZE);
259 let calling_caption = model.is_connecting().then(|| {
260 pulsing(
261 div()
262 .text_size(px(12.))
263 .text_color(theme::text_muted())
264 .child("Calling..."),
265 "call-calling",
266 )
267 });
268 let echo_avatar = if model.is_connecting() {
269 pulsing(echo_avatar, "call-echo-pulse")
270 } else {
271 echo_avatar
272 };
273 let echo_tile = tile(echo_avatar, ECHO_NAME, model.remote_speaking, false, calling_caption);
274 let own_avatar = person_avatar(
275 &state.directory,
276 me.map(|person| person.user_id.as_str()),
277 me.map_or("You", |person| person.display_name.as_str()),
278 AVATAR_SIZE,
279 );
280 let own_tile = tile(own_avatar, "You", model.local_speaking, model.muted, None);
281
282 let controls = h_flex()
283 .w_full()
284 .flex_none()
285 .py(px(18.))
286 .gap(px(12.))
287 .items_center()
288 .justify_center()
289 .border_t_1()
290 .border_color(theme::border())
291 .child(mute_button(app, model, "call-mute", CONTROL_SIZE))
292 .child(devices_button(app, model))
293 .child(leave_button(app, model, "call-leave", false));
294
295 Some(
296 v_flex()
297 .flex_1()
298 .min_w_0()
299 .h_full()
300 .bg(theme::background())
301 .child(header)
302 .child(
303 h_flex()
304 .flex_1()
305 .min_h_0()
306 .gap(px(20.))
307 .items_center()
308 .justify_center()
309 .child(echo_tile)
310 .child(own_tile),
311 )
312 .child(controls)
313 .into_any_element(),
314 )
315}