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