BlueGit

ict rusty-teams

Files

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

call_mini_window.rs

call_mini_window.rs 8.1 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}
29
30impl MiniWindowView {
31 pub fn new(windows: Entity<CallWindows>, cx: &mut Context<Self>) -> Self {
32 let app = windows.read(cx).app().clone();
33 let app_subscription = cx.observe(&app, |_, _, cx| cx.notify());
34 MiniWindowView {
35 windows,
36 app,
37 _app_subscription: app_subscription,
38 }
39 }
40}
41
42pub fn speaker_tile(model: &CallModel) -> Option<&Tile> {
43 let present: Vec<&Tile> = model.visible_tiles().into_iter().filter(|tile| tile.state == TileState::Present).collect();
44 let focused = model
45 .focus()
46 .filter(|focus| !focus.own)
47 .and_then(|focus| present.iter().find(|tile| tile.mri == focus.mri).copied());
48 focused
49 .or_else(|| present.iter().find(|tile| model.tile_speaking(tile)).copied())
50 .or_else(|| present.first().copied())
51}
52
53fn chip(text: String) -> Div {
54 div()
55 .max_w(px(180.))
56 .truncate()
57 .px(px(8.))
58 .py(px(3.))
59 .rounded(px(6.))
60 .bg(hsla(0., 0., 0., CHIP_BACKGROUND))
61 .text_size(px(12.))
62 .font_weight(FontWeight::SEMIBOLD)
63 .text_color(theme::white())
64 .child(text)
65}
66
67fn cover(image: std::sync::Arc<RenderImage>) -> Img {
68 img(ImageSource::Render(image)).size_full().object_fit(ObjectFit::Cover)
69}
70
71fn speaker_layer(state: &AppState, model: &CallModel, now: Instant) -> AnyElement {
72 let Some(call) = state.call.as_ref() else {
73 return div().into_any_element();
74 };
75 let Some(tile) = speaker_tile(model) else {
76 return v_flex()
77 .size_full()
78 .items_center()
79 .justify_center()
80 .gap(px(8.))
81 .child(icon(IconName::Phone, 28., theme::accent_text()))
82 .child(div().max_w_full().truncate().text_size(px(13.)).child(model.title.clone()))
83 .into_any_element();
84 };
85 let video = (tile.has_video || model.is_one_to_one())
86 .then(|| call.pictures.live(&VideoKey::Person(tile.mri.clone()), now))
87 .flatten();
88 match video {
89 Some(image) => cover(image).into_any_element(),
90 None => v_flex()
91 .size_full()
92 .items_center()
93 .justify_center()
94 .gap(px(8.))
95 .child(person_avatar(&state.directory, tile.user_id.as_deref(), &tile.name, SPEAKER_AVATAR))
96 .child(div().max_w_full().truncate().text_size(px(13.)).font_weight(FontWeight::SEMIBOLD).child(tile.name.clone()))
97 .into_any_element(),
98 }
99}
100
101fn self_view(state: &AppState, model: &CallModel, now: Instant) -> Option<AnyElement> {
102 let image = state
103 .call
104 .as_ref()
105 .filter(|_| model.camera_on)
106 .and_then(|call| call.pictures.live(&VideoKey::LocalCamera, now))?;
107 Some(
108 div()
109 .absolute()
110 .top(px(8.))
111 .right(px(8.))
112 .w(px(SELF_VIEW_WIDTH))
113 .h(px(SELF_VIEW_HEIGHT))
114 .overflow_hidden()
115 .rounded(px(6.))
116 .border_1()
117 .border_color(theme::border_strong())
118 .child(cover(image))
119 .into_any_element(),
120 )
121}
122
123impl Render for MiniWindowView {
124 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
125 let state = self.app.read(cx);
126 let Some(call) = state.call.as_ref() else {
127 return div().into_any_element();
128 };
129 let model = &call.model;
130 let now = Instant::now();
131 let (status, _) = status_line(model, now);
132 let windows = self.windows.clone();
133 let speaking = speaker_tile(model).is_some_and(|tile| model.tile_speaking(tile));
134 let speaker_name = speaker_tile(model).map(|tile| tile.name.clone());
135 div()
136 .id("call-mini-window")
137 .group("call-mini-window")
138 .relative()
139 .size_full()
140 .overflow_hidden()
141 .rounded(px(WINDOW_RADIUS))
142 .bg(theme::surface())
143 .border_2()
144 .border_color(if speaking { theme::green() } else { theme::border_strong() })
145 .text_color(theme::text())
146 .child(speaker_layer(state, model, now))
147 .child(
148 div()
149 .id("call-mini-window-drag")
150 .absolute()
151 .top_0()
152 .left_0()
153 .right_0()
154 .bottom(px(CONTROLS_HEIGHT))
155 .window_control_area(WindowControlArea::Drag)
156 .on_mouse_down(MouseButton::Left, |_, window, _| window.start_window_move())
157 .child(div().absolute().top(px(8.)).left(px(8.)).child(chip(status)))
158 .children(speaker_name.map(|name| div().absolute().bottom(px(6.)).left(px(8.)).child(chip(name))))
159 .children(self_view(state, model, now)),
160 )
161 .child(
162 h_flex()
163 .absolute()
164 .left_0()
165 .right_0()
166 .bottom_0()
167 .h(px(CONTROLS_HEIGHT))
168 .gap(px(10.))
169 .items_center()
170 .justify_center()
171 .bg(hsla(0., 0., 0., SHADE_BACKGROUND))
172 .opacity(0.)
173 .group_hover("call-mini-window", |controls| controls.opacity(1.))
174 .child(mute_button(&self.app, model, "call-mini-window-mute", CONTROL_SIZE))
175 .child(camera_button(&self.app, model).size(px(CONTROL_SIZE)))
176 .child(
177 round_control("call-mini-window-back", IconName::Maximize2, theme::surface_raised(), theme::text())
178 .size(px(CONTROL_SIZE))
179 .tooltip(|window, cx| Tooltip::new("Back to call").build(window, cx))
180 .on_click(move |_, _, cx| windows.update(cx, |windows, cx| windows.return_to_call(cx))),
181 )
182 .child(leave_button(&self.app, model, "call-mini-window-leave", true)),
183 )
184 .into_any_element()
185 }
186}
187
188#[cfg(test)]
189mod tests {
190 use super::speaker_tile;
191 use crate::call::{CallModel, Tile, TileState};
192
193 fn present(mri: &str, name: &str) -> Tile {
194 Tile {
195 state: TileState::Present,
196 ..Tile::invited(mri, name)
197 }
198 }
199
200 #[test]
201 fn speaker_is_the_speaking_tile_before_the_first_one() {
202 let mut model = CallModel::test();
203 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
204 assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada"));
205 model.speaking_mris = vec!["8:b".to_owned()];
206 assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Bo"));
207 }
208
209 #[test]
210 fn speaker_is_the_pinned_tile_over_the_speaking_one() {
211 let mut model = CallModel::test();
212 model.tiles = vec![present("8:a", "Ada"), present("8:b", "Bo")];
213 model.speaking_mris = vec!["8:b".to_owned()];
214 model.toggle_pin("8:a");
215 assert_eq!(speaker_tile(&model).map(|tile| tile.name.as_str()), Some("Ada"));
216 }
217
218 #[test]
219 fn nobody_present_means_no_speaker() {
220 let mut model = CallModel::test();
221 model.tiles = vec![Tile::invited("8:a", "Ada")];
222 assert!(speaker_tile(&model).is_none());
223 }
224}