BlueGit

ict rusty-teams

Files

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

share_overlay.rs

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