BlueGit

ict rusty-teams

Files

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

share_overlay.rs

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