BlueGit

ict rusty-teams

Files

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

call_layouts.rs

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