use std::time::Instant; use gpui_kit::assets::IconName; use gpui_kit::component::{ button::{Button, ButtonVariants as _}, h_flex, popover::Popover, tooltip::Tooltip, v_flex, }; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use super::avatar::person_avatar; use super::call_organizer::render_focus_stage; use super::call_view::{own_tile, person_tile}; use super::widgets::icon; use crate::app_state::AppState; use crate::call::{ ActiveCall, CallLayout, CallModel, GRID_PADDING, STRIP_GAP, SpeakerSplit, StripSplit, TILE_GAP, Tile, TileSize, focus_stage_size, gallery_grid, gallery_page, measured_stage, shown_bubbles, speaker_split, strip_split, }; use crate::theme; const MEASURE_TOLERANCE: f32 = 0.5; const OVERLAY_INSET: f32 = 16.; const CHIP_TOP: f32 = 12.; const TILE_RADIUS: f32 = 12.; const HIDDEN_LIST_WIDTH: f32 = 260.; const HIDDEN_LIST_HEIGHT: f32 = 320.; const HIDDEN_AVATAR: f32 = 28.; const STACK_MAX: usize = 3; const STACK_OVERLAP: f32 = 0.35; const BUBBLE_WIDTH: f32 = 340.; const BUBBLE_AVATAR: f32 = 28.; const BUBBLE_GAP: f32 = 8.; const BUBBLE_FADE_FROM: f32 = 0.9; const BUBBLE_BACKGROUND: f32 = 0.72; const CHIP_BACKGROUND: f32 = 0.6; pub const FULLSCREEN_HINT: &str = "Press Esc to exit full screen"; pub const GALLERY_PAGER_TAG: &str = "shell/islands/main/call-stage/pager"; const PAGER_HEIGHT: f32 = 32.; const PAGER_BUTTON: f32 = 28.; const PAGER_ICON: f32 = 16.; fn measure_stage(call: &ActiveCall) -> impl IntoElement { let stage_size = call.stage_size.clone(); canvas( |_, _, _| {}, move |bounds, _, window, _| { let measured = (f32::from(bounds.size.width), f32::from(bounds.size.height)); let known = stage_size.get(); if (known.0 - measured.0).abs() > MEASURE_TOLERANCE || (known.1 - measured.1).abs() > MEASURE_TOLERANCE { stage_size.set(measured); window.refresh(); } }, ) .absolute() .size_full() } fn dark_chip() -> Div { h_flex() .h(px(30.)) .px(px(12.)) .gap(px(8.)) .items_center() .rounded_full() .bg(hsla(0., 0., 0., CHIP_BACKGROUND)) .text_color(theme::white()) .text_size(px(13.)) .font_weight(FontWeight::SEMIBOLD) } fn without_video_chip(app: &Entity, model: &CallModel) -> Option { let count = model.hidden_without_video(); if !model.view.hide_no_video || count == 0 { return None; } let app = app.clone(); Some( div() .absolute() .top(px(CHIP_TOP)) .left_0() .right_0() .flex() .justify_center() .child( dark_chip() .id("call-no-video-chip") .cursor_pointer() .hover(|chip| chip.opacity(0.85)) .tooltip(|window, cx| { Tooltip::new("Show people without video").build(window, cx) }) .child(icon(IconName::VideoOff, 15., theme::white())) .child(format!("{count} without video")) .on_click(move |_, _, cx| { app.update(cx, |state, cx| state.toggle_call_hide_no_video(cx)) }), ) .into_any_element(), ) } fn show_me_pill(app: &Entity, model: &CallModel, bottom: f32) -> Option { if !model.view.hide_me { return None; } let app = app.clone(); Some( div() .absolute() .right(px(OVERLAY_INSET)) .bottom(px(bottom)) .child( dark_chip() .id("call-show-me") .cursor_pointer() .hover(|chip| chip.opacity(0.85)) .tooltip(|window, cx| Tooltip::new("Show me").build(window, cx)) .child(icon(IconName::Eye, 15., theme::white())) .child("You") .on_click(move |_, _, cx| { app.update(cx, |state, cx| state.set_call_hide_me(false, cx)) }), ) .into_any_element(), ) } fn chat_bubbles( app: &Entity, state: &AppState, call: &ActiveCall, now: Instant, bottom: f32, ) -> Option { if call.chat_open || !call.model.view.chat_bubbles { return None; } let bubbles = shown_bubbles(&call.bubbles, now); if bubbles.is_empty() { return None; } let shown = crate::call::BUBBLE_SHOWN; Some( v_flex() .absolute() .right(px(OVERLAY_INSET)) .bottom(px(bottom)) .items_end() .gap(px(BUBBLE_GAP)) .children(bubbles.into_iter().map(|bubble| { let app = app.clone(); let id = SharedString::from(format!("call-bubble-{}", bubble.id)); h_flex() .id(id.clone()) .max_w(px(BUBBLE_WIDTH)) .pl(px(6.)) .pr(px(14.)) .py(px(6.)) .gap(px(8.)) .items_center() .rounded_full() .bg(hsla(0., 0., 0., BUBBLE_BACKGROUND)) .text_color(theme::white()) .text_size(px(13.)) .cursor_pointer() .child(person_avatar( &state.directory, bubble.sender_id.as_deref(), &bubble.name, BUBBLE_AVATAR, )) .child( h_flex() .min_w_0() .flex_1() .gap(px(6.)) .child( div() .flex_none() .font_weight(FontWeight::BOLD) .child(bubble.name.clone()), ) .child(div().min_w_0().truncate().child(bubble.text.clone())), ) .on_click(move |_, _, cx| { app.update(cx, |state, cx| { if state.call_chat_thread().is_none() { state.toggle_call_chat(cx); } }) }) .with_animation(id, Animation::new(shown), |bubble, delta| { let fade = ((delta - BUBBLE_FADE_FROM) / (1. - BUBBLE_FADE_FROM)).clamp(0., 1.); bubble.opacity(1. - fade) }) })) .into_any_element(), ) } fn fullscreen_hint(call: &ActiveCall, now: Instant, bottom: f32) -> Option { call.fullscreen .filter(|fullscreen| fullscreen.hint_visible(now)) .map(|_| { div() .absolute() .left_0() .right_0() .bottom(px(bottom)) .flex() .justify_center() .child(dark_chip().h(px(32.)).child(FULLSCREEN_HINT)) .into_any_element() }) } fn stacked_avatars(state: &AppState, hidden: &[&Tile], diameter: f32) -> Div { let shown: Vec<&&Tile> = hidden.iter().take(STACK_MAX).collect(); let step = diameter * (1. - STACK_OVERLAP); div() .relative() .h(px(diameter)) .w(px(diameter + step * (shown.len().saturating_sub(1)) as f32)) .children(shown.into_iter().enumerate().map(|(index, tile)| { div() .absolute() .top_0() .left(px(step * index as f32)) .rounded_full() .bg(theme::row_hover()) .child(person_avatar( &state.directory, tile.user_id.as_deref(), &tile.name, diameter, )) })) } fn overflow_tile( app: &Entity, state: &AppState, size: TileSize, hidden: &[&Tile], ) -> AnyElement { let people: Vec<(String, String, Option)> = hidden .iter() .map(|tile| (tile.mri.clone(), tile.name.clone(), tile.user_id.clone())) .collect(); let diameter = (size.height * 0.3).clamp(22., 44.); let face = v_flex() .w(px(size.width)) .h(px(size.height)) .items_center() .justify_center() .gap(px(size.height * 0.06)) .rounded(px(TILE_RADIUS)) .bg(theme::row_hover()) .child(stacked_avatars(state, hidden, diameter)) .child( div() .text_size(px((size.height * 0.2).clamp(14., 28.))) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::text_muted()) .child(format!("+{}", hidden.len())), ); let app = app.clone(); Popover::new("call-hidden-popover") .anchor(Anchor::BottomLeft) .offset(px(8.)) .trigger( Button::new("call-hidden") .ghost() .p_0() .w(px(size.width)) .h(px(size.height)) .rounded(px(TILE_RADIUS)) .child(face), ) .content(move |_, _, cx| { let popover = cx.entity().downgrade(); let list = v_flex() .id("call-hidden-list") .max_h(px(HIDDEN_LIST_HEIGHT)) .overflow_y_scroll() .gap(px(2.)) .children(people.iter().map(|(mri, name, user_id)| { let app_for_click = app.clone(); let popover = popover.clone(); let pinned_mri = mri.clone(); h_flex() .id(SharedString::from(format!("call-hidden-{mri}"))) .flex_none() .w_full() .px(px(8.)) .py(px(6.)) .gap(px(10.)) .items_center() .rounded(px(8.)) .cursor_pointer() .hover(|row| row.bg(theme::row_hover())) .child(person_avatar( &app.read(cx).directory, user_id.as_deref(), name, HIDDEN_AVATAR, )) .child(div().min_w_0().flex_1().truncate().child(name.clone())) .on_click(move |_, window, cx| { app_for_click .update(cx, |state, cx| state.toggle_call_pin(&pinned_mri, cx)); popover .update(cx, |state, cx| state.dismiss(window, cx)) .ok(); }) })); v_flex().w(px(HIDDEN_LIST_WIDTH)).child(list) }) .into_any_element() } fn own_slot(model: &CallModel, shown: &[&Tile]) -> usize { match model.own_hand { None => shown.len(), Some(rank) => shown .iter() .filter(|tile| tile.hand.is_some_and(|other| other < rank)) .count(), } } fn rows_of(cells: Vec, columns: usize) -> Vec
{ let mut rows = Vec::new(); let mut remaining = cells.into_iter(); loop { let row: Vec = remaining.by_ref().take(columns).collect(); if row.is_empty() { return rows; } rows.push(h_flex().gap(px(TILE_GAP)).justify_center().children(row)); } } fn pager_button( app: &Entity, id: &'static str, glyph: IconName, tooltip: &'static str, forward: bool, enabled: bool, ) -> AnyElement { let app = app.clone(); let color = if enabled { theme::text() } else { theme::text_faint() }; div() .id(id) .size(px(PAGER_BUTTON)) .flex() .items_center() .justify_center() .rounded_full() .child(icon(glyph, PAGER_ICON, color)) .when(enabled, |button| { button .cursor_pointer() .hover(|button| button.bg(theme::row_hover())) .tooltip(move |window, cx| Tooltip::new(tooltip).build(window, cx)) .on_click(move |_, _, cx| { app.update(cx, |state, cx| state.turn_call_gallery_page(forward, cx)); }) }) .into_any_element() } fn gallery_pager(app: &Entity, page: usize, pages: usize) -> AnyElement { h_flex() .debug_selector(|| GALLERY_PAGER_TAG.into()) .h(px(PAGER_HEIGHT)) .px(px(2.)) .gap(px(4.)) .items_center() .rounded_full() .bg(theme::row_hover()) .child(pager_button( app, "call-gallery-previous", IconName::ChevronLeft, "Previous page", false, page > 0, )) .child( div() .min_w(px(40.)) .text_center() .text_size(px(12.)) .text_color(theme::text_soft()) .child(format!("{} / {}", page + 1, pages)), ) .child(pager_button( app, "call-gallery-next", IconName::ChevronRight, "Next page", true, page + 1 < pages, )) .into_any_element() } fn gallery_body( app: &Entity, state: &AppState, model: &CallModel, now: Instant, stage: (f32, f32), ) -> AnyElement { let page = gallery_page(model); let paged = page.pages > 1; let pager_room = if paged { PAGER_HEIGHT + TILE_GAP } else { 0. }; let cells = page.tiles.len() + usize::from(page.own); let grid = gallery_grid( cells, stage.0 - 2. * GRID_PADDING, stage.1 - 2. * GRID_PADDING - pager_room, TILE_GAP, ); let size = TileSize::sixteen_by_nine(grid.tile_width.floor()); let mut tiles: Vec = page .tiles .iter() .map(|tile| person_tile(app, state, model, tile, size, now)) .collect(); if page.own { tiles.insert( own_slot(model, &page.tiles), own_tile(app, state, model, size, now), ); } v_flex() .absolute() .inset_0() .items_center() .justify_center() .gap(px(TILE_GAP)) .children(rows_of(tiles, grid.columns)) .when(paged, |body| { body.child(gallery_pager(app, page.page, page.pages)) }) .into_any_element() } fn speaker_body( app: &Entity, state: &AppState, model: &CallModel, now: Instant, stage: (f32, f32), ) -> AnyElement { let SpeakerSplit { focus, others, own_cells, plan, } = speaker_split(model, stage); let size = TileSize::sixteen_by_nine(plan.tile_width.floor()); let has_filmstrip = plan.overflow.shown + own_cells + usize::from(plan.overflow.hidden > 0) > 0; let focus_size = focus_stage_size( stage, GRID_PADDING, TILE_GAP, has_filmstrip.then_some(size.height), ); let main = focus .as_ref() .and_then(|focus| render_focus_stage(app, state, focus, focus_size, now)); let shown = &others[..plan.overflow.shown]; let mut tiles: Vec = shown .iter() .map(|tile| person_tile(app, state, model, tile, size, now)) .collect(); if own_cells > 0 { tiles.insert( own_slot(model, shown), own_tile(app, state, model, size, now), ); } if plan.overflow.hidden > 0 { tiles.push(overflow_tile( app, state, size, &others[plan.overflow.shown..], )); } v_flex() .absolute() .inset_0() .p(px(GRID_PADDING)) .gap(px(TILE_GAP)) .child( h_flex() .flex_1() .min_h_0() .w_full() .items_center() .justify_center() .children(main), ) .when(!tiles.is_empty(), |column| { column.child( h_flex() .id("call-filmstrip") .flex_none() .w_full() .gap(px(TILE_GAP)) .justify_center() .children(tiles), ) }) .into_any_element() } fn people_strip( app: &Entity, state: &AppState, model: &CallModel, now: Instant, stage: (f32, f32), ) -> Stateful
{ let StripSplit { others, plan } = strip_split(model, stage); let size = TileSize::sixteen_by_nine(plan.tile_width); let shown = &others[..plan.overflow.shown]; let mut cells: Vec = shown .iter() .map(|remote| person_tile(app, state, model, remote, size, now)) .collect(); if model.own_cells() > 0 { let own_index = model.own_cell_index(cells.len()); cells.insert(own_index, own_tile(app, state, model, size, now)); } if plan.overflow.hidden > 0 { cells.push(overflow_tile( app, state, size, &others[plan.overflow.shown..], )); } h_flex() .id("call-strip") .debug_selector(|| "shell/islands/main/call-stage/share-strip".into()) .flex_none() .w_full() .justify_center() .gap(px(STRIP_GAP)) .children(cells) } fn share_body( app: &Entity, state: &AppState, model: &CallModel, now: Instant, share: AnyElement, layout: CallLayout, stage: (f32, f32), ) -> AnyElement { v_flex() .id("call-share-body") .absolute() .inset_0() .p(px(GRID_PADDING)) .gap(px(STRIP_GAP)) .when(layout != CallLayout::Content, |column| { column.child(people_strip(app, state, model, now, stage)) }) .child(h_flex().flex_1().min_h_0().w_full().child(share)) .into_any_element() } pub(super) fn stage_region( app: &Entity, state: &AppState, call: &ActiveCall, now: Instant, hold: Option, share_stage: Option, ) -> AnyElement { let model = &call.model; let layout = model.effective_layout(); let stage = measured_stage(call); let shows_people = hold.is_none() && !(share_stage.is_some() && layout == CallLayout::Content); let body = match (hold, share_stage) { (Some(hold), _) => v_flex().absolute().inset_0().child(hold).into_any_element(), (None, Some(share)) => share_body(app, state, model, now, share, layout, stage), (None, None) if layout == CallLayout::Speaker => { speaker_body(app, state, model, now, stage) } (None, None) => gallery_body(app, state, model, now, stage), }; div() .relative() .debug_selector(|| "shell/islands/main/call-stage".into()) .flex_1() .min_h_0() .w_full() .child(measure_stage(call)) .child(body) .children( without_video_chip(app, model) .filter(|_| shows_people) .into_iter() .chain(show_me_pill(app, model, OVERLAY_INSET).filter(|_| shows_people)) .chain(chat_bubbles(app, state, call, now, OVERLAY_INSET)) .chain(fullscreen_hint(call, now, OVERLAY_INSET)), ) .into_any_element() }