BlueGit

ict rusty-teams

Files

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

call_layouts.rs

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