mod mask;
pub use mask::ShapeMask;
#[derive(Debug, Clone, Default, PartialEq)]
pub enum WindowShape {
#[default]
Rectangle,
Circle,
Custom {
mask: Vec<u8>,
width: u32,
height: u32,
},
}
impl WindowShape {
pub fn custom(mask: Vec<u8>, width: u32, height: u32) -> Self {
Self::Custom { mask, width, height }
}
pub fn contains(&self, x: f32, y: f32, width: f32, height: f32) -> bool {
match self {
WindowShape::Rectangle => x >= 0.0 && x < width && y >= 0.0 && y < height,
WindowShape::Circle => {
let cx = width / 2.0;
let cy = height / 2.0;
let radius = width.min(height) / 2.0;
let dx = x - cx;
let dy = y - cy;
dx * dx + dy * dy <= radius * radius
}
WindowShape::Custom { mask, width: mw, height: mh } => {
let mx = (x / width * (*mw as f32)) as u32;
let my = (y / height * (*mh as f32)) as u32;
if mx >= *mw || my >= *mh {
return false;
}
let idx = ((my * mw + mx) * 4 + 3) as usize;
mask.get(idx).map(|&a| a > 128).unwrap_or(false)
}
}
}
}