| 1 | use std::time::Instant; |
| 2 | |
| 3 | use calling::VideoKey; |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use super::avatar::person_avatar; |
| 9 | use super::call_view::{camera_button, leave_button, mute_button, round_control, status_line}; |
| 10 | use super::widgets::icon; |
| 11 | use crate::app_state::AppState; |
| 12 | use crate::call::{CallModel, CallWindows, Tile, TileState}; |
| 13 | use crate::theme; |
| 14 | |
| 15 | const CONTROLS_HEIGHT: f32 = 44.; |
| 16 | const CONTROL_SIZE: f32 = 30.; |
| 17 | const WINDOW_RADIUS: f32 = 10.; |
| 18 | const SELF_VIEW_WIDTH: f32 = 80.; |
| 19 | const SELF_VIEW_HEIGHT: f32 = 45.; |
| 20 | const SPEAKER_AVATAR: f32 = 56.; |
| 21 | const CHIP_BACKGROUND: f32 = 0.6; |
| 22 | const SHADE_BACKGROUND: f32 = 0.55; |
| 23 | |
| 24 | pub struct MiniWindowView { |
| 25 | windows: Entity<CallWindows>, |
| 26 | app: Entity<AppState>, |
| 27 | _app_subscription: Subscription, |
| 28 | _media_subscription: Option<Subscription>, |
| 29 | } |
| 30 | |
| 31 | impl MiniWindowView { |
| 32 | pub fn new(windows: Entity<CallWindows>, cx: &mut Context<Self>) -> Self { |
| 33 | let app = windows.read(cx).app().clone(); |
| 34 | let app_subscription = cx.observe(&app, |_, _, cx| cx.notify()); |
| 35 | let media_subscription = app |
| 36 | .read(cx) |
| 37 | .call_media |
| 38 | .clone() |
| 39 | .map(|media| cx.observe(&media, |_, _, cx| cx.notify())); |
| 40 | MiniWindowView { |
| 41 | windows, |
| 42 | app, |
| 43 | _app_subscription: app_subscription, |
| 44 | _media_subscription: media_subscription, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | pub fn speaker_tile(model: &CallModel) -> Option<&Tile> { |
| 50 | let present: Vec<&Tile> = model.visible_tiles().into_iter().filter(|tile| tile.state == TileState::Present).collect(); |
| 51 | let focused = model |
| 52 | .focus() |
| 53 | .filter(|focus| !focus.own) |
| 54 | .and_then(|focus| present.iter().find(|tile| tile.mri == focus.mri).copied()); |
| 55 | focused |
| 56 | .or_else(|| present.iter().find(|tile| model.tile_speaking(tile)).copied()) |
| 57 | .or_else(|| present.first().copied()) |
| 58 | } |
| 59 | |
| 60 | fn chip(text: String) -> Div { |
| 61 | div() |
| 62 | .max_w(px(180.)) |
| 63 | .truncate() |
| 64 | .px(px(8.)) |
| 65 | .py(px(3.)) |
| 66 | .rounded(px(6.)) |
| 67 | .bg(hsla(0., 0., 0., CHIP_BACKGROUND)) |
| 68 | .text_size(px(12.)) |
| 69 | .font_weight(FontWeight::SEMIBOLD) |
| 70 | .text_color(theme::white()) |
| 71 | .child(text) |
| 72 | } |
| 73 | |
| 74 | fn cover(image: std::sync::Arc<RenderImage>) -> Img { |
| 75 | img(ImageSource::Render(image)).size_full().object_fit(ObjectFit::Cover) |
| 76 | } |
| 77 | |
| 78 | fn speaker_layer(state: &AppState, model: &CallModel, now: Instant) -> AnyElement { |
| 79 | let Some(call) = state.call.as_ref() else { |
| 80 | return div().into_any_element(); |
| 81 | }; |
| 82 | let Some(tile) = speaker_tile(model) else { |
| 83 | return v_flex() |
| 84 | .size_full() |
| 85 | .items_center() |
| 86 | .justify_center() |
| 87 | .gap(px(8.)) |
| 88 | .child(icon(IconName::Phone, 28., theme::accent_text())) |
| 89 | .child(div().max_w_full().truncate().text_size(px(13.)).child(model.title.clone())) |
| 90 | .into_any_element(); |
| 91 | }; |
| 92 | let video = (tile.has_video || model.is_one_to_one()) |
| 93 | .then(|| call.pictures.live(&VideoKey::Person(tile.mri.clone()), now)) |
| 94 | .flatten(); |
| 95 | match video { |
| 96 | Some(image) => cover(image).into_any_element(), |
| 97 | None => v_flex() |
| 98 | .size_full() |
| 99 | .items_center() |
| 100 | .justify_center() |
| 101 | .gap(px(8.)) |
| 102 | .child(person_avatar(&state.directory, tile.user_id.as_deref(), &tile.name, SPEAKER_AVATAR)) |
| 103 | .child(div().max_w_full().truncate().text_size(px(13.)).font_weight(FontWeight::SEMIBOLD).child(tile.name.clone())) |
| 104 | .into_any_element(), |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn self_view(state: &AppState, model: &CallModel, now: Instant) -> Option<AnyElement> { |
| 109 | let image = state |
| 110 | .call |
| 111 | .as_ref() |
| 112 | .filter(|_| model.camera_on) |
| 113 | .and_then(|call| call.pictures.live(&VideoKey::LocalCamera, now))?; |
| 114 | Some( |
| 115 | div() |
| 116 | .absolute() |
| 117 | .top(px(8.)) |
| 118 | .right(px(8.)) |
| 119 | .w(px(SELF_VIEW_WIDTH)) |
| 120 | .h(px(SELF_VIEW_HEIGHT)) |
| 121 | .overflow_hidden() |
| 122 | .rounded(px(6.)) |
| 123 | .border_1() |
| 124 | .border_color(theme::border_strong()) |
| 125 | .child(cover(image)) |
| 126 | .into_any_element(), |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | impl Render for MiniWindowView { |
| 131 | fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 132 | let state = self.app.read(cx); |
| 133 | let Some(call) = state.call.as_ref() else { |
| 134 | return div().into_any_element(); |
| 135 | }; |
| 136 | let model = &call.model; |
| 137 | let now = Instant::now(); |
| 138 | let (status, _) = status_line(model, now); |
| 139 | let windows = self.windows.clone(); |
| 140 | let speaking = speaker_tile(model).is_some_and(|tile| model.tile_speaking(tile)); |
| 141 | let speaker_name = speaker_tile(model).map(|tile| tile.name.clone()); |
| 142 | div() |
| 143 | .id("call-mini-window") |
| 144 | .group("call-mini-window") |
| 145 | .relative() |
| 146 | .size_full() |
| 147 | .overflow_hidden() |
| 148 | .rounded(px(WINDOW_RADIUS)) |
| 149 | .bg(theme::surface()) |
| 150 | .border_2() |
| 151 | .border_color(if speaking { theme::green() } else { theme::border_strong() }) |
| 152 | .text_color(theme::text()) |
| 153 | .child(speaker_layer(state, model, now)) |
| 154 | .child( |
| 155 | div() |
| 156 | .id("call-mini-window-drag") |
| 157 | .absolute() |
| 158 | .top_0() |
| 159 | .left_0() |
| 160 | .right_0() |
| 161 | .bottom(px(CONTROLS_HEIGHT)) |
| 162 | .window_control_area(WindowControlArea::Drag) |
| 163 | .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move()) |
| 164 | .child(div().absolute().top(px(8.)).left(px(8.)).child(chip(status))) |
| 165 | .children(speaker_name.map(|name| div().absolute().bottom(px(6.)).left(px(8.)).child(chip(name)))) |
| 166 | .children(self_view(state, model, now)), |
| 167 | ) |
| 168 | .child( |
| 169 | h_flex() |
| 170 | .absolute() |
| 171 | .left_0() |
| 172 | .right_0() |
| 173 | .bottom_0() |
| 174 | .h(px(CONTROLS_HEIGHT)) |
| 175 | .gap(px(10.)) |
| 176 | .items_center() |
| 177 | .justify_center() |
| 178 | .bg(hsla(0., 0., 0., SHADE_BACKGROUND)) |
| 179 | .opacity(0.) |
| 180 | .group_hover("call-mini-window", |controls| controls.opacity(1.)) |
| 181 | .child(mute_button(&self.app, model, "call-mini-window-mute", CONTROL_SIZE)) |
| 182 | .child(camera_button(&self.app, model).size(px(CONTROL_SIZE))) |
| 183 | .child( |
| 184 | round_control("call-mini-window-back", IconName::Maximize2, theme::surface_raised(), theme::text()) |
| 185 | .size(px(CONTROL_SIZE)) |
| 186 | .tooltip(|window, cx| Tooltip::new("Back to call").build(window, cx)) |
| 187 | .on_click(move |_, _, cx| windows.update(cx, |windows, cx| windows.return_to_call(cx))), |
| 188 | ) |
| 189 | .child(leave_button(&self.app, model, "call-mini-window-leave", true)), |
| 190 | ) |
| 191 | .into_any_element() |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #[cfg(test)] |
| 196 | mod tests { |
| 197 | use super::speaker_tile; |
| 198 | use crate::call::{CallModel, Tile, TileState}; |
| 199 | |
| 200 | fn present(mri: &str, name: &str) -> Tile { |
| 201 | Tile { |
| 202 | state: TileState::Present, |
| 203 | ..Tile::invited(mri, name) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #[test] |
| 208 | fn speaker_is_the_speaking_tile_before_the_first_one() { |
| 209 | let mut model = CallModel::test(); |
| 210 | model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")]; |
| 211 | assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada")); |
| 212 | model.speaking_mris = vec!["8:b".to_owned()]; |
| 213 | assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Bo")); |
| 214 | } |
| 215 | |
| 216 | #[test] |
| 217 | fn speaker_is_the_pinned_tile_over_the_speaking_one() { |
| 218 | let mut model = CallModel::test(); |
| 219 | model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")]; |
| 220 | model.speaking_mris = vec!["8:b".to_owned()]; |
| 221 | model.toggle_pin("8:a"); |
| 222 | assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada")); |
| 223 | } |
| 224 | |
| 225 | #[test] |
| 226 | fn nobody_present_means_no_speaker() { |
| 227 | let mut model = CallModel::test(); |
| 228 | model.tiles = vec![Tile::invited("8:a", "Ada")]; |
| 229 | assert!(speaker_tile(&model).is_none()); |
| 230 | } |
| 231 | } |