1#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct Rect {
11 pub x: f64,
12 pub y: f64,
13 pub w: f64,
14 pub h: f64,
15}
16
17impl Rect {
18 fn contains(&self, px: f64, py: f64) -> bool {
19 px >= self.x && px < self.x + self.w && py >= self.y && py < self.y + self.h
20 }
21}
22
23pub const FILLED: f64 = 0.92;
27
28pub fn covered_share(area: Rect, windows: &[Rect]) -> f64 {
30 const COLS: usize = 64;
31 const ROWS: usize = 36;
32 if area.w <= 0.0 || area.h <= 0.0 {
33 return 0.0;
34 }
35 let mut hit = 0usize;
36 for r in 0..ROWS {
37 let py = area.y + (r as f64 + 0.5) * area.h / ROWS as f64;
38 for c in 0..COLS {
39 let px = area.x + (c as f64 + 0.5) * area.w / COLS as f64;
40 if windows.iter().any(|w| w.contains(px, py)) {
41 hit += 1;
42 }
43 }
44 }
45 hit as f64 / (ROWS * COLS) as f64
46}
47
48pub fn filled(area: Rect, windows: &[Rect]) -> bool {
50 covered_share(area, windows) >= FILLED
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 const VISIBLE: Rect = Rect {
59 x: 0.0,
60 y: 37.0,
61 w: 1512.0,
62 h: 875.0,
63 };
64
65 #[test]
66 fn a_zoomed_window_fills_the_screen() {
67 assert!(filled(VISIBLE, &[VISIBLE]));
68 let almost = Rect {
70 x: 6.0,
71 y: 43.0,
72 w: 1500.0,
73 h: 860.0,
74 };
75 assert!(filled(VISIBLE, &[almost]));
76 }
77
78 #[test]
79 fn a_big_window_with_desktop_around_it_does_not() {
80 let big = Rect {
81 x: 150.0,
82 y: 120.0,
83 w: 1200.0,
84 h: 700.0,
85 };
86 assert!(!filled(VISIBLE, &[big]));
87 assert!(!filled(VISIBLE, &[]));
88 }
89
90 #[test]
91 fn two_tiles_side_by_side_fill_it_and_other_screens_do_not_count() {
92 let left = Rect {
93 x: 8.0,
94 y: 45.0,
95 w: 744.0,
96 h: 859.0,
97 };
98 let right = Rect {
99 x: 760.0,
100 y: 45.0,
101 w: 744.0,
102 h: 859.0,
103 };
104 assert!(filled(VISIBLE, &[left, right]));
105 assert!(!filled(VISIBLE, &[left]));
106 let elsewhere = Rect {
108 x: 1512.0,
109 y: 0.0,
110 w: 2560.0,
111 h: 1440.0,
112 };
113 assert!(!filled(VISIBLE, &[elsewhere]));
114 }
115}