#[derive(Clone, Copy, Debug)]
pub struct FrameVisibility {
occluded: bool,
physical_size: (u32, u32),
}
impl FrameVisibility {
pub fn new(width: u32, height: u32) -> Self {
Self { occluded: false, physical_size: (width, height) }
}
pub fn resize(&mut self, width: u32, height: u32) {
self.physical_size = (width, height);
}
pub fn set_occluded(&mut self, occluded: bool) -> bool {
std::mem::replace(&mut self.occluded, occluded) != occluded
}
pub fn is_drawable(&self) -> bool {
!self.occluded && self.physical_size.0 > 0 && self.physical_size.1 > 0
}
pub fn is_occluded(&self) -> bool {
self.occluded
}
pub fn physical_size(&self) -> (u32, u32) {
self.physical_size
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn occluded_windows_stay_asleep_through_resizes() {
let mut visibility = FrameVisibility::new(1280, 820);
assert!(visibility.is_drawable());
assert!(visibility.set_occluded(true));
assert!(!visibility.set_occluded(true));
visibility.resize(1600, 900);
assert!(!visibility.is_drawable());
assert!(visibility.set_occluded(false));
assert!(visibility.is_drawable());
}
#[test]
fn unoccluding_a_zero_sized_window_does_not_make_it_drawable() {
let mut visibility = FrameVisibility::new(1280, 820);
visibility.set_occluded(true);
visibility.resize(0, 820);
visibility.set_occluded(false);
assert!(!visibility.is_drawable());
visibility.resize(1280, 0);
assert!(!visibility.is_drawable());
visibility.resize(1280, 820);
assert!(visibility.is_drawable());
}
}