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
7use std::time::{Duration, Instant};
8
9/// A rectangle in one coordinate system (the Mac host: points, top-left
10/// origin, as the window server gives window bounds).
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct Rect {
13    pub x: f64,
14    pub y: f64,
15    pub w: f64,
16    pub h: f64,
17}
18
19impl Rect {
20    fn contains(&self, px: f64, py: f64) -> bool {
21        px >= self.x && px < self.x + self.w && py >= self.y && py < self.y + self.h
22    }
23}
24
25/// From this share of a screen's usable area covered, the screen counts as
26/// covered: a zoomed or maximized window, or windows tiled side by side
27/// (with the small gaps tiling leaves).
28pub const FILLED: f64 = 0.92;
29
30/// The share of `area` the windows cover together, sampled on a grid.
31pub fn covered_share(area: Rect, windows: &[Rect]) -> f64 {
32    const COLS: usize = 64;
33    const ROWS: usize = 36;
34    if area.w <= 0.0 || area.h <= 0.0 {
35        return 0.0;
36    }
37    let mut hit = 0usize;
38    for r in 0..ROWS {
39        let py = area.y + (r as f64 + 0.5) * area.h / ROWS as f64;
40        for c in 0..COLS {
41            let px = area.x + (c as f64 + 0.5) * area.w / COLS as f64;
42            if windows.iter().any(|w| w.contains(px, py)) {
43                hit += 1;
44            }
45        }
46    }
47    hit as f64 / (ROWS * COLS) as f64
48}
49
50/// Whether the windows all but fill `area`.
51pub fn filled(area: Rect, windows: &[Rect]) -> bool {
52    covered_share(area, windows) >= FILLED
53}
54
55/// From this share covered, the menu bar above the windows is made solid: an
56/// almost maximized window too, with some desktop around it.
57pub const ALMOST_FILLED: f64 = 0.85;
58
59/// How far below the menu bar a window's rounded corners reach, in points.
60pub const CORNERS: f64 = 24.0;
61
62/// What goes behind a screen's menu bar.
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum Backing {
65    /// Nothing: the wallpaper shows.
66    Clear,
67    /// One colour behind the menu bar.
68    Bar,
69    /// And in the corners of the windows right below it.
70    BarAndCorners,
71}
72
73/// The backing for windows so, at rest, on a screen whose usable part is
74/// `area`.
75pub fn backing(area: Rect, windows: &[Rect]) -> Backing {
76    if covered_share(area, windows) < ALMOST_FILLED {
77        return Backing::Clear;
78    }
79    let top = Rect { h: CORNERS, ..area };
80    if covered_share(top, windows) >= 0.97 {
81        Backing::BarAndCorners
82    } else {
83        Backing::Bar
84    }
85}
86
87/// How long windows that don't reach the menu bar must hold still before it
88/// turns solid above them.
89pub const REST: Duration = Duration::from_millis(80);
90
91/// A menu bar's backing from one look at the windows to the next. It changes
92/// as a window reaches the menu bar or starts to leave it (the window server
93/// tells where a window is all through its minimizing), not once it is still.
94#[derive(Clone, Copy, Debug)]
95pub struct Watch {
96    backing: Backing,
97    /// The share covered at the last look, since when it has been that, and
98    /// what it was when the backing went up.
99    share: f64,
100    since: Option<Instant>,
101    rest: f64,
102}
103
104impl Default for Watch {
105    fn default() -> Watch {
106        Watch {
107            backing: Backing::Clear,
108            share: 0.0,
109            since: None,
110            rest: 0.0,
111        }
112    }
113}
114
115impl Watch {
116    pub fn backing(&self) -> Backing {
117        self.backing
118    }
119
120    /// The backing now; and whether windows moved since the last look.
121    pub fn look(&mut self, now: Instant, area: Rect, windows: &[Rect]) -> (Backing, bool) {
122        let share = covered_share(area, windows);
123        let moved = (share - self.share).abs() > 1e-6;
124        if moved || self.since.is_none() {
125            self.since = Some(now);
126        }
127        self.share = share;
128        let still = self.since.is_some_and(|t| now >= t + REST);
129        self.backing = match (self.backing, backing(area, windows)) {
130            (_, Backing::Clear) => Backing::Clear,
131            // A window has the menu bar right above it, or just had.
132            (_, Backing::BarAndCorners) => Backing::BarAndCorners,
133            (Backing::BarAndCorners, Backing::Bar) => Backing::Clear,
134            (Backing::Bar, Backing::Bar) if share >= self.rest - 0.01 => Backing::Bar,
135            (Backing::Bar, Backing::Bar) => Backing::Clear,
136            (Backing::Clear, Backing::Bar) if still => {
137                self.rest = share;
138                Backing::Bar
139            }
140            (Backing::Clear, Backing::Bar) => Backing::Clear,
141        };
142        (self.backing, moved)
143    }
144}
145
146#[cfg(test)]
147mod tests {
148    use super::*;
149
150    /// A 1512 x 982 point screen: the menu bar is 37 high, the Dock 70.
151    const VISIBLE: Rect = Rect {
152        x: 0.0,
153        y: 37.0,
154        w: 1512.0,
155        h: 875.0,
156    };
157
158    #[test]
159    fn a_zoomed_window_fills_the_screen() {
160        assert!(filled(VISIBLE, &[VISIBLE]));
161        // A few points short of the edges still counts.
162        let almost = Rect {
163            x: 6.0,
164            y: 43.0,
165            w: 1500.0,
166            h: 860.0,
167        };
168        assert!(filled(VISIBLE, &[almost]));
169    }
170
171    #[test]
172    fn a_big_window_with_desktop_around_it_does_not() {
173        let big = Rect {
174            x: 150.0,
175            y: 120.0,
176            w: 1200.0,
177            h: 700.0,
178        };
179        assert!(!filled(VISIBLE, &[big]));
180        assert!(!filled(VISIBLE, &[]));
181    }
182
183    #[test]
184    fn the_menu_bar_is_solid_above_windows_that_almost_fill_the_screen() {
185        assert_eq!(backing(VISIBLE, &[]), Backing::Clear);
186        assert_eq!(backing(VISIBLE, &[VISIBLE]), Backing::BarAndCorners);
187        // Some desktop all around: the bar, and not a band below it.
188        let almost = Rect {
189            x: 30.0,
190            y: 60.0,
191            w: 1452.0,
192            h: 830.0,
193        };
194        assert!(!filled(VISIBLE, &[almost]));
195        assert_eq!(backing(VISIBLE, &[almost]), Backing::Bar);
196        // Full width, right under the bar, but only half the height.
197        let half = Rect {
198            h: 440.0,
199            ..VISIBLE
200        };
201        assert_eq!(backing(VISIBLE, &[half]), Backing::Clear);
202    }
203
204    /// A window minimizing and coming back, as macOS 26 reported one (the
205    /// screen's usable part is 31 points down), a look every 16 ms.
206    #[test]
207    fn the_menu_bar_clears_as_a_window_starts_to_minimize() {
208        let area = Rect {
209            x: 0.0,
210            y: 31.0,
211            w: 1024.0,
212            h: 674.0,
213        };
214        let at = |x: f64, y: f64, w: f64, h: f64| [Rect { x, y, w, h }];
215        let start = Instant::now();
216        let ms = |n: u64| start + Duration::from_millis(n);
217        let mut watch = Watch::default();
218        let full = at(0.0, 31.0, 1024.0, 674.0);
219        // Solid at the first look at a window right under it.
220        assert_eq!(watch.look(ms(0), area, &full).0, Backing::BarAndCorners);
221        assert_eq!(
222            watch.look(ms(16), area, &at(0.0, 31.0, 1024.0, 675.0)).0,
223            Backing::BarAndCorners
224        );
225        // Three points down: clear, and it stays so all the way, though the
226        // window still almost fills the screen and two looks see the same.
227        let away = [
228            at(0.0, 34.0, 1024.0, 675.0),
229            at(0.0, 34.0, 1024.0, 675.0),
230            at(5.0, 64.0, 1019.0, 675.0),
231            at(22.0, 99.0, 1001.0, 646.0),
232            at(245.0, 267.0, 759.0, 478.0),
233        ];
234        for (i, w) in away.iter().enumerate() {
235            let (backing, _) = watch.look(ms(32 + 16 * i as u64), area, w);
236            assert_eq!(backing, Backing::Clear, "look {i}");
237        }
238        // Coming back: clear until it is there, then solid at once.
239        assert_eq!(
240            watch.look(ms(300), area, &at(12.0, 81.0, 1011.0, 664.0)).0,
241            Backing::Clear
242        );
243        assert_eq!(
244            watch.look(ms(316), area, &at(0.0, 41.0, 1024.0, 675.0)).0,
245            Backing::Clear
246        );
247        assert_eq!(watch.look(ms(332), area, &full).0, Backing::BarAndCorners);
248        // Closed: clear.
249        assert_eq!(watch.look(ms(348), area, &[]), (Backing::Clear, true));
250        assert_eq!(watch.look(ms(364), area, &[]), (Backing::Clear, false));
251    }
252
253    #[test]
254    fn a_window_with_desktop_around_it_waits_to_hold_still() {
255        let start = Instant::now();
256        let ms = |n: u64| start + Duration::from_millis(n);
257        let almost = Rect {
258            x: 30.0,
259            y: 60.0,
260            w: 1452.0,
261            h: 830.0,
262        };
263        let mut watch = Watch::default();
264        assert_eq!(watch.look(ms(0), VISIBLE, &[almost]).0, Backing::Clear);
265        assert_eq!(watch.look(ms(16), VISIBLE, &[almost]).0, Backing::Clear);
266        assert_eq!(watch.look(ms(96), VISIBLE, &[almost]).0, Backing::Bar);
267        // It starts to leave the screen: clear, though it still almost fills.
268        let moved = Rect { y: 110.0, ..almost };
269        assert_eq!(watch.look(ms(112), VISIBLE, &[moved]).0, Backing::Clear);
270    }
271
272    #[test]
273    fn two_tiles_side_by_side_fill_it_and_other_screens_do_not_count() {
274        let left = Rect {
275            x: 8.0,
276            y: 45.0,
277            w: 744.0,
278            h: 859.0,
279        };
280        let right = Rect {
281            x: 760.0,
282            y: 45.0,
283            w: 744.0,
284            h: 859.0,
285        };
286        assert!(filled(VISIBLE, &[left, right]));
287        assert!(!filled(VISIBLE, &[left]));
288        // A window on the screen to the right.
289        let elsewhere = Rect {
290            x: 1512.0,
291            y: 0.0,
292            w: 2560.0,
293            h: 1440.0,
294        };
295        assert!(!filled(VISIBLE, &[elsewhere]));
296    }
297}