Skip to main content

clairlune_live/
cover.rs

1//! Whether app windows fill a screen. On macOS a zoomed window (a double
2//! click on its title bar) leaves the menu bar and the Dock showing, so the
3//! wallpaper's window never reports itself hidden: the Mac host also asks
4//! where the other apps' windows are and treats a screen whose usable area
5//! they all but fill (one zoomed window, or tiles side by side) as covered.
6
7/// A rectangle in one coordinate system (the Mac host: points, top-left
8/// origin, as the window server gives window bounds).
9#[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
23/// From this share of a screen's usable area covered, the screen counts as
24/// covered: a zoomed or maximized window, or windows tiled side by side
25/// (with the small gaps tiling leaves).
26pub const FILLED: f64 = 0.92;
27
28/// The share of `area` the windows cover together, sampled on a grid.
29pub 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
48/// Whether the windows all but fill `area`.
49pub 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    /// A 1512 x 982 point screen: the menu bar is 37 high, the Dock 70.
58    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        // A few points short of the edges still counts.
69        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        // A window on the screen to the right.
107        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}