BlueGit

ict rusty-teams

Files

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

call_layouts.rs

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