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/// From this share covered, the menu bar above the windows is made solid: an
54/// almost maximized window too, with some desktop around it.
55pub const ALMOST_FILLED: f64 = 0.85;
56
57/// How far below the menu bar a window's rounded corners reach, in points.
58pub const CORNERS: f64 = 24.0;
59
60/// What goes behind a screen's menu bar.
61#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62pub enum Backing {
63    /// Nothing: the wallpaper shows.
64    Clear,
65    /// One colour behind the menu bar.
66    Bar,
67    /// And in the corners of the windows right below it.
68    BarAndCorners,
69}
70
71/// The backing for windows so, on a screen whose usable part is `area`.
72pub 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    /// A 1512 x 982 point screen: the menu bar is 37 high, the Dock 70.
89    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        // A few points short of the edges still counts.
100        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        // Some desktop all around: the bar, and not a band below it.
126        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        // Full width, right under the bar, but only half the height.
135        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        // A window on the screen to the right.
159        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}