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
53pub const ALMOST_FILLED: f64 = 0.85;
56
57pub const CORNERS: f64 = 24.0;
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62pub enum Backing {
63 Clear,
65 Bar,
67 BarAndCorners,
69}
70
71pub fn backing(area: Rect, windows: &[Rect]) -> Backing {
73 if covered_share(area, windows) < ALMOST_FILLED {
74 return Backing::Clear;
75 }
76 let top = Rect { h: CORNERS, ..area };
77 if covered_share(top, windows) >= 0.97 {
78 Backing::BarAndCorners
79 } else {
80 Backing::Bar
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 const VISIBLE: Rect = Rect {
90 x: 0.0,
91 y: 37.0,
92 w: 1512.0,
93 h: 875.0,
94 };
95
96 #[test]
97 fn a_zoomed_window_fills_the_screen() {
98 assert!(filled(VISIBLE, &[VISIBLE]));
99 let almost = Rect {
101 x: 6.0,
102 y: 43.0,
103 w: 1500.0,
104 h: 860.0,
105 };
106 assert!(filled(VISIBLE, &[almost]));
107 }
108
109 #[test]
110 fn a_big_window_with_desktop_around_it_does_not() {
111 let big = Rect {
112 x: 150.0,
113 y: 120.0,
114 w: 1200.0,
115 h: 700.0,
116 };
117 assert!(!filled(VISIBLE, &[big]));
118 assert!(!filled(VISIBLE, &[]));
119 }
120
121 #[test]
122 fn the_menu_bar_is_solid_above_windows_that_almost_fill_the_screen() {
123 assert_eq!(backing(VISIBLE, &[]), Backing::Clear);
124 assert_eq!(backing(VISIBLE, &[VISIBLE]), Backing::BarAndCorners);
125 let almost = Rect {
127 x: 30.0,
128 y: 60.0,
129 w: 1452.0,
130 h: 830.0,
131 };
132 assert!(!filled(VISIBLE, &[almost]));
133 assert_eq!(backing(VISIBLE, &[almost]), Backing::Bar);
134 let half = Rect {
136 h: 440.0,
137 ..VISIBLE
138 };
139 assert_eq!(backing(VISIBLE, &[half]), Backing::Clear);
140 }
141
142 #[test]
143 fn two_tiles_side_by_side_fill_it_and_other_screens_do_not_count() {
144 let left = Rect {
145 x: 8.0,
146 y: 45.0,
147 w: 744.0,
148 h: 859.0,
149 };
150 let right = Rect {
151 x: 760.0,
152 y: 45.0,
153 w: 744.0,
154 h: 859.0,
155 };
156 assert!(filled(VISIBLE, &[left, right]));
157 assert!(!filled(VISIBLE, &[left]));
158 let elsewhere = Rect {
160 x: 1512.0,
161 y: 0.0,
162 w: 2560.0,
163 h: 1440.0,
164 };
165 assert!(!filled(VISIBLE, &[elsewhere]));
166 }
167}