BlueGit

ict rusty-teams

Files

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

call_view.rs

call_view.rs 13.2 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::{CallKind, CallModel, GridLayout, TileState, grid_layout, tile_size};
19use crate::call::TileSize;
20use crate::theme;
21
22const HEADER_HEIGHT: f32 = 60.;
23const RING_WIDTH: f32 = 3.;
24const RING_GAP: f32 = 4.;
25const CONTROL_SIZE: f32 = 44.;
26const CONTROL_ICON: f32 = 20.;
27const MENU_WIDTH: f32 = 280.;
28const PULSE_PERIOD: Duration = Duration::from_millis(1400);
29const DISABLED_OPACITY: f32 = 0.4;
30const TILE_GAP: f32 = 20.;
31const GRID_PADDING: f32 = 24.;
32pub const NO_MICROPHONE_HINT: &str = "No microphone found";
33pub const LOBBY_TEXT: &str = "Waiting in the lobby...";
34
35pub fn status_line(model: &CallModel, now: Instant) -> (String, Hsla) {
36 if model.lobby {
37 (LOBBY_TEXT.to_owned(), theme::amber())
38 } else if model.is_connecting() {
39 (model.connecting_text().to_owned(), theme::text_muted())
40 } else if model.is_reconnecting() {
41 ("Reconnecting...".to_owned(), theme::amber())
42 } else {
43 (model.timer_text(now), theme::text_muted())
44 }
45}
46
47pub fn pulsing(element: impl IntoElement + 'static, id: &'static str) -> AnyElement {
48 div()
49 .child(element)
50 .with_animation(
51 id,
52 Animation::new(PULSE_PERIOD).repeat(),
53 |element, delta| {
54 let wave = 1. - (delta * 2. - 1.).abs();
55 element.opacity(0.45 + 0.55 * wave)
56 },
57 )
58 .into_any_element()
59}
60
61fn speaking_ring(avatar: AnyElement, speaking: bool) -> Div {
62 div()
63 .p(px(RING_GAP))
64 .rounded_full()
65 .border(px(RING_WIDTH))
66 .border_color(if speaking {
67 theme::green()
68 } else {
69 transparent_black()
70 })
71 .child(avatar)
72}
73
74fn tile(
75 size: TileSize,
76 avatar: AnyElement,
77 name: &str,
78 speaking: bool,
79 muted: bool,
80 caption: Option<AnyElement>,
81) -> Div {
82 let badge = div()
83 .absolute()
84 .top(px(10.))
85 .right(px(10.))
86 .size(px(24.))
87 .flex()
88 .items_center()
89 .justify_center()
90 .rounded_full()
91 .bg(theme::red())
92 .child(icon(IconName::MicOff, 13., theme::white()));
93 v_flex()
94 .relative()
95 .w(px(size.width))
96 .h(px(size.height))
97 .items_center()
98 .justify_center()
99 .gap(px(12.))
100 .rounded(px(12.))
101 .bg(theme::surface())
102 .border_1()
103 .border_color(theme::border())
104 .child(speaking_ring(avatar, speaking))
105 .child(
106 v_flex()
107 .items_center()
108 .gap(px(2.))
109 .max_w(px(size.width - 16.))
110 .child(
111 div()
112 .max_w_full()
113 .truncate()
114 .text_size(px(14.))
115 .font_weight(FontWeight::SEMIBOLD)
116 .child(name.to_owned()),
117 )
118 .children(caption),
119 )
120 .when(muted, |tile| tile.child(badge))
121}
122
123fn overflow_tile(size: TileSize, hidden: usize) -> Div {
124 v_flex()
125 .w(px(size.width))
126 .h(px(size.height))
127 .items_center()
128 .justify_center()
129 .rounded(px(12.))
130 .bg(theme::surface())
131 .border_1()
132 .border_color(theme::border())
133 .child(
134 div()
135 .text_size(px(size.avatar * 0.5))
136 .font_weight(FontWeight::SEMIBOLD)
137 .text_color(theme::text_muted())
138 .child(format!("+{hidden}")),
139 )
140}
141
142fn round_control(id: &'static str, glyph: IconName, background: Hsla, foreground: Hsla) -> Stateful<Div> {
143 div()
144 .id(id)
145 .size(px(CONTROL_SIZE))
146 .flex()
147 .flex_none()
148 .items_center()
149 .justify_center()
150 .rounded_full()
151 .bg(background)
152 .cursor_pointer()
153 .hover(|button| button.opacity(0.85))
154 .child(icon(glyph, CONTROL_ICON, foreground))
155}
156
157pub fn mute_button(app: &Entity<AppState>, model: &CallModel, id: &'static str, size: f32) -> Stateful<Div> {
158 let (glyph, background, foreground) = if model.muted {
159 (IconName::MicOff, theme::red(), theme::white())
160 } else {
161 (IconName::Mic, theme::surface_raised(), theme::text())
162 };
163 let button = round_control(id, glyph, background, foreground).size(px(size));
164 if !model.can_unmute() {
165 return button
166 .opacity(DISABLED_OPACITY)
167 .cursor_default()
168 .tooltip(|window, cx| Tooltip::new(NO_MICROPHONE_HINT).build(window, cx));
169 }
170 let app = app.clone();
171 button.on_click(move |_, _, cx| {
172 cx.stop_propagation();
173 app.update(cx, |state, cx| state.toggle_call_mute(cx));
174 })
175}
176
177pub fn leave_button(app: &Entity<AppState>, model: &CallModel, id: &'static str, compact: bool) -> Stateful<Div> {
178 let label = if model.is_connecting() { "Cancel" } else { "Leave" };
179 let app = app.clone();
180 h_flex()
181 .id(id)
182 .h(px(if compact { 28. } else { CONTROL_SIZE }))
183 .px(px(if compact { 10. } else { 20. }))
184 .gap(px(8.))
185 .flex_none()
186 .items_center()
187 .justify_center()
188 .rounded_full()
189 .bg(theme::red())
190 .text_color(theme::white())
191 .text_size(px(if compact { 12. } else { 14. }))
192 .font_weight(FontWeight::SEMIBOLD)
193 .cursor_pointer()
194 .hover(|button| button.opacity(0.85))
195 .child(icon(IconName::PhoneOff, if compact { 14. } else { 18. }, theme::white()))
196 .child(label)
197 .on_click(move |_, _, cx| {
198 cx.stop_propagation();
199 app.update(cx, |state, cx| state.leave_call(cx));
200 })
201}
202
203fn chat_button(app: &Entity<AppState>) -> Stateful<Div> {
204 let app = app.clone();
205 round_control("call-chat", IconName::MessageCircle, theme::surface_raised(), theme::text())
206 .tooltip(|window, cx| Tooltip::new("Open the chat").build(window, cx))
207 .on_click(move |_, _, cx| app.update(cx, |state, cx| state.open_call_chat(cx)))
208}
209
210fn leave_menu(app: &Entity<AppState>, model: &CallModel) -> impl IntoElement {
211 let leave_app = app.clone();
212 let end_app = app.clone();
213 let can_end = model.can_end_meeting;
214 Button::new("call-leave-menu")
215 .ghost()
216 .p_0()
217 .size(px(CONTROL_SIZE))
218 .rounded_full()
219 .child(round_control(
220 "call-leave-menu-face",
221 IconName::ChevronUp,
222 theme::surface_raised(),
223 theme::text(),
224 ))
225 .dropdown_menu_with_anchor(Anchor::BottomRight, move |popup, _, _| {
226 let leave_app = leave_app.clone();
227 let end_app = end_app.clone();
228 let popup = popup.min_w(px(MENU_WIDTH)).item(
229 PopupMenuItem::new("Leave")
230 .icon(IconName::PhoneOff)
231 .on_click(move |_, _, cx| leave_app.update(cx, |state, cx| state.leave_call(cx))),
232 );
233 if can_end {
234 popup.item(
235 PopupMenuItem::new("End meeting for everyone")
236 .icon(IconName::PhoneOff)
237 .on_click(move |_, _, cx| end_app.update(cx, |state, cx| state.end_meeting_for_all(cx))),
238 )
239 } else {
240 popup
241 }
242 })
243}
244
245fn device_items(
246 entries: Vec<DeviceEntry>,
247 selected: &calling::DeviceChoice,
248 pick: impl Fn(calling::DeviceChoice, &mut App) + Clone + 'static,
249) -> Vec<PopupMenuItem> {
250 entries
251 .into_iter()
252 .map(|entry| {
253 let pick = pick.clone();
254 let choice = entry.choice.clone();
255 PopupMenuItem::new(entry.label)
256 .checked(&entry.choice == selected)
257 .on_click(move |_, _, cx| pick(choice.clone(), cx))
258 })
259 .collect()
260}
261
262fn devices_menu(popup: PopupMenu, app: &Entity<AppState>, model: &CallModel) -> PopupMenu {
263 let input_app = app.clone();
264 let output_app = app.clone();
265 let inputs = device_items(model.devices.input_entries(), &model.input, move |choice, cx| {
266 input_app.update(cx, |state, cx| state.select_call_input(choice, cx));
267 });
268 let outputs = device_items(model.devices.output_entries(), &model.output, move |choice, cx| {
269 output_app.update(cx, |state, cx| state.select_call_output(choice, cx));
270 });
271 let mut popup = popup
272 .min_w(px(MENU_WIDTH))
273 .item(PopupMenuItem::label("Microphone"));
274 if model.listen_only {
275 popup = popup.item(PopupMenuItem::new(NO_MICROPHONE_HINT).disabled(true));
276 } else {
277 for item in inputs {
278 popup = popup.item(item);
279 }
280 }
281 popup = popup.separator().item(PopupMenuItem::label("Speaker"));
282 for item in outputs {
283 popup = popup.item(item);
284 }
285 popup
286}
287
288fn devices_button(app: &Entity<AppState>, model: &CallModel) -> impl IntoElement {
289 let app = app.clone();
290 let model = model.clone();
291 Button::new("call-devices")
292 .ghost()
293 .p_0()
294 .size(px(CONTROL_SIZE))
295 .rounded_full()
296 .child(round_control(
297 "call-devices-face",
298 IconName::Volume2,
299 theme::surface_raised(),
300 theme::text(),
301 ))
302 .dropdown_menu_with_anchor(Anchor::BottomLeft, move |popup, _, _| {
303 devices_menu(popup, &app, &model)
304 })
305}
306
307fn remote_tiles(state: &AppState, model: &CallModel, size: TileSize, hidden_cap: GridLayout) -> Vec<AnyElement> {
308 let visible = model.visible_tiles();
309 let mut cells: Vec<AnyElement> = visible
310 .iter()
311 .take(hidden_cap.shown_others)
312 .map(|remote| {
313 let waiting = remote.state == TileState::Invited;
314 let avatar = person_avatar(&state.directory, remote.user_id.as_deref(), &remote.name, size.avatar);
315 let avatar = if waiting { pulsing(avatar, "call-invited-pulse") } else { avatar };
316 let caption = model.tile_caption(remote).map(|text| {
317 let label = div().text_size(px(12.)).text_color(theme::text_muted()).child(text);
318 if waiting { pulsing(label, "call-invited-caption") } else { label.into_any_element() }
319 });
320 tile(size, avatar, &remote.name, model.tile_speaking(remote), remote.muted, caption).into_any_element()
321 })
322 .collect();
323 if hidden_cap.hidden > 0 {
324 cells.push(overflow_tile(size, hidden_cap.hidden).into_any_element());
325 }
326 cells
327}
328
329pub fn render_call_view(app: &Entity<AppState>, state: &AppState) -> Option<AnyElement> {
330 let call = state.call.as_ref()?;
331 let model = &call.model;
332 let now = Instant::now();
333 let (status, status_color) = status_line(model, now);
334 let me = state.directory.me.as_ref();
335 let header = h_flex()
336 .w_full()
337 .h(px(HEADER_HEIGHT))
338 .flex_none()
339 .px(px(20.))
340 .gap(px(12.))
341 .items_center()
342 .border_b_1()
343 .border_color(theme::border())
344 .child(icon(IconName::Phone, 18., theme::accent_text()))
345 .child(
346 div()
347 .min_w_0()
348 .truncate()
349 .text_size(px(16.))
350 .font_weight(FontWeight::SEMIBOLD)
351 .child(model.title.clone()),
352 )
353 .child(div().flex_none().text_size(px(13.)).text_color(status_color).child(status));
354
355 let layout = grid_layout(model.visible_tiles().len());
356 let size = tile_size(layout.shown_others + usize::from(layout.hidden > 0) + 1);
357 let own_avatar = person_avatar(
358 &state.directory,
359 me.map(|person| person.user_id.as_str()),
360 me.map_or("You", |person| person.display_name.as_str()),
361 size.avatar,
362 );
363 let own_tile = tile(size, own_avatar, "You", model.local_speaking, model.muted, None);
364 let mut cells = remote_tiles(state, model, size, layout);
365 cells.push(own_tile.into_any_element());
366
367 let controls = h_flex()
368 .w_full()
369 .flex_none()
370 .py(px(18.))
371 .gap(px(12.))
372 .items_center()
373 .justify_center()
374 .border_t_1()
375 .border_color(theme::border())
376 .child(mute_button(app, model, "call-mute", CONTROL_SIZE))
377 .child(devices_button(app, model))
378 .when(call.conversation_id.is_some(), |row| row.child(chat_button(app)))
379 .child(leave_button(app, model, "call-leave", false))
380 .when(model.kind == CallKind::Meeting, |row| row.child(leave_menu(app, model)));
381
382 Some(
383 v_flex()
384 .flex_1()
385 .min_w_0()
386 .h_full()
387 .bg(theme::background())
388 .child(header)
389 .child(
390 h_flex()
391 .flex_1()
392 .min_h_0()
393 .flex_wrap()
394 .content_center()
395 .p(px(GRID_PADDING))
396 .gap(px(TILE_GAP))
397 .items_center()
398 .justify_center()
399 .children(cells),
400 )
401 .child(controls)
402 .into_any_element(),
403 )
404}