BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / call_mini_window.rs

call_mini_window.rs

call_mini_window.rs 10.8 KiB Download Raw
1use std::time::Instant;
2
3use calling::VideoKey;
4use gpui_kit::assets::IconName;
5use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
6use gpui_kit::prelude::FluentBuilder as _;
7use gpui_kit::*;
8
9use super::avatar::person_avatar;
10use super::call_share_gallery::{SHOW_MORE_PEOPLE, view_switch_button};
11use super::call_view::{camera_button, leave_button, mute_button, round_control, status_line};
12use super::widgets::icon;
13use crate::app_state::AppState;
14use crate::call::{CallModel, CallWindows, Tile, TileState};
15use crate::theme;
16
17pub(super) const CONTROLS_HEIGHT: f32 = 44.;
18pub(super) const CONTROL_SIZE: f32 = 30.;
19pub(super) const WINDOW_RADIUS: f32 = 10.;
20const SELF_VIEW_WIDTH: f32 = 80.;
21const SELF_VIEW_HEIGHT: f32 = 45.;
22const SPEAKER_AVATAR: f32 = 56.;
23const CHIP_BACKGROUND: f32 = 0.6;
24const SHADE_BACKGROUND: f32 = 0.55;
25
26pub struct MiniWindowView {
27 windows: Entity<CallWindows>,
28 app: Entity<AppState>,
29 _app_subscription: Subscription,
30 _media_subscription: Option<Subscription>,
31}
32
33impl MiniWindowView {
34 pub fn new(windows: Entity<CallWindows>, cx: &mut Context<Self>) -> Self {
35 let app = windows.read(cx).app().clone();
36 let app_subscription = cx.observe(&app, |_, _, cx| cx.notify());
37 let media_subscription = app
38 .read(cx)
39 .call_media
40 .clone()
41 .map(|media| cx.observe(&media, |_, _, cx| cx.notify()));
42 MiniWindowView {
43 windows,
44 app,
45 _app_subscription: app_subscription,
46 _media_subscription: media_subscription,
47 }
48 }
49}
50
51pub fn speaker_tile(model: &CallModel) -> Option<&Tile> {
52 let present: Vec<&Tile> = model
53 .visible_tiles()
54 .into_iter()
55 .filter(|tile| tile.state == TileState::Present)
56 .collect();
57 let focused = model
58 .focus()
59 .filter(|focus| !focus.own)
60 .and_then(|focus| present.iter().find(|tile| tile.mri == focus.mri).copied());
61 focused
62 .or_else(|| {
63 present
64 .iter()
65 .find(|tile| model.tile_speaking(tile))
66 .copied()
67 })
68 .or_else(|| present.first().copied())
69}
70
71pub(super) fn chip(text: String) -> Div {
72 tinted_chip(text, hsla(0., 0., 0., CHIP_BACKGROUND))
73}
74
75pub(super) fn tinted_chip(text: String, background: Hsla) -> Div {
76 div()
77 .max_w(px(180.))
78 .truncate()
79 .px(px(8.))
80 .py(px(3.))
81 .rounded(px(6.))
82 .bg(background)
83 .text_size(px(12.))
84 .font_weight(FontWeight::SEMIBOLD)
85 .text_color(theme::white())
86 .child(text)
87}
88
89fn cover(image: std::sync::Arc<RenderImage>) -> Img {
90 img(ImageSource::Render(image))
91 .size_full()
92 .object_fit(ObjectFit::Cover)
93}
94
95fn speaker_video(
96 state: &AppState,
97 model: &CallModel,
98 tile: &Tile,
99 now: Instant,
100) -> Option<std::sync::Arc<RenderImage>> {
101 let call = state.call.as_ref()?;
102 (tile.has_video || model.is_one_to_one())
103 .then(|| call.pictures.live(&VideoKey::Person(tile.mri.clone()), now))
104 .flatten()
105}
106
107pub(super) fn speaker_name_chip(
108 state: &AppState,
109 model: &CallModel,
110 now: Instant,
111) -> Option<Div> {
112 let tile = speaker_tile(model)?;
113 speaker_video(state, model, tile, now)?;
114 Some(chip(tile.name.clone()))
115}
116
117pub(super) fn speaker_layer(state: &AppState, model: &CallModel, now: Instant) -> AnyElement {
118 if state.call.is_none() {
119 return div().into_any_element();
120 }
121 let Some(tile) = speaker_tile(model) else {
122 return v_flex()
123 .size_full()
124 .items_center()
125 .justify_center()
126 .gap(px(8.))
127 .child(icon(IconName::Phone, 28., theme::accent_text()))
128 .child(
129 div()
130 .max_w_full()
131 .truncate()
132 .text_size(px(13.))
133 .child(model.title.clone()),
134 )
135 .into_any_element();
136 };
137 match speaker_video(state, model, tile, now) {
138 Some(image) => cover(image).into_any_element(),
139 None => v_flex()
140 .size_full()
141 .items_center()
142 .justify_center()
143 .gap(px(8.))
144 .child(person_avatar(
145 &state.directory,
146 tile.user_id.as_deref(),
147 &tile.name,
148 SPEAKER_AVATAR,
149 ))
150 .child(
151 div()
152 .max_w_full()
153 .truncate()
154 .text_size(px(13.))
155 .font_weight(FontWeight::SEMIBOLD)
156 .child(tile.name.clone()),
157 )
158 .into_any_element(),
159 }
160}
161
162pub(super) fn self_view(state: &AppState, model: &CallModel, now: Instant) -> Option<AnyElement> {
163 let image = state
164 .call
165 .as_ref()
166 .filter(|_| model.camera_on)
167 .and_then(|call| call.pictures.live(&VideoKey::LocalCamera, now))?;
168 Some(
169 div()
170 .absolute()
171 .top(px(8.))
172 .right(px(8.))
173 .w(px(SELF_VIEW_WIDTH))
174 .h(px(SELF_VIEW_HEIGHT))
175 .overflow_hidden()
176 .rounded(px(6.))
177 .border_1()
178 .border_color(theme::border_strong())
179 .child(cover(image))
180 .into_any_element(),
181 )
182}
183
184pub(super) fn speaker_speaking(model: &CallModel) -> bool {
185 speaker_tile(model).is_some_and(|tile| model.tile_speaking(tile))
186}
187
188pub(super) struct ControlIds {
189 pub mute: &'static str,
190 pub back: &'static str,
191 pub leave: &'static str,
192}
193
194pub(super) fn controls_row(
195 app: &Entity<AppState>,
196 model: &CallModel,
197 ids: ControlIds,
198 group: &'static str,
199 on_back: impl Fn(&mut App) + 'static,
200) -> Div {
201 h_flex()
202 .absolute()
203 .left_0()
204 .right_0()
205 .bottom_0()
206 .h(px(CONTROLS_HEIGHT))
207 .gap(px(10.))
208 .items_center()
209 .justify_center()
210 .bg(hsla(0., 0., 0., SHADE_BACKGROUND))
211 .opacity(0.)
212 .group_hover(group, |controls| controls.opacity(1.))
213 .child(mute_button(app, model, ids.mute, CONTROL_SIZE))
214 .child(camera_button(app, model).size(px(CONTROL_SIZE)))
215 .child(
216 round_control(
217 ids.back,
218 IconName::Maximize2,
219 theme::surface_raised(),
220 theme::text(),
221 )
222 .size(px(CONTROL_SIZE))
223 .tooltip(|window, cx| Tooltip::new("Back to call").build(window, cx))
224 .on_click(move |_, _, cx| on_back(cx)),
225 )
226 .child(leave_button(app, model, ids.leave, true))
227}
228
229impl Render for MiniWindowView {
230 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
231 let state = self.app.read(cx);
232 let Some(call) = state.call.as_ref() else {
233 return div().into_any_element();
234 };
235 let model = &call.model;
236 let now = Instant::now();
237 let (status, _) = status_line(model, now);
238 let windows = self.windows.clone();
239 let sharing = self.windows.read(cx).sharing();
240 let speaking = speaker_speaking(model);
241 let speaker_name = speaker_name_chip(state, model, now);
242 div()
243 .id("call-mini-window")
244 .group("call-mini-window")
245 .relative()
246 .size_full()
247 .overflow_hidden()
248 .rounded(px(WINDOW_RADIUS))
249 .bg(theme::surface())
250 .border_2()
251 .border_color(if speaking {
252 theme::green()
253 } else {
254 theme::border_strong()
255 })
256 .text_color(theme::text())
257 .child(speaker_layer(state, model, now))
258 .child(
259 div()
260 .id("call-mini-window-drag")
261 .absolute()
262 .top_0()
263 .left_0()
264 .right_0()
265 .bottom(px(CONTROLS_HEIGHT))
266 .window_control_area(WindowControlArea::Drag)
267 .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move())
268 .child(
269 div()
270 .absolute()
271 .top(px(8.))
272 .left(px(8.))
273 .child(chip(status)),
274 )
275 .children(speaker_name.map(|name| {
276 div().absolute().bottom(px(6.)).left(px(8.)).child(name)
277 }))
278 .children(self_view(state, model, now)),
279 )
280 .child(
281 controls_row(
282 &self.app,
283 model,
284 ControlIds {
285 mute: "call-mini-window-mute",
286 back: "call-mini-window-back",
287 leave: "call-mini-window-leave",
288 },
289 "call-mini-window",
290 move |cx| windows.update(cx, |windows, cx| windows.return_to_call(cx)),
291 )
292 .when(sharing, |row| {
293 row.child(
294 view_switch_button(
295 &self.windows,
296 "call-mini-window-more-people",
297 IconName::Users,
298 SHOW_MORE_PEOPLE,
299 )
300 .size(px(CONTROL_SIZE)),
301 )
302 }),
303 )
304 .into_any_element()
305 }
306}
307
308#[cfg(test)]
309mod tests {
310 use super::speaker_tile;
311 use crate::call::{CallModel, Tile, TileState};
312
313 fn present(mri: &str, name: &str) -> Tile {
314 Tile {
315 state: TileState::Present,
316 ..Tile::invited(mri, name)
317 }
318 }
319
320 #[test]
321 fn speaker_is_the_speaking_tile_before_the_first_one() {
322 let mut model = CallModel::test();
323 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
324 assert_eq!(
325 speaker_tile(&model).map(|tile| tile.name.as_str()),
326 Some("Ada")
327 );
328 model.speaking_mris = vec!["8:b".to_owned()];
329 assert_eq!(
330 speaker_tile(&model).map(|tile| tile.name.as_str()),
331 Some("Bo")
332 );
333 }
334
335 #[test]
336 fn speaker_is_the_pinned_tile_over_the_speaking_one() {
337 let mut model = CallModel::test();
338 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
339 model.speaking_mris = vec!["8:b".to_owned()];
340 model.toggle_pin("8:a");
341 assert_eq!(
342 speaker_tile(&model).map(|tile| tile.name.as_str()),
343 Some("Ada")
344 );
345 }
346
347 #[test]
348 fn nobody_present_means_no_speaker() {
349 let mut model = CallModel::test();
350 model.tiles = vec![Tile::invited("8:a", "Ada")];
351 assert!(speaker_tile(&model).is_none());
352 }
353}