use std::time::Instant; use calling::VideoKey; use gpui_kit::assets::IconName; use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use super::avatar::person_avatar; use super::call_share_gallery::{SHOW_MORE_PEOPLE, view_switch_button}; use super::call_view::{camera_button, leave_button, mute_button, round_control, status_line}; use super::widgets::icon; use crate::app_state::AppState; use crate::call::{CallModel, CallWindows, Tile, TileState}; use crate::theme; pub(super) const CONTROLS_HEIGHT: f32 = 44.; pub(super) const CONTROL_SIZE: f32 = 30.; pub(super) const WINDOW_RADIUS: f32 = 10.; const SELF_VIEW_WIDTH: f32 = 80.; const SELF_VIEW_HEIGHT: f32 = 45.; const SPEAKER_AVATAR: f32 = 56.; const CHIP_BACKGROUND: f32 = 0.6; const SHADE_BACKGROUND: f32 = 0.55; pub struct MiniWindowView { windows: Entity, app: Entity, _app_subscription: Subscription, _media_subscription: Option, } impl MiniWindowView { pub fn new(windows: Entity, cx: &mut Context) -> Self { let app = windows.read(cx).app().clone(); let app_subscription = cx.observe(&app, |_, _, cx| cx.notify()); let media_subscription = app .read(cx) .call_media .clone() .map(|media| cx.observe(&media, |_, _, cx| cx.notify())); MiniWindowView { windows, app, _app_subscription: app_subscription, _media_subscription: media_subscription, } } } pub fn speaker_tile(model: &CallModel) -> Option<&Tile> { let present: Vec<&Tile> = model .visible_tiles() .into_iter() .filter(|tile| tile.state == TileState::Present) .collect(); let focused = model .focus() .filter(|focus| !focus.own) .and_then(|focus| present.iter().find(|tile| tile.mri == focus.mri).copied()); focused .or_else(|| { present .iter() .find(|tile| model.tile_speaking(tile)) .copied() }) .or_else(|| present.first().copied()) } pub(super) fn chip(text: String) -> Div { tinted_chip(text, hsla(0., 0., 0., CHIP_BACKGROUND)) } pub(super) fn tinted_chip(text: String, background: Hsla) -> Div { div() .max_w(px(180.)) .truncate() .px(px(8.)) .py(px(3.)) .rounded(px(6.)) .bg(background) .text_size(px(12.)) .font_weight(FontWeight::SEMIBOLD) .text_color(theme::white()) .child(text) } fn cover(image: std::sync::Arc) -> Img { img(ImageSource::Render(image)) .size_full() .object_fit(ObjectFit::Cover) } fn speaker_video( state: &AppState, model: &CallModel, tile: &Tile, now: Instant, ) -> Option> { let call = state.call.as_ref()?; (tile.has_video || model.is_one_to_one()) .then(|| call.pictures.live(&VideoKey::Person(tile.mri.clone()), now)) .flatten() } pub(super) fn speaker_name_chip(state: &AppState, model: &CallModel, now: Instant) -> Option
{ let tile = speaker_tile(model)?; speaker_video(state, model, tile, now)?; Some(chip(tile.name.clone())) } pub(super) fn speaker_layer(state: &AppState, model: &CallModel, now: Instant) -> AnyElement { if state.call.is_none() { return div().into_any_element(); } let Some(tile) = speaker_tile(model) else { return v_flex() .size_full() .items_center() .justify_center() .gap(px(8.)) .child(icon(IconName::Phone, 28., theme::accent_text())) .child( div() .max_w_full() .truncate() .text_size(px(13.)) .child(model.title.clone()), ) .into_any_element(); }; match speaker_video(state, model, tile, now) { Some(image) => cover(image).into_any_element(), None => v_flex() .size_full() .items_center() .justify_center() .gap(px(8.)) .child(person_avatar( &state.directory, tile.user_id.as_deref(), &tile.name, SPEAKER_AVATAR, )) .child( div() .max_w_full() .truncate() .text_size(px(13.)) .font_weight(FontWeight::SEMIBOLD) .child(tile.name.clone()), ) .into_any_element(), } } pub(super) fn self_view(state: &AppState, model: &CallModel, now: Instant) -> Option { let image = state .call .as_ref() .filter(|_| model.camera_on) .and_then(|call| call.pictures.live(&VideoKey::LocalCamera, now))?; Some( div() .absolute() .top(px(8.)) .right(px(8.)) .w(px(SELF_VIEW_WIDTH)) .h(px(SELF_VIEW_HEIGHT)) .overflow_hidden() .rounded(px(6.)) .border_1() .border_color(theme::border_strong()) .child(cover(image)) .into_any_element(), ) } pub(super) fn speaker_speaking(model: &CallModel) -> bool { speaker_tile(model).is_some_and(|tile| model.tile_speaking(tile)) } pub(super) struct ControlIds { pub mute: &'static str, pub back: &'static str, pub leave: &'static str, } pub(super) fn controls_row( app: &Entity, model: &CallModel, ids: ControlIds, group: &'static str, on_back: impl Fn(&mut App) + 'static, ) -> Div { h_flex() .absolute() .left_0() .right_0() .bottom_0() .h(px(CONTROLS_HEIGHT)) .gap(px(10.)) .items_center() .justify_center() .bg(hsla(0., 0., 0., SHADE_BACKGROUND)) .opacity(0.) .group_hover(group, |controls| controls.opacity(1.)) .child(mute_button(app, model, ids.mute, CONTROL_SIZE)) .child(camera_button(app, model).size(px(CONTROL_SIZE))) .child( round_control( ids.back, IconName::Maximize2, theme::surface_raised(), theme::text(), ) .size(px(CONTROL_SIZE)) .tooltip(|window, cx| Tooltip::new("Back to call").build(window, cx)) .on_click(move |_, _, cx| on_back(cx)), ) .child(leave_button(app, model, ids.leave, true)) } impl Render for MiniWindowView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let state = self.app.read(cx); let Some(call) = state.call.as_ref() else { return div().into_any_element(); }; let model = &call.model; let now = Instant::now(); let (status, _) = status_line(model, now); let windows = self.windows.clone(); let sharing = self.windows.read(cx).sharing(); let speaking = speaker_speaking(model); let speaker_name = speaker_name_chip(state, model, now); div() .id("call-mini-window") .group("call-mini-window") .relative() .size_full() .overflow_hidden() .rounded(px(WINDOW_RADIUS)) .bg(theme::surface()) .border_2() .border_color(if speaking { theme::green() } else { theme::border_strong() }) .text_color(theme::text()) .child(speaker_layer(state, model, now)) .child( div() .id("call-mini-window-drag") .absolute() .top_0() .left_0() .right_0() .bottom(px(CONTROLS_HEIGHT)) .window_control_area(WindowControlArea::Drag) .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move()) .child( div() .absolute() .top(px(8.)) .left(px(8.)) .child(chip(status)), ) .children( speaker_name .map(|name| div().absolute().bottom(px(6.)).left(px(8.)).child(name)), ) .children(self_view(state, model, now)), ) .child( controls_row( &self.app, model, ControlIds { mute: "call-mini-window-mute", back: "call-mini-window-back", leave: "call-mini-window-leave", }, "call-mini-window", move |cx| windows.update(cx, |windows, cx| windows.return_to_call(cx)), ) .when(sharing, |row| { row.child( view_switch_button( &self.windows, "call-mini-window-more-people", IconName::Users, SHOW_MORE_PEOPLE, ) .size(px(CONTROL_SIZE)), ) }), ) .into_any_element() } } #[cfg(test)] mod tests { use super::speaker_tile; use crate::call::{CallModel, Tile, TileState}; fn present(mri: &str, name: &str) -> Tile { Tile { state: TileState::Present, ..Tile::invited(mri, name) } } #[test] fn speaker_is_the_speaking_tile_before_the_first_one() { let mut model = CallModel::test(); model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")]; assert_eq!( speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada") ); model.speaking_mris = vec!["8:b".to_owned()]; assert_eq!( speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Bo") ); } #[test] fn speaker_is_the_pinned_tile_over_the_speaking_one() { let mut model = CallModel::test(); model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")]; model.speaking_mris = vec!["8:b".to_owned()]; model.toggle_pin("8:a"); assert_eq!( speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada") ); } #[test] fn nobody_present_means_no_speaker() { let mut model = CallModel::test(); model.tiles = vec![Tile::invited("8:a", "Ada")]; assert!(speaker_tile(&model).is_none()); } }