use crate::geometry::Rect;
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DamageRegion {
pub rect: Rect,
}
impl DamageRegion {
#[inline]
pub const fn new(rect: Rect) -> Self {
Self { rect }
}
#[inline]
pub const fn from_coords(x: i32, y: i32, width: u32, height: u32) -> Self {
Self {
rect: Rect::new(x, y, width, height),
}
}
pub const EMPTY: Self = Self { rect: Rect::ZERO };
#[inline]
pub const fn is_empty(&self) -> bool {
self.rect.is_empty()
}
#[inline]
pub const fn area(&self) -> u64 {
self.rect.area()
}
#[inline]
pub fn union(&self, other: &DamageRegion) -> DamageRegion {
DamageRegion {
rect: self.rect.union(&other.rect),
}
}
#[inline]
pub fn intersection(&self, other: &DamageRegion) -> Option<DamageRegion> {
self.rect
.intersection(&other.rect)
.map(|r| DamageRegion { rect: r })
}
#[inline]
pub fn intersects(&self, other: &DamageRegion) -> bool {
self.rect.intersects(&other.rect)
}
#[inline]
pub const fn offset(&self, dx: i32, dy: i32) -> Self {
Self {
rect: self.rect.offset(dx, dy),
}
}
#[inline]
pub fn expand(&self, amount: i32) -> Self {
Self {
rect: self.rect.expand(amount),
}
}
}
impl From<Rect> for DamageRegion {
#[inline]
fn from(rect: Rect) -> Self {
Self::new(rect)
}
}
impl From<DamageRegion> for Rect {
#[inline]
fn from(region: DamageRegion) -> Self {
region.rect
}
}
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Hash)]
pub enum DamageHint {
#[default]
Full = 0,
None = 1,
Partial = 2,
Scroll = 3,
}
impl DamageHint {
#[inline]
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Full),
1 => Some(Self::None),
2 => Some(Self::Partial),
3 => Some(Self::Scroll),
_ => None,
}
}
#[inline]
pub const fn name(&self) -> &'static str {
match self {
Self::Full => "Full",
Self::None => "None",
Self::Partial => "Partial",
Self::Scroll => "Scroll",
}
}
#[inline]
pub const fn needs_compose(&self) -> bool {
!matches!(self, Self::None)
}
}