BlueGit

ict rusty-teams

Files

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

call_mini_window.rs

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