BlueGit

ict rusty-teams

Files

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

call_layouts.rs

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