| 1 | use std::time::Instant; |
| 2 | |
| 3 | use calling::VideoKey; |
| 4 | use gpui_kit::assets::IconName; |
| 5 | use gpui_kit::component::h_flex; |
| 6 | use gpui_kit::*; |
| 7 | |
| 8 | use super::call_mini_window::{ |
| 9 | chip, controls_row, self_view, speaker_layer, speaker_name_chip, speaker_speaking, tinted_chip, |
| 10 | ControlIds, WINDOW_RADIUS, |
| 11 | }; |
| 12 | use super::call_view::status_line; |
| 13 | use super::toolbar_placement::TimelineBounds; |
| 14 | use super::widgets::icon; |
| 15 | use crate::app_state::AppState; |
| 16 | use crate::theme; |
| 17 | use app_ui_kit::layout::ROW_PADDING; |
| 18 | |
| 19 | const MINI_WIDTH: f32 = 256.; |
| 20 | const MINI_HEIGHT: f32 = 144.; |
| 21 | const SHARE_MINI_WIDTH: f32 = 320.; |
| 22 | const SHARE_MINI_HEIGHT: f32 = 180.; |
| 23 | const MINI_RIGHT: f32 = 16.; |
| 24 | const MINI_BOTTOM: f32 = 40.; |
| 25 | |
| 26 | pub struct CallMiniView { |
| 27 | app: Entity<AppState>, |
| 28 | timeline: TimelineBounds, |
| 29 | _app_subscription: Subscription, |
| 30 | _media_subscription: Option<Subscription>, |
| 31 | } |
| 32 | |
| 33 | impl CallMiniView { |
| 34 | pub fn new(app: Entity<AppState>, timeline: TimelineBounds, cx: &mut Context<Self>) -> Self { |
| 35 | let app_subscription = cx.observe(&app, |_, _, cx| cx.notify()); |
| 36 | let media_subscription = app |
| 37 | .read(cx) |
| 38 | .call_media |
| 39 | .clone() |
| 40 | .map(|media| cx.observe(&media, |_, _, cx| cx.notify())); |
| 41 | CallMiniView { |
| 42 | app, |
| 43 | timeline, |
| 44 | _app_subscription: app_subscription, |
| 45 | _media_subscription: media_subscription, |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn stop_sharing_pill(app: &Entity<AppState>) -> Stateful<Div> { |
| 51 | let stop_app = app.clone(); |
| 52 | div() |
| 53 | .id("call-mini-stop-sharing") |
| 54 | .absolute() |
| 55 | .top(px(8.)) |
| 56 | .right(px(8.)) |
| 57 | .h(px(24.)) |
| 58 | .px(px(10.)) |
| 59 | .flex() |
| 60 | .items_center() |
| 61 | .gap(px(4.)) |
| 62 | .rounded_full() |
| 63 | .bg(theme::red()) |
| 64 | .text_color(theme::white()) |
| 65 | .text_size(px(12.)) |
| 66 | .font_weight(FontWeight::SEMIBOLD) |
| 67 | .cursor_pointer() |
| 68 | .hover(|button| button.opacity(0.85)) |
| 69 | .child(icon(IconName::ScreenShare, 14., theme::white())) |
| 70 | .child("Stop") |
| 71 | .on_click(move |_, _, cx| { |
| 72 | cx.stop_propagation(); |
| 73 | stop_app.update(cx, |state, cx| state.stop_call_share(cx)); |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | impl Render for CallMiniView { |
| 78 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 79 | let state = self.app.read(cx); |
| 80 | let Some(call) = state.call.as_ref().filter(|call| !call.viewing) else { |
| 81 | return div().into_any_element(); |
| 82 | }; |
| 83 | let model = &call.model; |
| 84 | let now = Instant::now(); |
| 85 | let (status, _) = status_line(model, now); |
| 86 | let viewport = window.viewport_size(); |
| 87 | let (right, bottom) = |
| 88 | self.timeline |
| 89 | .get() |
| 90 | .map_or((px(MINI_RIGHT), px(MINI_BOTTOM)), |timeline| { |
| 91 | ( |
| 92 | viewport.width - timeline.right() + px(ROW_PADDING), |
| 93 | viewport.height - timeline.bottom() + px(MINI_RIGHT), |
| 94 | ) |
| 95 | }); |
| 96 | let sharing = model.local_share.is_some(); |
| 97 | let share_image = sharing |
| 98 | .then(|| { |
| 99 | state |
| 100 | .call |
| 101 | .as_ref() |
| 102 | .and_then(|call| call.pictures.live(&VideoKey::LocalScreen, now)) |
| 103 | }) |
| 104 | .flatten(); |
| 105 | let open_app = self.app.clone(); |
| 106 | let back_app = self.app.clone(); |
| 107 | let speaker_name = speaker_name_chip(state, model, now); |
| 108 | let border = if sharing { |
| 109 | theme::red() |
| 110 | } else if speaker_speaking(model) { |
| 111 | theme::green() |
| 112 | } else { |
| 113 | theme::border_strong() |
| 114 | }; |
| 115 | let top_left = if sharing { |
| 116 | tinted_chip("Sharing".to_owned(), theme::red()) |
| 117 | } else { |
| 118 | chip(status) |
| 119 | }; |
| 120 | div() |
| 121 | .id("call-mini") |
| 122 | .group("call-mini") |
| 123 | .occlude() |
| 124 | .absolute() |
| 125 | .right(right) |
| 126 | .bottom(bottom) |
| 127 | .w(px(if sharing { |
| 128 | SHARE_MINI_WIDTH |
| 129 | } else { |
| 130 | MINI_WIDTH |
| 131 | })) |
| 132 | .h(px(if sharing { |
| 133 | SHARE_MINI_HEIGHT |
| 134 | } else { |
| 135 | MINI_HEIGHT |
| 136 | })) |
| 137 | .overflow_hidden() |
| 138 | .rounded(px(WINDOW_RADIUS)) |
| 139 | .bg(theme::surface()) |
| 140 | .border_2() |
| 141 | .border_color(border) |
| 142 | .text_color(theme::text()) |
| 143 | .shadow(theme::flyout_shadow()) |
| 144 | .cursor_pointer() |
| 145 | .on_click(move |_, _, cx| { |
| 146 | open_app.update(cx, |state, cx| state.show_call(true, cx)); |
| 147 | }) |
| 148 | .child(match share_image { |
| 149 | Some(image) => div() |
| 150 | .size_full() |
| 151 | .bg(hsla(0., 0., 0.04, 1.)) |
| 152 | .child( |
| 153 | img(ImageSource::Render(image)) |
| 154 | .size_full() |
| 155 | .object_fit(ObjectFit::Contain), |
| 156 | ) |
| 157 | .into_any_element(), |
| 158 | None => speaker_layer(state, model, now), |
| 159 | }) |
| 160 | .child(div().absolute().top(px(8.)).left(px(8.)).child(top_left)) |
| 161 | .children( |
| 162 | speaker_name |
| 163 | .map(|name| h_flex().absolute().bottom(px(6.)).left(px(8.)).child(name)), |
| 164 | ) |
| 165 | .children(if sharing { |
| 166 | Some(stop_sharing_pill(&self.app).into_any_element()) |
| 167 | } else { |
| 168 | self_view(state, model, now) |
| 169 | }) |
| 170 | .child(controls_row( |
| 171 | &self.app, |
| 172 | model, |
| 173 | ControlIds { |
| 174 | mute: "call-mini-mute", |
| 175 | back: "call-mini-back", |
| 176 | leave: "call-mini-leave", |
| 177 | }, |
| 178 | "call-mini", |
| 179 | move |cx| back_app.update(cx, |state, cx| state.show_call(true, cx)), |
| 180 | )) |
| 181 | .into_any_element() |
| 182 | } |
| 183 | } |