BlueGit

ict rusty-teams

Files

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

call_mini_window.rs

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