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