BlueGit

ict rusty-teams

Files

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

call_layouts.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
call_layouts.rs 19.8 KiB Download Raw
1use std::time::Instant;
2
3use gpui_kit::assets::IconName;
4use gpui_kit::component::scroll::ScrollableElement as _;
5use gpui_kit::component::{
6 button::{Button, ButtonVariants as _},
7 h_flex,
8 popover::Popover,
9 tooltip::Tooltip,
10 v_flex,
11};
12use gpui_kit::prelude::FluentBuilder as _;
13use gpui_kit::*;
14
15use super::avatar::person_avatar;
16use super::call_organizer::render_focus_stage;
17use super::call_view::{own_tile, person_tile};
18use super::widgets::icon;
19use crate::app_state::AppState;
20use crate::call::{
21 ActiveCall, CallLayout, CallModel, GRID_PADDING, STRIP_GAP, SpeakerSplit, StripSplit, TILE_GAP,
22 Tile, TileSize, focus_stage_size, gallery_grid, gallery_page, measured_stage, shown_bubbles,
23 speaker_split, strip_split,
24};
25use crate::theme;
26
27const MEASURE_TOLERANCE: f32 = 0.5;
28const OVERLAY_INSET: f32 = 16.;
29const CHIP_TOP: f32 = 12.;
30const TILE_RADIUS: f32 = 12.;
31const HIDDEN_LIST_WIDTH: f32 = 260.;
32const HIDDEN_LIST_HEIGHT: f32 = 320.;
33const HIDDEN_AVATAR: f32 = 28.;
34const STACK_MAX: usize = 3;
35const STACK_OVERLAP: f32 = 0.35;
36const BUBBLE_WIDTH: f32 = 340.;
37const BUBBLE_AVATAR: f32 = 28.;
38const BUBBLE_GAP: f32 = 8.;
39const BUBBLE_FADE_FROM: f32 = 0.9;
40const BUBBLE_BACKGROUND: f32 = 0.72;
41const CHIP_BACKGROUND: f32 = 0.6;
42pub const FULLSCREEN_HINT: &str = "Press Esc to exit full screen";
43pub const GALLERY_PAGER_TAG: &str = "shell/islands/main/call-stage/pager";
44const PAGER_HEIGHT: f32 = 32.;
45const PAGER_BUTTON: f32 = 28.;
46const PAGER_ICON: f32 = 16.;
47
48fn measure_stage(call: &ActiveCall) -> impl IntoElement {
49 let stage_size = call.stage_size.clone();
50 canvas(
51 |_, _, _| {},
52 move |bounds, _, window, _| {
53 let measured = (f32::from(bounds.size.width), f32::from(bounds.size.height));
54 let known = stage_size.get();
55 if (known.0 - measured.0).abs() > MEASURE_TOLERANCE
56 || (known.1 - measured.1).abs() > MEASURE_TOLERANCE
57 {
58 stage_size.set(measured);
59 window.refresh();
60 }
61 },
62 )
63 .absolute()
64 .size_full()
65}
66
67fn dark_chip() -> Div {
68 h_flex()
69 .h(px(30.))
70 .px(px(12.))
71 .gap(px(8.))
72 .items_center()
73 .rounded_full()
74 .bg(hsla(0., 0., 0., CHIP_BACKGROUND))
75 .text_color(theme::white())
76 .text_size(px(13.))
77 .font_weight(FontWeight::SEMIBOLD)
78}
79
80fn without_video_chip(app: &Entity<AppState>, model: &CallModel) -> Option<AnyElement> {
81 let count = model.hidden_without_video();
82 if !model.view.hide_no_video || count == 0 {
83 return None;
84 }
85 let app = app.clone();
86 Some(
87 div()
88 .absolute()
89 .top(px(CHIP_TOP))
90 .left_0()
91 .right_0()
92 .flex()
93 .justify_center()
94 .child(
95 dark_chip()
96 .id("call-no-video-chip")
97 .cursor_pointer()
98 .hover(|chip| chip.opacity(0.85))
99 .tooltip(|window, cx| {
100 Tooltip::new("Show people without video").build(window, cx)
101 })
102 .child(icon(IconName::VideoOff, 15., theme::white()))
103 .child(format!("{count} without video"))
104 .on_click(move |_, _, cx| {
105 app.update(cx, |state, cx| state.toggle_call_hide_no_video(cx))
106 }),
107 )
108 .into_any_element(),
109 )
110}
111
112fn show_me_pill(app: &Entity<AppState>, model: &CallModel, bottom: f32) -> Option<AnyElement> {
113 if !model.view.hide_me {
114 return None;
115 }
116 let app = app.clone();
117 Some(
118 div()
119 .absolute()
120 .right(px(OVERLAY_INSET))
121 .bottom(px(bottom))
122 .child(
123 dark_chip()
124 .id("call-show-me")
125 .cursor_pointer()
126 .hover(|chip| chip.opacity(0.85))
127 .tooltip(|window, cx| Tooltip::new("Show me").build(window, cx))
128 .child(icon(IconName::Eye, 15., theme::white()))
129 .child("You")
130 .on_click(move |_, _, cx| {
131 app.update(cx, |state, cx| state.set_call_hide_me(false, cx))
132 }),
133 )
134 .into_any_element(),
135 )
136}
137
138fn chat_bubbles(
139 app: &Entity<AppState>,
140 state: &AppState,
141 call: &ActiveCall,
142 now: Instant,
143 bottom: f32,
144) -> Option<AnyElement> {
145 if call.chat_open || !call.model.view.chat_bubbles {
146 return None;
147 }
148 let bubbles = shown_bubbles(&call.bubbles, now);
149 if bubbles.is_empty() {
150 return None;
151 }
152 let shown = crate::call::BUBBLE_SHOWN;
153 Some(
154 v_flex()
155 .absolute()
156 .right(px(OVERLAY_INSET))
157 .bottom(px(bottom))
158 .items_end()
159 .gap(px(BUBBLE_GAP))
160 .children(bubbles.into_iter().map(|bubble| {
161 let app = app.clone();
162 let id = SharedString::from(format!("call-bubble-{}", bubble.id));
163 h_flex()
164 .id(id.clone())
165 .max_w(px(BUBBLE_WIDTH))
166 .pl(px(6.))
167 .pr(px(14.))
168 .py(px(6.))
169 .gap(px(8.))
170 .items_center()
171 .rounded_full()
172 .bg(hsla(0., 0., 0., BUBBLE_BACKGROUND))
173 .text_color(theme::white())
174 .text_size(px(13.))
175 .cursor_pointer()
176 .child(person_avatar(
177 &state.directory,
178 bubble.sender_id.as_deref(),
179 &bubble.name,
180 BUBBLE_AVATAR,
181 ))
182 .child(
183 h_flex()
184 .min_w_0()
185 .flex_1()
186 .gap(px(6.))
187 .child(
188 div()
189 .flex_none()
190 .font_weight(FontWeight::BOLD)
191 .child(bubble.name.clone()),
192 )
193 .child(div().min_w_0().truncate().child(bubble.text.clone())),
194 )
195 .on_click(move |_, _, cx| {
196 app.update(cx, |state, cx| {
197 if state.call_chat_thread().is_none() {
198 state.toggle_call_chat(cx);
199 }
200 })
201 })
202 .with_animation(id, Animation::new(shown), |bubble, delta| {
203 let fade =
204 ((delta - BUBBLE_FADE_FROM) / (1. - BUBBLE_FADE_FROM)).clamp(0., 1.);
205 bubble.opacity(1. - fade)
206 })
207 }))
208 .into_any_element(),
209 )
210}
211
212fn fullscreen_hint(call: &ActiveCall, now: Instant, bottom: f32) -> Option<AnyElement> {
213 call.fullscreen
214 .filter(|fullscreen| fullscreen.hint_visible(now))
215 .map(|_| {
216 div()
217 .absolute()
218 .left_0()
219 .right_0()
220 .bottom(px(bottom))
221 .flex()
222 .justify_center()
223 .child(dark_chip().h(px(32.)).child(FULLSCREEN_HINT))
224 .into_any_element()
225 })
226}
227
228fn stacked_avatars(state: &AppState, hidden: &[&Tile], diameter: f32) -> Div {
229 let shown: Vec<&&Tile> = hidden.iter().take(STACK_MAX).collect();
230 let step = diameter * (1. - STACK_OVERLAP);
231 div()
232 .relative()
233 .h(px(diameter))
234 .w(px(diameter + step * (shown.len().saturating_sub(1)) as f32))
235 .children(shown.into_iter().enumerate().map(|(index, tile)| {
236 div()
237 .absolute()
238 .top_0()
239 .left(px(step * index as f32))
240 .rounded_full()
241 .bg(theme::row_hover())
242 .child(person_avatar(
243 &state.directory,
244 tile.user_id.as_deref(),
245 &tile.name,
246 diameter,
247 ))
248 }))
249}
250
251fn overflow_tile(
252 app: &Entity<AppState>,
253 state: &AppState,
254 size: TileSize,
255 hidden: &[&Tile],
256) -> AnyElement {
257 let people: Vec<(String, String, Option<String>)> = hidden
258 .iter()
259 .map(|tile| (tile.mri.clone(), tile.name.clone(), tile.user_id.clone()))
260 .collect();
261 let diameter = (size.height * 0.3).clamp(22., 44.);
262 let face = v_flex()
263 .w(px(size.width))
264 .h(px(size.height))
265 .items_center()
266 .justify_center()
267 .gap(px(size.height * 0.06))
268 .rounded(px(TILE_RADIUS))
269 .bg(theme::row_hover())
270 .child(stacked_avatars(state, hidden, diameter))
271 .child(
272 div()
273 .text_size(px((size.height * 0.2).clamp(14., 28.)))
274 .font_weight(FontWeight::SEMIBOLD)
275 .text_color(theme::text_muted())
276 .child(format!("+{}", hidden.len())),
277 );
278 let app = app.clone();
279 Popover::new("call-hidden-popover")
280 .anchor(Anchor::BottomLeft)
281 .offset(px(8.))
282 .trigger(
283 Button::new("call-hidden")
284 .ghost()
285 .p_0()
286 .w(px(size.width))
287 .h(px(size.height))
288 .rounded(px(TILE_RADIUS))
289 .child(face),
290 )
291 .content(move |_, _, cx| {
292 let popover = cx.entity().downgrade();
293 let list = v_flex()
294 .id("call-hidden-list")
295 .max_h(px(HIDDEN_LIST_HEIGHT))
296 .overflow_y_scrollbar()
297 .gap(px(2.))
298 .children(people.iter().map(|(mri, name, user_id)| {
299 let app_for_click = app.clone();
300 let popover = popover.clone();
301 let pinned_mri = mri.clone();
302 h_flex()
303 .id(SharedString::from(format!("call-hidden-{mri}")))
304 .flex_none()
305 .w_full()
306 .px(px(8.))
307 .py(px(6.))
308 .gap(px(10.))
309 .items_center()
310 .rounded(px(8.))
311 .cursor_pointer()
312 .hover(|row| row.bg(theme::row_hover()))
313 .child(person_avatar(
314 &app.read(cx).directory,
315 user_id.as_deref(),
316 name,
317 HIDDEN_AVATAR,
318 ))
319 .child(div().min_w_0().flex_1().truncate().child(name.clone()))
320 .on_click(move |_, window, cx| {
321 app_for_click
322 .update(cx, |state, cx| state.toggle_call_pin(&pinned_mri, cx));
323 popover
324 .update(cx, |state, cx| state.dismiss(window, cx))
325 .ok();
326 })
327 }));
328 v_flex().w(px(HIDDEN_LIST_WIDTH)).child(list)
329 })
330 .into_any_element()
331}
332
333fn own_slot(model: &CallModel, shown: &[&Tile]) -> usize {
334 match model.own_hand {
335 None => shown.len(),
336 Some(rank) => shown
337 .iter()
338 .filter(|tile| tile.hand.is_some_and(|other| other < rank))
339 .count(),
340 }
341}
342
343fn rows_of(cells: Vec<AnyElement>, columns: usize) -> Vec<Div> {
344 let mut rows = Vec::new();
345 let mut remaining = cells.into_iter();
346 loop {
347 let row: Vec<AnyElement> = remaining.by_ref().take(columns).collect();
348 if row.is_empty() {
349 return rows;
350 }
351 rows.push(h_flex().gap(px(TILE_GAP)).justify_center().children(row));
352 }
353}
354
355fn pager_button(
356 app: &Entity<AppState>,
357 id: &'static str,
358 glyph: IconName,
359 tooltip: &'static str,
360 forward: bool,
361 enabled: bool,
362) -> AnyElement {
363 let app = app.clone();
364 let color = if enabled {
365 theme::text()
366 } else {
367 theme::text_faint()
368 };
369 div()
370 .id(id)
371 .size(px(PAGER_BUTTON))
372 .flex()
373 .items_center()
374 .justify_center()
375 .rounded_full()
376 .child(icon(glyph, PAGER_ICON, color))
377 .when(enabled, |button| {
378 button
379 .cursor_pointer()
380 .hover(|button| button.bg(theme::row_hover()))
381 .tooltip(move |window, cx| Tooltip::new(tooltip).build(window, cx))
382 .on_click(move |_, _, cx| {
383 app.update(cx, |state, cx| state.turn_call_gallery_page(forward, cx));
384 })
385 })
386 .into_any_element()
387}
388
389fn gallery_pager(app: &Entity<AppState>, page: usize, pages: usize) -> AnyElement {
390 h_flex()
391 .debug_selector(|| GALLERY_PAGER_TAG.into())
392 .h(px(PAGER_HEIGHT))
393 .px(px(2.))
394 .gap(px(4.))
395 .items_center()
396 .rounded_full()
397 .bg(theme::row_hover())
398 .child(pager_button(
399 app,
400 "call-gallery-previous",
401 IconName::ChevronLeft,
402 "Previous page",
403 false,
404 page > 0,
405 ))
406 .child(
407 div()
408 .min_w(px(40.))
409 .text_center()
410 .text_size(px(12.))
411 .text_color(theme::text_soft())
412 .child(format!("{} / {}", page + 1, pages)),
413 )
414 .child(pager_button(
415 app,
416 "call-gallery-next",
417 IconName::ChevronRight,
418 "Next page",
419 true,
420 page + 1 < pages,
421 ))
422 .into_any_element()
423}
424
425fn gallery_body(
426 app: &Entity<AppState>,
427 state: &AppState,
428 model: &CallModel,
429 now: Instant,
430 stage: (f32, f32),
431) -> AnyElement {
432 let page = gallery_page(model);
433 let paged = page.pages > 1;
434 let pager_room = if paged { PAGER_HEIGHT + TILE_GAP } else { 0. };
435 let cells = page.tiles.len() + usize::from(page.own);
436 let grid = gallery_grid(
437 cells,
438 stage.0 - 2. * GRID_PADDING,
439 stage.1 - 2. * GRID_PADDING - pager_room,
440 TILE_GAP,
441 );
442 let size = TileSize::sixteen_by_nine(grid.tile_width.floor());
443 let mut tiles: Vec<AnyElement> = page
444 .tiles
445 .iter()
446 .map(|tile| person_tile(app, state, model, tile, size, now))
447 .collect();
448 if page.own {
449 tiles.insert(
450 own_slot(model, &page.tiles),
451 own_tile(app, state, model, size, now),
452 );
453 }
454 v_flex()
455 .absolute()
456 .inset_0()
457 .items_center()
458 .justify_center()
459 .gap(px(TILE_GAP))
460 .children(rows_of(tiles, grid.columns))
461 .when(paged, |body| {
462 body.child(gallery_pager(app, page.page, page.pages))
463 })
464 .into_any_element()
465}
466
467fn speaker_body(
468 app: &Entity<AppState>,
469 state: &AppState,
470 model: &CallModel,
471 now: Instant,
472 stage: (f32, f32),
473) -> AnyElement {
474 let SpeakerSplit {
475 focus,
476 others,
477 own_cells,
478 plan,
479 } = speaker_split(model, stage);
480 let size = TileSize::sixteen_by_nine(plan.tile_width.floor());
481 let has_filmstrip = plan.overflow.shown + own_cells + usize::from(plan.overflow.hidden > 0) > 0;
482 let focus_size = focus_stage_size(
483 stage,
484 GRID_PADDING,
485 TILE_GAP,
486 has_filmstrip.then_some(size.height),
487 );
488 let main = focus
489 .as_ref()
490 .and_then(|focus| render_focus_stage(app, state, focus, focus_size, now));
491 let shown = &others[..plan.overflow.shown];
492 let mut tiles: Vec<AnyElement> = shown
493 .iter()
494 .map(|tile| person_tile(app, state, model, tile, size, now))
495 .collect();
496 if own_cells > 0 {
497 tiles.insert(
498 own_slot(model, shown),
499 own_tile(app, state, model, size, now),
500 );
501 }
502 if plan.overflow.hidden > 0 {
503 tiles.push(overflow_tile(
504 app,
505 state,
506 size,
507 &others[plan.overflow.shown..],
508 ));
509 }
510 v_flex()
511 .absolute()
512 .inset_0()
513 .p(px(GRID_PADDING))
514 .gap(px(TILE_GAP))
515 .child(
516 h_flex()
517 .flex_1()
518 .min_h_0()
519 .w_full()
520 .items_center()
521 .justify_center()
522 .children(main),
523 )
524 .when(!tiles.is_empty(), |column| {
525 column.child(
526 h_flex()
527 .id("call-filmstrip")
528 .flex_none()
529 .w_full()
530 .gap(px(TILE_GAP))
531 .justify_center()
532 .children(tiles),
533 )
534 })
535 .into_any_element()
536}
537
538fn people_strip(
539 app: &Entity<AppState>,
540 state: &AppState,
541 model: &CallModel,
542 now: Instant,
543 stage: (f32, f32),
544) -> Stateful<Div> {
545 let StripSplit { others, plan } = strip_split(model, stage);
546 let size = TileSize::sixteen_by_nine(plan.tile_width);
547 let shown = &others[..plan.overflow.shown];
548 let mut cells: Vec<AnyElement> = shown
549 .iter()
550 .map(|remote| person_tile(app, state, model, remote, size, now))
551 .collect();
552 if model.own_cells() > 0 {
553 let own_index = model.own_cell_index(cells.len());
554 cells.insert(own_index, own_tile(app, state, model, size, now));
555 }
556 if plan.overflow.hidden > 0 {
557 cells.push(overflow_tile(
558 app,
559 state,
560 size,
561 &others[plan.overflow.shown..],
562 ));
563 }
564 h_flex()
565 .id("call-strip")
566 .debug_selector(|| "shell/islands/main/call-stage/share-strip".into())
567 .flex_none()
568 .w_full()
569 .justify_center()
570 .gap(px(STRIP_GAP))
571 .children(cells)
572}
573
574fn share_body(
575 app: &Entity<AppState>,
576 state: &AppState,
577 model: &CallModel,
578 now: Instant,
579 share: AnyElement,
580 layout: CallLayout,
581 stage: (f32, f32),
582) -> AnyElement {
583 v_flex()
584 .id("call-share-body")
585 .absolute()
586 .inset_0()
587 .p(px(GRID_PADDING))
588 .gap(px(STRIP_GAP))
589 .when(layout != CallLayout::Content, |column| {
590 column.child(people_strip(app, state, model, now, stage))
591 })
592 .child(h_flex().flex_1().min_h_0().w_full().child(share))
593 .into_any_element()
594}
595
596pub(super) fn stage_region(
597 app: &Entity<AppState>,
598 state: &AppState,
599 call: &ActiveCall,
600 now: Instant,
601 hold: Option<AnyElement>,
602 share_stage: Option<AnyElement>,
603) -> AnyElement {
604 let model = &call.model;
605 let layout = model.effective_layout();
606 let stage = measured_stage(call);
607 let shows_people = hold.is_none() && !(share_stage.is_some() && layout == CallLayout::Content);
608 let body = match (hold, share_stage) {
609 (Some(hold), _) => v_flex().absolute().inset_0().child(hold).into_any_element(),
610 (None, Some(share)) => share_body(app, state, model, now, share, layout, stage),
611 (None, None) if layout == CallLayout::Speaker => {
612 speaker_body(app, state, model, now, stage)
613 }
614 (None, None) => gallery_body(app, state, model, now, stage),
615 };
616 div()
617 .relative()
618 .debug_selector(|| "shell/islands/main/call-stage".into())
619 .flex_1()
620 .min_h_0()
621 .w_full()
622 .child(measure_stage(call))
623 .child(body)
624 .children(
625 without_video_chip(app, model)
626 .filter(|_| shows_people)
627 .into_iter()
628 .chain(show_me_pill(app, model, OVERLAY_INSET).filter(|_| shows_people))
629 .chain(chat_bubbles(app, state, call, now, OVERLAY_INSET))
630 .chain(fullscreen_hint(call, now, OVERLAY_INSET)),
631 )
632 .into_any_element()
633}