compositor: add RegionAttributes::contains

This commit is contained in:
Ivan Molodetskikh 2020-01-22 08:03:18 +03:00
parent d30567512c
commit 9cf5b415c6
No known key found for this signature in database
GPG Key ID: 02CE38DA47E9D691
1 changed files with 112 additions and 0 deletions

View File

@ -222,6 +222,22 @@ impl Default for RegionAttributes {
}
}
impl RegionAttributes {
/// Checks whether given point is inside the region.
pub fn contains(&self, point: (i32, i32)) -> bool {
let mut contains = false;
for (kind, rect) in &self.rects {
if rect.contains(point) {
match kind {
RectangleKind::Add => contains = true,
RectangleKind::Subtract => contains = false,
}
}
}
contains
}
}
/// A Compositor global token
///
/// This token can be cloned at will, and is the entry-point to
@ -500,3 +516,99 @@ pub enum SurfaceEvent {
callback: NewResource<wl_callback::WlCallback>,
},
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn region_attributes_empty() {
let region = RegionAttributes { rects: vec![] };
assert_eq!(region.contains((0, 0)), false);
}
#[test]
fn region_attributes_add() {
let region = RegionAttributes {
rects: vec![(
RectangleKind::Add,
Rectangle {
x: 0,
y: 0,
width: 10,
height: 10,
},
)],
};
assert_eq!(region.contains((0, 0)), true);
}
#[test]
fn region_attributes_add_subtract() {
let region = RegionAttributes {
rects: vec![
(
RectangleKind::Add,
Rectangle {
x: 0,
y: 0,
width: 10,
height: 10,
},
),
(
RectangleKind::Subtract,
Rectangle {
x: 0,
y: 0,
width: 5,
height: 5,
},
),
],
};
assert_eq!(region.contains((0, 0)), false);
assert_eq!(region.contains((5, 5)), true);
}
#[test]
fn region_attributes_add_subtract_add() {
let region = RegionAttributes {
rects: vec![
(
RectangleKind::Add,
Rectangle {
x: 0,
y: 0,
width: 10,
height: 10,
},
),
(
RectangleKind::Subtract,
Rectangle {
x: 0,
y: 0,
width: 5,
height: 5,
},
),
(
RectangleKind::Add,
Rectangle {
x: 2,
y: 2,
width: 2,
height: 2,
},
),
],
};
assert_eq!(region.contains((0, 0)), false);
assert_eq!(region.contains((5, 5)), true);
assert_eq!(region.contains((2, 2)), true);
}
}