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