BlueGit

ict rusty-teams

Files

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

call_layouts.rs

9ee83b18 History
call_layouts.rs 19.5 KiB Download Raw
1use std::time::Instant;
2
3use gpui_kit::component::scroll::ScrollableElement as _;
4use gpui_kit::assets::IconName;
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,
22 StripSplit, TILE_GAP, Tile, TileSize, focus_stage_size, gallery_grid, gallery_page,
23 measured_stage, shown_bubbles, 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 { theme::text() } else { theme::text_faint() };
365 div()
366 .id(id)
367 .size(px(PAGER_BUTTON))
368 .flex()
369 .items_center()
370 .justify_center()
371 .rounded_full()
372 .child(icon(glyph, PAGER_ICON, color))
373 .when(enabled, |button| {
374 button
375 .cursor_pointer()
376 .hover(|button| button.bg(theme::row_hover()))
377 .tooltip(move |window, cx| Tooltip::new(tooltip).build(window, cx))
378 .on_click(move |_, _, cx| {
379 app.update(cx, |state, cx| state.turn_call_gallery_page(forward, cx));
380 })
381 })
382 .into_any_element()
383}
384
385fn gallery_pager(app: &Entity<AppState>, page: usize, pages: usize) -> AnyElement {
386 h_flex()
387 .debug_selector(|| GALLERY_PAGER_TAG.into())
388 .h(px(PAGER_HEIGHT))
389 .px(px(2.))
390 .gap(px(4.))
391 .items_center()
392 .rounded_full()
393 .bg(theme::row_hover())
394 .child(pager_button(app, "call-gallery-previous", IconName::ChevronLeft, "Previous page", false, page > 0))
395 .child(
396 div()
397 .min_w(px(40.))
398 .text_center()
399 .text_size(px(12.))
400 .text_color(theme::text_soft())
401 .child(format!("{} / {}", page + 1, pages)),
402 )
403 .child(pager_button(app, "call-gallery-next", IconName::ChevronRight, "Next page", true, page + 1 < pages))
404 .into_any_element()
405}
406
407fn gallery_body(
408 app: &Entity<AppState>,
409 state: &AppState,
410 model: &CallModel,
411 now: Instant,
412 stage: (f32, f32),
413) -> AnyElement {
414 let page = gallery_page(model);
415 let paged = page.pages > 1;
416 let pager_room = if paged { PAGER_HEIGHT + TILE_GAP } else { 0. };
417 let cells = page.tiles.len() + usize::from(page.own);
418 let grid = gallery_grid(
419 cells,
420 stage.0 - 2. * GRID_PADDING,
421 stage.1 - 2. * GRID_PADDING - pager_room,
422 TILE_GAP,
423 );
424 let size = TileSize::sixteen_by_nine(grid.tile_width.floor());
425 let mut tiles: Vec<AnyElement> = page
426 .tiles
427 .iter()
428 .map(|tile| person_tile(app, state, model, tile, size, now))
429 .collect();
430 if page.own {
431 tiles.insert(
432 own_slot(model, &page.tiles),
433 own_tile(app, state, model, size, now),
434 );
435 }
436 v_flex()
437 .absolute()
438 .inset_0()
439 .items_center()
440 .justify_center()
441 .gap(px(TILE_GAP))
442 .children(rows_of(tiles, grid.columns))
443 .when(paged, |body| body.child(gallery_pager(app, page.page, page.pages)))
444 .into_any_element()
445}
446
447fn speaker_body(
448 app: &Entity<AppState>,
449 state: &AppState,
450 model: &CallModel,
451 now: Instant,
452 stage: (f32, f32),
453) -> AnyElement {
454 let SpeakerSplit { focus, others, own_cells, plan } = speaker_split(model, stage);
455 let size = TileSize::sixteen_by_nine(plan.tile_width.floor());
456 let has_filmstrip = plan.overflow.shown + own_cells + usize::from(plan.overflow.hidden > 0) > 0;
457 let focus_size = focus_stage_size(stage, GRID_PADDING, TILE_GAP, has_filmstrip.then_some(size.height));
458 let main = focus
459 .as_ref()
460 .and_then(|focus| render_focus_stage(app, state, focus, focus_size, now));
461 let shown = &others[..plan.overflow.shown];
462 let mut tiles: Vec<AnyElement> = shown
463 .iter()
464 .map(|tile| person_tile(app, state, model, tile, size, now))
465 .collect();
466 if own_cells > 0 {
467 tiles.insert(
468 own_slot(model, shown),
469 own_tile(app, state, model, size, now),
470 );
471 }
472 if plan.overflow.hidden > 0 {
473 tiles.push(overflow_tile(
474 app,
475 state,
476 size,
477 &others[plan.overflow.shown..],
478 ));
479 }
480 v_flex()
481 .absolute()
482 .inset_0()
483 .p(px(GRID_PADDING))
484 .gap(px(TILE_GAP))
485 .child(
486 h_flex()
487 .flex_1()
488 .min_h_0()
489 .w_full()
490 .items_center()
491 .justify_center()
492 .children(main),
493 )
494 .when(!tiles.is_empty(), |column| {
495 column.child(
496 h_flex()
497 .id("call-filmstrip")
498 .flex_none()
499 .w_full()
500 .gap(px(TILE_GAP))
501 .justify_center()
502 .children(tiles),
503 )
504 })
505 .into_any_element()
506}
507
508fn people_strip(
509 app: &Entity<AppState>,
510 state: &AppState,
511 model: &CallModel,
512 now: Instant,
513 stage: (f32, f32),
514) -> Stateful<Div> {
515 let StripSplit { others, plan } = strip_split(model, stage);
516 let size = TileSize::sixteen_by_nine(plan.tile_width);
517 let shown = &others[..plan.overflow.shown];
518 let mut cells: Vec<AnyElement> = shown
519 .iter()
520 .map(|remote| person_tile(app, state, model, remote, size, now))
521 .collect();
522 if model.own_cells() > 0 {
523 let own_index = model.own_cell_index(cells.len());
524 cells.insert(own_index, own_tile(app, state, model, size, now));
525 }
526 if plan.overflow.hidden > 0 {
527 cells.push(overflow_tile(app, state, size, &others[plan.overflow.shown..]));
528 }
529 h_flex()
530 .id("call-strip")
531 .debug_selector(|| "shell/islands/main/call-stage/share-strip".into())
532 .flex_none()
533 .w_full()
534 .justify_center()
535 .gap(px(STRIP_GAP))
536 .children(cells)
537}
538
539fn share_body(
540 app: &Entity<AppState>,
541 state: &AppState,
542 model: &CallModel,
543 now: Instant,
544 share: AnyElement,
545 layout: CallLayout,
546 stage: (f32, f32),
547) -> AnyElement {
548 v_flex()
549 .id("call-share-body")
550 .absolute()
551 .inset_0()
552 .p(px(GRID_PADDING))
553 .gap(px(STRIP_GAP))
554 .when(layout != CallLayout::Content, |column| {
555 column.child(people_strip(app, state, model, now, stage))
556 })
557 .child(h_flex().flex_1().min_h_0().w_full().child(share))
558 .into_any_element()
559}
560
561pub(super) fn stage_region(
562 app: &Entity<AppState>,
563 state: &AppState,
564 call: &ActiveCall,
565 now: Instant,
566 hold: Option<AnyElement>,
567 share_stage: Option<AnyElement>,
568) -> AnyElement {
569 let model = &call.model;
570 let layout = model.effective_layout();
571 let stage = measured_stage(call);
572 let shows_people = hold.is_none() && !(share_stage.is_some() && layout == CallLayout::Content);
573 let body = match (hold, share_stage) {
574 (Some(hold), _) => v_flex().absolute().inset_0().child(hold).into_any_element(),
575 (None, Some(share)) => share_body(app, state, model, now, share, layout, stage),
576 (None, None) if layout == CallLayout::Speaker => {
577 speaker_body(app, state, model, now, stage)
578 }
579 (None, None) => gallery_body(app, state, model, now, stage),
580 };
581 div()
582 .relative()
583 .debug_selector(|| "shell/islands/main/call-stage".into())
584 .flex_1()
585 .min_h_0()
586 .w_full()
587 .child(measure_stage(call))
588 .child(body)
589 .children(
590 without_video_chip(app, model)
591 .filter(|_| shows_people)
592 .into_iter()
593 .chain(show_me_pill(app, model, OVERLAY_INSET).filter(|_| shows_people))
594 .chain(chat_bubbles(app, state, call, now, OVERLAY_INSET))
595 .chain(fullscreen_hint(call, now, OVERLAY_INSET)),
596 )
597 .into_any_element()
598}