From 9cf5b415c67a2782c8d5688aefd69052ea382fd1 Mon Sep 17 00:00:00 2001 From: Ivan Molodetskikh Date: Wed, 22 Jan 2020 08:03:18 +0300 Subject: [PATCH] compositor: add RegionAttributes::contains --- src/wayland/compositor/mod.rs | 112 ++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/src/wayland/compositor/mod.rs b/src/wayland/compositor/mod.rs index 74a833f..7e5a71f 100644 --- a/src/wayland/compositor/mod.rs +++ b/src/wayland/compositor/mod.rs @@ -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, }, } + +#[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); + } +}