BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / share_overlay.rs

share_overlay.rs

share_overlay.rs 13.0 KiB Download Raw
1use calling::ShareKind;
2
3use super::model::{CallModel, Tile, TileState};
4use crate::notify::platform::{ScreenRect, WorkArea};
5
6pub const GALLERY_WIDTH: f32 = 192.;
7pub const GALLERY_INSET: f32 = 3.;
8pub const GALLERY_TILE_WIDTH: f32 = GALLERY_WIDTH - 2. * GALLERY_INSET;
9pub const GALLERY_TILE_HEIGHT: f32 = GALLERY_TILE_WIDTH * 9. / 16.;
10pub const GALLERY_TITLE_HEIGHT: f32 = 32.;
11pub const GALLERY_CONTROLS_HEIGHT: f32 = 40.;
12pub const GALLERY_DIVIDER_HEIGHT: f32 = 20.;
13pub const GALLERY_MAX_TILES: usize = 4;
14pub const GALLERY_MARGIN: f32 = 30.;
15pub const GALLERY_POSITION_KEY: &str = "call_gallery_position";
16pub const MINIMIZE_OVERLAP_PERCENT: i64 = 5;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum LocalShare {
20 None,
21 Screen,
22 Window,
23}
24
25impl LocalShare {
26 pub fn of(kind: Option<ShareKind>, sharing: bool) -> LocalShare {
27 match (sharing, kind) {
28 (false, _) => LocalShare::None,
29 (true, Some(ShareKind::Window)) => LocalShare::Window,
30 (true, _) => LocalShare::Screen,
31 }
32 }
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum Monitor {
37 None,
38 Mini,
39 Gallery,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum Surface {
44 ShareBar,
45 Border,
46 Mini,
47 Gallery,
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub struct OverlayPlan {
52 pub monitor: Monitor,
53 pub share_bar: bool,
54}
55
56pub fn overlay_plan(
57 in_call: bool,
58 share: LocalShare,
59 main_minimized: bool,
60 speaker_only: bool,
61) -> OverlayPlan {
62 let monitor = match (in_call && main_minimized, share) {
63 (false, _) => Monitor::None,
64 (true, LocalShare::None) => Monitor::Mini,
65 (true, _) if speaker_only => Monitor::Mini,
66 (true, _) => Monitor::Gallery,
67 };
68 OverlayPlan {
69 monitor,
70 share_bar: in_call && share != LocalShare::None,
71 }
72}
73
74pub fn excluded_from_capture(surface: Surface) -> bool {
75 matches!(surface, Surface::ShareBar | Surface::Border)
76}
77
78fn overlap_area(first: ScreenRect, second: ScreenRect) -> i64 {
79 let width = (first.right.min(second.right) - first.left.max(second.left)).max(0);
80 let height = (first.bottom.min(second.bottom) - first.top.max(second.top)).max(0);
81 i64::from(width) * i64::from(height)
82}
83
84pub fn portal_minimize(portal: bool, share: LocalShare) -> bool {
85 portal && share == LocalShare::Screen
86}
87
88pub fn minimize_on_share_start(
89 share: LocalShare,
90 main: Option<ScreenRect>,
91 shared: Option<ScreenRect>,
92 portal: bool,
93) -> bool {
94 if share != LocalShare::Screen {
95 return false;
96 }
97 if portal_minimize(portal, share) {
98 return true;
99 }
100 let (Some(main), Some(shared)) = (main, shared) else {
101 return true;
102 };
103 let main_area = i64::from(main.width().max(0)) * i64::from(main.height().max(0));
104 if main_area == 0 {
105 return true;
106 }
107 overlap_area(main, shared) * 100 >= MINIMIZE_OVERLAP_PERCENT * main_area
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111pub struct GalleryLayout {
112 pub tiles: usize,
113 pub overflow: usize,
114 pub divider: bool,
115 pub self_tile: bool,
116}
117
118impl GalleryLayout {
119 pub fn height(&self) -> f32 {
120 let segments = self.tiles + usize::from(self.self_tile);
121 let gaps = (segments + usize::from(self.divider)).saturating_sub(1);
122 GALLERY_TITLE_HEIGHT
123 + GALLERY_INSET
124 + segments as f32 * GALLERY_TILE_HEIGHT
125 + if self.divider {
126 GALLERY_DIVIDER_HEIGHT
127 } else {
128 0.
129 }
130 + gaps as f32 * GALLERY_INSET
131 + GALLERY_INSET
132 + GALLERY_CONTROLS_HEIGHT
133 }
134}
135
136pub fn gallery_layout(others: usize, self_tile: bool, available_height: f32) -> GalleryLayout {
137 let build = |tiles: usize| GalleryLayout {
138 tiles,
139 overflow: others - tiles,
140 divider: others > 0 && self_tile,
141 self_tile,
142 };
143 let mut layout = build(others.min(GALLERY_MAX_TILES));
144 while layout.tiles > 1 && layout.height() > available_height {
145 layout = build(layout.tiles - 1);
146 }
147 layout
148}
149
150pub fn gallery_available_height(work: WorkArea) -> f32 {
151 (work.bottom - work.top) as f32 / work.scale - 2. * GALLERY_MARGIN
152}
153
154pub fn gallery_rect(
155 work: WorkArea,
156 layout: GalleryLayout,
157 origin: Option<(i32, i32)>,
158) -> ScreenRect {
159 let scaled = |logical: f32| (logical * work.scale).round() as i32;
160 let width = scaled(GALLERY_WIDTH);
161 let height = scaled(layout.height());
162 let margin = scaled(GALLERY_MARGIN);
163 let (left, top) =
164 origin.unwrap_or((work.right - width - margin, work.bottom - height - margin));
165 let left = left.clamp(work.left, (work.right - width).max(work.left));
166 let top = top.clamp(work.top, (work.bottom - height).max(work.top));
167 ScreenRect {
168 left,
169 top,
170 right: left + width,
171 bottom: top + height,
172 }
173}
174
175fn prioritized(model: &CallModel, tile: &Tile) -> bool {
176 model.is_pinned(&tile.mri) || tile.spotlight.is_some() || tile.hand.is_some()
177}
178
179pub fn gallery_candidates(model: &CallModel) -> Vec<&Tile> {
180 let mut tiles: Vec<&Tile> = model
181 .layout_tiles()
182 .into_iter()
183 .filter(|tile| tile.state == TileState::Present)
184 .collect();
185 tiles.sort_by_key(|tile| {
186 (
187 !prioritized(model, tile),
188 !(model.tile_speaking(tile) || model.is_recent_speaker(&tile.mri)),
189 )
190 });
191 tiles
192}
193
194#[cfg(test)]
195mod tests {
196 use super::*;
197
198 fn work(scale: f32) -> WorkArea {
199 WorkArea {
200 left: 0,
201 top: 0,
202 right: 1920,
203 bottom: 1040,
204 scale,
205 }
206 }
207
208 fn rect(left: i32, top: i32, right: i32, bottom: i32) -> ScreenRect {
209 ScreenRect {
210 left,
211 top,
212 right,
213 bottom,
214 }
215 }
216
217 #[test]
218 fn decision_table() {
219 use LocalShare::*;
220 use Monitor as M;
221 let rows = [
222 (None, false, M::None, false),
223 (None, true, M::Mini, false),
224 (Screen, false, M::None, true),
225 (Screen, true, M::Gallery, true),
226 (Window, false, M::None, true),
227 (Window, true, M::Gallery, true),
228 ];
229 for (share, minimized, monitor, share_bar) in rows {
230 assert_eq!(
231 overlay_plan(true, share, minimized, false),
232 OverlayPlan { monitor, share_bar },
233 "{share:?} minimized={minimized}"
234 );
235 }
236 }
237
238 #[test]
239 fn speaker_only_swaps_the_gallery_for_the_mini_tile() {
240 let plan = overlay_plan(true, LocalShare::Screen, true, true);
241 assert_eq!(plan.monitor, Monitor::Mini);
242 assert_eq!(
243 overlay_plan(true, LocalShare::None, true, true).monitor,
244 Monitor::Mini
245 );
246 }
247
248 #[test]
249 fn nothing_shows_outside_a_call() {
250 let plan = overlay_plan(false, LocalShare::None, true, false);
251 assert_eq!(plan.monitor, Monitor::None);
252 assert!(!plan.share_bar);
253 }
254
255 #[test]
256 fn only_the_share_chrome_stays_out_of_screenshots() {
257 assert!(excluded_from_capture(Surface::ShareBar));
258 assert!(excluded_from_capture(Surface::Border));
259 assert!(!excluded_from_capture(Surface::Mini));
260 assert!(!excluded_from_capture(Surface::Gallery));
261 }
262
263 #[test]
264 fn unknown_source_kind_counts_as_a_screen() {
265 assert_eq!(LocalShare::of(None, true), LocalShare::Screen);
266 assert_eq!(
267 LocalShare::of(Some(ShareKind::Window), true),
268 LocalShare::Window
269 );
270 assert_eq!(
271 LocalShare::of(Some(ShareKind::Screen), false),
272 LocalShare::None
273 );
274 }
275
276 #[test]
277 fn screen_share_minimizes_only_when_the_main_window_is_on_the_shared_monitor() {
278 let monitor = rect(0, 0, 1920, 1080);
279 let on_monitor = Some(rect(100, 100, 1100, 800));
280 let other_monitor = Some(rect(2000, 100, 3000, 800));
281 let edge = Some(rect(1880, 0, 2880, 1000));
282 assert!(minimize_on_share_start(
283 LocalShare::Screen,
284 on_monitor,
285 Some(monitor),
286 false
287 ));
288 assert!(!minimize_on_share_start(
289 LocalShare::Screen,
290 other_monitor,
291 Some(monitor),
292 false
293 ));
294 assert!(!minimize_on_share_start(
295 LocalShare::Screen,
296 edge,
297 Some(monitor),
298 false
299 ));
300 assert!(minimize_on_share_start(
301 LocalShare::Screen,
302 None,
303 Some(monitor),
304 false
305 ));
306 assert!(minimize_on_share_start(
307 LocalShare::Screen,
308 on_monitor,
309 None,
310 false
311 ));
312 }
313
314 #[test]
315 fn portal_minimize_forces_a_screen_and_spares_a_window() {
316 let monitor = Some(rect(0, 0, 1920, 1080));
317 let other = Some(rect(2000, 100, 3000, 800));
318 assert!(portal_minimize(true, LocalShare::Screen));
319 assert!(!portal_minimize(true, LocalShare::Window));
320 assert!(!portal_minimize(false, LocalShare::Screen));
321 assert!(minimize_on_share_start(
322 LocalShare::Screen,
323 other,
324 monitor,
325 true
326 ));
327 assert!(!minimize_on_share_start(
328 LocalShare::Window,
329 monitor,
330 monitor,
331 true
332 ));
333 }
334
335 #[test]
336 fn window_share_and_no_share_never_minimize() {
337 let monitor = Some(rect(0, 0, 1920, 1080));
338 assert!(!minimize_on_share_start(
339 LocalShare::Window,
340 monitor,
341 monitor,
342 false
343 ));
344 assert!(!minimize_on_share_start(
345 LocalShare::None,
346 monitor,
347 monitor,
348 false
349 ));
350 }
351
352 #[test]
353 fn gallery_stacks_four_tiles_a_divider_and_the_own_tile() {
354 let layout = gallery_layout(9, true, 2000.);
355 assert_eq!(
356 layout,
357 GalleryLayout {
358 tiles: 4,
359 overflow: 5,
360 divider: true,
361 self_tile: true
362 }
363 );
364 let expected = GALLERY_TITLE_HEIGHT
365 + 5. * GALLERY_TILE_HEIGHT
366 + GALLERY_DIVIDER_HEIGHT
367 + 7. * GALLERY_INSET
368 + GALLERY_CONTROLS_HEIGHT;
369 assert!((layout.height() - expected).abs() < 0.01);
370 }
371
372 #[test]
373 fn gallery_chrome_matches_teams() {
374 let chrome = GalleryLayout {
375 tiles: 0,
376 overflow: 0,
377 divider: false,
378 self_tile: false,
379 }
380 .height();
381 assert_eq!(chrome, 78.);
382 assert_eq!(GALLERY_TILE_WIDTH, 186.);
383 }
384
385 #[test]
386 fn gallery_drops_tiles_to_fit_a_short_screen() {
387 let layout = gallery_layout(9, true, 500.);
388 assert!(layout.height() <= 500.);
389 assert_eq!(layout.tiles, 2);
390 assert_eq!(layout.overflow, 7);
391 assert_eq!(gallery_layout(9, true, 10.).tiles, 1);
392 }
393
394 #[test]
395 fn gallery_without_others_has_no_divider() {
396 let layout = gallery_layout(0, true, 2000.);
397 assert_eq!(
398 (layout.tiles, layout.divider, layout.self_tile),
399 (0, false, true)
400 );
401 let hidden_self = gallery_layout(3, false, 2000.);
402 assert_eq!((hidden_self.tiles, hidden_self.divider), (3, false));
403 }
404
405 #[test]
406 fn gallery_defaults_to_the_bottom_right_with_the_teams_margin() {
407 let layout = gallery_layout(2, true, 2000.);
408 let placed = gallery_rect(work(1.), layout, None);
409 assert_eq!(placed.width(), 192);
410 assert_eq!((placed.right, placed.bottom), (1920 - 30, 1040 - 30));
411 assert_eq!(placed.height(), layout.height().round() as i32);
412 }
413
414 #[test]
415 fn gallery_keeps_a_dragged_origin_inside_the_work_area() {
416 let layout = gallery_layout(4, true, 2000.);
417 assert_eq!(gallery_rect(work(1.), layout, Some((200, 100))).left, 200);
418 let pushed = gallery_rect(work(1.), layout, Some((5000, 900)));
419 assert_eq!((pushed.right, pushed.bottom), (1920, 1040));
420 }
421
422 #[test]
423 fn gallery_scales_with_the_monitor() {
424 let layout = gallery_layout(1, false, 2000.);
425 let placed = gallery_rect(work(2.), layout, None);
426 assert_eq!(placed.width(), 384);
427 assert_eq!(gallery_available_height(work(2.)), 520. - 60.);
428 }
429
430 fn present(mri: &str, name: &str) -> Tile {
431 Tile {
432 state: TileState::Present,
433 ..Tile::invited(mri, name)
434 }
435 }
436
437 #[test]
438 fn candidates_put_pinned_then_speakers_first() {
439 let mut model = CallModel::test();
440 model.tiles = vec![
441 present("8:a", "Ada"),
442 present("8:b", "Bo"),
443 present("8:c", "Cy"),
444 Tile::invited("8:d", "Dee"),
445 ];
446 model.speaking_mris = vec!["8:c".to_owned()];
447 model.toggle_pin("8:b");
448 let names: Vec<&str> = gallery_candidates(&model)
449 .iter()
450 .map(|tile| tile.name.as_str())
451 .collect();
452 assert_eq!(names, ["Bo", "Cy", "Ada"]);
453 }
454}