#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Rect {
pub x: f64,
pub y: f64,
pub w: f64,
pub h: f64,
}
impl Rect {
fn contains(&self, px: f64, py: f64) -> bool {
px >= self.x && px < self.x + self.w && py >= self.y && py < self.y + self.h
}
}
pub const FILLED: f64 = 0.92;
pub fn covered_share(area: Rect, windows: &[Rect]) -> f64 {
const COLS: usize = 64;
const ROWS: usize = 36;
if area.w <= 0.0 || area.h <= 0.0 {
return 0.0;
}
let mut hit = 0usize;
for r in 0..ROWS {
let py = area.y + (r as f64 + 0.5) * area.h / ROWS as f64;
for c in 0..COLS {
let px = area.x + (c as f64 + 0.5) * area.w / COLS as f64;
if windows.iter().any(|w| w.contains(px, py)) {
hit += 1;
}
}
}
hit as f64 / (ROWS * COLS) as f64
}
pub fn filled(area: Rect, windows: &[Rect]) -> bool {
covered_share(area, windows) >= FILLED
}
#[cfg(test)]
mod tests {
use super::*;
const VISIBLE: Rect = Rect {
x: 0.0,
y: 37.0,
w: 1512.0,
h: 875.0,
};
#[test]
fn a_zoomed_window_fills_the_screen() {
assert!(filled(VISIBLE, &[VISIBLE]));
let almost = Rect {
x: 6.0,
y: 43.0,
w: 1500.0,
h: 860.0,
};
assert!(filled(VISIBLE, &[almost]));
}
#[test]
fn a_big_window_with_desktop_around_it_does_not() {
let big = Rect {
x: 150.0,
y: 120.0,
w: 1200.0,
h: 700.0,
};
assert!(!filled(VISIBLE, &[big]));
assert!(!filled(VISIBLE, &[]));
}
#[test]
fn two_tiles_side_by_side_fill_it_and_other_screens_do_not_count() {
let left = Rect {
x: 8.0,
y: 45.0,
w: 744.0,
h: 859.0,
};
let right = Rect {
x: 760.0,
y: 45.0,
w: 744.0,
h: 859.0,
};
assert!(filled(VISIBLE, &[left, right]));
assert!(!filled(VISIBLE, &[left]));
let elsewhere = Rect {
x: 1512.0,
y: 0.0,
w: 2560.0,
h: 1440.0,
};
assert!(!filled(VISIBLE, &[elsewhere]));
}
}