use std::time::{Duration, Instant};
#[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
}
pub const ALMOST_FILLED: f64 = 0.85;
pub const CORNERS: f64 = 24.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Backing {
Clear,
Bar,
BarAndCorners,
}
pub fn backing(area: Rect, windows: &[Rect]) -> Backing {
if covered_share(area, windows) < ALMOST_FILLED {
return Backing::Clear;
}
let top = Rect { h: CORNERS, ..area };
if covered_share(top, windows) >= 0.97 {
Backing::BarAndCorners
} else {
Backing::Bar
}
}
pub const REST: Duration = Duration::from_millis(80);
#[derive(Clone, Copy, Debug)]
pub struct Watch {
backing: Backing,
share: f64,
since: Option<Instant>,
rest: f64,
}
impl Default for Watch {
fn default() -> Watch {
Watch {
backing: Backing::Clear,
share: 0.0,
since: None,
rest: 0.0,
}
}
}
impl Watch {
pub fn backing(&self) -> Backing {
self.backing
}
pub fn look(&mut self, now: Instant, area: Rect, windows: &[Rect]) -> (Backing, bool) {
let share = covered_share(area, windows);
let moved = (share - self.share).abs() > 1e-6;
if moved || self.since.is_none() {
self.since = Some(now);
}
self.share = share;
let still = self.since.is_some_and(|t| now >= t + REST);
self.backing = match (self.backing, backing(area, windows)) {
(_, Backing::Clear) => Backing::Clear,
(_, Backing::BarAndCorners) => Backing::BarAndCorners,
(Backing::BarAndCorners, Backing::Bar) => Backing::Clear,
(Backing::Bar, Backing::Bar) if share >= self.rest - 0.01 => Backing::Bar,
(Backing::Bar, Backing::Bar) => Backing::Clear,
(Backing::Clear, Backing::Bar) if still => {
self.rest = share;
Backing::Bar
}
(Backing::Clear, Backing::Bar) => Backing::Clear,
};
(self.backing, moved)
}
}
#[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 the_menu_bar_is_solid_above_windows_that_almost_fill_the_screen() {
assert_eq!(backing(VISIBLE, &[]), Backing::Clear);
assert_eq!(backing(VISIBLE, &[VISIBLE]), Backing::BarAndCorners);
let almost = Rect {
x: 30.0,
y: 60.0,
w: 1452.0,
h: 830.0,
};
assert!(!filled(VISIBLE, &[almost]));
assert_eq!(backing(VISIBLE, &[almost]), Backing::Bar);
let half = Rect {
h: 440.0,
..VISIBLE
};
assert_eq!(backing(VISIBLE, &[half]), Backing::Clear);
}
#[test]
fn the_menu_bar_clears_as_a_window_starts_to_minimize() {
let area = Rect {
x: 0.0,
y: 31.0,
w: 1024.0,
h: 674.0,
};
let at = |x: f64, y: f64, w: f64, h: f64| [Rect { x, y, w, h }];
let start = Instant::now();
let ms = |n: u64| start + Duration::from_millis(n);
let mut watch = Watch::default();
let full = at(0.0, 31.0, 1024.0, 674.0);
assert_eq!(watch.look(ms(0), area, &full).0, Backing::BarAndCorners);
assert_eq!(
watch.look(ms(16), area, &at(0.0, 31.0, 1024.0, 675.0)).0,
Backing::BarAndCorners
);
let away = [
at(0.0, 34.0, 1024.0, 675.0),
at(0.0, 34.0, 1024.0, 675.0),
at(5.0, 64.0, 1019.0, 675.0),
at(22.0, 99.0, 1001.0, 646.0),
at(245.0, 267.0, 759.0, 478.0),
];
for (i, w) in away.iter().enumerate() {
let (backing, _) = watch.look(ms(32 + 16 * i as u64), area, w);
assert_eq!(backing, Backing::Clear, "look {i}");
}
assert_eq!(
watch.look(ms(300), area, &at(12.0, 81.0, 1011.0, 664.0)).0,
Backing::Clear
);
assert_eq!(
watch.look(ms(316), area, &at(0.0, 41.0, 1024.0, 675.0)).0,
Backing::Clear
);
assert_eq!(watch.look(ms(332), area, &full).0, Backing::BarAndCorners);
assert_eq!(watch.look(ms(348), area, &[]), (Backing::Clear, true));
assert_eq!(watch.look(ms(364), area, &[]), (Backing::Clear, false));
}
#[test]
fn a_window_with_desktop_around_it_waits_to_hold_still() {
let start = Instant::now();
let ms = |n: u64| start + Duration::from_millis(n);
let almost = Rect {
x: 30.0,
y: 60.0,
w: 1452.0,
h: 830.0,
};
let mut watch = Watch::default();
assert_eq!(watch.look(ms(0), VISIBLE, &[almost]).0, Backing::Clear);
assert_eq!(watch.look(ms(16), VISIBLE, &[almost]).0, Backing::Clear);
assert_eq!(watch.look(ms(96), VISIBLE, &[almost]).0, Backing::Bar);
let moved = Rect { y: 110.0, ..almost };
assert_eq!(watch.look(ms(112), VISIBLE, &[moved]).0, Backing::Clear);
}
#[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]));
}
}