BlueGit

ict rusty-teams

Files

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

call_mini_window.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
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(state: &AppState, model: &CallModel, now: Instant) -> Option<Div> {
108 let tile = speaker_tile(model)?;
109 speaker_video(state, model, tile, now)?;
110 Some(chip(tile.name.clone()))
111}
112
113pub(super) fn speaker_layer(state: &AppState, model: &CallModel, now: Instant) -> AnyElement {
114 if state.call.is_none() {
115 return div().into_any_element();
116 }
117 let Some(tile) = speaker_tile(model) else {
118 return v_flex()
119 .size_full()
120 .items_center()
121 .justify_center()
122 .gap(px(8.))
123 .child(icon(IconName::Phone, 28., theme::accent_text()))
124 .child(
125 div()
126 .max_w_full()
127 .truncate()
128 .text_size(px(13.))
129 .child(model.title.clone()),
130 )
131 .into_any_element();
132 };
133 match speaker_video(state, model, tile, now) {
134 Some(image) => cover(image).into_any_element(),
135 None => v_flex()
136 .size_full()
137 .items_center()
138 .justify_center()
139 .gap(px(8.))
140 .child(person_avatar(
141 &state.directory,
142 tile.user_id.as_deref(),
143 &tile.name,
144 SPEAKER_AVATAR,
145 ))
146 .child(
147 div()
148 .max_w_full()
149 .truncate()
150 .text_size(px(13.))
151 .font_weight(FontWeight::SEMIBOLD)
152 .child(tile.name.clone()),
153 )
154 .into_any_element(),
155 }
156}
157
158pub(super) fn self_view(state: &AppState, model: &CallModel, now: Instant) -> Option<AnyElement> {
159 let image = state
160 .call
161 .as_ref()
162 .filter(|_| model.camera_on)
163 .and_then(|call| call.pictures.live(&VideoKey::LocalCamera, now))?;
164 Some(
165 div()
166 .absolute()
167 .top(px(8.))
168 .right(px(8.))
169 .w(px(SELF_VIEW_WIDTH))
170 .h(px(SELF_VIEW_HEIGHT))
171 .overflow_hidden()
172 .rounded(px(6.))
173 .border_1()
174 .border_color(theme::border_strong())
175 .child(cover(image))
176 .into_any_element(),
177 )
178}
179
180pub(super) fn speaker_speaking(model: &CallModel) -> bool {
181 speaker_tile(model).is_some_and(|tile| model.tile_speaking(tile))
182}
183
184pub(super) struct ControlIds {
185 pub mute: &'static str,
186 pub back: &'static str,
187 pub leave: &'static str,
188}
189
190pub(super) fn controls_row(
191 app: &Entity<AppState>,
192 model: &CallModel,
193 ids: ControlIds,
194 group: &'static str,
195 on_back: impl Fn(&mut App) + 'static,
196) -> Div {
197 h_flex()
198 .absolute()
199 .left_0()
200 .right_0()
201 .bottom_0()
202 .h(px(CONTROLS_HEIGHT))
203 .gap(px(10.))
204 .items_center()
205 .justify_center()
206 .bg(hsla(0., 0., 0., SHADE_BACKGROUND))
207 .opacity(0.)
208 .group_hover(group, |controls| controls.opacity(1.))
209 .child(mute_button(app, model, ids.mute, CONTROL_SIZE))
210 .child(camera_button(app, model).size(px(CONTROL_SIZE)))
211 .child(
212 round_control(
213 ids.back,
214 IconName::Maximize2,
215 theme::surface_raised(),
216 theme::text(),
217 )
218 .size(px(CONTROL_SIZE))
219 .tooltip(|window, cx| Tooltip::new("Back to call").build(window, cx))
220 .on_click(move |_, _, cx| on_back(cx)),
221 )
222 .child(leave_button(app, model, ids.leave, true))
223}
224
225impl Render for MiniWindowView {
226 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
227 let state = self.app.read(cx);
228 let Some(call) = state.call.as_ref() else {
229 return div().into_any_element();
230 };
231 let model = &call.model;
232 let now = Instant::now();
233 let (status, _) = status_line(model, now);
234 let windows = self.windows.clone();
235 let sharing = self.windows.read(cx).sharing();
236 let speaking = speaker_speaking(model);
237 let speaker_name = speaker_name_chip(state, model, now);
238 div()
239 .id("call-mini-window")
240 .group("call-mini-window")
241 .relative()
242 .size_full()
243 .overflow_hidden()
244 .rounded(px(WINDOW_RADIUS))
245 .bg(theme::surface())
246 .border_2()
247 .border_color(if speaking {
248 theme::green()
249 } else {
250 theme::border_strong()
251 })
252 .text_color(theme::text())
253 .child(speaker_layer(state, model, now))
254 .child(
255 div()
256 .id("call-mini-window-drag")
257 .absolute()
258 .top_0()
259 .left_0()
260 .right_0()
261 .bottom(px(CONTROLS_HEIGHT))
262 .window_control_area(WindowControlArea::Drag)
263 .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move())
264 .child(
265 div()
266 .absolute()
267 .top(px(8.))
268 .left(px(8.))
269 .child(chip(status)),
270 )
271 .children(
272 speaker_name
273 .map(|name| div().absolute().bottom(px(6.)).left(px(8.)).child(name)),
274 )
275 .children(self_view(state, model, now)),
276 )
277 .child(
278 controls_row(
279 &self.app,
280 model,
281 ControlIds {
282 mute: "call-mini-window-mute",
283 back: "call-mini-window-back",
284 leave: "call-mini-window-leave",
285 },
286 "call-mini-window",
287 move |cx| windows.update(cx, |windows, cx| windows.return_to_call(cx)),
288 )
289 .when(sharing, |row| {
290 row.child(
291 view_switch_button(
292 &self.windows,
293 "call-mini-window-more-people",
294 IconName::Users,
295 SHOW_MORE_PEOPLE,
296 )
297 .size(px(CONTROL_SIZE)),
298 )
299 }),
300 )
301 .into_any_element()
302 }
303}
304
305#[cfg(test)]
306mod tests {
307 use super::speaker_tile;
308 use crate::call::{CallModel, Tile, TileState};
309
310 fn present(mri: &str, name: &str) -> Tile {
311 Tile {
312 state: TileState::Present,
313 ..Tile::invited(mri, name)
314 }
315 }
316
317 #[test]
318 fn speaker_is_the_speaking_tile_before_the_first_one() {
319 let mut model = CallModel::test();
320 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
321 assert_eq!(
322 speaker_tile(&model).map(|tile| tile.name.as_str()),
323 Some("Ada")
324 );
325 model.speaking_mris = vec!["8:b".to_owned()];
326 assert_eq!(
327 speaker_tile(&model).map(|tile| tile.name.as_str()),
328 Some("Bo")
329 );
330 }
331
332 #[test]
333 fn speaker_is_the_pinned_tile_over_the_speaking_one() {
334 let mut model = CallModel::test();
335 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
336 model.speaking_mris = vec!["8:b".to_owned()];
337 model.toggle_pin("8:a");
338 assert_eq!(
339 speaker_tile(&model).map(|tile| tile.name.as_str()),
340 Some("Ada")
341 );
342 }
343
344 #[test]
345 fn nobody_present_means_no_speaker() {
346 let mut model = CallModel::test();
347 model.tiles = vec![Tile::invited("8:a", "Ada")];
348 assert!(speaker_tile(&model).is_none());
349 }
350}