BlueGit

ict rusty-teams

Files

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

call_layouts.rs

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